网站建设资讯

NEWS

网站建设资讯

序列化反序列化实现学生管理系统(小白级别的很麻烦)-创新互联

package StudentIOTest02;

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

public class Student implements java.io.Serializable {
    @Serial
    private static final long serialVersionUID = -6284145330510018985L;
    private  int no;
    private  String name;
    private  boolean sex;
    private  Birth  date;//不参与序列化
    private  Serializable serializable;
    private  DeSerializable deSerializable;
    private transient Liststudents;
//list使用transient关键字 是为了给toString一个标记 运用三目运算符来决定toString输出形式
//(当然可以用两个toString方法)
    public Student() {
//        默认初始化
        this.serializable = new Serializable();
        this.deSerializable = new DeSerializable();
        this.students = new ArrayList();
    }

    public Student(int no, String name, boolean sex, Birth date) {
        this.no = no;
        this.name = name;
        this.sex = sex;
        this.date = date;
    }
//    由于进入系统需要初始化已有学生
    public void loading() throws IOException {
        this.students.add(new Student(1,"zhangsan",true,new Birth(2004,5,2)));
        this.students.add(new Student(2,"lisi",true,new Birth(1981,7,14)));
        this.serializable.ouputStrean(this.students);
    }
//    [1]查看学生列表
    public void  checkList() throws IOException, ClassNotFoundException {
        this.deSerializable.inputStream();
    }
//    [2]增加学生
    public void addStudent() throws IOException {
        while(true)
        {
            System.out.println("请输入学生的学号");
            Scanner scanner = new Scanner(System.in);
            int num =scanner.nextInt();
//增加需要在末尾加   获取学号大值 加入学生
            if(num == this.students.size()+1)
            {
                System.out.println("请依次输入学生的姓名、性别、出生年月日");
                this.students.add(new Student(num,scanner.next(),scanner.nextBoolean(),new Birth(scanner.nextInt(),scanner.nextInt(),scanner.nextInt())));
                this.serializable.ouputStrean(this.students);
                System.out.println("添加成功");
                this.serializable.ouputStrean(this.students);
                Collections.sort(this.students, new Comparator() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o1.no - o2.no;
                    }
                });
                break;
            }else System.out.println("请重新输入");
        }

    }
//    [3]删除学生
    public void deleteStudent( ) throws IOException {
        while(true)
        {
            Scanner scanner = new Scanner(System.in);
            int n1 = scanner.nextInt();
            if(n1 >0 && n1< this.students.size()) {
//移除元素是需要-1 因为是下标
                this.students.remove(n1 - 1);
                this.serializable.ouputStrean(this.students);
                System.out.println("删除成功");
                this.serializable.ouputStrean(this.students);
//进行排序
                Collections.sort(this.students, new Comparator() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o1.no - o2.no;
                    }
                });
                break;
            }else System.out.println("您输入的学号不存在,重新输入");

        }

    }
//[4]查看某个学生详细信息
    public void readAll()
    {
        while(true)
        {
            System.out.println("输入您要查看学生的学号");
            Scanner scanner = new Scanner(System.in);
            int no = scanner.nextInt();
            if (no == 0 || no >this.students.size())
            {
                System.out.println("不存在该学生");
            }else if(! (this.students.get(no-1)  instanceof  Student))
            {
                System.out.println("不存在该学生,请重新输入");
            }else
            {
                System.out.println(this.students.get(no-1).toString());
                break;
            }
        }
    }
//    查找某位学生是否存在
    public void searchStudent()
    {
        while(true)
        {
            System.out.println("输入您要查找学生的学号");
            Scanner scanner = new Scanner(System.in);
            int no = scanner.nextInt();
            if (no == 0 || no >this.students.size())
            {
                System.out.println("不存在该学生");
            }else if(! (this.students.get(no-1)  instanceof  Student))
            {
                System.out.println("不存在该学生");
            }else
            {
                System.out.println("找到了该学生!!");
                System.out.println(this.students.get(no-1).toString());
                break;
            }
        }

    }
