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

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

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 ,

Liên kết rút gọn:

https://gokisoft.com/1071

Bình luận

avatar
GokiSoft.com [Teacher]
2022-07-22 13:27:01


#DataMgr.java


/*
 * 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 bt1071;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class DataMgr {
    List<Student> dataList;
    Scanner scan;
    
    private static DataMgr instance = null;
    
    private DataMgr() {
        dataList = new ArrayList<>();
        scan = new Scanner(System.in);
    }
    
    public static DataMgr getInstance() {
        if(instance == null) {
            instance = new DataMgr();
        }
        return instance;
    }
    
    public void showAllId() {
        System.out.print("\nDanh sach ID: ");
        for (Student student : dataList) {
            System.out.print(student.getId() + " ");
        }
        System.out.println("");
    }
    
    public boolean checkIdExist(int id) {
        for (Student student : dataList) {
            if(student.getId() == id) return true;
        }
        return false;
    }
    
    public void input() {
        System.out.println("Nhap so sinh vien can them: ");
        int n = Utility.readInt();
        
        for (int i = 0; i < n; i++) {
            Student std = new Student();
            std.input();
            
            dataList.add(std);
        }
    }
    
    public void edit() {
        System.out.println("Nhap ma sinh vien can sua: ");
        int id = Utility.readInt();
        
        for (Student student : dataList) {
            if(student.getId() == id) {
                //Sua du lieu
                student.input();
                break;
            }
        }
    }
    
    public void delete() {
        System.out.println("Nhap sinh vien can xoa: ");
        int id = Utility.readInt();
        
        for (Student student : dataList) {
            if(student.getId() == id) {
                dataList.remove(student);
                break;
            }
        }
    }
    
    public void sortByGPA() {
        Collections.sort(dataList, (t1,t2) -> {
            return (t1.getGpa() > t2.getGpa())?1:-1;
        });
    }
    
    public void sortByName() {
        Collections.sort(dataList, (t1,t2) -> {
            return t1.getName().compareToIgnoreCase(t2.getName());
        });
    }
    
    public void display() {
        dataList.forEach((student) -> {
            student.display();
        });
    }
    
    public void saveFile() {
        String filename = "student.txt";
        
        FileWriter writer = null;
        BufferedWriter bWriter = null;
        
        try {
            writer = new FileWriter(filename);
            bWriter = new BufferedWriter(writer);
            
            for (Student student : dataList) {
                bWriter.write(student.toString());
            }
        } catch (IOException ex) {
            Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(bWriter != null) {
                try {
                    bWriter.close();
                } catch (IOException ex) {
                    Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(writer != null) {
                try {
                    writer.close();
                } catch (IOException ex) {
                    Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        System.out.println("Luu file thanh cong!!!");
    }
    
    public void readFile() {
        String filename = "student.txt";
        
        FileReader reader = null;
        BufferedReader bReader = null;
        
        try {
            reader = new FileReader(filename);
            bReader = new BufferedReader(reader);
            
            String line;
            while((line = bReader.readLine()) != null) {
                if(line.trim().isEmpty()) continue;
                
                Student std = new Student();
                
                std.parse(line);
                
                dataList.add(std);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IdDuplicateException ex) {
            Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(bReader != null) {
                try {
                    bReader.close();
                } catch (IOException ex) {
                    Logger.getLogger(DataMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        System.out.println("Doc file thanh cong!!!");
    }
}


#IdDuplicateException.java


/*
 * 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 bt1071;

/**
 *
 * @author Administrator
 */
public class IdDuplicateException extends Exception{
    
}


#Main.java


/*
 * 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 bt1071;

/**
 *
 * @author Administrator
 */
