By GokiSoft.com| 15:33 12/07/2023|
Java Advanced

File - Quản lý thông tin sinh viên

Viết chương trình quản lý sinh viên. Mỗi đối tượng sinh viên có các thuộc tính sau: id, name, age, address và gpa (điểm trung bình). Yêu cầu: tạo ra một menu với các chức năng sau: 

/****************************************/1. Add student.2. Edit student by id.3. Delete student by id.4. Sort student by gpa.5. Sort student by name.6. Show student.

7. Lưu thông tin sv vào file student.txt

8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình0. Exit./****************************************/

Chú ý mỗi thông tin sinh viên được lưu trên 1 dòng. Các thuộc tính cách nhau bằng dấu ,

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

trung [C1907L]
trung

2020-04-27 14:31:36



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jv2;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Student {
    int id,age;
    String name, address;
    float gpa;

    public void input() {
        Scanner inp = new Scanner(System.in);
        System.out.println("Nhap vao id sinh vien");
        id = Integer.parseInt(inp.nextLine());
        System.out.println("Nhap vao ten sinh vien");
        name = inp.nextLine();
        System.out.println("Nha vao dia chi");
        address = inp.nextLine();
        System.out.println("Nhap vao tuoi");
        age = Integer.parseInt(inp.nextLine());
        System.out.println("Nhap vao gpa");
        gpa = Float.parseFloat(inp.nextLine());
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", age=" + age + ", name=" + name + ", address=" + address + ", gpa=" + gpa + '}';
    }
    
    public void output() {
        System.out.println(toString());
    }
    
    public Student() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public float getGpa() {
        return gpa;
    }

    public void setGpa(float gpa) {
        this.gpa = gpa;
    }
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jv2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author prdox
 */
public class StudentMgr {

    List<Student> lstStudent;
    
    private static StudentMgr instance = null;
    
    public StudentMgr() {
        lstStudent = new ArrayList<>();
    }
    
    public synchronized static StudentMgr getInstance() {
        if (instance == null) {
            instance = new StudentMgr();
        }
        return instance;
    }
    
    public static void addStudent() {
        Student nStd = new Student();
        nStd.input();
        getInstance().lstStudent.add(nStd);
    }
    
    public static void editStudentById(int id) {
        Student edtStd = null;
        for (Student std : getInstance().lstStudent) {
            if (std.getId() == id) {
                edtStd = std;
                break;
            }
        }
        if (edtStd == null) return;
        Scanner inp = new Scanner(System.in);
        System.out.println("Nhap vao ten sinh vien");
        edtStd.setName(inp.nextLine());
        System.out.println("Nha vao dia chi");
        edtStd.setAddress(inp.nextLine());
        System.out.println("Nhap vao tuoi");
        edtStd.setAge(Integer.parseInt(inp.nextLine()));
        System.out.println("Nhap vao gpa");
        edtStd.setGpa(Float.parseFloat(inp.nextLine()));
    }
    
    public static void deleteStudentById(int id) {
        for (Student std : getInstance().lstStudent) {
            if (std.getId() == id) {
                getInstance().lstStudent.remove(std);
                break;
            }
        }
    }
    
    public static void sortStudentByGPA() {
        for (int i = 0; i < getInstance().lstStudent.size()-1; i++) {
            for (int j = i+1; j < getInstance().lstStudent.size(); j++) {
                if (getInstance().lstStudent.get(i).getGpa() > getInstance().lstStudent.get(j).getGpa()) {
                    Collections.swap(getInstance().lstStudent,i,j);
                }
            }
        }
    }
    
    public static void sortStudentByName() {
        for (int i = 0; i < getInstance().lstStudent.size()-1; i++) {
            for (int j = i+1; j < getInstance().lstStudent.size(); j++) {
                if (getInstance().lstStudent.get(i).getName().compareTo(getInstance().lstStudent.get(j).getName()) > 0) {
                    Collections.swap(getInstance().lstStudent,i,j);
                }
            }
        }
    }
    
    public static void showStudent() {
        getInstance().lstStudent.forEach((std) -> {
            std.output();
        });
    }
    
    public static void writeToFile() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("student.txt");
            String line;
            byte[] b;
            for (Student std : getInstance().lstStudent) {
                line = std.toString();
                b = line.getBytes();
                fos.write(b);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    public static void readFromFile() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("student.txt");
            StringBuilder builder = new StringBuilder();
            int code;
            while ((code = fis.read()) != -1) {
                builder.append((char) code);
            }
            System.out.println(builder.toString());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(StudentMgr.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jv2;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Main {
    public static void main(String[] args) {
        int choose,id;
        Scanner inp = new Scanner(System.in);
        do {
            showMenu();
            choose = Integer.parseInt(inp.nextLine());
            switch(choose) {
                case 1:
                    StudentMgr.addStudent();
                    break;
                case 2:
                    id = Integer.parseInt(inp.nextLine());
                    StudentMgr.editStudentById(id);
                    break;
                case 3:
                    id = Integer.parseInt(inp.nextLine());
                    StudentMgr.deleteStudentById(id);
                    break;
                case 4:
                    StudentMgr.sortStudentByGPA();
                    break;
                case 5:
                    StudentMgr.sortStudentByName();
                    break;
                case 6:
                    StudentMgr.showStudent();
                    break;
                case 7:
                    StudentMgr.writeToFile();
                    break;
                case 8:
                    StudentMgr.readFromFile();
                    break;
                case 9:
                    System.out.println("Bye bye");
                    break;
                default:
                    System.out.println("Invalid input");
            }
            
        } while (choose != 9);
    }
    
    public static void showMenu() {
        System.out.println("1. Add student.");
        System.out.println("2. Edit student by id.");
        System.out.println("3. Delete student by id.");
        System.out.println("4. Sort student by gpa.");
        System.out.println("5. Sort student by name.");
        System.out.println("6. Show student.");
        System.out.println("7. Lưu thông tin sv vào file student.txt");
        System.out.println("8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình");
        System.out.println("9. Exit.");
    }
}



hoangkhiem [C1907L]
hoangkhiem

2020-04-27 14:00:30



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Java2B3;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class Mani {

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        Student std = new Student();
        List<Student> arrsdt = new ArrayList<>();
        int luachon, them, i;
        do {
            System.out.println("1. Add student.");
            System.out.println("2. Edit student by id.");
            System.out.println("3. Delete student by id.");
            System.out.println("4. Sort student by gpa.");
            System.out.println("5. Sort student by name.");
            System.out.println("6. Show student.");
            System.out.println("7. Lưu thông tin sv vào file student.txt");
            System.out.println("8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình");
            System.out.println("0. Thoát");
            luachon = Integer.parseInt(input.nextLine());
            switch (luachon) {
                case 1:
                    System.out.println("Mời nhập số lượng sinh viên cần thêm : ");
                    them = Integer.parseInt(input.nextLine());
                    for (i = 0; i < them; i++) {
                        std.inputS();
                        arrsdt.add(std);
                    }
                    break;
                case 2:
                    System.out.println("nhập id cần sửa là : ");
                    String id = input.nextLine();
                    int count = 0;
                    for (Student editstudent : arrsdt) {
                        if (editstudent.getRollno().equalsIgnoreCase(id)) {
                            editstudent.inputS();
                            count++;
                        }
                        if (count == 0) {
                            System.out.println("Khong tim thấy nhé ");
                        }
                    }

                    break;
                case 3:
                    System.out.println("Nhập id cần xóa : ");
                    String id3 = input.nextLine();
                    arrsdt.stream().filter((detelestudent) -> (detelestudent.getRollno() == null ? id3 == null : detelestudent.getRollno().equals(id3))).forEachOrdered((detelestudent) -> {
                        arrsdt.remove(detelestudent);
                    });
                    break;
                case 4:

                    break;
                case 5:
                    break;
                case 6:
                    System.out.println("Hiển thị thông tin sv");
                    arrsdt.forEach((student) -> {
                        student.outputS();
                    });
                    break;
                case 7:
                    FileOutputStream fos = null;
                    ObjectOutputStream oos = null;
                    try {
                        fos = new FileOutputStream("student.txt");
                        oos = new ObjectOutputStream(fos);

                        oos.writeObject(arrsdt);
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(Mani.class.getName()).log(Level.SEVERE, null, ex);

                    } catch (IOException ex) {
                        Logger.getLogger(Mani.class.getName()).log(Level.SEVERE, null, ex);

                    } finally {
                        if (fos != null) {
                            try {
                                fos.close();
                            } catch (IOException ex) {
                                Logger.getLogger(Mani.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        if (oos != null) {
                            try {
                                oos.close();
                            } catch (IOException ex) {
                                Logger.getLogger(Mani.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                    break;
                case 8:
                    break;
                case 9:
                    System.out.println("Tạm biệt");
                    break;
                default:
                    System.out.println("Mời bạn lua chon dung");
                    break;

            }
        } while (luachon != 9);
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Java2B3;

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Student {
          String rollno, fullname;
      String age,diachi;
      float gpa;
    public Student() {
    }

    public Student(String rollno, String fullname, String age, String diachi, float gpa) {
        this.rollno = rollno;
        this.fullname = fullname;
        this.age = age;
        this.diachi = diachi;
        this.gpa = gpa;
    }

    
   

    public String getRollno() {
        return rollno;
    }

    public void setRollno(String rollno) {
        this.rollno = rollno;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public float getGpa() {
        return gpa;
    }

    public void setGpa(float gpa) {
        this.gpa = gpa;
    }

 

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getDiachi() {
        return diachi;
    }

    public void setDiachi(String diachi) {
        this.diachi = diachi;
    }
    
    
     public void inputS() {
        Scanner nhap = new Scanner(System.in);
        System.out.println("Mời nhập RollNo : ");
        rollno = nhap.nextLine();
        System.out.println("Mời nhập tên: ");
        fullname = nhap.nextLine();
        System.out.println("Mời nhập Tuổi: ");
        age = nhap.nextLine();
        System.out.println("Mời nhập địa chỉ: ");
        diachi = nhap.nextLine();
         System.out.println("Mời nhập điểm 0>điểm<10 : ");
         gpa = Float.parseFloat(nhap.nextLine());
    }

    public void outputS() {
        System.out.println(toString());
    }



    @Override
    public String toString() {
        return "Student{" + "rollno=" + rollno + ", fullname=" + fullname + ", age=" + age + ", diachi=" + diachi + ", gpa=" + gpa + '}';
    }
   
}



Phạm Ngọc Minh [T1907A]
Phạm Ngọc Minh

2020-04-15 07:12:48



package QLSV;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {
      static List<Student> studentList = new ArrayList();
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        
        int choose;
        
        do {
            menu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    editStudent();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    sortStudentGpa();
                    break;
                case 5:
                    sortStudentName();
                    break;
                case 6:
                    showMenu();
                    break;
                case 7:
                    saveStudent();
                    break;
                case 8:
                    readStudent();
                    break;
                case 9:
                    System.out.println("Thoat");
                    break;
                default:
                    System.out.println("Exit!!");
                    break;
            }
        } while (choose != 9);
    }
    static void addStudent() {
        System.out.println("Add student:");
        int n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            Student std = new Student();
            std.input();

            studentList.add(std);
        }
    }

    static void editStudent() {
        System.out.println("Nhap id can sua:");
        String id = scan.nextLine();
        int count = 0;
        for (Student editStudent : studentList) {
            if (editStudent.getId().equalsIgnoreCase(id)) {
                editStudent.input();
                count++;
            }
        }
        if (count == 0) {
            System.out.println("Khong tin thay id SV");
        }
    }

    static void deleteStudent() {
        System.out.println("Nhap id can xoa");
        String id = scan.nextLine();
        for (Student deleteStudent : studentList) {
            if (deleteStudent.getId() == id) {
                studentList.remove(deleteStudent);
            }
        }
    }

    static void sortStudentGpa() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getGpa() > o2.getGpa()) {
                    return 1;
                }
                return -1;
            }
        });
        for (int i = 0; i < studentList.size(); i++) {
            studentList.get(i).display();
        }
    }

    static void sortStudentName() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        for (int i = 0; i < studentList.size(); i++) {
            studentList.get(i).display();
        }
    }

    static void showMenu() {
        System.out.println("Hien thi thong tin SV");
        for (Student student : studentList) {
            student.display();
        }
    }

    static void saveStudent() {
        
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("student.txt");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(studentList);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            if(fos != null){
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
            if(oos != null){
            try {
                oos.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }
    }

    static void readStudent() {

        FileInputStream fis = null;
        ObjectInputStream ois = null;

        try {
            fis = new FileInputStream("student.txt");
            ois = new ObjectInputStream(fis);
            studentList = (List<Student>) ois.readObject();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            if(fis != null){
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
            if(ois != null){
            try {
                ois.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }        
    }
    static void menu(){
        System.out.println("1. Add student");
        System.out.println("2. Edit student by id");
        System.out.println("3. Delete student by id.");
        System.out.println("4. Sort student by gpa.");
        System.out.println("5. Sort student by name.");
        System.out.println("6. Show student.");
        System.out.println("7. Lưu thông tin sv vào file student.txt");
        System.out.println("8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình");
        System.out.println("9. Thoat");
        System.out.println("Choose:");
    }
}



package QLSV;

import java.util.Scanner;

public class Student {
    int id, age, gpa;
    String name, address;

    public Student(int id, int age, int gpa, String name, String address) {
        this.id = id;
        this.age = age;
        this.gpa = gpa;
        this.name = name;
        this.address = address;
        
    }

 
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGpa() {
        return gpa;
    }

    public void setGpa(int gpa) {
        this.gpa = gpa;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    public void input(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Nhập id: ");
        id = Integer.parseInt(scanner.nextLine());
        
        System.out.println("Nhập name:");
        name = scanner.nextLine();
        
        System.out.println("Nhập age:");
        age = Integer.parseInt(scanner.nextLine());
    }

}



thienphu [T1907A]
thienphu

2020-03-26 01:02:05



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package QuanliSv;

import java.io.Serializable;
import java.util.Scanner;

/**
 *
 * @author Thien Phu
 */
public class Student implements Serializable {

    private String id, name, address;
    private int age;
    private float gpa;

    public Student() {
    }

    public Student(String id, String name, String address, int age, float gpa) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.age = age;
        this.gpa = gpa;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getGpa() {
        return gpa;
    }

    public void setGpa(float gpa) {
        this.gpa = gpa;
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + ", gpa=" + gpa + '}';
    }

    public void inputStd() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap ID SV:");
        this.id = sc.nextLine();
        System.out.println("Nhap tên Sv: ");
        name = sc.nextLine();
        System.out.println("Nhập Age Sv:");
        age = Integer.parseInt(sc.nextLine());
        System.out.println("Nhập Address Sv:");
        address = sc.nextLine();
        System.out.println("Nhap GPA SV:");
        gpa = Float.parseFloat(sc.nextLine());

    }

    public void display() {
        System.out.println(toString());
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package QuanliSv;

import com.sun.istack.internal.logging.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;

/**
 *
 * @author Thien Phu
 */
public class StudentManager {

    private static Scanner sc = new Scanner(System.in);
    List<Student> stdList;

    public StudentManager() {
        stdList = new ArrayList<>();
    }

    public void inputStudent() {
        while (true) {
            Student st = new Student();
            st.inputStd();
            stdList.add(st);
            System.out.println("Ban co muốn thêm sinh viên hay ko");
            System.out.println("1:Y");
            System.out.println("2:N");
            String choose = sc.nextLine();
            if (choose.equalsIgnoreCase("N")) {
                break;
            }
        }
    }

    //show student 
    public void showdanhsachSV() {
        if (stdList.isEmpty()) {
            System.out.println("Chua co du lieu");
        } else {
            for (Student stdList1 : stdList) {
                stdList1.display();
            }
        }
    }

    //edit sinh vien theo id
    public void editSv() {
        System.out.println("Nhap ID Student can edit");
        String idStd = sc.nextLine();
        Student st = null;
        for (Student stdList1 : stdList) {
            if (stdList1.getId().equalsIgnoreCase(idStd)) {
                st = stdList1;
                break;
            }
        }
        if (st != null) {
            System.out.println("Edit name");
            st.setName(sc.nextLine());
            System.out.println("Edit age:");
            st.setAge(Integer.parseInt(sc.nextLine()));
            System.out.println("Edit Address");
            st.setAddress(sc.nextLine());
            System.out.println("Edit GPA");
            st.setGpa(Float.parseFloat(sc.nextLine()));

        } else {
            System.out.println("Khong ton tai sinh vien co ID = " + idStd);
        }

    }

    //deleted sv theo id
    public void deletedSVbyID() {
        System.out.println("Nhap ID Sv can xoa: ");
        String id = sc.nextLine();
        Student st = null;
        for (Student stdList1 : stdList) {
            if (stdList1.getId().equalsIgnoreCase(id)) {
                st = stdList1;
                break;
            }
        }
        if (st != null) {
            stdList.remove(stdList.remove(st));
            System.out.println("Xoa thanh cong SV ID = " + id);
            System.out.println("Danh sach Sv sau khi xoa");
            showdanhsachSV();
        } else {
            System.out.println("Khong ton tai Sv co ID nay");
        }

    }

    //sort std by gpa
    public void sortbyGpa() {
        Collections.sort(stdList, new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                return o1.getGpa() < o2.getGpa() ? -1 : 1;
            }
        });
        System.out.println("Danh sach sau khi sap xep");
        showdanhsachSV();
    }

    public void sortbyName() {
        Collections.sort(stdList, new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        System.out.println("Danh sach sau khi sap xep khi ten");
        showdanhsachSV();
    }

    //save vào file
    public void savebyfile() {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;

        try {
            fos = new FileOutputStream(new File("D:\\baitapjava\\Java01\\src\\QuanliSv\\student.dat"));
            oos = new ObjectOutputStream(fos);

            oos.writeObject(stdList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (oos != null) {
                try {
                    oos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void readfile() {

        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(new File("D:\\baitapjava\\Java01\\src\\QuanliSv\\student.dat"));
            ois = new ObjectInputStream(fis);

            stdList = (List<Student>) ois.readObject();
        } catch (FileNotFoundException ex) {
            java.util.logging.Logger.getLogger(StudentManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(StudentManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StudentManager.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    java.util.logging.Logger.getLogger(StudentManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                    java.util.logging.Logger.getLogger(StudentManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package QuanliSv;

import java.util.Scanner;

/**
 *
 * @author Thien Phu
 */
public class Main {

    public static void main(String[] args) {
        StudentManager stdManager = new StudentManager();
        Scanner sc = new Scanner(System.in);
        int choosse;
        do {
            showmenu();
            choosse = Integer.parseInt(sc.nextLine());
            switch (choosse) {
                case 1:
                    stdManager.inputStudent();
                    break;
                case 2:
                    stdManager.editSv();
                    break;
                case 3:
                    stdManager.deletedSVbyID();
                    break;
                case 4:
                    stdManager.sortbyGpa();
                    break;
                case 5:
                    stdManager.sortbyName();
                    break;
                case 6:
                    stdManager.showdanhsachSV();
                    break;
                case 7:
                    stdManager.savebyfile();
                    break;
                case 8:
                    stdManager.readfile();
                    break;
                case 0:
                    System.out.println("Exit thanh cong");
                    break;
                default:
                    System.err.println("Nhap sai roi");
                    break;
            }
        } while (choosse != 0);

    }

    public static void showmenu() {
        System.out.println("1:Add Student");
        System.out.println("2:Edit Student by id:");
        System.out.println("3:Deleted Student by id:");
        System.out.println("4:Sort student by gpa");
        System.out.println("5:Sort student by name");
        System.out.println("6:Show Student:");
        System.out.println("7:Save Student in student.txt");
        System.out.println("8:Read Student by file");
        System.out.println("0:Exit");
        System.out.println("Choose:");
    }
}



Đường Thanh Bình [T1907A]
Đường Thanh Bình

2020-03-25 10:40:14



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package QlySv;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Administrator
 */
public class Main {
      static List<Student> studentList = new ArrayList();
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        
        int choose;
        
        do {
            menu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    editStudent();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    sortStudentGpa();
                    break;
                case 5:
                    sortStudentName();
                    break;
                case 6:
                    showMenu();
                    break;
                case 7:
                    saveStudent();
                    break;
                case 8:
                    readStudent();
                    break;
                case 9:
                    System.out.println("Thoat");
                    break;
                default:
                    System.out.println("Exit!!");
                    break;
            }
        } while (choose != 9);
    }
    static void addStudent() {
        System.out.println("Add student:");
        int n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            Student std = new Student();
            std.input();

            studentList.add(std);
        }
    }

    static void editStudent() {
        System.out.println("Nhap id can sua:");
        String id = scan.nextLine();
        int count = 0;
        for (Student editStudent : studentList) {
            if (editStudent.getId().equalsIgnoreCase(id)) {
                editStudent.input();
                count++;
            }
        }
        if (count == 0) {
            System.out.println("Khong tin thay id SV");
        }
    }

    static void deleteStudent() {
        System.out.println("Nhap id can xoa");
        String id = scan.nextLine();
        for (Student deleteStudent : studentList) {
            if (deleteStudent.getId() == id) {
                studentList.remove(deleteStudent);
            }
        }
    }

    static void sortStudentGpa() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getGpa() > o2.getGpa()) {
                    return 1;
                }
                return -1;
            }
        });
        for (int i = 0; i < studentList.size(); i++) {
            studentList.get(i).display();
        }
    }

    static void sortStudentName() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        for (int i = 0; i < studentList.size(); i++) {
            studentList.get(i).display();
        }
    }

    static void showMenu() {
        System.out.println("Hien thi thong tin SV");
        for (Student student : studentList) {
            student.display();
        }
    }

    static void saveStudent() {
        
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("student.txt");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(studentList);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            if(fos != null){
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
            if(oos != null){
            try {
                oos.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }
    }

    static void readStudent() {

        FileInputStream fis = null;
        ObjectInputStream ois = null;

        try {
            fis = new FileInputStream("student.txt");
            ois = new ObjectInputStream(fis);
            studentList = (List<Student>) ois.readObject();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            if(fis != null){
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
            if(ois != null){
            try {
                ois.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }        
    }
    static void menu(){
        System.out.println("1. Add student");
        System.out.println("2. Edit student by id");
        System.out.println("3. Delete student by id.");
        System.out.println("4. Sort student by gpa.");
        System.out.println("5. Sort student by name.");
        System.out.println("6. Show student.");
        System.out.println("7. Lưu thông tin sv vào file student.txt");
        System.out.println("8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình");
        System.out.println("9. Thoat");
        System.out.println("Choose:");
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package QlySv;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Student {
    int id, age, gpa;
    String name, address;

    public Student(int id, int age, int gpa, String name, String address) {
        this.id = id;
        this.age = age;
        this.gpa = gpa;
        this.name = name;
        this.address = address;
        
    }

 
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGpa() {
        return gpa;
    }

    public void setGpa(int gpa) {
        this.gpa = gpa;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    public void input(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Nhập id: ");
        id = Integer.parseInt(scanner.nextLine());
        
        System.out.println("Nhập name:");
        name = scanner.nextLine();
        
        System.out.println("Nhập age:");
        age = Integer.parseInt(scanner.nextLine());
    }

}



Minh Nghia [T1907A]
Minh Nghia

2020-03-24 13:53:25



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package File.Student;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;



/**
 *
 * @author Administrator
 */
public class Test {
    static List<Student> studentList = new ArrayList();
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        
        int choose;
        
        do {
            menu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    editStudent();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    sortStudentGpa();
                    break;
                case 5:
                    sortStudentName();
                    break;
                case 6:
                    showMenu();
                    break;
                case 7:
                    saveStudent();
                    break;
                case 8:
                    readStudent();
                    break;
                case 9:
                    System.out.println("Thoat");
                    break;
                default:
                    System.out.println("Exit!!");
                    break;
            }
        } while (choose != 9);
    }
    static void addStudent() {
        System.out.println("Add student:");
        int n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            Student std = new Student();
            std.input();

            studentList.add(std);
        }
    }

    static void editStudent() {
        System.out.println("Nhap id can sua:");
        String id = scan.nextLine();
        int count = 0;
        for (Student editStudent : studentList) {
            if (editStudent.getId().equalsIgnoreCase(id)) {
                editStudent.input();
                count++;
            }
        }
        if (count == 0) {
            System.out.println("Khong tin thay id SV");
        }
    }

    static void deleteStudent() {
        System.out.println("Nhap id can xoa");
        String id = scan.nextLine();
        for (Student deleteStudent : studentList) {
            if (deleteStudent.getId() == id) {
                studentList.remove(deleteStudent);
            }
        }
    }

    static void sortStudentGpa() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getGpa() > o2.getGpa()) {
                    return 1;
                }
                return -1;
            }
        });
        for (int i = 0; i < studentList.size(); i++) {
            studentList.get(i).display();
        }
    }

    static void sortStudentName() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        for (int i = 0; i < studentList.size(); i++) {
            studentList.get(i).display();
        }
    }

    static void showMenu() {
        System.out.println("Hien thi thong tin SV");
        for (Student student : studentList) {
            student.display();
        }
    }

    static void saveStudent() {
        
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("student.txt");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(studentList);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);

        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            if(fos != null){
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
            if(oos != null){
            try {
                oos.close();
            } catch (IOException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }
    }

    static void readStudent() {

        FileInputStream fis = null;
        ObjectInputStream ois = null;

        try {
            fis = new FileInputStream("student.txt");
            ois = new ObjectInputStream(fis);
            studentList = (List<Student>) ois.readObject();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            if(fis != null){
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
            if(ois != null){
            try {
                ois.close();
            } catch (IOException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }        
    }
    static void menu(){
        System.out.println("1. Add student");
        System.out.println("2. Edit student by id");
        System.out.println("3. Delete student by id.");
        System.out.println("4. Sort student by gpa.");
        System.out.println("5. Sort student by name.");
        System.out.println("6. Show student.");
        System.out.println("7. Lưu thông tin sv vào file student.txt");
        System.out.println("8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình");
        System.out.println("9. Thoat");
        System.out.println("Choose:");
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package File.Student;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Student {
    int  age;
    String id,name,address;
    float gpa;

    public Student() {
    }

    public Student(String id, int age, String name, String address, float gpa) {
        this.id = id;
        this.age = age;
        this.name = name;
        this.address = address;
        this.gpa = gpa;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public float getGpa() {
        return gpa;
    }

    public void setGpa(float gpa) {
        this.gpa = gpa;
    }
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap id");
        id  = scan.nextLine();
        System.out.println("Nhap ten:");
        name = scan.nextLine();
        System.out.println("Nhap tuoi:");
        age = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap dia chi:");
        address = scan.nextLine();
        System.out.println("Nhap diem trung binh:");
        gpa = Float.parseFloat(scan.nextLine());
        
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", age=" + age + ", name=" + name + ", address=" + address + ", gpa=" + gpa + '}';
    }
    public void display(){
        System.out.println(toString());
    }
}



lê văn phương [T1907A]
lê văn phương

2020-03-24 08:58:22



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quan_ly_sv;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 *
 * @author HOME
 */
public class Main {
   public static void main(String[] args) {
        Map<String, Student> studentHashMap = new HashMap<>();
        
        Scanner input = new Scanner(System.in);
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(input.nextLine());
            
            switch(choose) {
                case 1:
                    System.out.println("Nhap N: ");
                    int n = Integer.parseInt(input.nextLine());
                    for (int i = 0; i < n; i++) {
                        Student std = new Student();
                        std.input();
                        
                        studentHashMap.put(std.getRollNo(), std);
                    }
                    break;
                case 2:
                    System.out.println("Thong tin sinh vien");
                    for (Map.Entry<String, Student> entry : studentHashMap.entrySet()) {
                        Student value = entry.getValue();
                        value.display();
                    }
                    break;
                case 3:
                    System.out.println("Nhap MSV can tim kiem: ");
                    String rollNo = input.nextLine();
                    
                    Student stdFind = studentHashMap.get(rollNo);
                    if(stdFind != null) {
                        stdFind.display();
                    } else {
                        System.out.println("Ko tim thay sv voi MSV nhap vao");
                    }
                    break;
                case 4:
                    System.out.println("Exit!!!");
                    break;
            }
        } while(choose != 4);
    }
    
    static void showMenu() {
        System.out.println("1. Nhap N sinh vien");
        System.out.println("2. In thong tin sinh vien");
        System.out.println("3. Tim kiem sinh vien theo RollNo");
        System.out.println("4. Thoat");
        System.out.println("Chon: ");
    }
   
}
    




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quan_ly_sv;

import java.util.Scanner;

/**
 *
 * @author HOME
 */
public class Student {
  String name, rollNo, sex, email, address;
    int age;

    public Student() {
    }

    public Student(String name, String rollNo, String sex, String email, String address, int age) {
        this.name = name;
        this.rollNo = rollNo;
        this.sex = sex;
        this.email = email;
        this.address = address;
        this.age = age;
    }
    
    public void input() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap ten: ");
        name = input.nextLine();
        
        System.out.println("Nhap MSV: ");
        rollNo = input.nextLine();
        
        System.out.println("Nhap gioi tinh: ");
        sex = input.nextLine();
        
        System.out.println("Nhap Email: ");
        email = input.nextLine();
        
        System.out.println("Nhap dia chi: ");
        address = input.nextLine();
        
        System.out.println("Nhap tuoi: ");
        age = Integer.parseInt(input.nextLine());
    }

    @Override
    public String toString() {
        return "Student{" + "name=" + name + ", rollNo=" + rollNo + ", sex=" + sex + ", email=" + email + ", address=" + address + ", age=" + age + '}';
    }
    
    public void display() {
//        System.out.println(toString());
        System.out.println(this);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
}



Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-03-23 16:52:37



package java2_Advanced.BaiTapNgay23_3_2020.File_quanLiSinhVien;

import java.io.Serializable;
import java.util.Scanner;

public class Student implements Serializable {
    String id, name,addRess;
    int age;
    Float gpa;

    public Student() {
    }

    public Student(String id, String name, String addRess, int age, Float gpa) {
        this.id = id;
        this.name = name;
        this.addRess = addRess;
        this.age = age;
        this.gpa = gpa;
    }

    public void input(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Nhap id:");
        id = scanner.nextLine();
        System.out.println("Nhap ten:");
        name = scanner.nextLine();
        System.out.println("Nhap tuoi:");
        age = Integer.parseInt(scanner.nextLine());
        System.out.println("Nhap dia chi:");
        addRess = scanner.nextLine();
        System.out.println("Nhap diem trung binh:");
        gpa = Float.parseFloat(scanner.nextLine());
    }
    public void output(){
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Student {" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", addRess='" + addRess + '\'' +
                ", age=" + age +
                ", gpa=" + gpa +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddRess() {
        return addRess;
    }

    public void setAddRess(String addRess) {
        this.addRess = addRess;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Float getGpa() {
        return gpa;
    }

    public void setGpa(Float gpa) {
        this.gpa = gpa;
    }
}



package java2_Advanced.BaiTapNgay23_3_2020.File_quanLiSinhVien;


import java.io.*;
import java.util.*;


public class Manipulation {
    Scanner scanner = new Scanner(System.in);

    List<Student> list = new ArrayList<>();

    public void addStudent() {
        System.out.print("Nhap so sinh vien can them: ");
        int n = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < n; i++) {
            Student student = new Student();
            student.input();
            list.add(student);
        }
    }

    public void editStudent() {
        System.out.println("Nhap id cua hoc sinh can sua: ");
        String id = scanner.nextLine();
        for (Student edit : list) {
            if (edit.id.equalsIgnoreCase(id)) {    // tìm id của sinh viên trong mảng
                edit.input();                        // tìm được thì sẽ nhập lại thông tin cho sinh viên đó.
            } else {
                System.out.println("Khon tim thay sinh vien co id: " + id);
            }
        }
    }

    public void deleteStudent() {
        System.out.println("Nhap id cua hoc sinh can xóa: ");
        String id = scanner.nextLine();
        for (Student edit : list) {
            if (edit.id.equalsIgnoreCase(id)) {           // tìm id của sinh viên trong mảng
                list.remove(edit);                     // tìm được thì sẽ xóa thông tin cho sinh viên đó khỏi mảng.
            }
        }
    }

    public void sortStudent_gpa() {
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student student, Student t1) {
                if (student.gpa > t1.gpa) {                    //so sanh điểm và sắp xếp theo chiều tăng dần.
                    return 1;
                }
                return -1;
            }
        });
    }

    public void sortStudent_name() {
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student student, Student t1) {
                return student.getName().compareToIgnoreCase(t1.getName());  //sắp xếp tên từ a -> z.
            }
        });
    }

    public void display() {
        for (Student hienthi : list) {
            hienthi.output();                  // hiển thị thông tin sinh viên trong mảng.
        }
    }


    public void saveStudent() {
        FileOutputStream fos = null;                         // khai báo nhập dữ liệu vào cho file
        ObjectOutputStream oos = null;                      // khai báo nhập dữ liệu vào cho đối tượng
        try {
            fos = new FileOutputStream("student.txt");   //tạo 1 file có tên là student.txt
            oos = new ObjectOutputStream(fos);
            oos.writeObject(list);               // truyền dữ liệu vào cho file

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();                 // đóng ....         (bắt buộc phải đóng nếu như có dữ liệu truyền vào)
                    fos.close();                  //đóng file.        (bắt buộc phải đóng nếu như có dữ liệu truyền vào)
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void showStudent() {
        FileInputStream fis = null;                   // khai báo hiển thị dữ liệu của file
        ObjectInputStream ois = null;                  // khai báo hiển thị dữ liệu của đối tượng.
        try {
            fis = new FileInputStream("student.txt");
            ois = new ObjectInputStream(fis);

            Object obj = ois.readObject();                            // gọi ra dữ liệu và gán vào Object
            System.out.println(obj);                                // hiển thị dữ liệu trên màng hình.

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("Nothing !!!");
            }
        }
    }
}



package java2_Advanced.BaiTapNgay23_3_2020.File_quanLiSinhVien;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Manipulation manipulation = new Manipulation();
        int choose;

        do {
            showMenu();
            System.out.println("Nhap lua chon: ");
            choose = Integer.parseInt(scanner.nextLine());

            switch (choose) {
                case 1:
                    manipulation.addStudent();
                    break;
                case 2:
                    manipulation.editStudent();
                    break;
                case 3:
                    manipulation.deleteStudent();
                    break;
                case 4:
                    manipulation.sortStudent_gpa();
                    break;
                case 5:
                    manipulation.sortStudent_name();
                    break;
                case 6:
                    manipulation.display();
                    break;
                case 7:
                    manipulation.saveStudent();
                    break;
                case 8:
                    manipulation.showStudent();
                    break;
                case 9:
                    System.out.println("Thoat.");
                    break;
                default:
                    System.err.println("Nhap sai");
                    break;
            }

        } while (choose != 9);

    }

    static void showMenu() {
        System.out.println("/****************************************/");
        System.out.println("1. Thêm sinh viên");
        System.out.println("2. Chỉnh sửa học sinh theo id.");
        System.out.println("3. Xóa sinh viên theo id.");
        System.out.println("4. Sắp xếp học sinh bằng diem trung binh.");
        System.out.println("5. Sắp xếp học sinh theo tên.");
        System.out.println("6. Hiển thị học sinh.");
        System.out.println("7. Lưu thông tin sv vào file student.txt");
        System.out.println("8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình");
        System.out.println("9. Exit.");
        System.out.println("/****************************************/");
    }
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-03-23 14:14:07



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package File;

import java.io.Serializable;
import java.util.Scanner;

/**
 *
 * @author DELL
 */
public class Student implements Serializable{
    String  id, name, address;
    Double  gpa ;
    int  age;

    public Student() {
    }

    public Student(String id, String name, String address, Double gpa, int age) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.gpa = gpa;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Double getGpa() {
        return gpa;
    }

    public void setGpa(Double gpa) {
        this.gpa = gpa;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("ID : ");
        id =scan.nextLine();
        System.out.println("Ten");
        name = scan.nextLine();
        System.out.println("tuoi : ");
        age = Integer.parseInt(scan.nextLine());
        System.out.println("Dia chi : ");
        address = scan.nextLine();
        System.out.println("Diem TB : ");
        gpa = Double.parseDouble(scan.nextLine());
    }
    public void display(){
        System.out.println(""+id+","+name+","+age+","+address+","+gpa);
    }
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package File;


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

/**
 *
 * @author DELL
 */
public class Test {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        Map<String, Student> stdHM = new HashMap<>();
        Set<Entry<String, Student>> entrySet = stdHM.entrySet();

        List<Entry<String, Student>> listOfEntry = new ArrayList<>(entrySet);
        int c;
        do {
            menu();
            System.out.println(" >>>>>>>>>>>>>>>>>>>  ");
            System.out.println("Lua chon : ");
            c = Integer.parseInt(scan.nextLine());
            switch (c) {
                case 1:
                    System.out.println(" >>>>>>>>>>>>>>>>>>>  ");
                    Case1(stdHM);
                    break;

                case 2:
                    System.out.println(" >>>>>>>>>>>>>>>>>>>  ");
                    Case2(stdHM);
                    break;
                case 3:
                    System.out.println(" >>>>>>>>>>>>>>>>>>>  ");
                    Case3(stdHM);
                    break;

                case 4:
                    Case4(stdHM, listOfEntry, entrySet);
                    break;
                case 5:
                    Case5(stdHM, listOfEntry, entrySet);
                    break;

                case 6:
                    System.out.println(" >>>>>>>>>>>>>>>>>>>  ");
                    case6(stdHM, listOfEntry);
                    break;
                case 7:
                     Case7(listOfEntry);
                    break;
                case 8:
                    case8(listOfEntry);
                    break;

                case 0:
                default:

                    break;

            }

        } while (c != 0);

    }

    public static void menu() {
        System.out.println("/****************************************/");
        System.out.println("1. Add student.");
        System.out.println("2. Edit student by id.");
        System.out.println("3. Delete student by id.");
        System.out.println("4. Sort student by gpa.");
        System.out.println("5. Sort student by name.");
        System.out.println("6. Show student.");
        System.out.println("7. Lưu thông tin sv vào file student.txt");
        System.out.println("8. Đọc thông tin sv từ file student.txt và hiển thị ra màn hình");
        System.out.println("0. Exit.");
        System.out.println("/****************************************/");
    }

    public static void Case1(Map<String, Student> stdHM) {
        Scanner scan = new Scanner(System.in);
        int n;
        System.out.println("Nhap N: ");
        n = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < n; i++) {
            Student std = new Student();
            std.input();
            stdHM.put(std.getId(), std);
        }

    }

    public static void Case2(Map< String, Student> stdHM) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Nhap MSV can tim kiem: ");
        String id = scan.nextLine();
        Student stdFind = stdHM.get(id);
        if (stdFind != null) {

            System.out.println("Ten");
            stdFind.setName(scan.nextLine());
            System.out.println("tuoi : ");
            stdFind.setAge(Integer.parseInt(scan.nextLine()));
            System.out.println("Dia chi : ");
            stdFind.setAddress(scan.nextLine());
            System.out.println("Diem TB : ");
            stdFind.setGpa(Double.parseDouble(scan.nextLine()));
        } else {
            System.out.println("khong ton tai Id");
        }

    }

    public static void Case3(Map<String, Student> stdHM) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Nhap MSV can tim kiem: ");
        String id = scan.nextLine();
        Student stdFind = stdHM.remove(id);

    }

    public static void Case4(Map<String, Student> stdHM, List<Entry<String, Student>> listOfEntry,Set<Entry<String, Student>> entrySet) {
        Set<Entry<String, Student>> entrySet1 = stdHM.entrySet();

        List<Entry<String, Student>> listOfEntry1 = new ArrayList<>(entrySet1);

        Collections.sort(listOfEntry1, new Comparator<Entry<String, Student>>() {
            @Override
            public int compare(Entry<String, Student> o1, Entry<String, Student> o2) {
                if (o1.getValue().getGpa() > o2.getValue().getGpa()) {
                    return -1;
                }
                return 1;
            }
        });
        listOfEntry.removeAll(entrySet);
        for (int i = 0; i < listOfEntry1.size(); i++) {
            listOfEntry.add(listOfEntry1.get(i));
        }

    }

    public static void Case5(Map<String, Student> stdHM, List<Entry<String, Student>> listOfEntry,Set<Entry<String, Student>> entrySet) {
          Set<Entry<String, Student>> entrySet2 = stdHM.entrySet();

                    List<Entry<String, Student>> listOfEntry2 = new ArrayList<>(entrySet2);
                    Collections.sort(listOfEntry2, new Comparator<Entry<String, Student>>() {
                        @Override
                        public int compare(Entry<String, Student> o1, Entry<String, Student> o2) {
                            return o1.getValue().getName().compareToIgnoreCase(o2.getValue().getName());
                        }
                    });
                    listOfEntry.removeAll(entrySet);
                    for (int i = 0; i < listOfEntry2.size(); i++) {
                        
                        listOfEntry.add(listOfEntry2.get(i));
                    }
    }

    public static void case6(Map<String, Student> stdHM, List<Entry<String, Student>> listOfEntry) {

        for (Map.Entry<String, Student> entry : stdHM.entrySet()) {
            String key = entry.getKey();
            Student value = entry.getValue();
            value.display();
        }
        System.out.println("sap xep : ");
        for (int i = 0; i < listOfEntry.size(); i++) {
            listOfEntry.get(i).getValue().display();
        }

    }
    public static void Case7( List<Entry<String, Student>> listOfEntry) {
        
        
        ArrayList<Student> stdt = new ArrayList<>();
        for (int i = 0; i < listOfEntry.size(); i++) {
            Student std1 = new Student();
            std1 = listOfEntry.get(i).getValue();
            stdt.add(std1);
        }
        
        try {
            FileOutputStream fos = new FileOutputStream("d:/Student.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            
            oos.writeObject(stdt);
            
            oos.close();
            fos.close();
            
        } catch (IOException ex) {
            System.err.println("Loi ghi file : \n "+ex);
        }
    }
    public static void case8( List<Entry<String, Student>> listOfEntry) {
        try {
            FileInputStream fis = new FileInputStream("d:/Student.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            
            ArrayList<Student> stdt = (ArrayList<Student>) ois.readObject();
            for (Student student : stdt) {
                student.display();
            }
            
            
        } catch (Exception ex) {
            System.err.println("Loi doc file : \n"+ex);
        }
    }

}