使用Java代码实现学生管理系统的增删改查
时间: 2023-09-23 16:04:05 浏览: 87
好的,以下是使用Java代码实现学生管理系统的增删改查的示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> students;
public StudentManagementSystem() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
public void updateStudent(Student student, String name, int age, String gender) {
student.setName(name);
student.setAge(age);
student.setGender(gender);
}
public Student findStudentById(int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
public void displayAllStudents() {
for (Student student : students) {
System.out.println(student.toString());
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManagementSystem system = new StudentManagementSystem();
// 添加学生
Student student1 = new Student(1, "张三", 18, "男");
Student student2 = new Student(2, "李四", 19, "女");
system.addStudent(student1);
system.addStudent(student2);
// 显示所有学生
System.out.println("所有学生信息:");
system.displayAllStudents();
// 根据ID查找学生
System.out.println("请输入要查找的学生ID:");
int id = scanner.nextInt();
Student foundStudent = system.findStudentById(id);
if (foundStudent == null) {
System.out.println("未找到该学生!");
} else {
System.out.println("查找到的学生信息:");
System.out.println(foundStudent.toString());
}
// 更新学生信息
System.out.println("请输入要更新的学生ID:");
id = scanner.nextInt();
foundStudent = system.findStudentById(id);
if (foundStudent == null) {
System.out.println("未找到该学生!");
} else {
System.out.println("请输入新的学生姓名、年龄、性别:");
String name = scanner.next();
int age = scanner.nextInt();
String gender = scanner.next();
system.updateStudent(foundStudent, name, age, gender);
System.out.println("更新成功!");
}
// 删除学生
System.out.println("请输入要删除的学生ID:");
id = scanner.nextInt();
foundStudent = system.findStudentById(id);
if (foundStudent == null) {
System.out.println("未找到该学生!");
} else {
system.removeStudent(foundStudent);
System.out.println("删除成功!");
}
// 显示所有学生
System.out.println("所有学生信息:");
system.displayAllStudents();
}
}
class Student {
private int id;
private String name;
private int age;
private String gender;
public Student(int id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "学生ID:" + id + ",姓名:" + name + ",年龄:" + age + ",性别:" + gender;
}
}
```
这个示例实现了一个简单的学生管理系统,包括添加学生、删除学生、更新学生信息、查找学生和显示所有学生等功能。在主函数中,我们添加了两个学生,然后显示所有学生的信息,接着让用户输入要查找的学生ID,根据ID查找学生并显示其信息,然后让用户输入要更新的学生ID以及新的姓名、年龄、性别,更新学生信息并提示更新成功,接着让用户输入要删除的学生ID,删除学生并提示删除成功,最后再次显示所有学生的信息。
阅读全文