public class Main {
    public static void main(String[] args) {
        int choose;
        
        do {
            showMenu();
            System.out.println("Nhap lua chon: ");
            choose = Utility.readInt();
            
            switch(choose) {
                case 1:
                    DataMgr.getInstance().input();
                    break;
                case 2:
                    DataMgr.getInstance().edit();
                    break;
                case 3:
                    DataMgr.getInstance().delete();
                    break;
                case 4:
                    DataMgr.getInstance().sortByGPA();
                    DataMgr.getInstance().display();
                    break;
                case 5:
                    DataMgr.getInstance().sortByName();
                    DataMgr.getInstance().display();
                    break;
                case 6:
                    DataMgr.getInstance().display();
                    break;
                case 7:
                    DataMgr.getInstance().saveFile();
                    break;
                case 8:
                    DataMgr.getInstance().readFile();
                    DataMgr.getInstance().display();
                    break;
                case 9:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 9);
    }
    
    static void showMenu() {
        System.out.println("1. Them sv");
        System.out.println("2. Sua sinh vien");
        System.out.println("3. Xoa sinh vien");
        System.out.println("4. Sap xep theo GPA");
        System.out.println("5. Sap xep theo ten");
        System.out.println("6. Hien thi");
        System.out.println("7. Luu file");
        System.out.println("8. Doc file");
        System.out.println("9. Thoat");
        System.out.println("Chon: ");
    }
}


#Student.java


/*
 * 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 bt1071;

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

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

    public Student() {
        id = 0;
    }

    public Student(int 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 void input() {
        Scanner scan = new Scanner(System.in);
        if(id == 0) {
            DataMgr.getInstance().showAllId();
            System.out.println("Nhap ID: ");
            do {
                id = Utility.readInt();
                if(!DataMgr.getInstance().checkIdExist(id)) break;
                System.out.println("Nhap lai ID: ");
            } while(true);
        }
        
        System.out.println("Nhap tuoi: ");
        age = Utility.readInt();
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
        System.out.println("Nhap GPA: ");
        gpa = Utility.readFloat();
    }

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

    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;
    }
    
    public void parse(String line) throws IdDuplicateException {
        String[] elements = line.split(",");
        id = Integer.parseInt(elements[0].trim());
        
        if(DataMgr.getInstance().checkIdExist(id)) {
           throw new IdDuplicateException();
        }
        
        age = Integer.parseInt(elements[1].trim());
        name = elements[2].trim();
        address = elements[3].trim();
        gpa = Float.parseFloat(elements[4].trim());
    }
}


#Test.java


/*
 * 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 bt1071;

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.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class Test {
    public static void main(String[] args) {
//        test01();
//        test01_01();
//        test02();
//        test02_01();
//        test03();
        test03_01();
    }
    
    static void test03_01() {
        List<Student> list = new ArrayList<>();
        
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        
        try {
            fis = new FileInputStream("student-list-2.dat");
            ois = new ObjectInputStream(fis);
            
            Object obj;
            while((obj = ois.readObject()) != null) {
                list.add((Student) obj);
            }
        } 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);
                }
            }
        }
        
        for (Student student : list) {
            student.display();
        }
    }
    
    static void test03() {
        List<Student> stdList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Student std = new Student();
            std.input();
            
            stdList.add(std);
        }
        
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        
        try {
            fos = new FileOutputStream("student-list-2.dat");
            oos = new ObjectOutputStream(fos);
            
            for (Student student : stdList) {
                oos.writeObject(student);
            }
        } 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 test02_01() {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        
        try {
            fis = new FileInputStream("student-list.dat");
            ois = new ObjectInputStream(fis);
            
            List<Student> list = (List<Student>) ois.readObject();
            for (Student student : list) {
                student.display();
            }
        } 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 test02() {
        List<Student> stdList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Student std = new Student();
            std.input();
            
            stdList.add(std);
        }
        
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        
        try {
            fos = new FileOutputStream("student-list.dat");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(stdList);
        } 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 test01_01() {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        
        try {
            fis = new FileInputStream("student.dat");
            ois = new ObjectInputStream(fis);
            
            Student std = (Student) ois.readObject();
            std.display();
        } 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 test01() {
        Student std = new Student();
        std.input();
        
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        
        try {
            fos = new FileOutputStream("student.dat");
            oos = new ObjectOutputStream(fos);
            
            oos.writeObject(std);
        } 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);
                }
            }
        }
        
    }
}


#Utility.java


/*
 * 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 bt1071;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Utility {
    public static int readInt() {
        Scanner scan = new Scanner(System.in);
        int value;
        do {
            try {
                value = Integer.parseInt(scan.nextLine());
                return value;
            } catch(Exception e) {
                System.out.println("Nhap lai: ");
            }
        } while(true);
    }
    
    public static float readFloat() {
        Scanner scan = new Scanner(System.in);
        float value;
        do {
            try {
                value = Float.parseFloat(scan.nextLine());
                return value;
            } catch(Exception e) {
                System.out.println("Nhap lai: ");
            }
        } while(true);
    }
}


avatar
Nguyễn Hùng Anh [community,C2009G]
2021-09-26 23:18:44


#Main.java


/*
 * 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 lesson11.bt1071;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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 Admin
 */
public class Main {
    static List<Student> studentList;
    static Scanner scan;
    
