温馨提示×

java实现学生管理系统(集合ArrayList实现)

小亿
132
2023-12-19 12:16:10
栏目: 编程语言

以下是一个简单的Java实现学生管理系统的代码,使用ArrayList集合来存储学生信息。

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

class Student {
    private String name;
    private int age;
    private String id;

    public Student(String name, int age, String id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getId() {
        return id;
    }
}

public class StudentManagementSystem {
    private ArrayList<Student> students;

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

    public void addStudent(Student student) {
        students.add(student);
    }

    public void removeStudent(String id) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(id)) {
                students.remove(i);
                break;
            }
        }
    }

    public void displayStudents() {
        for (Student student : students) {
            System.out.println("Name: " + student.getName());
            System.out.println("Age: " + student.getAge());
            System.out.println("ID: " + student.getId());
            System.out.println("------------------------");
        }
    }

    public static void main(String[] args) {
        StudentManagementSystem system = new StudentManagementSystem();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("1. Add Student");
            System.out.println("2. Remove Student");
            System.out.println("3. Display Students");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("Enter student name: ");
                    String name = scanner.next();
                    System.out.print("Enter student age: ");
                    int age = scanner.nextInt();
                    System.out.print("Enter student ID: ");
                    String id = scanner.next();
                    Student student = new Student(name, age, id);
                    system.addStudent(student);
                    break;
                case 2:
                    System.out.print("Enter student ID to remove: ");
                    String removeId = scanner.next();
                    system.removeStudent(removeId);
                    break;
                case 3:
                    system.displayStudents();
                    break;
                case 4:
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
                    break;
            }
        }
    }
}

这个程序中,Student类表示学生,包含姓名、年龄和学号属性。StudentManagementSystem类表示学生管理系统,包含一个ArrayList集合来存储学生信息。

main方法中,使用一个无限循环来接受用户的输入操作。用户可以选择添加学生、删除学生、显示学生列表或退出程序。根据用户的选择,调用StudentManagementSystem类中的相应方法来执行相应的操作。

注意:这只是一个简单的示例,没有进行输入验证和错误处理。在实际使用中,可能需要添加更多的功能和逻辑来满足实际需求。

0