编写代码完成如下要求:
完成学生类 Student 的编写
属性:姓名、年龄、学科、成绩等
完成文件操作类 FileOperate 的编写
功能:void write(List list) 将集合数据写入文件
List read(File file) 读取文件中存储的集合数据
完成学生管理类 StudentManage 的编写
功能:
- 控制台录入学生考试成绩(学号、姓名、学科、成绩),退出当前菜单时将学生信息保存至文件中
- 控制台修改学生考试成绩(学号、学科),退出当前菜单时将学生信息保存至文件中
- 控制台查看指定学科的全部学生成绩(从高到低逆序排序)
- 控制台查看指定学生的全部成绩信息(从高到低逆序排序)
Student类
package java0609;
import java.io.Serializable;
import java.util.Objects;
public class Student implements Serializable, Comparable<Student> {
private String code;
private String name;
private String subject;
private int score;
public Student() {}
public Student(String code, String subject) {
this.code = code;
this.subject = subject;
}
public Student(String code, String name, String subject, int score) {
this.code = code;
this.name = name;
this.subject = subject;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
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;
}
@Override
public String toString() {
return "学号:" + code + " " +
"姓名:" + name + " " +
"学科:" + subject + " " +
"得分:" + score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(code, student.code) &&
Objects.equals(subject, student.subject);
}
@Override
public int hashCode() {
return Objects.hash(code, subject);
}
@Override
public int compareTo(Student s) {
if (this.getSubject().equals(s.getSubject())) {
return s.getScore() - this.getScore();
} else {
return s.getSubject().compareTo(this.getSubject());
}
}
}
FileOperate类
package java0609;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FileOperate {
public static <T> void write(List<T> list, File target) {
if (!target.exists()) {
try {
target.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(target);
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;
}
}
StudentManage类
package java0609;
import java.io.File;
import java.util.*;
public class StudentManage {
public static final File STUDENT_SCORE = new File("d:/stu_score.data");
public static List<Student> students;
static {
getStudents();
}
public static List<Student> getStudents() {
students = FileOperate.read(STUDENT_SCORE);
return students;
}
/**
* 根据姓名获取学生
* @param name
* @return
*/
public static List<Student> getStudentsByName(String name) {
List<Student> list = new ArrayList<>();
for (Student s : students) {
if (s.getName().contains(name)) {
list.add(s);
}
}
Collections.sort(list);
return list;
}
/**
* 根据学号获取学生
* @param code
* @return
*/
public static List<Student> getStudentsByCode(String code) {
List<Student> list = new ArrayList<>();
for (Student s : students) {
if (s.getCode().equals(code)) {
list.add(s);
}
}
Collections.sort(list);
return list;
}
/**
* 根据学科获取学生
* @param subject
* @return
*/
public static List<Student> getStudentsBySubject(String subject) {
List<Student> list = new ArrayList<>();
for (Student s : students) {
if (s.getSubject().equals(subject)) {
list.add(s);
}
}
Collections.sort(list);
return list;
}
// 打印学生信息
public static void printStudents(List<Student> list) {
System.out.println();
for (Student s : list) {
System.out.println(s);
}
System.out.println();
}
// 打印默认信息
public static void printDefault() {
System.out.println();
System.out.println("输入有误,请重新输入!");
System.out.println();
}
// 打印输出不匹配信息
public static void printInputMismatch() {
System.out.println();
System.out.println("输入不匹配,请重新输入!");
System.out.println();
}
// 录入学生成绩
public static void registerStudentInfo() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println();
System.out.println("1. 开始录入学生成绩");
System.out.println("0. 退出录入学生成绩");
System.out.println();
System.out.print("请输入:");
try {
int input = sc.nextInt();
if (input == 0) {
FileOperate.write(students, STUDENT_SCORE);
break;
}
if (input == 1) {
System.out.println();
System.out.print("请输入学生学号:");
String code = sc.next();
System.out.print("请输入学生姓名:");
String name = sc.next();
System.out.print("请输入考试科目:");
String subject = sc.next();
System.out.print("请输入考试成绩:");
int score = sc.nextInt();
Student stu = new Student(code, name, subject, score);
if (students.contains(stu))
students.remove(stu);
students.add(stu);
} else {
printDefault();
}
} catch (InputMismatchException e) {
printInputMismatch();
}
}
}
// 修改学生信息
public static void modifyStudentInfo() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println();
System.out.println("1. 开始修改学生成绩");
System.out.println("0. 退出修改学生成绩");
System.out.println();
System.out.print("请输入:");
try {
int input = sc.nextInt();
if (input == 0) {
FileOperate.write(students, STUDENT_SCORE);
break;
}
if (input == 1) {
System.out.println();
System.out.print("请输入学生学号:");
String code = sc.next();
System.out.print("请输入考试科目:");
String subject = sc.next();
Student stu = new Student(code, subject);
int index = students.indexOf(stu);
if (index > -1) {
System.out.print("请输入考试成绩:");
int score = sc.nextInt();
Student s = students.get(index);
s.setScore(score);
students.set(index, s);
} else {
System.out.println();
System.out.println("该学生该学科成绩还未录入");
}
} else {
printDefault();
}
} catch (InputMismatchException e) {
printInputMismatch();
}
}
}
// 查看学生成绩
public static void getStudentInfo() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println();
System.out.println("1. 查看所有学生考试信息");
System.out.println("2. 查看指定学生考试信息");
System.out.println("3. 查看指定科目考试信息");
System.out.println("0. 退出查看学生考试信息");
System.out.println();
System.out.print("请输入:");
try {
int input = sc.nextInt();
if (input == 0) break;
switch (input) {
case 1: {
getStudents();
printStudents(students);
break;
}
case 2: {
System.out.println();
System.out.print("请输入学生姓名:");
List<Student> list = getStudentsByName(sc.next());
printStudents(list);
break;
}
case 3: {
System.out.println();
System.out.print("请输入考试科目:");
List<Student> list = getStudentsBySubject(sc.next());
printStudents(list);
break;
}
default: {
printDefault();
}
}
} catch (InputMismatchException e) {
printInputMismatch();
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println();
System.out.println("1. 录入学生考试信息");
System.out.println("2. 修改学生考试信息");
System.out.println("3. 查看学生考试信息");
System.out.println("0. 退出学生考试信息");
System.out.println();
System.out.print("请输入:");
try {
int input = sc.nextInt();
if (input == 0) break;
switch (input) {
case 1: {
// 录入学生成绩
registerStudentInfo();
break;
}
case 2: {
// 修改学生成绩
modifyStudentInfo();
break;
}
case 3: {
// 查看学生成绩
getStudentInfo();
break;
}
default: {
printDefault();
}
}
} catch (InputMismatchException e) {
printInputMismatch();
}
}
}
}