运用java,写一个学生学籍管理系统,能够实现添加,删除,查找,修改,统计学生成绩到数据文件,计算平均成绩,计算合格率的功能,
时间: 2024-05-14 21:12:04 浏览: 83
学生学籍管理系统 有添加 删除 修改等功能
以下是一个简单的学生学籍管理系统的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 removeStudent(Student student) {
students.remove(student);
}
public Student findStudent(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
public void updateStudent(Student student) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(student.getId())) {
students.set(i, student);
return;
}
}
}
public void saveToFile(String fileName) {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
out.writeObject(students);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadFromFile(String fileName) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
students = (List<Student>) in.readObject();
in.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public double getAverageScore() {
double sum = 0;
for (Student student : students) {
sum += student.getScore();
}
return sum / students.size();
}
public double getPassRate() {
int passCount = 0;
for (Student student : students) {
if (student.isPass()) {
passCount++;
}
}
return (double) passCount / students.size();
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.loadFromFile("students.dat");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加学生");
System.out.println("2. 删除学生");
System.out.println("3. 查找学生");
System.out.println("4. 修改学生");
System.out.println("5. 统计学生成绩到数据文件");
System.out.println("6. 计算平均成绩");
System.out.println("7. 计算合格率");
System.out.println("0. 退出");
System.out.print("> ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("请输入学号:");
String id = scanner.nextLine();
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入成绩:");
double score = scanner.nextDouble();
scanner.nextLine();
Student student = new Student(id, name, score);
system.addStudent(student);
System.out.println("添加成功!");
break;
case 2:
System.out.print("请输入学号:");
id = scanner.nextLine();
student = system.findStudent(id);
if (student != null) {
system.removeStudent(student);
System.out.println("删除成功!");
} else {
System.out.println("查无此人!");
}
break;
case 3:
System.out.print("请输入学号:");
id = scanner.nextLine();
student = system.findStudent(id);
if (student != null) {
System.out.println(student);
} else {
System.out.println("查无此人!");
}
break;
case 4:
System.out.print("请输入学号:");
id = scanner.nextLine();
student = system.findStudent(id);
if (student != null) {
System.out.print("请输入姓名:");
name = scanner.nextLine();
System.out.print("请输入成绩:");
score = scanner.nextDouble();
scanner.nextLine();
student.setName(name);
student.setScore(score);
system.updateStudent(student);
System.out.println("修改成功!");
} else {
System.out.println("查无此人!");
}
break;
case 5:
System.out.print("请输入文件名:");
String fileName = scanner.nextLine();
system.saveToFile(fileName);
System.out.println("保存成功!");
break;
case 6:
double averageScore = system.getAverageScore();
System.out.println("平均成绩为:" + averageScore);
break;
case 7:
double passRate = system.getPassRate();
System.out.println("合格率为:" + passRate);
break;
case 0:
system.saveToFile("students.dat");
System.out.println("退出成功!");
return;
}
}
}
}
class Student implements Serializable {
private String id;
private String name;
private double score;
public Student(String id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
public void setName(String name) {
this.name = name;
}
public void setScore(double score) {
this.score = score;
}
public boolean isPass() {
return score >= 60;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",成绩:" + score;
}
}
```
阅读全文