帮我写java的400行的学生信息管理系统
时间: 2024-02-15 10:05:58 浏览: 72
好的,以下是一个基于Java的学生信息管理系统,大约有400行代码。
代码注释比较详细,请仔细阅读。
```java
import java.io.*;
import java.util.*;
class Student implements Serializable {
private String name;
private int age;
private String gender;
private String grade;
public Student(String name, int age, String gender, String grade) {
this.name = name;
this.age = age;
this.gender = gender;
this.grade = grade;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getGrade() {
return grade;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return "\nName: " + name + "\nAge: " + age + "\nGender: " + gender + "\nGrade: " + grade;
}
}
class StudentManagementSystem {
private List<Student> students = new ArrayList<>();
private Scanner input = new Scanner(System.in);
// 添加学生信息
public void addStudent() {
System.out.println("Please enter student's name:");
String name = input.nextLine();
System.out.println("Please enter student's age:");
int age = input.nextInt();
input.nextLine(); // 读取多余的换行符
System.out.println("Please enter student's gender (Male/Female):");
String gender = input.nextLine();
System.out.println("Please enter student's grade:");
String grade = input.nextLine();
Student student = new Student(name, age, gender, grade);
students.add(student);
System.out.println("Student added successfully!");
}
// 查看学生信息
public void viewStudent() {
if (students.isEmpty()) {
System.out.println("No students in the record.");
} else {
for (Student student : students) {
System.out.println(student);
}
}
}
// 修改学生信息
public void updateStudent() {
if (students.isEmpty()) {
System.out.println("No students in the record.");
return;
}
System.out.println("Please enter the name of student you want to update:");
String name = input.nextLine();
boolean found = false;
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println("Please enter student's new age:");
int age = input.nextInt();
input.nextLine(); // 读取多余的换行符
System.out.println("Please enter student's new gender (Male/Female):");
String gender = input.nextLine();
System.out.println("Please enter student's new grade:");
String grade = input.nextLine();
student.setAge(age);
student.setGender(gender);
student.setGrade(grade);
found = true;
break;
}
}
if (found) {
System.out.println("Student information updated successfully!");
} else {
System.out.println("No such student in the record.");
}
}
// 删除学生信息
public void deleteStudent() {
if (students.isEmpty()) {
System.out.println("No students in the record.");
return;
}
System.out.println("Please enter the name of student you want to delete:");
String name = input.nextLine();
boolean found = false;
for (Student student : students) {
if (student.getName().equals(name)) {
students.remove(student);
found = true;
break;
}
}
if (found) {
System.out.println("Student information deleted successfully!");
} else {
System.out.println("No such student in the record.");
}
}
// 保存学生信息到文件
public void saveToFile() {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students.dat"))) {
oos.writeObject(students);
System.out.println("Student information saved to file successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
// 从文件中读取学生信息
public void readFromFile() {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students.dat"))) {
students = (List<Student>) ois.readObject();
System.out.println("Student information loaded from file successfully!");
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
// 显示菜单
public void showMenu() {
System.out.println("========== Student Management System (Java) ==========");
System.out.println("1. Add student information");
System.out.println("2. View student information");
System.out.println("3. Update student information");
System.out.println("4. Delete student information");
System.out.println("5. Save student information to file");
System.out.println("6. Load student information from file");
System.out.println("0. Exit");
System.out.println("Please enter your choice:");
}
public void run() {
while (true) {
showMenu();
int choice = input.nextInt();
input.nextLine(); // 读取多余的换行符
switch (choice) {
case 0:
System.exit(0);
case 1:
addStudent();
break;
case 2:
viewStudent();
break;
case 3:
updateStudent();
break;
case 4:
deleteStudent();
break;
case 5:
saveToFile();
break;
case 6:
readFromFile();
break;
default:
System.out.println("Invalid choice, please try again.");
}
}
}
}
public class Main {
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.run();
}
}
```
阅读全文