運用序列化與反序列化完成學生信息系統


1、請使用序列化和反序列化機制,完成學生信息管理系統。

系統打開時顯示以下信息:
歡迎使用學生信息管理系統,請認真閱讀以下使用說明:
請輸入不同的功能編號來選擇不同的功能:
[1]查看學生列表
[2]保存學生
[3]刪除學生
[4]查看某個學生詳細信息

--------------------------------------------------------------------
學生信息列表展示
學號 姓名 性別
------------------------------------
1 zhangsan 男
2 lisi 女
.....

--------------------------------------------------------------------
查看某個學生詳細信息
學號:1
姓名:張三
生日:1990-10-10
性別:男
郵箱:zhangsan@123.com

---------------------------------------------------------------------
刪除學生時,需要讓用戶繼續輸入刪除的學生編號,根據編號刪除學生。

注意:請使用序列化和反序列化,以保證關閉之后,學生數據不丟失。
學生數據要存儲到文件中。

 

根據題意我增加了一些功能,具體功能如下

[1]查看學生列表
[2]保存學生
[3]刪除學生
[4]查看某個學生詳細信息
[5]添加學生
[6]退出系統
[0]調出使用菜單

在增加新學生信息進入系統時可根據學生的學號進行自動排序。

扔出代碼:

package com.cy.javase.serializable;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.*;

public class StudentSystem extends Thread{
    private static List<Student> students= null;
    @Override
    public void run() {
        //調用初始化方法讀取學生信息
        students= StudentSystemInitialized.initialized();
        System.out.println("歡迎使用學生信息管理系統,請認真閱讀以下使用說明:");
        System.out.println("請輸入不同的功能編號來選擇不同的功能:");
        System.out.println("[1]查看學生列表\n[2]保存學生\n[3]刪除學生\n[4]查看某個學生詳細信息\n[5]添加學生\n[6]退出系統\n[0]調出使用菜單");
        while (true){
            Scanner s = new Scanner(System.in);
            int i =  s.nextInt();
            switch (i){
                case 0:
                    System.out.println("[1]查看學生列表\n[2]保存學生\n[3]刪除學生\n[4]查看某個學生詳細信息\n[5]添加學生\n[6]退出系統\n[0]調出使用菜單");
                    break;
                case 1:
                    System.out.println("----------------------------------------------");
                    System.out.println("學生信息展示");
                    System.out.println("學號          姓名          性別");
                    System.out.println("----------------------------------------------");
                    for (Student student:students){
                        System.out.println(student.getNo()+"         "+student.getName()+"          "+ (student.isGender()?"男":"女"));
                    }
                    System.out.println("-----------------------------------------------");
                    break;
                case 2:
                    save();
                    break;
                case 3:
                    System.out.print("請輸入要刪除學生信息的學號:");
                    deleStudent(new Scanner(System.in).nextInt());
                    break;
                case 4:
                    System.out.print("請輸入要查看學生信息的學號:");
                    information(new Scanner(System.in).nextInt());
                    break;
                case 5:
                    System.out.print("請輸入學生的下列信息:\n[姓名]:");
                    String name = new Scanner(System.in).next();
                    System.out.print("[學號]:");
                    int no = new Scanner(System.in).nextInt();
                    System.out.print("[性別(男為true,女為false)]:");
                    boolean gender = new Scanner(System.in).nextBoolean();
                    System.out.print("[生日(yy-MM-dd)]:");
                    String birth = new Scanner(System.in).next();
                    System.out.print("郵箱:");
                    String email = new Scanner(System.in).next();
                    addStudent(name,no,gender,birth,email);
                    break;
                case 6:
                    System.exit(0);
                default:
            }
        }
    }
    //添加學生功能
    private void addStudent(String name, int no, boolean gender, String birth, String email){
        Student stu = new Student(name, no,  gender,  birth,  email);
        //根據學生學號進行排序放入ArrayList數組
        if(students.size() == 0){
            students.add(stu);
        }else {
            for (Student student : students) {
                if (student.getNo()>stu.getNo()){
                    students.add(students.indexOf(student),stu);
                    return;
                }
            }
            students.add(stu);
        }
    }
    //刪除學生功能
    private void deleStudent(int no){
        Student str = null;
        for (Student student:students){
            if(student.getNo() == no){
                str = student;
            }
        }
        students.remove(students.indexOf(str));
    }
    //學生信息詳情展示功能
    private void information(int no){
        Student str = null;
        for (Student student:students){
            if(student.getNo() == no){
                str = student;
            }
        }
        System.out.println("-----------------------------------------------");
        System.out.println("學號:"+str.getNo()+"\n"+"姓名:"+str.getName()+"\n"+"生日:"+str.getBirth()+"\n"+"性別:"+(str.isGender()?"男":"女")+"\n"+"郵箱:"+str.getEmail());
        System.out.println("-----------------------------------------------");
    }
    //保存功能
    private void save(){
        ObjectOutputStream oos= null;
        try {
            //此處因idea工具Thread.currentThread().getContextClassLoader().getResource().getPath()方法讀取的默認路徑為類的根目錄下out/production/類名
            //所以序列化文件的輸出地址要手寫至初始化方法可讀取的地址。未滿足OCP原則,有待改進。
             oos= new ObjectOutputStream(new FileOutputStream("out/production/day4/students"));
             oos.writeObject(students);
             oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null!=oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

初始化學生信息方法

package com.cy.javase.serializable;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

public class StudentSystemInitialized {
    public static List<Student> initialized(){
        ObjectInputStream ois = null;
        //逆序列化學生信息文件,若沒有文件,則初始化一個空的學生信息文件。因為OCP原則讀取文件的地址用到了Thread.currentThread().getContextClassLoader().getResource().getPath()方法
        try {
            ois = new ObjectInputStream(new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("students").getPath()));
            List<Student> students =  (List<Student>) ois.readObject();
            return students;
        } catch (IOException | ClassNotFoundException|NullPointerException e) {
            return new ArrayList<Student>();
        }finally {
            if (ois!=null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

學生類

package com.cy.javase.serializable;

import java.io.Serializable;
import java.util.Objects;

public class Student implements Serializable {
    private String name ;
    private int no;
    private boolean gender;
    private String birth;
    private String email;

    public Student(){
    }

    public Student(String name, int no, boolean gender, String birth, String email) {
        this.name = name;
        this.no = no;
        this.gender = gender;
        this.birth = birth;
        this.email = email;
    }

    public String getName() {
        return name;
    }

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

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public boolean isGender() {
        return gender;
    }

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

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getEmail() {
        return email;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return no == student.no &&
                gender == student.gender &&
                Objects.equals(name, student.name) &&
                Objects.equals(birth, student.birth) &&
                Objects.equals(email, student.email);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, no, gender, birth, email);
    }
}

測試主方法

package com.cy.javase.serializable;


public class Test {
    public static void main(String[] args) {
        Thread thread = new StudentSystem();
        thread.run();
    }
}

測試結果示例

運行程序后先加入三個學生的信息

保存后顯示當前學生信息,可見學生以按學號排序

退出后重啟程序,程序可逆序列化保存的序列化文件

測試刪除程序

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM