By GokiSoft.com| 20:50 22/07/2022|
Java Advanced

[Source Code] File - Quản lý thông tin sinh viên - C2108L

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


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


Tags:



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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó