Java

java怎么用集合ArrayList实现学生管理系统

小亿
91
2023-10-25 13:06:00
栏目: 编程语言

要使用ArrayList来实现学生管理系统,首先需要定义一个Student类来表示学生对象。每个学生对象可以有学号、姓名、性别、年龄等属性。

然后,在学生管理系统中可以进行如下操作:

  1. 添加学生:向ArrayList中添加一个学生对象。
  2. 删除学生:从ArrayList中删除指定学生对象。
  3. 修改学生信息:更新ArrayList中指定学生对象的属性。
  4. 查询学生信息:根据学号、姓名等条件在ArrayList中查找学生对象。
  5. 显示所有学生信息:遍历ArrayList,显示所有学生的信息。

下面是一个简单的示例代码:

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagementSystem {
    private ArrayList<Student> students;

    public StudentManagementSystem() {
        students = new ArrayList<Student>();
    }

    public void addStudent(Student student) {
        students.add(student);
        System.out.println("添加学生成功!");
    }

    public void deleteStudent(String studentId) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getStudentId().equals(studentId)) {
                students.remove(i);
                System.out.println("删除学生成功!");
                return;
            }
        }
        System.out.println("找不到指定学生!");
    }

    public void updateStudent(String studentId, Student newStudent) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getStudentId().equals(studentId)) {
                students.set(i, newStudent);
                System.out.println("更新学生信息成功!");
                return;
            }
        }
        System.out.println("找不到指定学生!");
    }

    public void queryStudent(String studentId) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getStudentId().equals(studentId)) {
                System.out.println("学生信息如下:");
                System.out.println(students.get(i));
                return;
            }
        }
        System.out.println("找不到指定学生!");
    }

    public void displayAllStudents() {
        System.out.println("所有学生信息如下:");
        for (Student student : students) {
            System.out.println(student);
        }
    }

    public static void main(String[] args) {
        StudentManagementSystem system = new StudentManagementSystem();
        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("0. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 读取换行符

            switch (choice) {
                case 1:
                    System.out.println("请输入学生学号:");
                    String studentId = scanner.nextLine();
                    System.out.println("请输入学生姓名:");
                    String name = scanner.nextLine();
                    System.out.println("请输入学生性别:");
                    String gender = scanner.nextLine();
                    System.out.println("请输入学生年龄:");
                    int age = scanner.nextInt();
                    scanner.nextLine(); // 读取换行符

                    Student newStudent = new Student(studentId, name, gender, age);
                    system.addStudent(newStudent);
                    break;
                case 2:
                    System.out.println("请输入要删除的学生学号:");
                    String deleteStudentId = scanner.nextLine();
                    system.deleteStudent(deleteStudentId);
                    break;
                case 3:
                    System.out.println("请输入要修改的学生学号:");
                    String updateStudentId = scanner.nextLine();
                    System.out.println("请输入新的学生姓名:");
                    String newName = scanner.nextLine();
                    System.out.println("请输入新的学生性别:");
                    String newGender = scanner.nextLine();
                    System.out.println("请输入新的学生年龄:");
                    int newAge = scanner.nextInt();
                    scanner.nextLine(); // 读取换行符

                    Student updatedStudent = new Student(updateStudentId, newName, newGender, newAge);
                    system.updateStudent(updateStudentId, updatedStudent);
                    break;
                case 4:
                    System.out.println("请输入要查询的学生学号:");
                    String queryStudentId = scanner.nextLine();
                    system.queryStudent(queryStudentId);
                    break;
                case 5:
                    system.displayAllStudents();
                    break;
                case 0

0
看了该问题的人还看了