用Java设计基于文件的学生成绩管理系统,包含添加,修改,查询,删除,保存学生信息的功能
时间: 2024-05-18 15:14:01 浏览: 153
基于Java的学生成绩管理系统的设计
您好!对于基于文件的学生成绩管理系统,您可以使用 Java 来设计并实现。下面是一个简单的实现示例,包含添加、修改、查询、删除和保存学生信息的功能:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
private static final String FILE_PATH = "students.txt";
private static List<Student> students;
public static void main(String[] args) {
students = new ArrayList<>();
loadStudents();
Scanner scanner = new Scanner(System.in);
int choice;
do {
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("0. 退出");
System.out.print("请选择操作:");
choice = scanner.nextInt();
scanner.nextLine(); // 读取换行符
switch (choice) {
case 1:
addStudent(scanner);
break;
case 2:
modifyStudent(scanner);
break;
case 3:
queryStudent(scanner);
break;
case 4:
deleteStudent(scanner);
break;
case 5:
saveStudents();
break;
case 0:
System.out.println("感谢使用!");
break;
default:
System.out.println("无效的选择,请重新输入!");
break;
}
} while (choice != 0);
}
private static void addStudent(Scanner scanner) {
System.out.print("请输入学生姓名:");
String name = scanner.nextLine();
System.out.print("请输入学生学号:");
String id = scanner.nextLine();
System.out.print("请输入学生成绩:");
double score = scanner.nextDouble();
scanner.nextLine(); // 读取换行符
Student student = new Student(name, id, score);
students.add(student);
System.out.println("学生添加成功!");
}
private static void modifyStudent(Scanner scanner) {
System.out.print("请输入要修改的学生学号:");
String id = scanner.nextLine();
boolean found = false;
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.print("请输入新的学生姓名:");
String name = scanner.nextLine();
System.out.print("请输入新的学生成绩:");
double score = scanner.nextDouble();
scanner.nextLine(); // 读取换行符
student.setName(name);
student.setScore(score);
found = true;
System.out.println("学生信息修改成功!");
break;
}
}
if (!found) {
System.out.println("未找到该学生!");
}
}
private static void queryStudent(Scanner scanner) {
System.out.print("请输入要查询的学生学号:");
String id = scanner.nextLine();
boolean found = false;
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println("学生姓名:" + student.getName());
System.out.println("学生成绩:" + student.getScore());
found = true;
break;
}
}
if (!found) {
System.out.println("未找到该学生!");
}
}
private static void deleteStudent(Scanner scanner) {
System.out.print("请输入要删除的学生学号:");
String id = scanner.nextLine();
boolean removed = false;
for (Student student : students) {
if (student.getId().equals(id)) {
students.remove(student);
removed = true;
System.out.println("学生删除成功!");
break;
}
}
if (!removed) {
System.out.println("未找到该学生!");
}
}
private static void saveStudents() {
try (PrintWriter writer = new PrintWriter(FILE_PATH)) {
for (Student student : students) {
writer.println(student.getName() + "," + student.getId() + "," + student.getScore());
}
System.out.println("学生信息保存成功!");
} catch (IOException e) {
System.out.println("保存学生信息失败:" + e.getMessage());
}
}
private static void loadStudents() {
File file = new File(FILE_PATH);
if (!file.exists()) {
return;
}
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
if (parts.length == 3) {
String name = parts[0];
String id = parts[1];
double score = Double.parseDouble(parts[2]);
Student student = new Student(name, id, score);
students.add(student);
}
}
System.out.println("学生信息加载成功!");
} catch (IOException e) {
System.out.println("加载学生信息失败:" + e.getMessage());
}
}
static class Student {
private String name;
private String id;
private double score;
public Student(String name, String id, double score) {
this.name = name;
this.id = id;
this.score = score;
}
// getter 和 setter 方法省略
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
}
```
该示例使用一个 `Student` 类来表示学生对象,包含姓名、学号和成绩等属性。学生信息保存在一个 `List` 集合中,在程序运行时从文件中加载,并在退出前保存到文件中。
注意:该示例只是一个简单的实现,没有进行输入验证和异常处理。在实际应用中,您可能需要添加额外的功能和改进代码以满足实际需求。
阅读全文