要求

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

    系统打开时显示以下信息:欢迎使用学生信息管理系统,请认真阅读以下使用说明:请输入不同的功能编号来选择不同的功能:[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() {}
}

使用序列化反序列化实现学生管理系统相关推荐

  1. JUnit单元测试简版的学生管理系统

    JUnit的作用是:在庞大的程序项目中,要测试一个功能模块,不需要将整个庞大的项目都运行,只需要将需要测试的功能块进行JUnit测试就行 非常的方便,也很清晰,提高的开发的速度. 目前普遍使用的JUn ...

  2. 序列化/反序列化,我忍你很久了,淦!

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 工具人 上次不知道是哪个小伙伴留言说,关于对象 「序列化和反序列化 ...

  3. 字段变成小写 序列化_序列化/反序列化

    序列化是干啥用的? 序列化的原本意图是希望对一个Java对象作一下"变换",变成字节序列,这样一来方便持久化存储到磁盘,避免程序运行结束后对象就从内存里消失,另外变换成字节序列也更 ...

  4. 基于BootStrap,FortAweSome,Ajax的学生管理系统

    一. 基于BootStrap,FortAweSome,Ajax的学生管理系统代码部分 1.students.html <1>html页面文件 <!DOCTYPE html> & ...

  5. python学生管理系统-学生管理系统python

    广告关闭 腾讯云+校园是针对学生用户推出的专项扶持计划,1核2G云服务器9元/月起,云数据库2元/月起,并享受按购买价续费的优惠,助力莘莘学子轻松上云 print(该学生不存在)return none ...

  6. 【Android基础】学生管理系统

    用户可以输入姓名.性别.年龄三个字段,通过点击添加学生按钮,将学生信息展示到开始为空的ScrollView控件中,ScrollView控件只能包裹一个控件,我这里包裹的是LinearLayout.点击 ...

  7. 序列化/反序列化,我忍你很久了

    本文 Github开源项目:github.com/hansonwang99/JavaCollection 中已收录,有详细自学编程学习路线.面试题和面经.编程资料及系列技术文章等,资源持续更新中- 工 ...

  8. android的学生管理系统,Android版学生管理系统

    用户可以输入姓名.性别.年龄三个字段,通过点击添加学生按钮,将学生信息展示到开始为空的ScrollView控件中,ScrollView控件只能包裹一个控件,我这里包裹的是LinearLayout.点击 ...

  9. 用python设计学生管理系统_python+tkinter实现学生管理系统

    本文实例为大家分享了python+tkinter实现学生管理系统的具体代码,供大家参考,具体内容如下 from tkinter import * from tkinter.messagebox imp ...

最新文章

  1. 选redis还是memcache?
  2. oracle存储过程、声明变量、for循环(转)
  3. Tomcat相关目录及配置文件总结
  4. linux删除grid数据文件,MongoDB进阶系列(13)——GridFS大文件的添加、获取、查看、删除...
  5. Mysql数据库---约束类型_mysql数据库的数据类型及约束
  6. IntelliJ IDEA自动生成自定义的类注释和方法注释
  7. creo管道设计教程_Creo产品设计教程:握力器弹簧建模,一个技巧轻松搞定
  8. sql实现对多个条件分组排序方法和区别
  9. 剑指offer.数值的整数次方
  10. shell训练营Day30
  11. java短链接生成方法
  12. 快速掌握Photoshop简单用法
  13. 【Linux os7】--详细搭建LAMP+安装Zabbix4监控服务
  14. 从二维到三维,可见Web3D技术的重要性,让线上3D产品展示所见即所得
  15. npm 安装出现 UNMET DEPENDENCY 的解决方案
  16. 线程安全的随机数生成
  17. C++ time.h 库详解
  18. 原创经典-为什么Spring中的IOC(控制反转)能够降低耦合性(解耦)?
  19. 70道关于JavaScript的常见面试题解答
  20. 编译 bonjour

热门文章

  1. (二)深入了解超文本
  2. MapReduce实现计数
  3. Enterprise Solution 应用程序开发框架培训
  4. 作为面试官的一些经历,希望能给找工作的朋友一些参考
  5. 一文看懂用Python读取Excel数据
  6. 面试题:在日常工作中怎么做MySQL优化的?
  7. 面试官Diss我能力不如工作3年
  8. 年薪不到 25.2 万退学费,廖雪峰的“大数据高级开发”课程招生
  9. Linux 给新用户授予、设置Tomcat目录的使用权限
  10. Linux下安装配置Nexus