Day27.IO流作业:学生信息管理系统

 1、请使用序列化和反序列化机制,完成学生信息管理系统。

系统打开时显示以下信息:
欢迎使用学生信息管理系统,请认真阅读以下使用说明:
请输入不同的功能编号来选择不同的功能:
[1]查看学生列表
[2]保存学生
[3]删除学生
[4]查看某个学生详细信息

--------------------------------------------------------------------
学生信息列表展示
学号            姓名            性别
--------------------------------------------------------------------
1                zhangsan        男
2                lisi            女

--------------------------------------------------------------------
查看某个学生详细信息
学号:1
姓名:张三
生日:1990-10-10
性别:男
邮箱:zhangsan@123.com

  • 主程序、main方法 
import java.io.*;
import java.util.Scanner;
import java.util.TreeSet;

public class Text {
    public static void main(String[] args) {
        StudentService srv = new StudentService();      //学生系统后台
        TreeSet<Student> students = null;      //初始化学生集合
        File file = new File("IOfile\\StudentObj");     //file对象
        ObjectOutputStream oos = null;          //对象输出流
        ObjectInputStream ois = null;       //对象输入流
        //如果对象文件不存在
        if(!(file.exists())){
            //创建学生集合,集合中自带两个学生
            students= new TreeSet<>();
            Student zs = new Student(1,"张三","1990-10-10","男","zhangsan@123.com");
            Student ls = new Student(2,"李四","1991-7-24","男","lisi@123.com");
            students.add(zs);
            students.add(ls);
            //IO流
            try {    //创建对象流,传入file路径
                oos = new ObjectOutputStream(new FileOutputStream(file));
                oos.writeObject(students);    //写入文件

                oos.flush();
            } catch (IOException e) {...} finally {...}}

        }else{      //对象文件存在,读取文件
            try {   //创建对象输入流对象,传入文件路径
                ois = new ObjectInputStream(new FileInputStream(file));
                students = (TreeSet<Student>)ois.readObject();      //读取
            } catch (IOException e) {...} finally {...}}

        }
        Scanner scanner = null; //scanner用户交互
        System.out.println("欢迎使用学生信息管理系统,请认真阅读以下使用说明:");
        while (true){
            scanner = new Scanner(System.in);
            int i = -1;
            System.out.println("-------------------------------------------------");
            System.out.println("请输入不同的功能编号来选择不同的功能:\n" +
                    "[0]退出程序\n" +
                    "[1]查看学生列表\n" +
                    "[2]加入学生\n" +
                    "[3]删除学生\n" +
                    "[4]查看学生个人详细信息");
            if (scanner.hasNextInt()){
                i = scanner.nextInt();
            }else {
                System.out.println("非法字符,请认真阅读使用说明。");
                continue;
            }
            switch (i){
                case 1:
                    srv.printList(students);
                    break;
                case 2:
                    srv.addStudent(students);
                    break;
                case 3:
                    srv.delStudent(students);
                    break;
                case 4:
                    srv.print(students);
                    break;
            }
            if(i == 0){
                System.out.println("数据保存中,请勿关闭应用。");
                break;
            }
        }
        try {   //保存集合对象信息
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(students);
            oos.flush();
        } catch (IOException e) {...} finally {...}}

        System.out.println("存储成功,欢迎再次使用。");
    }
}
  • StudentService 
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class StudentService {
    Scanner scanner = new Scanner(System.in);

    //添加学生方法
    public void addStudent(TreeSet<Student> ts){
        int ID = -1;
        String name;
        String birthDay;
        String gender;
        String email;
        System.out.println("请输入学生学号:"); //学号
        while (true){
            if(scanner.hasNextInt()) {
                ID = scanner.nextInt();
                System.out.println("学号为:" +ID);
                break;
            }
            System.out.println("输入错误,请重新输入:");
            scanner = new Scanner(System.in);
        }
        //遍历集合,查找是否有重复ID
        if(queryID(ts,ID)!=null){
            System.out.println("已存在学号为"+ID+"的学生,添加失败。");
            return;
        }

        System.out.println("请输入学生姓名:");
        name = scanner.next();
        System.out.println(name);
        System.out.println("请输入学生生日:xxxx-xx-xx");
        birthDay = scanner.next();
        System.out.println(birthDay);
        System.out.println("请输入学生性别:");
        gender = scanner.next();
        System.out.println(gender);
        System.out.println("请输入学生邮箱:");
        email = scanner.next();
        System.out.println(email);
        Student s = new Student(ID,name,birthDay,gender,email);
        ts.add(s);
        System.out.println("已添加学生信息");
    }
    //删除学生方法
    public void delStudent(TreeSet<Student> ts){
        int id = -1;
        System.out.println("请输入要删除的学生ID:");
        if(scanner.hasNextInt()) {
            id = scanner.nextInt();
            System.out.println("删除学号为:" +id);
            Student st = queryID(ts,id);        //判断学号为id的对象是否存在
            if(st!=null){
                ts.remove(st);
                System.out.println("删除成功");     //待加入判断,打印学生信息,是否删除
            }else {
                System.out.println("当前学号不存在,删除失败。");
            }
        }else {
            System.out.println("输入的不是学号,删除失败。");
        }
    }
    //打印学生列表
    public void printList(TreeSet<Student> ts){
        Iterator it = ts.iterator();
        System.out.println("-------------------------------------------------");
        System.out.println("学生信息列表展示");
        System.out.println("学号     姓名     性别");
        while (it.hasNext()){
            Student s = (Student) it.next();
            System.out.println(s.getId()+"       "+s.getName()+"      "+s.getGender());
        }
    }
    //打印学生信息
    public void print(TreeSet<Student> ts){
        System.out.println("请输入要查询的学号:");
        if(scanner.hasNextInt()){
            int id = scanner.nextInt();
            Student st = queryID(ts,id);
            if(st!=null){
                System.out.println("-------------------------------------------------");
                System.out.println("学号:"+st.getId()+'\n'+"姓名:"+st.getName()+'\n'+"生日:"+st.getBirthDay()+'\n'+"性别:"+st.getGender()+'\n'+"邮箱:"+st.getEmail());
            }else{
                System.out.println("该学号不存在,查询失败");
            }
        }else {
            System.out.println("非法字符,查询失败");
        }
    }
    //遍历集合,查找学生ID ,找到返回学生对象内存地址
    public Student queryID(TreeSet<Student> ts,int id){
        Iterator it = ts.iterator();
        while (it.hasNext()){
            Student s = (Student) it.next();
            if(s.getId() == id){
                return s;
            }
        }
        return null;
    }
  • 学生对象:
import java.io.Serializable;

public class Student implements Comparable<Student>, Serializable {
    
    private static final long serialVersionUID = 1L;
    private int id ;
    private String name;
    private String birthDay;
    private String gender;
    private String email;

    //get、set、无参、有参省略

    @Override
    public String toString() {
        return getId()+""+getName()+""+getGender();
    }

    public int compareTo(Student s) {
        return this.getId()-s.getId();
    }

}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值