使用序列化反序列化实现学生管理系统
生活随笔
收集整理的这篇文章主要介绍了
使用序列化反序列化实现学生管理系统
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
要求
请使用序列化和反序列化机制,完成学生信息管理系统。
系统打开时显示以下信息:欢迎使用学生信息管理系统,请认真阅读以下使用说明:请输入不同的功能编号来选择不同的功能:[1]查看学生列表[2]保存学生[3]删除学生[4]查看某个学生详细信息--------------------------------------------------------------------学生信息列表展示学号 姓名 性别------------------------------------1 zhangsan 男2 lisi 女.....--------------------------------------------------------------------查看某个学生详细信息学号:1姓名:张三生日:1990-10-10性别:男邮箱:zhangsan@123.com---------------------------------------------------------------------删除学生时,需要让用户继续输入删除的学生编号,根据编号删除学生。注意:请使用序列化和反序列化,以保证关闭之后,学生数据不丢失。学生数据要存储到文件中。代码
import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set;public class StuInfoMagSys {//查看学生列表private void showStu(){System.out.println("-------------------------------");System.out.println("学生信息列表展示:");System.out.println("学号\t姓名\t性别");Map<String, Student> map = readStuMap();Set<Map.Entry<String, Student>> set = map.entrySet();for (Map.Entry<String, Student> me : set){System.out.println(me.getKey()+ "\t" +me.getValue().getName()+ "\t" + ((me.getValue().isSex() == true) ? "男" : "女"));}System.out.println("------------------------------");}//保存学生private void saveStu(){Scanner scanner = new Scanner(System.in);Student student = new Student();System.out.print("请输入学生学号:");student.setNo(scanner.next());System.out.print("请输入学生姓名:");student.setName(scanner.next());Birthday bir = new Birthday();System.out.print("请输入学生出生年份:");bir.setYear(scanner.nextInt());System.out.print("请输入学生出生月份:");bir.setMonth(scanner.nextInt());System.out.print("请输入学生出生日:");bir.setDay(scanner.nextInt());student.setBir(bir);System.out.print("请输入学生性别:");String sex = scanner.next();student.setSex(sex.equals("男") ? true : false);System.out.print("请输入学生邮箱:");student.setEmail(scanner.next());Map<String, Student> map = readStuMap();map.put(student.getNo(), student);saveMap(map);}//读取学生map集合private Map<String, Student> readStuMap(){ObjectInputStream ois = null;try {ois = new ObjectInputStream(new FileInputStream("day32homework//src//StuInfo"));Map<String, Student> map = (Map<String, Student>) ois.readObject();return map;} catch (FileNotFoundException exception) {exception.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {if (ois != null) {try {ois.close();} catch (IOException e) {e.printStackTrace();}}}return null;}//存储map集合至StuInfoprivate void saveMap(Map<String, Student> map){ObjectOutputStream oos = null;try {oos = new ObjectOutputStream(new FileOutputStream("day32homework//src//StuInfo"));oos.writeObject(map);oos.flush();} catch (IOException e) {e.printStackTrace();}finally {if (oos != null) {try {oos.close();} catch (IOException e) {e.printStackTrace();}}}}//删除学生private void deleteStuInfo(){//用户输入学生学号Scanner s = new Scanner(System.in);System.out.print("请输入要删除学生学号:");String no = s.next();//删除学生信息Map<String, Student> map = readStuMap();map.remove(no);//操作后将集合重新存储saveMap(map);}//查看学生详细信息private void detailedStuInfo(){//用户输入学生学号Scanner s = new Scanner(System.in);System.out.print("请输入学生学号:");String no = s.next();Set<Map.Entry<String, Student>> set = readStuMap().entrySet();//true:存在该学号 false: 不存在该学号boolean flag = false;for (Map.Entry<String, Student> me : set){if (no.equals(me.getKey())){System.out.println( me.getValue());flag = true;}}if (flag == false){System.out.println("学号不存在!");}}/*** 打印系统操作界面* 接受用户下一步操作*/public void sysInterface(){while (true){BufferedReader br = null;try {br = new BufferedReader(new FileReader("day32homework//src//StuInfoMagSys"));String readLine = "";while ((readLine = br.readLine()) != null){System.out.println(readLine);}} catch (FileNotFoundException exception) {exception.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (br != null) {try {br.close();} catch (IOException e) {e.printStackTrace();}}}//用户输入Scanner s = new Scanner(System.in);System.out.print("请输入编号:");int choice = s.nextInt();if (choice == 1){//查看学生列表showStu();System.out.print("输入任意字符,回车键后继续操作:");s.next();}else if (choice == 2){//保存学生saveStu();System.out.print("输入任意字符,回车键后继续操作:");s.next();}else if (choice == 3){//删除学生deleteStuInfo();System.out.print("输入任意字符,回车键后继续操作:");s.next();}else if (choice == 4){//查看某个学生详细信息detailedStuInfo();System.out.print("输入任意字符,回车键后继续操作:");s.next();}else if (choice == 5){System.exit(0);}}} } import java.io.Serial; import java.io.Serializable;public class Student implements Serializable {@Serialprivate static final long serialVersionUID = -2835838366069385845L;private String no;private String name;private Birthday bir;/**性别* false: 女* true: 男*/private boolean sex;private String email;public Student() {}public Student(String no, String name, Birthday bir, boolean sex, String email) {this.no = no;this.name = name;this.bir = bir;this.sex = sex;this.email = email;}public String getNo() {return no;}public void setNo(String no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Birthday getBir() {return bir;}public void setBir(Birthday bir) {this.bir = bir;}public boolean isSex() {return sex;}public void setSex(boolean sex) {this.sex = sex;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Overridepublic boolean equals(Object obj) {if (obj == null || obj instanceof Student) return false;if (obj == this) return true;Student s = (Student) obj;return this.no == s.no;}@Overridepublic String toString() {return "学号:" + no + "\n姓名:" + name + "\n生日:" + bir + "\n性别:" + (sex ? "男" : "女") + "\n邮箱:" + email;} } import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.*;/*1、请使用序列化和反序列化机制,完成学生信息管理系统。系统打开时显示以下信息:欢迎使用学生信息管理系统,请认真阅读以下使用说明:请输入不同的功能编号来选择不同的功能:[1]查看学生列表[2]保存学生[3]删除学生[4]查看某个学生详细信息--------------------------------------------------------------------学生信息列表展示学号 姓名 性别------------------------------------1 zhangsan 男2 lisi 女.....--------------------------------------------------------------------查看某个学生详细信息学号:1姓名:张三生日:1990-10-10性别:男邮箱:zhangsan@123.com---------------------------------------------------------------------删除学生时,需要让用户继续输入删除的学生编号,根据编号删除学生。注意:请使用序列化和反序列化,以保证关闭之后,学生数据不丢失。学生数据要存储到文件中。*/ public class Homework {public static void main(String[] args) {StuInfoMagSys stuInfoMagSys = new StuInfoMagSys();stuInfoMagSys.sysInterface();} } import java.io.Serializable;public class Birthday implements Serializable {private int year;private int month;private int day;@Overridepublic String toString() {return year + "-" + month + "-" + 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;}public Birthday(int year, int month, int day) {this.year = year;this.month = month;this.day = day;}public Birthday() {} } 新人创作打卡挑战赛发博客就能抽奖!定制产品红包拿不停!总结
以上是生活随笔为你收集整理的使用序列化反序列化实现学生管理系统的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 3t硬盘坏道检测需要多久_卤素检测报告需
- 下一篇: css碎步测量,CORS系统控制点点位可