java student management system

[Case] ​​Student Management System

In a school, it is very troublesome to manage the flow of students. This case requires writing a student management system to realize the functions of adding, deleting, modifying and querying student information. The specific requirements for each function are as follows:

The home page of the system: used to display all the operations of the system, and select the functions to be used according to the user's input on the console.

Query function: After the user selects this function, the information of all students will be printed on the console.

Add function: After the user selects this function, the user is required to enter the basic information of the student ID, name, age and place of residence in the console. When entering the student number, judge whether the student number is occupied, if it is occupied, the addition will fail, and a corresponding prompt will be given; otherwise, the addition will be successful.

Delete function: After the user selects this function, the user will be prompted to enter the student ID of the student to be deleted in the console. If the student ID entered by the user exists, it will prompt that the deletion is successful, otherwise, it will prompt that the deletion has failed.

Modify function: After the user selects this function, the user will be prompted to enter the student ID, name, age and residence student information to be modified in the console, and use the input student ID to determine whether there is such a person, and if so, modify the original Otherwise, it will prompt that the student information to be modified does not exist.

Exit function: After the user selects this function, the program closes normally.

This case requires the use of the list collection to store custom objects, and the common methods of the list collection to implement related operations.

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

public class StudentManagementSystem {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        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. 退出系统");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    queryAllStudents(students);
                    break;
                case 2:
                    addNewStudent(students, scanner);
                    break;
                case 3:
                    deleteStudent(students, scanner);
                    break;
                case 4:
                    modifyStudent(students, scanner);
                    break;
                case 5:
                    System.out.println("感谢您使用本系统!");
                    return;
                default:
                    System.out.println("无效选项,请重新输入!");
            }
        }
    }

    private static void queryAllStudents(ArrayList<Student> students) {
        if (students.isEmpty()) {
            System.out.println("当前没有任何学生信息!");
        } else {
            for (Student student : students) {
                System.out.println(student);
            }
        }
    }

    private static void addNewStudent(ArrayList<Student> students, Scanner scanner) {
        System.out.print("请输入新学生的学号:");
        String id = scanner.next();
        for (Student student : students) {
            if (student.getId().equals(id)) {
                System.out.println("该学号已经被占用,请重新输入!");
                return;
            }
        }
        System.out.print("请输入新学生的姓名:");
        String name = scanner.next();
        System.out.print("请输入新学生的年龄:");
        int age = scanner.nextInt();
        System.out.print("请输入新学生的居住地:");
        String address = scanner.next();
        Student newStudent = new Student(id, name, age, address);
        students.add(newStudent);
        System.out.println("添加成功!");
    }

    private static void deleteStudent(ArrayList<Student> students, Scanner scanner) {
 System.out.print("请输入需要删除的学生的学号:");
        String id = scanner.next();
        boolean found = false;
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getId().equals(id)) {
                students.remove(i);
                found = true;
                break;
            }
        }
        if (found) {
            System.out.println("删除成功!");
        } else {
            System.out.println("该学号不存在,请重新输入!");
        }
    }

    private static void modifyStudent(ArrayList<Student> students, Scanner scanner) {
        System.out.print("请输入需要修改的学生的学号:");
        String id = scanner.next();
        boolean found = false;
        for (Student student : students) {
            if (student.getId().equals(id)) {
                System.out.print("请输入新的学生姓名:");
                String name = scanner.next();
                System.out.print("请输入新的学生年龄:");
                int age = scanner.nextInt();
                System.out.print("请输入新的学生居住地:");
                String address = scanner.next();
                student.setName(name);
                student.setAge(age);
                student.setAddress(address);
                found = true;
                break;
            }
        }
        if (found) {
            System.out.println("修改成功!");
        } else {
            System.out.println("该学号不存在,请重新输入!");
        }
    }
}

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

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

    public String getId() {
        return id;
    }

    public void setId(String 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 getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "学号:" + id + ",姓名:" + name + ",年龄:" + age + ",居住地:" + address;
    }
}

Guess you like

Origin blog.csdn.net/m0_61594817/article/details/129778302