Java Review Notes - Student Management System


Student Management System

One, the demand part

need

Take the console method to write the student management system.

analyze

initial menu

"-------------欢迎进入学生管理系统----------------"
"1:添加学生"
"2:删除学生"
"3:修改学生"
"4:查询学生"
"5:退出"
"请输入您的选择:"

student class

Attributes: id, name, age, home address

add function

To enter and add each student information with the keyboard, the following requirements need to be met:

  • unique id

delete function

To enter the student ID to be deleted by keyboard, the following requirements must be met:

  • id exist delete
  • The id does not exist, you need to prompt that it does not exist, and return to the initial menu

modify function

To enter the student id to be modified by keyboard, the following requirements need to be met

  • id exists, continue to enter other information
  • The id does not exist, you need to prompt that it does not exist, and return to the initial menu

query function

To print all student information, the following requirements need to be met

  • If there is no student information, it will prompt: there is currently no student information, please add it and then query
  • If there is student information, it needs to be output in the following format. (Don't worry too much about alignment)
id		姓名		年龄		家庭住址
2023001	张三		23		 南京
2023002	李四		24		 北京
2023003	王五		25		 广州
2023004	赵六	 	26		 深圳

Second, the implementation part

(1) Initialize the main interface

To initialize the main interface of the student management system, you can operate in the following way:

  1. Create a student management system object for managing student information.
  2. Use a loop to keep the main screen displayed until the user chooses to exit.
  3. In the loop, print the main interface menu for the user to choose an operation.
  4. According to the user's choice, call the corresponding method of the student management system object to operate.
  5. According to the operation result, display the corresponding feedback information to the user.

The following is a sample code snippet that demonstrates how to initialize the main interface of the student management system:

import java.util.Scanner;

public class StudentManagementSystem {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int choice = 0;
        
        while (choice != 5) {
    
    
            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.print("请输入您的选择: ");
            
            choice = scanner.nextInt();
            
            switch (choice) {
    
    
                case 1:
                    // 添加学生逻辑
                    break;
                case 2:
                    // 删除学生逻辑
                    break;
                case 3:
                    // 修改学生逻辑
                    break;
                case 4:
                    // 查询学生逻辑
                    break;
                case 5:
                    System.out.println("感谢使用学生管理系统,再见!");
                    break;
                default:
                    System.out.println("无效的选择,请重新输入!");
                    break;
            }
            
            System.out.println(); // 输出空行,增加可读性
        }
        
        scanner.close();
    }
}

In this example, we first created a StudentManagementSystem object system for managing student information. Then, in an infinite loop, print the main interface menu for the user to choose an action. According to the user's choice, we call the corresponding student management system method to operate. When the user chooses to exit, we use System.exit(0) to end the execution of the program.

(2) Write the student class

Attributes contained in the student class: id, name, age, home address

To write a student class, you can do it in the following way:

  • Define the student class, set its properties and methods.
  • According to the requirements, determine the attributes of the student class, such as id, name, age, and home address.
  • Add corresponding accessor (getter) and modifier (setter) methods for each property, used to get and set the value of the property.

Here is an example student class snippet that demonstrates how to write a student class:

public class Student {
    
    
    private int id;
    private String name;
    private int age;
    private String address;
    
    public Student(int id, String name, int age, String address) {
    
    
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }
    
    // Getter and Setter methods
    
    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 getAddress() {
    
    
        return address;
    }
    
    public void setAddress(String address) {
    
    
        this.address = address;
    }
}

(3) Write the method of adding students

public void addStudent() {
    
                                    
    Scanner scanner = new Scanner(System.in);             
                                                          
    System.out.println("请输入学生ID:");                       
    int id = scanner.nextInt();                           
    scanner.nextLine(); // 消费掉换行符                         
                                                          
    System.out.println("请输入学生姓名:");                       
    String name = scanner.nextLine();                     
                                                          
    System.out.println("请输入学生年龄:");                       
    int age = scanner.nextInt();                          
    scanner.nextLine(); // 消费掉换行符                         
                                                          
    System.out.println("请输入学生家庭住址:");                     
    String address = scanner.nextLine();                  
                                                          
    Student student = new Student(id, name, age, address);
    students.add(student);                                
                                                          
    System.out.println("学生添加成功!");                        
}                                                         

(4) Write the method of deleting students

public void deleteStudent() {
    
                    
    Scanner scanner = new Scanner(System.in);
                                             
    System.out.println("请输入要删除的学生ID:");      
    int id = scanner.nextInt();              
                                             
    boolean found = false;                   
                                             
    for (Student student : students) {
    
           
        if (student.getId() == id) {
    
             
            students.remove(student);        
            found = true;                    
            System.out.println("学生删除成功!");   
            break;                           
        }                                    
    }                                        
                                             
    if (!found) {
    
                                
        System.out.println("未找到匹配的学生ID!");   
    }                                        
}              

