Java student management system (detailed explanation)



The following will share some of my methods and program codes for your reference when doing this exercise (complete project code is attached at the end).

I divided the whole project into four parts: Student.Java, Main.Java, FunctionModule.java, Extents.Java



student class


First create the student class and constructor


代码如下:

package com.studentmodule;

/**
 * @author wenjie
 */

public class Student {
    
    
    private int stuId;
    private int grade;
    private String name;
    private int age;
    private String address;

    public Student() {
    
    
    }

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

    public int getStuId() {
    
    
        return stuId;
    }

    public void setStuId(int stuId) {
    
    
        this.stuId = stuId;
    }

    public int getGrade() {
    
    
        return grade;
    }

    public void setGrade(int grade) {
    
    
        this.grade = grade;
    }

    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;
    }
}


main method


Next, start "running" from the main method!



package com.studentmodule;

import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;

    /**
     * @author wenjie
     */
    public class Main {
    
    

    static final int COUNT = 2;
    static final String MYSID = "admin";
    static final String MYPASSWD = "666666";

    public static void main(String[] args) throws AWTException {
    
    
        interFace();
    }


First interface login


First run the program to enter the following first interface for login selection, select 1 to enter the login interface, select 0 to exit the program, all selection programs have error judgments, the following selection judgments are the same, if the input is wrong, it will prompt to re-enter.


insert image description here


代码如下:

public static void interFace() throws AWTException {
    
    
        System.out.println(">首界面\n");
        System.out.println("\t*****************************************************************");
        System.out.println("\t                           首界面                              ");
        System.out.println("\t ---------------------------------------------------------------");
        System.out.println("\t                开始登录                   请输入1               ");
        System.out.println("\t ---------------------------------------------------------------");
        System.out.println("\t                退出                      请输入0               ");
        System.out.println("\t*****************************************************************");
        Scanner sc = new Scanner(System.in);
        System.out.print("\n请输入您的选择: ");
        while (true) {
    
    
            int choose = sc.nextInt();
            if (choose == 1) {
    
    
                register();
                break;
            } else if (choose == 0) {
    
    
                System.out.println("退出成功!");
                System.exit(0);
            } else {
    
    
                System.out.print("看清选项! 再给你一次机会:");
            }
        }
    }

Enter the correct user name and password to log in through the keyboard. You have three chances to enter. If you enter incorrectly three times, the program will automatically exit.


代码如下:

public static void register() throws AWTException {
    
    
        for (int i = COUNT; i >= 0; i--) {
    
    
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入您的用户名: ");
            String loginSid = sc.nextLine();
            System.out.print("请输入您的密码: ");
            String loginPassWd = sc.nextLine();
            if (loginSid.equals(MYSID) && loginPassWd.equals(MYPASSWD)) {
    
    
                Extents.clearConsole();
                System.out.println("欢迎登录! 用户:" + MYSID + "\n\n");
                menu();
                break;
            } else {
    
    
                if (i == 0) {
    
    
                    System.out.println("你是不是傻!");
                    System.exit(0);
                }
                System.out.println("错了错了, 你还有 " + i + " 次机会");
            }
        }
    }


Function selection interface


After the input is correct, enter the following function selection interface, enter the corresponding serial number to perform four operations on the student information (add, delete, modify, check), and enter 'q' to return to the upper level, and enter 'p' to exit the program.


insert image description here


代码如下:


public static void menu() throws AWTException {
    
    
        ArrayList<Student> stu = new ArrayList<>();
        while (true) {
    
    
            System.out.println(">首界面>功能界面\n");
            System.out.println("\t*****************************************************************");
            System.out.println("\t                      欢迎来到学生管理系统!                      ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.println("\t                         1.添加学生信息                          ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.println("\t                         2.删除学生信息                          ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.println("\t                         3.修改学生信息                          ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.println("\t                         4.查看学生信息                          ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.println("\t         q:返回上级菜单                  p:退出管理系统          ");
            System.out.println("\t******************************************************************");
            Scanner sc = new Scanner(System.in);
            System.out.print("\n请输入您的选择:");
            String choose = sc.nextLine();
            switch (choose) {
    
    
                case "1":
                    FunctionalBlock.addStu(stu);
                    break;
                case "2":
                    Extents.clearConsole();
                    FunctionalBlock.deleteStu(stu);
                    break;
                case "3":
                    Extents.clearConsole();
                    FunctionalBlock.updateStu(stu);
                    break;
                case "4":
                    Extents.clearConsole();
                    FunctionalBlock.showStu(stu);
                    Extents.isShow(sc);
                    break;
                case "q":
                    Extents.clearConsole();
                    interFace();
                    return;
                case "p":
                    System.out.println("退出成功!");
                    System.exit(0);
                default:
                    Extents.clearConsole();
                    System.out.println("这都错!看清选项再选\n\n\n");
                    break;
            }
        }
    }


Add student information


If no student information has been added (the number of students is 0), when you delete, modify, view, etc., you will be prompted "The data is empty, please add data and then operate!" At this time, enter 1 to add students, and enter the number you want to add Student information, after inputting, it will be printed out and asked whether to add, input 1 to add the student, otherwise input 0 to cancel the student addition, after adding a student information, it will also print out all student information, and finally you can choose to continue adding or return to the superior . Here, the last student number after the meeting is displayed, because each student number is unique, so I let it increment by 1 after adding a student without manual entry.


insert image description here


代码如下:


public static void addStu(ArrayList<Student> stu) throws AWTException {
    
    
        while (true) {
    
    
            Extents.clearConsole();
            System.out.println(">首界面>功能界面>添加学生信息\n\n");
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入学生班级(例:3): ");
            int grade = sc.nextInt();
            System.out.print("请输入学生姓名(例:张三): ");
            String name = sc.next();
            System.out.print("请输入学生年龄(例:18): ");
            int age = sc.nextInt();
            System.out.print("请输入学生地址(例:深圳): ");
            String address = sc.next();
            System.out.println("\n\n-----------------------------------------------------");
            System.out.println("班级: " + grade + "\t\t姓名: " + name + "\t\t年龄: " + age + "\t\t地址: " + address);
            System.out.print("\n\n是否添加该学生信息? [Yes(1) / No(0)] :");
            Extents.isAdd(stu, sc, grade, name, age, address);
            System.out.println("\n\n\n>首界面>功能界面>添加学生信息\n");
            System.out.println("\t                继续添加                   请输入1               ");
            System.out.println();
            System.out.println("\t                返回上级                   请输入0               ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.print("\n请输入您的选择:");
            while (true) {
    
    
                int choose = sc.nextInt();
                if (choose == 1) {
    
    
                    break;
                } else if (choose == 0) {
    
    
                    Extents.clearConsole();
                    return;
                } else {
    
    
                    System.out.print("看清选项! 再给你一次机会: ");
                }
            }
        }
    }


public static void isAdd(ArrayList<Student> stu, Scanner sc, int grade, String name, int age, String address) throws AWTException {
    
    
        while (true) {
    
    
            int is = sc.nextInt();
            if (is == 0) {
    
    
                Extents.clearConsole();
                System.out.println("取消成功!");
                break;
            } else if (is == 1) {
    
    
                Student s = new Student(stuId, grade, name, age, address);
                stu.add(s);
                stuId += 1;
                Extents.clearConsole();
                System.out.println("添加信息成功!\n\n");
                FunctionalBlock.showStu(stu);
                break;
            }
            System.out.print("\n输入错误!请重新输入:");
        }
    }

delete student information


After adding student information, we enter 0 to return to the upper menu and then select 2 to delete students. First, all student information will be printed out, and then enter the student number of the student information you want to delete. If the student number does not exist, it will prompt and Re-enter, and finally confirm whether to delete the student information, select 1 to delete, select 0 to cancel the deletion, after deleting, it will print all the student information after the operation again, and you will find that the number of students has changed from 3 to 2 at this time, and the newly deleted The information is gone, and you can choose to continue deleting and return to the previous menu.


insert image description here


代码如下:


public static void deleteStu(ArrayList<Student> stu) throws AWTException {
    
    
        Scanner sc = new Scanner(System.in);
        showStu(stu);
        while (true) {
    
    
            System.out.print("\n请输入要删除的学生学号:");
            int sid = sc.nextInt();
            sc.nextLine();
            int flag = Extents.getFlag(stu, sid);
            if (flag == -1) {
    
    
                System.out.print("\n该学号不存在,请重新输入\n");
            } else {
    
    
                System.out.print("\n是否删除学号为:" + sid + " 的学生信息? [Yes(1) / No(0)] :");
                Extents.isDlete(stu, sc, flag);
                System.out.println("\n\n\n>首界面>功能界面>删除学生信息\n");
                System.out.println("\t                继续删除                   请输入1                ");
                System.out.println();
                System.out.println("\t                返回上级                   请输入0                ");
                System.out.println("\t ----------------------------------------------------------------");
                System.out.print("\n请输入您的选择: ");
                while (true) {
    
    
                    int choose = sc.nextInt();
                    if (choose == 1) {
    
    
                        break;
                    } else if (choose == 0) {
    
    
                        Extents.clearConsole();
                        return;
                    } else {
    
    
                        System.out.print("看清选项! 再给你一次机会: ");
                    }
                }
            }
        }
    }


public static void isDlete(ArrayList<Student> stu, Scanner sc, int flag) throws AWTException {
    
    
        while (true) {
    
    
            int is = sc.nextInt();
            if (is == 0) {
    
    
                Extents.clearConsole();
                System.out.println("取消成功!");
                break;
            } else if (is == 1) {
    
    
                stu.remove(flag);
                Extents.clearConsole();
                System.out.println("删除学生信息成功!\n\n");
                FunctionalBlock.showStu(stu);
                break;
            }
            System.out.print("\n输入错误!请重新输入:");
        }
    }

Modify student information


After deleting the student information whose student number is 2, we enter 0 to return to the upper menu, and then enter 3 to modify the operation. Also print out all the student information first, and select the student information student number you want to modify. If the student number does not exist, it will be executed Prompt and re-enter, and then enter the student information to be modified in turn according to the prompts. After the input, it will print out the student information before and after modification for you to compare. Finally, enter 1 to modify and enter 0 to cancel the modification. After modification, print again All student information is checked for confirmation.


insert image description here


代码如下:


public static void updateStu(ArrayList<Student> stu) throws AWTException {
    
    
        Scanner sc = new Scanner(System.in);
        while (true) {
    
    
            showStu(stu);
            System.out.print("\n\n请输入要修改信息的学生学号:");
            int sidUpdate = sc.nextInt();
            int flag = Extents.getFlag(stu, sidUpdate);
            Extents.clearConsole();
            if (flag == -1) {
    
    
                System.out.print("该学号不存在,请重新输入\n\n\n ");
            } else {
    
    
                System.out.println(">首界面>功能界面>修改学生信息\n\n");
                System.out.print("请输入学生班级: ");
                int grade = sc.nextInt();
                System.out.print("请输入学生姓名: ");
                String name = sc.next();
                System.out.print("请输入学生年龄: ");
                int age = sc.nextInt();
                System.out.print("请输入学生地址: ");
                String address = sc.next();
                Extents.clearConsole();
                Extents.getFlag(stu, sidUpdate);
                Student s1 = stu.get(flag);
                System.out.println(">首界面>功能界面>修改学生信息");
                System.out.println("\n\n------------------------------------------------------------------");
                System.out.println("修改前——>\n");
                System.out.println("学号:" + s1.getStuId() + "\t\t班级: " + s1.getGrade() + "\t\t姓名: "
                        + s1.getName() + "\t\t年龄: " + s1.getAge() + "\t\t地址: " + s1.getAddress());
                System.out.println("\n\n------------------------------------------------------------------");
                System.out.println("修改后——>\n");
                System.out.println("学号:" + sidUpdate + "\t\t班级: " + grade + "\t\t姓名: " + name + "\t\t年龄: "
                        + age + "\t\t地址: " + address);
                System.out.print("\n\n是否修改该学生信息? [Yes(1) / No(0)] :");
                Extents.isUpdata(stu, sc, sidUpdate, flag, grade, name, age, address);
                System.out.println("\n\n\n>首界面>功能界面>修改学生信息\n");
                System.out.println("\t                继续修改                   请输入1              ");
                System.out.println();
                System.out.println("\t                返回上级                   请输入0              ");
                System.out.println("\t --------------------------------------------------------------");
                System.out.print("\n请输入您的选择:");
                while (true) {
    
    
                    int choose = sc.nextInt();
                    if (choose == 1) {
    
    
                        Extents.clearConsole();
                        break;
                    } else if (choose == 0) {
    
    
                        Extents.clearConsole();
                        return;
                    } else {
    
    
                        System.out.print("看清选项! 再给你一次机会: ");
                    }
                }
            }
        }
    }


public static void isUpdata(ArrayList<Student> stu, Scanner sc, int sidUpdate, int flag, int grade, String name, int age, String address) throws AWTException {
    
    
        while (true) {
    
    
            int is = sc.nextInt();
            if (is == 0) {
    
    
                Extents.clearConsole();
                System.out.println("取消成功!");
                break;
            } else if (is == 1) {
    
    
                Student newStu = new Student(sidUpdate, grade, name, age, address);
                stu.set(flag, newStu);
                Extents.clearConsole();
                System.out.println("修改学生信息成功!\n\n");
                FunctionalBlock.showStu(stu);
                break;
            }
            System.out.print("\n输入错误!请重新输入:");
        }
    }

View student information


Finally, we check students. In fact, in the previous addition, deletion, and modification, the search function was frequently called to print out all student information, but let’s talk about it, enter 0 to return to the upper menu, and then enter 4 to view all For student information, enter 0 to return to the upper menu, and finally enter 'q' to exit the program.


insert image description here


代码如下:


public static void showStu(ArrayList<Student> stu) throws AWTException {
    
    
        if (stu.size() == 0) {
    
    
            Extents.clearConsole();
            System.out.println("数据为空,请添加数据后操作!\n\n");
            Main.menu();
        } else {
    
    
            System.out.println(">学生信息显示\n");
            System.out.println("学生个数:"+stu.size() + "人\n");
            System.out.println("\t ----------------------------------------------------------------");
            System.out.println("\t   学号\t\t" + "   班级\t\t" + " \t姓名\t" + "\t\t年龄" + " \t\t\t地址");
            System.out.println("\t  --------------------------------------------------------------");
            /*for (int i = 0; i < stu.size(); i++) {
                Student s = stu.get(i);*/
            for (Student s : stu) {
    
    
                System.out.println("\t\t" + s.getStuId() + "\t\t\t" + s.getGrade() + " \t\t\t" + s.getName()
                        + " \t\t\t" + s.getAge() + "  \t\t\t" + s.getAddress() + "\n");
                System.out.println("\t  --------------------------------------------------------------");
            }
        }
    }


public static void isShow(Scanner sc) throws AWTException {
    
    
        System.out.println("\n\n\n>首界面>功能界面>查看学生信息\n\n");
        System.out.println("\t              返回上级                     请输入0           ");
        System.out.println("\t ---------------------------------------------------------------");
        System.out.print("\n请输入您的选择: ");
        while (true) {
    
    
            int choose1 = sc.nextInt();
            if (choose1 == 0) {
    
    
                Extents.clearConsole();
                break;
            } else {
    
    
                System.out.print("看清选项! 再给你一次机会: ");
            }
        }
    }


Student number traversal and clearing the console method


One of the following two methods is used to traverse the student number, find the student number of the corresponding student to obtain the student information and then operate on the student information of the student number

Another method is to clear the console. This method is explained in detail in my previous article. If you want to know more, you can click this link to view it.


链接:

IDEA clears the console and runs the cmd command in Java to realize the screen clearing operation



public static int getFlag(ArrayList<Student> stu, int sid) {
    
    
        int flag = -1;
        for (int i = 0; i < stu.size(); i++) {
    
    
            Student s = stu.get(i);
            if (s.getStuId() == sid) {
    
    
                flag = i;
                break;
            }
        }
        return flag;
    }


public static void clearConsole() throws AWTException {
    
    
        Robot r = new Robot();
        // 按下Ctrl键
        r.keyPress(KeyEvent.VK_CONTROL);
        // 按下R键
        r.keyPress(KeyEvent.VK_R);
        // 释放R键
        r.keyRelease(KeyEvent.VK_R);
        // 释放Ctrl键
        r.keyRelease(KeyEvent.VK_CONTROL);
        r.delay(50);
    }

Well, the program is almost finished running here, and all the functions are basically introduced, that is, the four basic operations (add, delete, modify, check).



Attachment: Student Management System Code


Finally, attach the code of the complete student management system project


Student .Java


package com.studentmodule;

/**
 * @author wenjie
 */

public class Student {
    
    
    private int stuId;
    private int grade;
    private String name;
    private int age;
    private String address;

    public Student() {
    
    
    }

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

    public int getStuId() {
    
    
        return stuId;
    }

    public void setStuId(int stuId) {
    
    
        this.stuId = stuId;
    }

    public int getGrade() {
    
    
        return grade;
    }

    public void setGrade(int grade) {
    
    
        this.grade = grade;
    }

    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;
    }
}


Main.Java


package com.studentmodule;

import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author wenjie
 */
public class Main {
    
    

    static final int COUNT = 2;
    static final String MYSID = "admin";
    static final String MYPASSWD = "666666";

    public static void main(String[] args) throws AWTException {
    
    
        interFace();
    }

    public static void interFace() throws AWTException {
    
    
        System.out.println(">首界面\n");
        System.out.println("\t*****************************************************************");
        System.out.println("\t                           首界面                              ");
        System.out.println("\t ---------------------------------------------------------------");
        System.out.println("\t                开始登录                   请输入1               ");
        System.out.println("\t ---------------------------------------------------------------");
        System.out.println("\t                退出                      请输入0               ");
        System.out.println("\t*****************************************************************");
        Scanner sc = new Scanner(System.in);
        System.out.print("\n请输入您的选择: ");
        while (true) {
    
    
            int choose = sc.nextInt();
            if (choose == 1) {
    
    
                register();
                break;
            } else if (choose == 0) {
    
    
                System.out.println("退出成功!");
                System.exit(0);
            } else {
    
    
                System.out.print("看清选项! 再给你一次机会:");
            }
        }
    }

    public static void register() throws AWTException {
    
    
        for (int i = COUNT; i >= 0; i--) {
    
    
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入您的用户名: ");
            String loginSid = sc.nextLine();
            System.out.print("请输入您的密码: ");
            String loginPassWd = sc.nextLine();
            if (loginSid.equals(MYSID) && loginPassWd.equals(MYPASSWD)) {
    
    
                Extents.clearConsole();
                System.out.println("欢迎登录! 用户:" + MYSID + "\n\n");
                menu();
                break;
            } else {
    
    
                if (i == 0) {
    
    
                    System.out.println("你是不是傻!");
                    System.exit(0);
                }
                System.out.println("错了错了, 你还有 " + i + " 次机会");
            }
        }
    }

    public static void menu() throws AWTException {
    
    
        ArrayList<Student> stu = new ArrayList<>();
        while (true) {
    
    
            System.out.println(">首界面>功能界面\n");
            System.out.println("\t*****************************************************************");
            System.out.println("\t                      欢迎来到学生管理系统!                      ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.println("\t                         1.添加学生信息                          ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.println("\t                         2.删除学生信息                          ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.println("\t                         3.修改学生信息                          ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.println("\t                         4.查看学生信息                          ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.println("\t         q:返回上级菜单                  p:退出管理系统          ");
            System.out.println("\t******************************************************************");
            Scanner sc = new Scanner(System.in);
            System.out.print("\n请输入您的选择:");
            String choose = sc.nextLine();
            switch (choose) {
    
    
                case "1":
                    FunctionalBlock.addStu(stu);
                    break;
                case "2":
                    Extents.clearConsole();
                    FunctionalBlock.deleteStu(stu);
                    break;
                case "3":
                    Extents.clearConsole();
                    FunctionalBlock.updateStu(stu);
                    break;
                case "4":
                    Extents.clearConsole();
                    FunctionalBlock.showStu(stu);
                    Extents.isShow(sc);
                    break;
                case "q":
                    Extents.clearConsole();
                    interFace();
                    return;
                case "p":
                    System.out.println("退出成功!");
                    System.exit(0);
                default:
                    Extents.clearConsole();
                    System.out.println("这都错!看清选项再选\n\n\n");
                    break;
            }
        }
    }
}

FunctionModule.java


package com.studentmodule;


import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author wenjie
 */
public class FunctionalBlock {
    
    

    public static void addStu(ArrayList<Student> stu) throws AWTException {
    
    
        while (true) {
    
    
            Extents.clearConsole();
            System.out.println(">首界面>功能界面>添加学生信息\n\n");
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入学生班级(例:3): ");
            int grade = sc.nextInt();
            System.out.print("请输入学生姓名(例:张三): ");
            String name = sc.next();
            System.out.print("请输入学生年龄(例:18): ");
            int age = sc.nextInt();
            System.out.print("请输入学生地址(例:深圳): ");
            String address = sc.next();
            System.out.println("\n\n-----------------------------------------------------");
            System.out.println("班级: " + grade + "\t\t姓名: " + name + "\t\t年龄: " + age + "\t\t地址: " + address);
            System.out.print("\n\n是否添加该学生信息? [Yes(1) / No(0)] :");
            Extents.isAdd(stu, sc, grade, name, age, address);
            System.out.println("\n\n\n>首界面>功能界面>添加学生信息\n");
            System.out.println("\t                继续添加                   请输入1               ");
            System.out.println();
            System.out.println("\t                返回上级                   请输入0               ");
            System.out.println("\t ---------------------------------------------------------------");
            System.out.print("\n请输入您的选择:");
            while (true) {
    
    
                int choose = sc.nextInt();
                if (choose == 1) {
    
    
                    break;
                } else if (choose == 0) {
    
    
                    Extents.clearConsole();
                    return;
                } else {
    
    
                    System.out.print("看清选项! 再给你一次机会: ");
                }
            }
        }
    }

    public static void deleteStu(ArrayList<Student> stu) throws AWTException {
    
    
        Scanner sc = new Scanner(System.in);
        showStu(stu);
        while (true) {
    
    
            System.out.print("\n请输入要删除的学生学号:");
            int sid = sc.nextInt();
            sc.nextLine();
            int flag = Extents.getFlag(stu, sid);
            if (flag == -1) {
    
    
                System.out.print("\n该学号不存在,请重新输入\n");
            } else {
    
    
                System.out.print("\n是否删除学号为:" + sid + " 的学生信息? [Yes(1) / No(0)] :");
                Extents.isDlete(stu, sc, flag);
                System.out.println("\n\n\n>首界面>功能界面>删除学生信息\n");
                System.out.println("\t                继续删除                   请输入1                ");
                System.out.println();
                System.out.println("\t                返回上级                   请输入0                ");
                System.out.println("\t ----------------------------------------------------------------");
                System.out.print("\n请输入您的选择: ");
                while (true) {
    
    
                    int choose = sc.nextInt();
                    if (choose == 1) {
    
    
                        break;
                    } else if (choose == 0) {
    
    
                        Extents.clearConsole();
                        return;
                    } else {
    
    
                        System.out.print("看清选项! 再给你一次机会: ");
                    }
                }
            }
        }
    }

    public static void updateStu(ArrayList<Student> stu) throws AWTException {
    
    
        Scanner sc = new Scanner(System.in);
        while (true) {
    
    
            showStu(stu);
            System.out.print("\n\n请输入要修改信息的学生学号:");
            int sidUpdate = sc.nextInt();
            int flag = Extents.getFlag(stu, sidUpdate);
            Extents.clearConsole();
            if (flag == -1) {
    
    
                System.out.print("该学号不存在,请重新输入\n\n\n ");
            } else {
    
    
                System.out.println(">首界面>功能界面>修改学生信息\n\n");
                System.out.print("请输入学生班级: ");
                int grade = sc.nextInt();
                System.out.print("请输入学生姓名: ");
                String name = sc.next();
                System.out.print("请输入学生年龄: ");
                int age = sc.nextInt();
                System.out.print("请输入学生地址: ");
                String address = sc.next();
                Extents.clearConsole();
                Extents.getFlag(stu, sidUpdate);
                Student s1 = stu.get(flag);
                System.out.println(">首界面>功能界面>修改学生信息");
                System.out.println("\n\n------------------------------------------------------------------");
                System.out.println("修改前——>\n");
                System.out.println("学号:" + s1.getStuId() + "\t\t班级: " + s1.getGrade() + "\t\t姓名: "
                        + s1.getName() + "\t\t年龄: " + s1.getAge() + "\t\t地址: " + s1.getAddress());
                System.out.println("\n\n------------------------------------------------------------------");
                System.out.println("修改后——>\n");
                System.out.println("学号:" + sidUpdate + "\t\t班级: " + grade + "\t\t姓名: " + name + "\t\t年龄: "
                        + age + "\t\t地址: " + address);
                System.out.print("\n\n是否修改该学生信息? [Yes(1) / No(0)] :");
                Extents.isUpdata(stu, sc, sidUpdate, flag, grade, name, age, address);
                System.out.println("\n\n\n>首界面>功能界面>修改学生信息\n");
                System.out.println("\t                继续修改                   请输入1              ");
                System.out.println();
                System.out.println("\t                返回上级                   请输入0              ");
                System.out.println("\t --------------------------------------------------------------");
                System.out.print("\n请输入您的选择:");
                while (true) {
    
    
                    int choose = sc.nextInt();
                    if (choose == 1) {
    
    
                        Extents.clearConsole();
                        break;
                    } else if (choose == 0) {
    
    
                        Extents.clearConsole();
                        return;
                    } else {
    
    
                        System.out.print("看清选项! 再给你一次机会: ");
                    }
                }
            }
        }
    }

    public static void showStu(ArrayList<Student> stu) throws AWTException {
    
    
        if (stu.size() == 0) {
    
    
            Extents.clearConsole();
            System.out.println("数据为空,请添加数据后操作!\n\n");
            Main.menu();
        } else {
    
    
            System.out.println(">学生信息显示\n");
            System.out.println("学生个数:"+stu.size() + "人\n");
            System.out.println("\t ----------------------------------------------------------------");
            System.out.println("\t   学号\t\t" + "   班级\t\t" + " \t姓名\t" + "\t\t年龄" + " \t\t\t地址");
            System.out.println("\t  --------------------------------------------------------------");
            /*for (int i = 0; i < stu.size(); i++) {
                Student s = stu.get(i);*/
            for (Student s : stu) {
    
    
                System.out.println("\t\t" + s.getStuId() + "\t\t\t" + s.getGrade() + " \t\t\t" + s.getName()
                        + " \t\t\t" + s.getAge() + "  \t\t\t" + s.getAddress() + "\n");
                System.out.println("\t  --------------------------------------------------------------");
            }
        }
    }
}

Extents.Java


package com.studentmodule;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author wenjie
 */
public class Extents {
    
    

    public static int stuId = 1;

    public static void isAdd(ArrayList<Student> stu, Scanner sc, int grade, String name, int age, String address) throws AWTException {
    
    
        while (true) {
    
    
            int is = sc.nextInt();
            if (is == 0) {
    
    
                Extents.clearConsole();
                System.out.println("取消成功!");
                break;
            } else if (is == 1) {
    
    
                Student s = new Student(stuId, grade, name, age, address);
                stu.add(s);
                stuId += 1;
                Extents.clearConsole();
                System.out.println("添加信息成功!\n\n");
                FunctionalBlock.showStu(stu);
                break;
            }
            System.out.print("\n输入错误!请重新输入:");
        }
    }

    public static void isDlete(ArrayList<Student> stu, Scanner sc, int flag) throws AWTException {
    
    
        while (true) {
    
    
            int is = sc.nextInt();
            if (is == 0) {
    
    
                Extents.clearConsole();
                System.out.println("取消成功!");
                break;
            } else if (is == 1) {
    
    
                stu.remove(flag);
                Extents.clearConsole();
                System.out.println("删除学生信息成功!\n\n");
                FunctionalBlock.showStu(stu);
                break;
            }
            System.out.print("\n输入错误!请重新输入:");
        }
    }

    public static void isUpdata(ArrayList<Student> stu, Scanner sc, int sidUpdate, int flag, int grade, String name, int age, String address) throws AWTException {
    
    
        while (true) {
    
    
            int is = sc.nextInt();
            if (is == 0) {
    
    
                Extents.clearConsole();
                System.out.println("取消成功!");
                break;
            } else if (is == 1) {
    
    
                Student newStu = new Student(sidUpdate, grade, name, age, address);
                stu.set(flag, newStu);
                Extents.clearConsole();
                System.out.println("修改学生信息成功!\n\n");
                FunctionalBlock.showStu(stu);
                break;
            }
            System.out.print("\n输入错误!请重新输入:");
        }
    }

    public static void isShow(Scanner sc) throws AWTException {
    
    
        System.out.println("\n\n\n>首界面>功能界面>查看学生信息\n\n");
        System.out.println("\t              返回上级                     请输入0           ");
        System.out.println("\t ---------------------------------------------------------------");
        System.out.print("\n请输入您的选择: ");
        while (true) {
    
    
            int choose1 = sc.nextInt();
            if (choose1 == 0) {
    
    
                Extents.clearConsole();
                break;
            } else {
    
    
                System.out.print("看清选项! 再给你一次机会: ");
            }
        }
    }

    public static int getFlag(ArrayList<Student> stu, int sid) {
    
    
        int flag = -1;
        for (int i = 0; i < stu.size(); i++) {
    
    
            Student s = stu.get(i);
            if (s.getStuId() == sid) {
    
    
                flag = i;
                break;
            }
        }
        return flag;
    }

    public static void clearConsole() throws AWTException {
    
    
        Robot r = new Robot();
        // 按下Ctrl键
        r.keyPress(KeyEvent.VK_CONTROL);
        // 按下R键
        r.keyPress(KeyEvent.VK_R);
        // 释放R键
        r.keyRelease(KeyEvent.VK_R);
        // 释放Ctrl键
        r.keyRelease(KeyEvent.VK_CONTROL);
        r.delay(50);
    }
}


Summarize

  The above is all the codes of the entire project, because most of them are composed of basic Java grammar, selection structure and loop structure, so there are no comments. I believe that after learning some basic grammar in front of Java, you should be able to see a Probably, the rest can be handed over to Baidu.

Guess you like

Origin blog.csdn.net/m0_67559541/article/details/125856765
Recommended