import java.io.*;
import java.util.*;
/*1、请使用序列化和反序列化机制,完成学生信息管理系统。
系统打开时显示以下信息:
欢迎使用学生信息管理系统,请认真阅读以下使用说明:
请输入不同的功能编号来选择不同的功能:
[1]查看学生列表
[2]保存学生
[3]删除学生
[4]查看某个学生详细信息
--------------------------------------------------------------------
学生信息列表展示
学号 姓名 性别
------------------------------------
1 zhangsan 男
2 lisi 女
.....
--------------------------------------------------------------------
查看某个学生详细信息
学号:1
姓名:张三
生日:1990-10-10
性别:男
邮箱:zhangsan@123.com
---------------------------------------------------------------------
删除学生时,需要让用户继续输入删除的学生编号,根据编号删除学生。
注意:请使用序列化和反序列化,以保证关闭之后,学生数据不丢失。
学生数据要存储到文件中。
*/
public class HomeWorkMap {
public static void main(String[] args) {
Operate1 operate = new Operate1();
operate.initialization();
System.out.println("欢迎使用学生信息管理系统,请认真阅读以下使用说明:");
Scanner s = new Scanner(System.in);
System.out.println("请输入不同的功能编号来选择不同的功能:" +"\n"+
"[1]查看学生列表" +"\n"+
"[2]保存学生" +"\n"+
"[3]删除学生" +"\n"+
"[4]查看某个学生详细信息"
);
while (true){
int num = s.nextInt();
if(num == 1){
operate.checkList();
}else if(num == 2){
operate.save();
}else if(num == 3){
operate.delete();
}else if(num == 4){
operate.checkStudent();
}else if(num == 5){
System.exit(0);
}else{
System.out.println("输入的编号有误,请重新输入");
}
}
}
}
// 操作类(包含初始化、四个功能、序列化、反序列化)
class Operate1{
Map<Integer,Students> map = new HashMap<>();
// 初始化,先往集合中存放几个对象
public void initialization(){
map.put(0,new Students(01,"zhangtiezhu","男","1990-10-10","zhangtiezhu@123.com"));
map.put(1,new Students(02,"licuihua","女","1992-09-24","licuihua@123.com"));
map.put(2,new Students(03,"wangergou","男","1988-05-15","wangergou@123.com"));
serialize();
}
// 功能[1]:查看学生列表
public void checkList(){
System.out.println("当前学生列表:");
deserialize();
}
// 功能[2]:保存学生
public void save(/*Student student*/){
Scanner s = new Scanner(System.in);
map.put(map.size(),new Students(s.nextInt(),s.next(),s.next(),s.next(),s.next()));
System.out.println("学生保存完毕");
serialize();
}
// 功能[3]:删除学生
public void delete(){
System.out.println("请输入要删除的学生编号");
Scanner s1 = new Scanner(System.in);
int studentNo = s1.nextInt();
if(studentNo < 0 || studentNo >= map.size()){
System.out.println("该编号不存在");
return;
}
map.remove(studentNo);
serialize();
}
// 功能[4]:检查具体某个学生的详细信息
public void checkStudent(){
Scanner s2 = new Scanner(System.in);
System.out.println("请输入要查询的学生编号");
int studentNo = s2.nextInt();
if(studentNo < 0 || studentNo >= map.size()){
System.out.println("该编号不存在");
return;
}
System.out.println(map.get(studentNo).toString2());
}
// 序列化
public void serialize(){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("students"));
oos.writeObject(map);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 反序列化
public void deserialize(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("students"));
Map<Integer,Students> deserializeMap = (Map<Integer,Students>)ois.readObject();
Set<Map.Entry<Integer,Students>> set = deserializeMap.entrySet();
//System.out.println("共有" + deserializeList.size() + "名学生");
for (Map.Entry<Integer,Students> me:set
) {
System.out.println(me);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
// 学生类
class Students implements Serializable{
private static final long serialVersionUID = 7005772656827302995L;
private int no;
private String name;
private String sex;
private String birth;
private String mail;
public Students() {
}
public Students(int no, String name, String sex, String birth, String mail) {
this.no = no;
this.name = name;
this.sex = sex;
this.birth = birth;
this.mail = mail;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
// 学生简要信息
public String toString() {
/*return "Student{" +
"学号:" + no +
", 姓名:'" + name + '\'' +
", 性别:" + sex +
'}';*/
return "学号:" + no +
", 姓名:'" + name + '\'' +
", 性别:" + sex;
}
// 输出更详细的信息
public String toString2() {
return "Student{" +
"no=" + no +
", name='" + name + '\'' +
", sex=" + sex +
", birth='" + birth + '\'' +
", mail='" + mail + '\'' +
'}';
}
}