用java中的io流文件设计一个简单的学生成绩系统,该学生管理系统再次登录时,之前保存的学生信息可以查出来。具有以下功能:添加一个学生;查找一个学生;根据编号更新学生的基本信息(基本信息为姓名与学号);根据编号删除学生;根据编号输入学生的各门成绩(语文、数学、外语,成绩为int类型);退出系统。
时间: 2023-07-15 15:11:57 浏览: 73
好的,下面是一个简单的学生成绩管理系统的设计,具体实现如下:
1. 创建一个名为`Student`的类,用于封装学生信息,包括姓名、学号和成绩。
```java
public class Student {
private String name;
private int id;
private int chineseScore;
private int mathScore;
private int englishScore;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
}
```
2. 创建一个名为`StudentManager`的类,用于管理学生信息。可以通过该类实现添加、查找、更新和删除学生信息的功能。在该类中,我们使用文件流将学生信息保存到文件中,以便再次启动程序时可以读取之前保存的学生信息。
```java
import java.io.*;
import java.util.ArrayList;
public class StudentManager {
private ArrayList<Student> students;
private File file;
public StudentManager(String fileName) {
students = new ArrayList<>();
file = new File(fileName);
if (file.exists()) {
loadFromFile();
} else {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void add(Student student) {
students.add(student);
saveToFile();
}
public Student find(int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
public void update(int id, String name, int newId) {
Student student = find(id);
if (student != null) {
student.setName(name);
student.setId(newId);
saveToFile();
}
}
public void delete(int id) {
Student student = find(id);
if (student != null) {
students.remove(student);
saveToFile();
}
}
public void setScore(int id, int chineseScore, int mathScore, int englishScore) {
Student student = find(id);
if (student != null) {
student.setChineseScore(chineseScore);
student.setMathScore(mathScore);
student.setEnglishScore(englishScore);
saveToFile();
}
}
private void saveToFile() {
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(students);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadFromFile() {
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
students = (ArrayList<Student>) ois.readObject();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
```
3. 创建一个名为`Main`的类,用于演示学生成绩管理系统的功能。在主函数中,我们创建一个`StudentManager`对象,并通过控制台输入来实现添加、查找、更新和删除学生信息的功能。
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
StudentManager manager = new StudentManager("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. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("请输入学生姓名:");
String name = scanner.next();
System.out.print("请输入学号:");
int id = scanner.nextInt();
manager.add(new Student(name, id));
System.out.println("添加成功!");
break;
case 2:
System.out.print("请输入学号:");
id = scanner.nextInt();
Student student = manager.find(id);
if (student != null) {
System.out.println("姓名:" + student.getName());
System.out.println("学号:" + student.getId());
System.out.println("语文成绩:" + student.getChineseScore());
System.out.println("数学成绩:" + student.getMathScore());
System.out.println("英语成绩:" + student.getEnglishScore());
} else {
System.out.println("学号为" + id + "的学生不存在!");
}
break;
case 3:
System.out.print("请输入学号:");
id = scanner.nextInt();
System.out.print("请输入新的姓名:");
name = scanner.next();
System.out.print("请输入新的学号:");
int newId = scanner.nextInt();
manager.update(id, name, newId);
System.out.println("更新成功!");
break;
case 4:
System.out.print("请输入学号:");
id = scanner.nextInt();
manager.delete(id);
System.out.println("删除成功!");
break;
case 5:
System.out.print("请输入学号:");
id = scanner.nextInt();
System.out.print("请输入语文成绩:");
int chineseScore = scanner.nextInt();
System.out.print("请输入数学成绩:");
int mathScore = scanner.nextInt();
System.out.print("请输入英语成绩:");
int englishScore = scanner.nextInt();
manager.setScore(id, chineseScore, mathScore, englishScore);
System.out.println("输入成功!");
break;
case 6:
System.exit(0);
break;
default:
System.out.println("无效的选择!");
}
}
}
}
```
以上就是一个简单的学生成绩管理系统的实现,可以通过文件流来保存和读取学生信息,从而实现再次启动程序时可以查出之前保存的学生信息的功能。
阅读全文