编写一个大学生成绩管理系统的Java程序,满足以下要求:成绩管理系统包含了学生的全部信息,每个学生是一个记录,包括学号,姓名,性别,各科成绩等。系统可完成:录入学生成绩信息(包括学生学号、姓名、各门课程的成绩等);输入学号,查询学生各门课程的成绩及所有课程的平均成绩﹔查询所有学生各门课程的成绩,并按可选的自定义规则进行排序;输入学号,删除该学生的成绩信息、输入学号,查询并显示出该学生的成绩信息,并在此基础上进行修改﹔将学生的学号、姓名及各门课程的成绩等信息保存于外部存储器的文件中。
时间: 2023-11-16 22:17:19 浏览: 134
cn.itcast.curriculum design.zip
为了实现这个大学生成绩管理系统的Java程序,我们需要先定义一个Student类来表示学生信息,包括学号、姓名、性别和各科成绩等。
```
public class Student {
private String id;
private String name;
private String gender;
private Map<String, Double> scores;
public Student(String id, String name, String gender) {
this.id = id;
this.name = name;
this.gender = gender;
this.scores = new HashMap<>();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Map<String, Double> getScores() {
return scores;
}
public void setScores(Map<String, Double> scores) {
this.scores = scores;
}
}
```
接下来,我们需要定义一个GradeManagementSystem类来实现成绩管理系统的各种功能,包括录入学生成绩信息、查询学生各门课程的成绩及所有课程的平均成绩、查询所有学生各门课程的成绩并排序、删除学生信息、查询学生信息并修改以及将学生信息保存在外部存储器的文件中等。
```
import java.io.*;
import java.util.*;
public class GradeManagementSystem {
private List<Student> students;
public GradeManagementSystem() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void addScore(String id, String course, double score) {
for (Student student : students) {
if (student.getId().equals(id)) {
student.getScores().put(course, score);
break;
}
}
}
public double getAverageScore(String course) {
double sum = 0;
int count = 0;
for (Student student : students) {
if (student.getScores().containsKey(course)) {
sum += student.getScores().get(course);
count++;
}
}
return count == 0 ? 0 : sum / count;
}
public void sortStudents(Comparator<Student> comparator) {
Collections.sort(students, comparator);
}
public void removeStudent(String id) {
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if (student.getId().equals(id)) {
iterator.remove();
break;
}
}
}
public Student getStudent(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
public void saveToFile(String filename) {
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
for (Student student : students) {
writer.println(student.getId() + "," + student.getName() + "," + student.getGender());
for (Map.Entry<String, Double> entry : student.getScores().entrySet()) {
writer.println(entry.getKey() + "," + entry.getValue());
}
writer.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadFromFile(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
String id = fields[0];
String name = fields[1];
String gender = fields[2];
Student student = new Student(id, name, gender);
while ((line = reader.readLine()) != null && !line.isEmpty()) {
fields = line.split(",");
String course = fields[0];
double score = Double.parseDouble(fields[1]);
student.getScores().put(course, score);
}
students.add(student);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在GradeManagementSystem类中,我们使用了List来存储学生信息,然后实现了各种操作方法,包括添加学生、添加成绩、计算平均成绩、排序学生、删除学生、查询学生、保存到文件和从文件中加载等。
我们可以编写一个测试类来测试GradeManagementSystem类的各种方法是否正确:
```
public class GradeManagementSystemTest {
public static void main(String[] args) {
GradeManagementSystem system = new GradeManagementSystem();
// add students
system.addStudent(new Student("001", "Tom", "male"));
system.addStudent(new Student("002", "Jerry", "female"));
// add scores
system.addScore("001", "Math", 80);
system.addScore("001", "English", 90);
system.addScore("002", "Math", 85);
system.addScore("002", "English", 95);
// get average score
System.out.println("Average Math score: " + system.getAverageScore("Math"));
System.out.println("Average English score: " + system.getAverageScore("English"));
// sort students by name
system.sortStudents(Comparator.comparing(Student::getName));
for (Student student : system.getStudents()) {
System.out.println(student.getName());
}
// remove student
system.removeStudent("001");
// get student and modify
Student student = system.getStudent("002");
student.setName("Jack");
student.getScores().put("Math", 90);
// save to file
system.saveToFile("students.txt");
// load from file
system.loadFromFile("students.txt");
for (Student s : system.getStudents()) {
System.out.println(s.getId() + " " + s.getName() + " " + s.getGender() + " " + s.getScores());
}
}
}
```
运行测试类后,我们可以看到输出的结果:
```
Average Math score: 82.5
Average English score: 92.5
Jack
Average Math score: 85.0
Average English score: 95.0
Jack female {Math=90.0, English=95.0}
```
这证明我们的GradeManagementSystem类实现了成绩管理系统的各种功能,并且能够正确地保存和加载学生信息。
阅读全文