    public static void main(String[] args) {
        studentList = new ArrayList<>();
        int c;
        scan = new Scanner(System.in);
        do {            
            showMenu();
            c = scan.nextInt();
            switch(c) {
                case 1:
                    input();
                    break;
                case 2:
                    editById();
                    break;
                case 3:
                    deleteById();
                    break;
                case 4:
                    sortByGpa();
                    display();
                    break;
                case 5:
                    sortByName();
                    display();
                    break;
                case 6:
                    display();
                    break;
                case 7:
                    saveFile();
                    break;
                case 8:
                    readFile();
                    break;
                default:
                    System.out.println("Wrong choise!!! Please enter again.");
                    break;
            }
        } while (c != 0);
    }
    
    static void showMenu() {
        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. Soft student by gpa");
        System.out.println("5. Soft student by name");
        System.out.println("6. Show students");
        System.out.println("7. Save into student.txt");
        System.out.println("8. Read student.txt and show");
        System.out.println("0. Exit");
        System.out.println("/****************************************/");
        System.out.println("Your choice: ");
    }
    
    static void input() {
        Student std = new Student();
        std.input();
        
        studentList.add(std);
    }
    
    static void display() {
        System.out.println("***Students Detail Information***");
        studentList.forEach(student -> {
            student.display();
        });
    }
    
    static void editById() {
        boolean isExisted = false;
        System.out.println("Enter id to edit: ");
        int findId = scan.nextInt();
        for (Student student : studentList) {
            if(student.getId() == findId){
                isExisted = true;
                student.input();
                System.out.println("Edit success!!!");
            }
        }
        if(!isExisted){
            System.out.println("Id: " + findId + " is not exist");
        }
    }
    
    static void deleteById() {
        boolean isExisted = false;
        System.out.println("Enter id to delete: ");
        int deleteId = scan.nextInt();
        for (Student student : studentList) {
            if(student.getId() == deleteId) {
                isExisted = true;
                studentList.remove(student);
                System.out.println("Deleted id: " + deleteId);
            }
        }
        if(!isExisted) {
            System.out.println("Id: " + deleteId + " is not exist");
        }
    }
    
    static void sortByName() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return s1.getName().compareToIgnoreCase(s2.getName());
            }
        });
    }
    
    static void sortByGpa() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return s1.getGpa() > s2.getGpa() ? 1 : -1;
            }
            
        });
    }
    
    static void saveFile() {
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream("student.txt");
            
            for (Student student : studentList) {
                String line = student.getFileLine();
                byte[] data = line.getBytes("utf8");
                
                fos.write(data);
            }
            System.out.println("Save success!!!");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException 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);
                }
            }
        }
    }
    
    static void readFile() {
        
        FileInputStream fis = null;
        StringBuilder builder = new StringBuilder();
        try {
            fis = new FileInputStream("student.txt");
            
            int code;
            while((code = fis.read()) != -1) {
                builder.append((char) code);
            }
            System.out.println(builder.toString());
        } 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(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        
    }
    
}




#Student.java


