马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
[code]import java.io.Serializable;
import java.util.Objects;
public class Student implements Serializable {
private static final long serialVersionUID= 1L;
private int stuNo;
private String name;
private boolean sex;
private String email;
private String birthday;
public Student() {
}
public Student(int stuNo, String name, boolean sex, String email, String birthday) {
this.stuNo = stuNo;
this.name = name;
this.sex = sex;
this.email = email;
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Student(int stuNo, String name, boolean sex) {
this.stuNo = stuNo;
this.name = name;
this.sex = sex;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return stuNo == student.stuNo &&
sex == student.sex &&
Objects.equals(name, student.name) &&
Objects.equals(email, student.email) &&
Objects.equals(birthday, student.birthday);
}
@Override
public int hashCode() {
return Objects.hash(stuNo, name, sex, email, birthday);
}
@Override
public String toString() {
return "Student{" +
"stuNo=" + stuNo +
", name='" + name + '\'' +
", sex=" + sex +
", email='" + email + '\'' +
", birthday='" + birthday + '\'' +
'}';
}
}
[/code]
import org.omg.PortableInterceptor.INACTIVE;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class ShowStudentPage {
public void rukou(){
while (true){
/* 欢迎使用学生信息管理系统,请认真阅读以下使用说明:
请输入不同的功能编号来选择不同的功能:
[1]查看学生列表
[2]保存学生
[3]删除学生
[4]查看某个学生详细信息*/
System.out.println("欢迎使用学生信息管理系统,请认真阅读以下使用说明:+\n" +
"请输入不同的功能编号来选择不同的功能:"+"\n"+"" +
" [1]查看学生列表\n" +
" [2]保存学生\n" +
" [3]删除学生\n" +
" [4]查看某个学生详细信息");
Scanner s = new Scanner(System.in);
System.out.print("请输入功能编号:");
int no = s.nextInt();
if (no == 1){
showStuInfo();
}else if (no == 2){
saveStudent();
}else if(no == 3 ){
deleteStu();
}else if(no == 4){
showStuAllInfo();
}else{
System.out.println("输入的编号有误,请重新输入!");
}
}
}
//保存学生
public void saveStudent() {
Scanner s = new Scanner(System.in);
Student student = new Student();
System.out.println("请输入学生编号:");
student.setStuNo(s.nextInt());
System.out.println("请输入学生姓名:");
student.setName(s.next());
System.out.println("请输入学生性别:");
student.setSex((s.next().equals("男")));
System.out.println("请输入学生生日:");
student.setBirthday(s.next());
System.out.println("请输入学生邮箱:");
student.setEmail(s.next());
Map<Integer, Student> studentMap = readMap();
studentMap.put(student.getStuNo(),student);
saveMap(studentMap);
}
//展示学生列表
public void showStuInfo() {
System.out.println("------------------------------");
System.out.println("学生信息列表展示");
System.out.println("------------------------------");
System.out.println("学号\t姓名\t性别");
Map<Integer,Student> map= readMap();
Set<Map.Entry<Integer,Student>> set = map.entrySet();
for (Map.Entry<Integer,Student> s : set) {
System.out.println(s.getKey()+"\t" + s.getValue().getStuNo() + "\t" + s.getValue().getName() +
(s.getValue().isSex()?"男" : "女"));
}
}
//展示学生详细信息
public void showStuAllInfo(){
Scanner s = new Scanner(System.in);
System.out.print("请输入学生编号:");
int no = s.nextInt();
Map<Integer,Student> map= readMap();
Set<Map.Entry<Integer,Student>> set = map.entrySet();
for (Map.Entry<Integer,Student> s1 : set) {
if (no==s1.getKey()){
System.out.println("学号:" + s1.getKey()+ "\n" +"姓名:" +s1.getValue().getName() + "\n"+"生日:" +
s1.getValue().getBirthday() + "\n" + "性别:"+
(s1.getValue().isSex()?"男" : "女") + "\n"+ "邮箱:" + s1.getValue().getEmail());
}else{
System.out.println("您输入的学生编号不存在,请重新输入!");
} }
}
//删除学生
public void deleteStu() {
Scanner s3 = new Scanner(System.in);
System.out.print("请输入要删除的学生编号:");
int no = s3.nextInt();
Map<Integer,Student> map = readMap();
map.remove(no);
saveMap(map);
}
//序列化学生集合
public void saveMap(Map<Integer,Student> map) {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("StuInfo"));
oos.writeObject(map);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//反序列化
public Map<Integer, Student> readMap() {
ObjectInputStream ois = null;
try {
Map<Integer,Student> map = null;
ois = new ObjectInputStream(new FileInputStream("StuInfo"));
if (null != ois){
map = (Map<Integer,Student>) ois.readObject();
}
return map;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
public class Test {
public static void main(String[] args) {
ShowStudentPage showStudentPage = new ShowStudentPage();
showStudentPage.rukou();
}
}
最后运行报错欢迎使用学生信息管理系统,请认真阅读以下使用说明:+
请输入不同的功能编号来选择不同的功能:
[1]查看学生列表
[2]保存学生
[3]删除学生
[4]查看某个学生详细信息
请输入功能编号:1
------------------------------
学生信息列表展示
------------------------------
学号 姓名 性别
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2335)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2804)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:802)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at ShowStudentPage.readMap(ShowStudentPage.java:139)
at ShowStudentPage.showStuInfo(ShowStudentPage.java:78)
at ShowStudentPage.rukou(ShowStudentPage.java:28)
at Test.main(Test.java:4)
Exception in thread "main" java.lang.NullPointerException
at ShowStudentPage.showStuInfo(ShowStudentPage.java:79)
at ShowStudentPage.rukou(ShowStudentPage.java:28)
at Test.main(Test.java:4)
Process finished with exit code 1
|