使用序列化反序列化实现学生管理系统
生活随笔
收集整理的這篇文章主要介紹了
使用序列化反序列化实现学生管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
要求
請使用序列化和反序列化機制,完成學生信息管理系統。
系統打開時顯示以下信息:歡迎使用學生信息管理系統,請認真閱讀以下使用說明:請輸入不同的功能編號來選擇不同的功能:[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硬盘坏道检测需要多久_卤素检测报告需
- 下一篇: OJ1034: 夏季促销