帮我写一个学生集成管理系统,要求不用Array List接口,学生类包括学号姓名以及三门成绩,并且能实现录入功能、平均成绩排序、学号排序、查找功能以及删除功能,并能一次性把数据读入内春和退出时保存在文件内
时间: 2024-03-18 20:42:14 浏览: 50
好的,这是一个比较复杂的项目,需要考虑到很多细节。我会尽力帮您完成这个项目。
首先,我们需要定义一个学生类,包括学号、姓名和三门成绩:
```java
public class Student {
private int id;
private String name;
private double score1;
private double score2;
private double score3;
public Student(int id, String name, double score1, double score2, double score3) {
this.id = id;
this.name = name;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
}
// getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore1() {
return score1;
}
public void setScore1(double score1) {
this.score1 = score1;
}
public double getScore2() {
return score2;
}
public void setScore2(double score2) {
this.score2 = score2;
}
public double getScore3() {
return score3;
}
public void setScore3(double score3) {
this.score3 = score3;
}
}
```
接下来,我们需要定义一个管理系统类,用来管理学生信息。该类包括以下功能:
- 录入学生信息
- 平均成绩排序
- 学号排序
- 查找学生信息
- 删除学生信息
- 读取和保存数据
下面是代码实现:
```java
import java.io.*;
import java.util.*;
public class StudentManagementSystem {
private List<Student> students;
public StudentManagementSystem() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void sortAverageScore() {
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
double avg1 = (o1.getScore1() + o1.getScore2() + o1.getScore3()) / 3;
double avg2 = (o2.getScore1() + o2.getScore2() + o2.getScore3()) / 3;
return Double.compare(avg2, avg1);
}
});
}
public void sortId() {
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return Integer.compare(o1.getId(), o2.getId());
}
});
}
public Student findStudentById(int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
public void deleteStudentById(int id) {
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if (student.getId() == id) {
iterator.remove();
break;
}
}
}
public void readFromFile(String filename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
int id = Integer.parseInt(fields[0]);
String name = fields[1];
double score1 = Double.parseDouble(fields[2]);
double score2 = Double.parseDouble(fields[3]);
double score3 = Double.parseDouble(fields[4]);
Student student = new Student(id, name, score1, score2, score3);
students.add(student);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveToFile(String filename) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (Student student : students) {
writer.write(student.getId() + "," + student.getName() + "," +
student.getScore1() + "," + student.getScore2() + "," + student.getScore3() + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这个管理系统类中,我们使用了ArrayList来存储学生信息,而不是使用Array List接口。我们定义了一个addStudent方法,用来录入学生信息。sortAverageScore和sortId方法分别用来实现按平均成绩排序和按学号排序。findStudentById方法用来查找学生信息,deleteStudentById方法用来删除学生信息。readFromFile和saveToFile方法用来读取和保存数据。
最后,我们可以在main函数中测试这个学生集成管理系统:
```java
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.readFromFile("students.txt");
system.addStudent(new Student(1001, "张三", 80, 90, 85));
system.addStudent(new Student(1002, "李四", 75, 85, 90));
system.addStudent(new Student(1003, "王五", 70, 80, 75));
system.sortAverageScore();
for (Student student : system.students) {
System.out.println(student.getName() + " 平均成绩:" +
(student.getScore1() + student.getScore2() + student.getScore3()) / 3);
}
System.out.println("按学号排序:");
system.sortId();
for (Student student : system.students) {
System.out.println(student.getName() + " 学号:" + student.getId());
}
System.out.println("查找学号为1001的学生:");
Student student = system.findStudentById(1001);
if (student != null) {
System.out.println("学号:" + student.getId() + " 姓名:" + student.getName());
} else {
System.out.println("未找到该学生");
}
System.out.println("删除学号为1003的学生:");
system.deleteStudentById(1003);
for (Student s : system.students) {
System.out.println(s.getName());
}
system.saveToFile("students.txt");
}
```
这个测试代码中,我们首先使用readFromFile方法从文件中读取数据,然后添加三个学生信息,然后按平均成绩排序和按学号排序,并查找学号为1001的学生信息,然后删除学号为1003的学生信息,最后使用saveToFile方法保存数据到文件中。
阅读全文