By GokiSoft.com| 20:25 14/04/2023|
Java Advanced

[Source Code] Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java - C2206L

Chương trình quản lý sinh viên Java + Import/Export JSON + File - Lập Trình Java

#ClassRoom.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 com.gokisoft.xml.bt2549;

import java.io.Serializable;
import java.util.List;

/**
 *
 * @author teacher
 */
public class ClassRoom implements Serializable{
    String name;
    String address;
    List<Student> studentList;

    public ClassRoom() {
    }

    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 List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    @Override
    public String toString() {
        return "name=" + name + ", address=" + address;
    }
    
    public void display() {
        System.out.println(this);
        System.out.println("THONG TIN SINH VIEN");
        for (Student student : studentList) {
            student.display();
        }
    }
}

#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 com.gokisoft.xml.bt2549;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author teacher
 */
public class Main {
    static List<ClassRoom> classList = new ArrayList<>();
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    readFile();
                    break;
                case 2:
                    display();
                    break;
                case 3:
                    saveFile();
                    break;
                case 4:
                    System.out.println("Thoat!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 4);
    }
    
    static void showMenu() {
        System.out.println("1. Doc du lieu class.json");
        System.out.println("2. Hien thi");
        System.out.println("3. Luu du lieu");
        System.out.println("4. Thoat");
        System.out.println("Chon: ");
    }

    private static void readFile() {
        FileReader reader = null;
        try {
            reader = new FileReader("class.json");
            Type listType = new TypeToken<List<ClassRoom>>(){}.getType();
            classList = new Gson().fromJson(reader, listType);
            
            for (ClassRoom classRoom : classList) {
                classRoom.display();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if(reader != null) {
                    reader.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }

    private static void display() {
        for (ClassRoom classRoom : classList) {
            classRoom.display();
        }
    }

    private static void saveFile() {
        for (ClassRoom classRoom : classList) {
            FileOutputStream fos = null;
            ObjectOutputStream oos = null;
            
            try {
                fos = new FileOutputStream(classRoom.getName() + ".obj");
                oos = new ObjectOutputStream(fos);
                
                oos.writeObject(classRoom);
            } 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(oos != null) {
                    try {
                        oos.close();
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                
                if(fos != null) {
                    try {
                        fos.close();
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        System.out.println("=== LUU FILE THANH CONG ===");
    }
}

#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 com.gokisoft.xml.bt2549;

import java.io.Serializable;

/**
 *
 * @author teacher
 */
public class Student implements Serializable{
    String fullname;
    String birthday;
    String email;
    String address;
    String gender;

    public Student() {
    }

    public String getFullname() {
        return fullname;
    }

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

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    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 String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "fullname=" + fullname + ", birthday=" + birthday + ", email=" + email + ", address=" + address + ", gender=" + gender;
    }
    
    public void display() {
        System.out.println(this);
    }
}
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 đó