/*
 * 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 lesson11.bt1071;

import java.util.Scanner;

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

    public Student() {
    }

    public Student(int id, int age, float 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 float getGpa() {
        return gpa;
    }

    public void setGpa(float 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 String getFileLine() {
        return id + "," + name + "," + age + "," + address + "," + gpa + "\n";
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", age=" + age + ", gpa=" + gpa + ", name=" + name + ", address=" + address + '}';
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter Student's id: ");
        id = scan.nextInt();
        
        System.out.println("Enter student's name: ");
        name = scan.next();
        
        System.out.println("Enter student's age: ");
        age = scan.nextInt();
        
        System.out.println("Enter student's address: ");
        address = scan.next();
        
        System.out.println("Enter student's gpa: ");
        gpa = scan.nextFloat();
    }
    
    public void display() {
        System.out.println(this);
    }
}


avatar
Nguyễn Anh Vũ [T2008A]
2021-03-17 07:06:14

#Student


/*
 * 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 BT1;

/**
 *
 * @author NAV
 */
public class Student {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
    String Rollno;
    String Name;
    String Sex;
    int Age;
    String Email;
    String Address;
    
    public Student(){
    }
    public Student(String Rollno, String Name, String Sex, int Age, String Email, String Address){
        this.Rollno = Rollno;
        this.Name = Name;
        this.Sex = Sex;
        this.Age = Age;
        this.Email = Email;
        this.Address = Address;
    }
    public String getRollno(){
        return Rollno = Rollno;
    }
    public void setRollno(String Rollno){
        this.Rollno = Rollno;
    }
    
    public String getName(){
        return Name = Name;
    }
    public void setName(String Name){
        this.Name = Name;
    }
    
    public String getSex(){
        return Sex = Sex;
    }
    public void setSex(String Sex){
        this.Sex = Sex;
    }
    
    public int getAge(){
        return Age = Age;
    }
    public void setAge(int Age){
        this.Age = Age;
    }
    
    public String getEmail(){
        return Email = Email;
    }
    public void setEmail(String Email){
        this.Email = Email;
    }
    
    public String getAddress() {
        return Address;
    }
    public void setAddress(String Address) {
        this.Address = Address;
    }

    @Override
    public String toString() {
        return "Student{" + "Rollno=" + Rollno + ", Name=" + Name + ", Sex=" + Sex + ", Age=" + Age + ", Email=" + Email + ", Address=" + Address + '}';
    }
    
    public void Input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap Ma SV:");
        Rollno = scan.nextLine();
        System.out.println("Nhap ten SV:");
        Name = scan.nextLine();
        System.out.println("Nhap Gioi Tinh:");
        Sex = scan.nextLine();
        System.out.println("Nhap tuoi:");
        Age = scan.nextInt();
        System.out.println("Nhap Email:");
        Email = scan.nextLine();
        System.out.println("Nhap Dia Chi:");
        Address = scan.nextLine();
    }
}


avatar
Nguyễn Anh Vũ [T2008A]
2021-03-17 07:04:42


#Mail
/*
 * 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 BT1;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 *
 * @author NAV
 */
public class Main {
     /**
     * @param args the command line arguments
     */
     public static void main(String[] args) {
          // TODO code application logic here
       Scanner scan = new Scanner(System.in);
        Map<String, Student> maps = new HashMap<>();
        int choose;
        do{
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            switch(choose){
                case 1:
                        System.out.println("Nhap so SV:");
                        int num = Integer.parseInt(scan.nextLine());
                        for (int i = 0; i < num; i++) {
                            Student std = new Student();
                            std.Input();
                            maps.put(std.Rollno, std);
                    }
                    break;
                case 2:
                    maps.entrySet().forEach((student) -> {
                        System.out.println(student);
            });
                    break;
                case 3:
                    break;
                case 4:
                    System.out.println("Ket thuc!!!");
                    break;
                default :
                    System.out.println("Nhap sai!!!");
                    break;
            }
        }while(choose!=4);
          
    }
    static void showMenu(){
        System.out.println("1.Nhap thong tin N Sinh vien");
        System.out.println("2.In thong tin SV");
        System.out.println("3.Tim kiem thong tin Sinh vien theo maSV");
        System.out.println("4.Thoat!!!");
    }
}


avatar
Triệu Văn Lăng [T2008A]
2021-03-16 08:38: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 bai1071;