//    6 修改某位同学的信息
    public void correct() throws IOException {
        while(true) {
            System.out.println("输入您所修改学生的学号");
            Scanner scanner = new Scanner(System.in);
            int no = scanner.nextInt();
            if (no == 0 || no >this.students.size()) {
                System.out.println("不存在该学生");
            } else if (!(this.students.get(no - 1) instanceof Student)) {
                System.out.println("不存在该学生");
            } else {
               Scanner scanner1 = new Scanner(System.in);
                System.out.println("请依次输入该学生的学号、姓名、性别(True或False[True==男])、出生年、月、日");
               Student student = new Student(scanner1.nextInt(),scanner.next(),scanner.nextBoolean(),
                       new Birth(scanner1.nextInt(),scanner1.nextInt(),scanner1.nextInt()));
               this.students.remove(no);//移除该学生
                this.students.add(student);//加入新的信息
                this.serializable.ouputStrean(this.students);
//                按照升序排序
                Collections.sort(this.students, new Comparator() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o1.no - o2.no;
                    }
                });

                break;

            }
        }
    }

    public int getNo() {
        return no;
    }

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

    public String getName() {
        return name;
    }

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

    public boolean isSex() {
        return sex;
    }

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

    public Birth getDate() {
        return date;
    }

    public void setDate(Birth date) {
        this.date = date;
    }

    public Serializable getSerializable() {
        return serializable;
    }

    public void setSerializable(Serializable serializable) {
        this.serializable = serializable;
    }

    public DeSerializable getDeSerializable() {
        return deSerializable;
    }

    public void setDeSerializable(DeSerializable deSerializable) {
        this.deSerializable = deSerializable;
    }

    public ListgetStudents() {
        return students;
    }

    public void setStudents(Liststudents) {
        this.students = students;
    }
    @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;
    }

    @Override
    public String toString() {
        return (this.students == null)?("学生学号"+this.getNo()+"\n"+
                "名字"+this.getName()+"\n"+
                "性别"+(this.isSex()?"男":"女")+"\n"+
                "生日"+this.date.getYear()+"-"+this.date.getMonth()+""+this.date.getDay()):"学生学号"+this.getNo()+"\t"+
                "名字"+this.getName()+"\t"+
                "性别"+(this.isSex()?"男":"女")+"\t" ;
    }

    @Override
    public int hashCode() {
        return Objects.hash(no);
    }
}
//学生生日
class Birth implements java.io.Serializable
{
    @Serial
    private static final long serialVersionUID = 4769685585810521642L;
    private  int year;
    private int month;
    private int day;

    public Birth() {
    }

    public Birth(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
}

//序列化
class Serializable
{
    public void ouputStrean(Liststudents) throws IOException {
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\33769\\Desktop\\JavaTestIOFile\\Test02"));
        outputStream.writeObject(students);
//        刷新
        outputStream.flush();
//        关闭
        outputStream.close();
    }
}
//反序列化
class DeSerializable
{
    public void inputStream() throws IOException, ClassNotFoundException {
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("C:\\Users\\33769\\Desktop\\JavaTestIOFile\\Test02"));
      Liststudents1 = (List) inputStream.readObject();
        for (Student student: students1
             ) {
            System.out.println(student.toString());
        }
//        关闭
        inputStream.close();
    }

}

Test测试实现代码:

成都创新互联网站建设由有经验的网站设计师、开发人员和项目经理组成的专业建站团队,负责网站视觉设计、用户体验优化、交互设计和前端开发等方面的工作,以确保网站外观精美、网站设计、做网站易于使用并且具有良好的响应性。
package StudentIOTest02;

import java.io.IOException;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
//    初始化系统
        Student student = new Student();
        try {
            student.loading();//加载学生
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        Scanner scanner = new Scanner(System.in);
       A: while(true)
        {
            System.out.println("欢迎使用学生信息管理系统,请认真阅读以下使用说明:\n" +
                    "请输入不同的功能编号来选择不同的功能:\n" +
                    "[1]查看学生列表\n" +
                    "[2]增加学生 \n" +
                    "[3]删除学生\n" +
                    "[4]查看某个学生详细信息\n"+
                    "[5]查找某位学生\n"+
                    "[6]修改某位同学的信息");
            int no = scanner.nextInt();
            if(no<0 || no >6)
            {
                System.out.println("请重新输入");
            }else if(no == 1)
            {
                try {
                    student.checkList();
                    continue  A;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }else if(no == 2)
            {
                try {
                    student.addStudent();
                    continue  A;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else if (no == 3) {
                try {
                    student.deleteStudent();
                    continue  A;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }else if(no == 4)
            {
                student.readAll();
                continue A;
            }else if(no == 5)
            {
                student.searchStudent();
                continue  A;
            }else if (no == 6)
            {
                try {
                    student.correct();
                    continue  A;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻标题:序列化反序列化实现学生管理系统(小白级别的很麻烦)-创新互联
标题链接:http://cdweb.net/article/djoiie.html