编写代码完成如下要求:
完成学生类 Student 的编写
属性:姓名、年龄、学科、成绩等
完成文件操作类 FileOperate 的编写
功能:void write(List list) 将集合数据写入文件
List read(File file) 读取文件中存储的集合数据
完成学生管理类 StudentManage 的编写
功能:
- 控制台录入学生考试成绩(学号、姓名、学科、成绩),退出当前菜单时将学生信息保存至文件中
- 控制台修改学生考试成绩(学号、学科),退出当前菜单时将学生信息保存至文件中
- 控制台查看指定学科的全部学生成绩(从高到低逆序排序)
- 控制台查看指定学生的全部成绩信息(从高到低逆序排序)
import java.io.Serializable;
public class Student implements Serializable, Comparable<Student>{
private String code;//学号
private String name;//名字
private String subject;//学科
private int score;//成绩
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
//无参构造
public Student() {
super();
}
//四参构造
public Student(String code, String name, String subject, int score) {
super();
this.code = code;
this.name = name;
this.subject = subject;
this.score = score;
}
//两参构造
public Student(String code, String subject) {
super();
this.code = code;
this.subject = subject;
}
@Override
public int compareTo(Student o) {
if (this.getSubject().equals(o.getSubject())) {
return o.getScore() - this.getScore();
} else {
return o.getSubject().compareTo(this.getSubject());
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((code == null) ? 0 : code.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + score;
result = prime * result + ((subject == null) ? 0 : subject.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Student)) {
return false;
}
Student other = (Student) obj;
if (code == null) {
if (other.code != null) {
return false;
}
} else if (!code.equals(other.code)) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (score != other.score) {
return false;
}
if (subject == null) {
if (other.subject != null) {
return false;
}
} else if (!subject.equals(other.subject)) {
return false;
}
return true;
}
}
public class FileOperate {
public static <T> void write(List<T> list, File file) {
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static <T> List<T> read(File file) {
List<T> list = new ArrayList<>();
if (!file.exists()) {
return list;
}
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
list = (List<T>) ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}
}