(5) Write and modify student methods

public void updateStudent() {
    
                        
    Scanner scanner = new Scanner(System.in);    
                                                 
    System.out.println("请输入要修改的学生ID:");          
    int id = scanner.nextInt();                  
    scanner.nextLine(); // 消费掉换行符                
                                                 
    boolean found = false;                       
                                                 
    for (Student student : students) {
    
               
        if (student.getId() == id) {
    
                 
            System.out.println("请输入学生姓名:");      
            String name = scanner.nextLine();    
            student.setName(name);               
                                                 
            System.out.println("请输入学生年龄:");      
            int age = scanner.nextInt();         
            scanner.nextLine(); // 消费掉换行符        
            student.setAge(age);                 
                                                 
            System.out.println("请输入学生家庭住址:");    
            String address = scanner.nextLine(); 
            student.setAddress(address);         
                                                 
            found = true;                        
            System.out.println("学生修改成功!");       
            break;                               
        }                                        
    }                                            
                                                 
    if (!found) {
    
                                    
        System.out.println("未找到匹配的学生ID!");       
    }                                            
}                                                

(6) Write the method of querying students

public void displayStudents() {
    
                                                                                                              
    if (students.isEmpty()) {
    
                                                                                                                
        System.out.println("当前无学生信息,请添加后再查询!");                                                                                          
    } else {
    
                                                                                                                                 
        System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址");                                                                                    
        for (Student student : students) {
    
                                                                                                   
            System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "\t\t" + student.getAddress());
        }                                                                                                                                
    }                                                                                                                                    
}                                                                                                                                        

(7) Integrate code, integrate system

package net.army.java.test;

/**
 * 功能:学生管理系统
 * 日期:2023年09月02日
 * 作者:梁辰兴
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentManagementSystem {
    
    
    private List<Student> students;

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

    public void addStudent() {
    
    
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入学生ID:");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消费掉换行符

        System.out.println("请输入学生姓名:");
        String name = scanner.nextLine();

        System.out.println("请输入学生年龄:");
        int age = scanner.nextInt();
        scanner.nextLine(); // 消费掉换行符

        System.out.println("请输入学生家庭住址:");
        String address = scanner.nextLine();

        Student student = new Student(id, name, age, address);
        students.add(student);

        System.out.println("学生添加成功!");
    }

    public void deleteStudent() {
    
    
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入要删除的学生ID:");
        int id = scanner.nextInt();

        boolean found = false;

        for (Student student : students) {
    
    
            if (student.getId() == id) {
    
    
                students.remove(student);
                found = true;
                System.out.println("学生删除成功!");
                break;
            }
        }

        if (!found) {
    
    
            System.out.println("未找到匹配的学生ID!");
        }
    }

    public void updateStudent() {
    
    
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入要修改的学生ID:");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消费掉换行符

        boolean found = false;

        for (Student student : students) {
    
    
            if (student.getId() == id) {
    
    
                System.out.println("请输入学生姓名:");
                String name = scanner.nextLine();
                student.setName(name);

                System.out.println("请输入学生年龄:");
                int age = scanner.nextInt();
                scanner.nextLine(); // 消费掉换行符
                student.setAge(age);

                System.out.println("请输入学生家庭住址:");
                String address = scanner.nextLine();
                student.setAddress(address);

                found = true;
                System.out.println("学生修改成功!");
                break;
            }
        }

        if (!found) {
    
    
            System.out.println("未找到匹配的学生ID!");
        }
    }

    public void displayStudents() {
    
    
        if (students.isEmpty()) {
    
    
            System.out.println("当前无学生信息,请添加后再查询!");
        } else {
    
    
            System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址");
            for (Student student : students) {
    
    
                System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "\t\t" + student.getAddress());
            }
        }
    }

    public static void main(String[] args) {
    
    
        StudentManagementSystem system = new StudentManagementSystem();

        // 菜单
        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("请输入选择:");

            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();

            switch (choice) {
    
    
                case 1:
                    system.addStudent();
                    break;
                case 2:
                    system.deleteStudent();
                    break;
                case 3:
                    system.updateStudent();
                    break;
                case 4:
                    system.displayStudents();
                    break;
                case 5:
                    System.out.println("谢谢使用,再见!");
                    System.exit(0);
                default:
                    System.out.println("无效的选择,请重新输入!");
                    break;
            }
        }
    }
}

running result:
insert image description here

Guess you like

Origin blog.csdn.net/m0_62617719/article/details/132654846