import java.util.Scanner;

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

    public Student() {
    }

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

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public int getId() {
        return id;
    }

    public int getAge() {
        return age;
    }

    public float getGpa() {
        return gpa;
    }

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

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

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

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

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

    @Override
    public String toString() {
        return "Student{" + "name=" + name + ", address=" + address + ", id=" + id + ", age=" + age + ", gpa=" + gpa + '}';
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap id: ");
        id = Integer.parseInt(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 DTB: ");
        gpa = Float.parseFloat(scan.nextLine());
        
    }
    
    public void display() {
        System.out.println(this);
    }

    void removeStudent(int id) {
    }
    
    public String getLine() {
        return id + "," + name + "," + age + "," + address + "," + gpa + "\n";
    }
    
    public void parse(String line) {
        String[] params = line.split(",");
        
        try {
            id = Integer.parseInt(params[0]);
            name = params[1];
            age = Integer.parseInt(params[2]);
            address = params[3];
            gpa = Float.parseFloat(params[4]);
        } catch(ArrayIndexOutOfBoundsException ex) {
        } finally {
        }
    }
    
    
    
}


avatar
Triệu Văn Lăng [T2008A]
2021-03-16 08:37:59



/*
 * 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 bai1071;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MTLS
 */
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        ArrayList<Student> studentList = new ArrayList<>();
        int choose;
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            switch (choose) {

                case 0:
                    System.out.println("Exit");
                    break;
                case 1:
                    System.out.println("Nhap so sinh vien muon them: ");
                    int n = Integer.parseInt(scan.nextLine());
                    for (int i = 0; i < n; i++) {
                        Student std = new Student();
                        std.input();
                        studentList.add(std);
                    }
                    break;
                case 2:
                    System.out.println("Nhap id sinh vien can sua: ");
                    int ids = Integer.parseInt(scan.nextLine());
                    Student stdF = studentList.get(ids);
                    Student std = new Student();
                    if (stdF != null) {
                        System.out.println("Da tim thay sinh vien co id: " + ids);
                        std.input();
                    } else {
                        System.out.println("Khong tim thay sinh vien co id: " + ids);
                    }

                    break;
                case 3:
                    System.out.println("Nhap id sinh vien can xoa: ");
                    int id = Integer.parseInt(scan.nextLine());
                    for (Student student : studentList) {
                        if (student.getId() == id) {
                            studentList.remove(id);
                            return;
                        }
                    }
                    break;
                case 4:
                    Collections.sort(studentList, new Comparator<Student>() {
                        @Override
                        public int compare(Student o1, Student o2) {
                            if (o1.getGpa() > o2.getGpa()) {
                                return -1;
                            }
                            return 1;
                        }
                    });
                    break;
                case 5:
                    Collections.sort(studentList, (Student o1, Student o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
                    break;
                case 6:
                    System.out.println("Thong tin sinh vien: ");
                    studentList.forEach((student) -> {
                        student.display();
                    });

                    break;
                case 7:
                    FileOutputStream fos = null;
                     {
                        try {
                            //                    File file = new File("QLStudent.txt");
                            fos = new FileOutputStream("Student.txt");
                            for (Student student : studentList) {
                                String line = student.getLine();
                                try {
                                    byte[] b = line.getBytes("utf8");
                                    try {
                                        fos.write(b);
                                    } catch (IOException ex) {
                                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                } catch (UnsupportedEncodingException ex) {
                                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                        } catch (FileNotFoundException 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);
                                }
                            }
                        }
                    }

                    break;
                case 8:
                    FileInputStream fis = null;
                    InputStreamReader reader = null;
                    BufferedReader brd = null;
                     {
                        try {
                            fis = new FileInputStream("Student.txt");
                            reader = new InputStreamReader(fis, StandardCharsets.UTF_8);
                            brd = new BufferedReader(brd);
                            String line = null;
                            while ((line = brd.readLine()) != null) {
                                if (line.isEmpty()) {
                                    continue;
                                }
                                Student std1 = new Student();
                                std1.parse(line);
                                studentList.add(std1);
                            }

                        } 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 (fis != null) {
                                try {
                                    fis.close();
                                } catch (IOException ex) {
                                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                            if (reader != null) {
                                try {
                                    reader.close();
                                } catch (IOException ex) {
                                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                            if (brd != null) {
                                try {
                                    brd.close();
                                } catch (IOException ex) {
                                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                        }
                    }

                    break;
                default:
                    System.out.println("Vui long nhap lai");
                    break;

            }
        } while (choose != 0);
    }

    private 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. Save file");
        System.out.println("8. Read file and display");
        System.out.println("0. Exit");

    }

}


avatar
TRẦN VĂN ĐIỆP [Teacher]
2021-03-15 07:45:55


#Student.java


/*
 * 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 bt1071;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Student {
    //id: tu tang -> giai phap la gi?
    static int count = 0;
    
    int id = 0, age;
    String name, address;
    float gpa;

    public Student() {
        id = ++count;
    }

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

    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;
    }
    
    public void input() {
        Scanner abc = new Scanner(System.in);
//        System.out.println("Nhap ID: ");
//        id = Integer.parseInt(abc.nextLine());
        
        System.out.println("Nhap ten: ");
        name = abc.nextLine();
        
        System.out.println("Nhap dia chi: ");
        address = abc.nextLine();
        
        System.out.println("Nhap tuoi: ");
        age = Integer.parseInt(abc.nextLine());
        
        System.out.println("Nhap GPA: ");
        gpa = Float.parseFloat(abc.nextLine());
    }
    
    public String getFileLine() {
        return id + "," + name + "," + age + "," + address + "," + gpa + "\n";
    }
    
    public void parse(String line) {
        String[] params = line.split(",");
        
        try {
            id = Integer.parseInt(params[0]);
            name = params[1];
            age = Integer.parseInt(params[2]);
            address = params[3];
            gpa = Float.parseFloat(params[4]);
        } catch(ArrayIndexOutOfBoundsException ex) {
        } finally {
        }
    }
    
    public void display() {
        System.out.println(this);
    }

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


#Main.java


/*
 * 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 bt1071;

import aptech.java2.lession3.FileInOutStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
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 Diep.Tran
 */
public class Main {
    static List<Student> studentList = new ArrayList<>();
    static Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args) {
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    inputStudent();
                    break;
                case 2:
                    editStudentById();
                    break;
                case 3:
                    deleteStudentById();
                    break;
                case 4:
                    sortStudentByGPA();
                    break;
                case 5:
                    sortStudentByName();
                    break;
                case 6:
                    displayStudent();
                    break;
                case 7:
                    saveFile();
                    break;
                case 8:
                    readFile();
                    break;
                case 9:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 9);
    }
    
    static void showMenu() {
        System.out.println("1. Them sinh vien");
        System.out.println("2. Sua sinh vien theo id");
        System.out.println("3. Xoa sinh vien theo id");
        System.out.println("4. Sap xep theo GPA");
        System.out.println("5. Sap xep theo ten");
        System.out.println("6. Hien thi thong tin sinh vien");
        System.out.println("7. Luu vao file student.txt");
        System.out.println("8. Doc noi dung sinh vien tu student.txt");
        System.out.println("9. Thoat");
        System.out.println("Chon: ");
    }

    private static void inputStudent() {
        System.out.println("Nhap so sinh vien can them: ");
        int n = Integer.parseInt(scan.nextLine());
        
        for (int i = 0; i < n; i++) {
            Student std = new Student();
            std.input();
            
            studentList.add(std);
        }
    }

    private static void editStudentById() {
        System.out.println("Nhap id sinh vien can sua: ");
        int id = Integer.parseInt(scan.nextLine());
        
        for (Student student : studentList) {
            if(student.getId() == id) {
                student.input();
                break;
            }
        }
    }

    private static void deleteStudentById() {
        System.out.println("Nhap id sinh vien can xoa: ");
        int id = Integer.parseInt(scan.nextLine());
        
        for (Student student : studentList) {
            if(student.getId() == id) {
                studentList.remove(student);
                break;
            }
        }
    }

    private static void sortStudentByGPA() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getGpa() > o2.getGpa()) {
                    return -1;
                }
                return 1;
            }
        });
    }

    private static void sortStudentByName() {
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareToIgnoreCase(o2.getName());
                //Z-A (A-Z: them dau - truoc)
            }
        });
    }

    private static void displayStudent() {
        for (Student student : studentList) {
            student.display();
        }
    }

    private static void saveFile() {
        //Luu file
        System.out.println("Bat dau luu:");
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream("student.txt", true);
            
            //luu du lieu
            for (Student student : studentList) {
                String line = student.getFileLine();
                //chuyen string to byte[]
                byte[] b = line.getBytes("utf8");
                //Save
                fos.write(b);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException 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);
                }
            }
        }
    }

    private static void readFile() {
        FileInputStream fis = null;
        InputStreamReader reader = null;
        BufferedReader bufferedReader = null;
        
        try {
            fis = new FileInputStream("student.txt");
            
            reader = new InputStreamReader(fis, StandardCharsets.UTF_8);
            
            bufferedReader = new BufferedReader(reader);
            
            String line = null;
            
            while((line = bufferedReader.readLine()) != null) {
                if(line.isEmpty()) {
                    continue;
                }
                Student std = new Student();
                std.parse(line);
                
                studentList.add(std);
            }
        } 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(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            if(bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }
}


avatar
Do Trung Duc [T2008A]
2021-03-15 03:36: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 ManagerStudent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TrungDuc
 */
public class ManagerStudent {

    static ArrayList<Student> listStudent = new ArrayList<>();

    public static void main(String[] agrs) {

        int option;
        do {
            Menu();
            Scanner scan = new Scanner(System.in);
            System.out.println("Nhap lua chon option = ");
            option = Integer.parseInt(scan.nextLine());

            switch (option) {
                case 1:
                    addStudent();
                    break;

                case 2:
                    editStudentById();
                    break;

                case 3:
                    deleteStudentById();
                    break;

                case 4:
                    sortStudentByGpa();
                    break;

                case 5:
                    sortStudentByName();
                    break;

//                case 6:
//                    showAllStudent();
//                    break;
                case 7:
                    saveInFile();
                    break;

                case 8:
                    readFromFile();
                    break;

                case 0:
                    System.out.println("Exit... ");
                    break;
            }

        } while (option != 0);

    }

    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("0.Exit.");
    }

    static void addStudent() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap so luong hoc sinh can them: ");
        int N = Integer.parseInt(scan.nextLine());
        for (int i = 0; i < N; i++) {
            System.out.format("Nhap thong tin hoc sinh thu %d", i+1);
            Student student = new Student();
            student.input();
            listStudent.add(student);
        }

    }

    static void editStudentById() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap Id hoc sinh can sua: ");
        int id = Integer.parseInt(scan.nextLine());

        for (Student student : listStudent) {
            if (student.getId() == id) {
                listStudent.remove(student);
                student.input();
                listStudent.add(student);
            }
        }
    }

    static void deleteStudentById() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap Id hoc sinh can xoa: ");
        int id = Integer.parseInt(scan.nextLine());

        for (Student student : listStudent) {
            if (student.getId() == id) {
                listStudent.remove(student);
            }
        }
    }

    static void sortStudentByGpa() {
        Collections.sort(listStudent, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getGpa() > o2.getGpa()) {
                    return -1;
                } else {
                    return 1;
                }
            }
        });
        for (Student student : listStudent) {
            student.display();
        }
    }

    static void sortStudentByName() {
        Collections.sort(listStudent, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return (o1.getName().compareTo(o2.getName()));
            }
        });
        for (Student student : listStudent) {
            student.display();
        }
    }

    public static void saveInFile() {
        FileOutputStream fos = null;

        try {
            File file = new File("student.txt");
            fos = new FileOutputStream(file);
            for (Student student : listStudent) {
                byte[] b = student.toString().getBytes("utf8");
                fos.write(b);
            }
            System.out.println("Danh sanh hoc sinh da duoc luu lai");
        } catch (Exception ex) {
            Logger.getLogger(ManagerStudent.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(ManagerStudent.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void readFromFile() {
        FileInputStream fis = null;
        InputStreamReader reader = null;
        BufferedReader bReader = null;

        try {
            fis = new FileInputStream("student.txt");
            reader = new InputStreamReader(fis, StandardCharsets.UTF_8);
            bReader = new BufferedReader(reader);
            String s;
            while ((s = bReader.readLine()) != null) {
                System.out.println(s);
            }

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

}


avatar
Do Trung Duc [T2008A]
2021-03-15 03:35:53



/*
 * 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 ManagerStudent;

import java.util.Scanner;

/**
 *
 * @author TrungDuc
 */
public class Student {

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

    public Student() {
    }

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

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    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 hoc sinh: ");
        id = Integer.parseInt(scan.nextLine());

        System.out.println("Nhap ten hoc sinh: ");
        name = scan.nextLine();

        System.out.println("Nhap tuoi hoc sinh: ");
        age = Integer.parseInt(scan.nextLine());

        System.out.println("Nhap dia chi hoc sinh: ");
        address = scan.nextLine();

        System.out.println("Nhap diem trung binh hoc sinh: ");
        gpa = Float.parseFloat(scan.nextLine());
    }
    
    public void display(){
        System.out.println(this);
    }

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

}


avatar
Trần Văn Lâm [T2008A]
2021-03-14 14:50:32


#Main.java


/*
 * 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 std;

import com.sun.java.swing.plaf.windows.WindowsTreeUI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import static jdk.nashorn.internal.runtime.Debug.id;

/**
 *
 * @author Administrator
 */
public class Main {
    public static void main(String[] args) {
        List<Student> listStudent = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        int choose;
        do{
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            switch(choose){
                case 1:
                    System.out.println("Them SV:");
                    int N = Integer.parseInt(scan.nextLine());
                    for (int i = 0; i < N; i++) {
                        Student std = new Student();
                        std.Input();
                        listStudent.add(std);
                    }
                    break;
                case 2:
                    String idE;
                    int count = 0, edit = 0;
                    System.out.println("ID Sv thong tin can sua:");
                    idE = scan.nextLine();
                    for (Student student : listStudent) {
                        if (student.id.equalsIgnoreCase(idE)){
                            student.Input();
                            edit++;
                        } 
                    }
                    break;
                case 3:
                    System.out.println("ID Sv muon xoa thong tin:");
                    String idD;
                    int delete = 0;
                    idD = scan.nextLine();
                    for (Student student : listStudent) {
                        if(student.id.equalsIgnoreCase(idD)){
                            listStudent.remove(student);
                            delete++;
                        }
                    }
                    break;
                case 4:
                    Collections.sort(listStudent, (Student o1, Student o2) -> {
                        if(o1.gpa < o2.gpa) return 1;
                        return -1;
                    });
                    break;
                case 5:
                    Collections.sort(listStudent, (Student o1, Student o2) -> o1.name.compareToIgnoreCase(o2.name));
                    break;
                case 6:
                    for (Student student : listStudent) {
                        System.out.println(student);
                    }
                    break;
                case 7:
                    
                    break;
                case 8:
                    break;
                case 0:
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        
        }while(choose!=9);
    }
    static void showMenu(){
        System.out.println("1.Them SV");
        System.out.println("2.Chinh sua thong tin SV theo ID");
        System.out.println("3.Xoa thong tin SV theo ID");
        System.out.println("4.Sắp xếp SV theo gpa.");
        System.out.println("5.Sắp xếp sinh viên theo ten.");
        System.out.println("6.Hien thi thong tin SV");
        System.out.println("7.Luu thong tin sv vao file student.txt");
        System.out.println("8.Doc thong tin SV tu file student.txt va in ra man hinh");
        System.out.println("0.Thoat!!!");
    }
}


#Student.java


/*
 * 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 std;

import java.util.Scanner;

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

    public Student() {
    }

    public Student(String id, String name, int age, String address, float gpa) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
        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 int getAge() {
        return age;
    }

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

    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;
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + ", 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());
        
    }
    public void Display(){
        System.out.println("toString");
    }
}