用java设计个学生管理系统,能对学生成绩进行增删改查,并用io将数据保存
时间: 2024-02-28 13:56:53 浏览: 61
StudentInfo.rar
好的,我可以为您提供一份简单的Java代码,实现学生管理系统并通过IO流进行数据的保存。
首先,我们需要定义一个学生类,包括学生的姓名、学号、性别和成绩等信息,代码如下:
```java
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private String id;
private String gender;
private float score;
public Student(String name, String id, String gender, float score) {
this.name = name;
this.id = id;
this.gender = gender;
this.score = score;
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
}
```
然后,我们需要编写一个学生管理类,实现学生信息的增删改查和数据的保存,代码如下:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentManager {
private List<Student> studentList;
private String filename;
public StudentManager(String filename) {
this.filename = filename;
this.studentList = load();
}
private List<Student> load() {
List<Student> list = new ArrayList<>();
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
list = (List<Student>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
System.out.println("Load error: " + e.getMessage());
}
return list;
}
private void save() {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
oos.writeObject(studentList);
} catch (IOException e) {
System.out.println("Save error: " + e.getMessage());
}
}
public void add() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the student name: ");
String name = scanner.nextLine();
System.out.print("Enter the student ID: ");
String id = scanner.nextLine();
System.out.print("Enter the student gender: ");
String gender = scanner.nextLine();
System.out.print("Enter the student score: ");
float score = scanner.nextFloat();
scanner.nextLine();
Student student = new Student(name, id, gender, score);
studentList.add(student);
save();
}
public void delete() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the student ID: ");
String id = scanner.nextLine();
boolean found = false;
for (Student student : studentList) {
if (student.getId().equals(id)) {
studentList.remove(student);
found = true;
break;
}
}
if (!found) {
System.out.println("Student not found!");
} else {
save();
}
}
public void update() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the student ID: ");
String id = scanner.nextLine();
boolean found = false;
for (Student student : studentList) {
if (student.getId().equals(id)) {
System.out.print("Enter the new student name: ");
String name = scanner.nextLine();
System.out.print("Enter the new student gender: ");
String gender = scanner.nextLine();
System.out.print("Enter the new student score: ");
float score = scanner.nextFloat();
scanner.nextLine();
student.setName(name);
student.setGender(gender);
student.setScore(score);
found = true;
break;
}
}
if (!found) {
System.out.println("Student not found!");
} else {
save();
}
}
public void search() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the student ID: ");
String id = scanner.nextLine();
boolean found = false;
for (Student student : studentList) {
if (student.getId().equals(id)) {
System.out.println("Name: " + student.getName() + ", Gender: " + student.getGender() + ", Score: " + student.getScore());
found = true;
break;
}
}
if (!found) {
System.out.println("Student not found!");
}
}
}
```
最后,在主函数中,我们可以创建一个学生管理类对象,用于处理用户输入的命令,代码如下:
```java
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the filename to save the student info: ");
String filename = scanner.nextLine();
StudentManager manager = new StudentManager(filename);
while (true) {
System.out.println("1. Add student");
System.out.println("2. Delete student");
System.out.println("3. Update student");
System.out.println("4. Search student");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
manager.add();
break;
case 2:
manager.delete();
break;
case 3:
manager.update();
break;
case 4:
manager.search();
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice!");
}
}
}
}
```
这样,我们就实现了一个简单的学生管理系统,并通过IO流将数据保存到文件中。
阅读全文