Java语言,做出班级成绩综合评测系统,要能用io流存储和读取数据,代码实现
时间: 2023-11-30 13:04:44 浏览: 98
使用java语言实现的一个学生成绩管理系统
5星 · 资源好评率100%
好的,下面是Java代码实现班级成绩综合评测系统,可以用io流存储和读取数据的功能:
```java
import java.io.*;
import java.util.*;
class Student implements Serializable {
private String name;
private String id;
private Map<String, Double> scores;
public Student(String name, String id) {
this.name = name;
this.id = id;
scores = new HashMap<>();
}
public void addScore(String subject, double score) {
scores.put(subject, score);
}
public void removeScore(String subject) {
scores.remove(subject);
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public double getScore(String subject) {
return scores.get(subject);
}
public double getAverage() {
double sum = 0.0;
for (double score : scores.values()) {
sum += score;
}
return sum / scores.size();
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Name: " + name + "\n");
sb.append("ID: " + id + "\n");
sb.append("Scores:\n");
for (String subject : scores.keySet()) {
sb.append(subject + ": " + scores.get(subject) + "\n");
}
sb.append("Average: " + getAverage() + "\n");
return sb.toString();
}
}
class ClassScore implements Serializable {
private List<Student> students;
public ClassScore() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
public Student getStudent(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
public List<Student> getAllStudents() {
return students;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Class Score:\n");
for (Student student : students) {
sb.append(student.toString() + "\n");
}
return sb.toString();
}
}
public class ClassScoreSystem {
private ClassScore classScore;
public ClassScoreSystem() {
classScore = new ClassScore();
}
public void addStudent(Student student) {
classScore.addStudent(student);
}
public void removeStudent(Student student) {
classScore.removeStudent(student);
}
public Student getStudent(String id) {
return classScore.getStudent(id);
}
public List<Student> getAllStudents() {
return classScore.getAllStudents();
}
public void saveToFile(String filename) throws IOException {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
out.writeObject(classScore);
out.close();
}
public void loadFromFile(String filename) throws IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
classScore = (ClassScore) in.readObject();
in.close();
}
public static void main(String[] args) {
ClassScoreSystem system = new ClassScoreSystem();
// Add some students and their scores
Student s1 = new Student("Tom", "001");
s1.addScore("Math", 80.0);
s1.addScore("English", 85.0);
s1.addScore("Chinese", 90.0);
system.addStudent(s1);
Student s2 = new Student("Jerry", "002");
s2.addScore("Math", 85.0);
s2.addScore("English", 90.0);
s2.addScore("Chinese", 95.0);
system.addStudent(s2);
// Save the data to file
try {
system.saveToFile("classscore.dat");
} catch (IOException e) {
e.printStackTrace();
}
// Clear the data and load from file
system = new ClassScoreSystem();
try {
system.loadFromFile("classscore.dat");
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// Print the data
System.out.println(system.toString());
}
}
```
以上代码实现了班级成绩综合评测系统,其中使用了ObjectOutputStream和ObjectInputStream实现了数据的存储和读取。您可以根据实际需求进行修改和完善。
阅读全文