[Java combat project] 200+ lines of code to build a multi-functional student information management system

Project Objectives:

  • 1. Realize administrator (admin) login
  • 2. Realize three ways to view student information
    • View all student information
    • View all student information by ID
    • View student name by ID
  • 3. Add student information
  • 4. Delete student information
  • 5. Realize two ways to modify student information
    • Modify all student information according to ID
    • Modify student specified information based on ID

0. Project realization planning

  • thinking plan

Sharpening a knife is not a mistake in chopping firewood. Before starting a complete project, the most important thing we should complete is to sort out the ideas and function planning of the project. On the whole, the whole project is similar to the addition, deletion, modification and query function of the database.


project planning


The user operates the main through the console to complete functions such as adding, deleting, modifying and checking or exiting the system. Although the system user verification and addition, deletion and modification are implemented at the same level, logically, verification is prioritized over addition, deletion and modification, that is, log in first, and then operate.

  • code planning

Code planning is like instantiating thinking planning. According to the structure of the thinking map, we can easily associate each function in the thinking map with each function.


code planning


Function name Function
exitLogin Exit system
userValidataLogin Login authentication
addStudentInfo Add student information
selectStudentInfo Query student information
delStudentInfo delete student information
modifyStudentInfo Modify student information
== Everything is ready! Let's enter the world of code! ==

1. Implementation of non-core functions

  • Exit system

If password verification is the entrance to the system, then exiting the system is like entering the system. In order to ensure the continuous operation of the system, instead of a single method call, an infinite loop is often used in the code, and the exit of exiting the system is very important. In addition, there are more than one situations in the entire system that need to exit, and it is packaged into a method, which is highly readable and convenient.

//1、退出方法
public static int exitLogin() {
    
    
        System.out.println("exit.");
        return 0;
    }

  • System user login

This system login only implements the admin password verification of the admin user. If you need to register, you can use the ArrayList to store it. If you need to switch between multiple users to log in, you can traverse the ArrayList for verification. This is similar to the viewing and modification methods in the core functions later.

//2、登录验证方法
    public static boolean userValidataLogin(Scanner sc) {
    
    
        System.out.println("欢迎登录!");
        System.out.print("请输入用户名:");
        String LoginUserName = sc.next();
        System.out.print("请输入密码:");
        String LoginUserPass = sc.next();
        if (LoginUserName.equals("admin") && LoginUserPass.equals("admin")) {
    
    
            System.out.println("登陆成功!");
            System.out.println("欢迎您:" + LoginUserName);
            System.out.println("验证成功!");
        } else {
    
    
            System.out.println("账号或密码错误!!!请重新输入!");
            userValidataLogin(sc);
        }
        return true;
    }

2. Implementation of core functions

  • main program entry

After defining the login and logout, you can use the main method to test the call. In order to maintain the coherence and continuity of the system, you can directly use the infinite loop to run repeatedly.

public class StudentManagerSystem {
    
    
	//登录选择部分
    public static void main(String[] args) {
    
    
        System.out.println("---------------------------欢迎登录学生管理系统--------------------------");
        System.out.println("1 登录        2  退出");
        System.out.println("-----------------------------------------------------------------------");
        Scanner sc = new Scanner(System.in);
        System.out.print("请选择(1 or 2):");
        int userInputInt = sc.nextInt();
        boolean flg = false;
        switch (userInputInt) {
    
    
            case 1:
                flg = userValidataLogin(sc);
                break;
            case 2:
                exitLogin();
                break;
        }

If the user selects 1, the validation method is called, and if it doesn't, it is called recursively (in userValidataLoginthe method). If 2 is selected, the exit method is called.
log recursive call
1: Login failed, call yourself recursively to verify again; 2: IDEA's smart prompt, if there is recursion, this symbol will appear


main+login verification method+exit system method test results:
login test


After completing the verification, enter the system, and the operation mode is continuous operation.

		//密码正确,登录成功!
        while (flg) {
    
    
            System.out.println("---------------------------请选择操作的信息对应的数字--------------------------");
            System.out.println("1.查看           2.添加          3.删除          4.修改         5.退出        -");
            System.out.println("-----------------------------------------------------------------------------");
            System.out.print("请选择:");
            int userInt = sc.nextInt();
            //提前断定用户是否想要退出
            if (userInt == 5) {
    
    
                exitLogin();
                break;
            } else if (userInt == 4) {
    
     
            //等于4,则调用修改学生信息函数
                int tmpRetInt = modifyStudentInfo(sc);
                if (tmpRetInt == -1) {
    
    
                //接收返回值,如果等于-1,则表示用户需要退出
                    exitLogin();
                    break;
                }
            }
            switch (userInt) {
    
    //正常调用
                case 1:
                    selectStudentInfo(sc);
                    break;
                case 2:
                    addStudentInfo(sc);
                    break;
                case 3:
                    delStudentInfo(sc);
                    break;
                default:
                    System.out.println("输入有误!请重新输入!");
            }
        }
    }

  • View student information

First declare a global collection for storing all student information

public class StudentManagerSystem {
    
    

    //定义一个空集合,定义单个元素为列表
    static List<String[]> stuArrayList = new ArrayList<>();
//4、查询学生信息
    public static void selectStudentInfo(Scanner sc) {
    
    
        System.out.println("---------------------------请选择操作的信息对应的数字--------------------------");
        System.out.println("1.查看所有学生信息   2.根据ID查询学生信息   3.根据ID查询学生姓名    4.返回上一层  ");
        System.out.println("-----------------------------------------------------------------------------");
        System.out.print("请选择:");
        int userInputInt = sc.nextInt();
        //如果等于4,直接退出
        if (userInputInt == 4) {
    
    
            return;
        }
        switch (userInputInt) {
    
    
            case 1:
                System.out.println("----------------------------------所有学生信息--------------------------------");
                System.out.println("|学号    |姓名    |年龄    |性别    |年级    |电话        |Email       |地址");
                for (int i = 0; i < stuArrayList.size(); i++) {
    
    
                    System.out.println(Arrays.toString(stuArrayList.get(i)));
                }
                break;
            case 2:
                System.out.print("请输入要查询的学生ID:");
                String userInputStrID = sc.next();
                for (int listIndex = 0; listIndex < stuArrayList.size(); listIndex++) {
    
    
                    if ((stuArrayList.get(listIndex))[0].equals(userInputStrID)) {
    
    
                        System.out.println("----------------------------------该生所有信息--------------------------------");
                        System.out.println("|学号    |姓名    |年龄    |性别    |年级    |电话        |Email       |地址");
                        System.out.println(Arrays.toString(stuArrayList.get(listIndex)));
                    } else {
    
    
                        System.out.println("学生ID不存在!");
                    }
                }
                break;
            case 3:
                System.out.print("请输入要查询的学生ID:");
                String userInputStrIDName = sc.next();
                for (int listIndex = 0; listIndex < stuArrayList.size(); listIndex++) {
    
    
                    if ((stuArrayList.get(listIndex))[0].equals(userInputStrIDName)) {
    
    
                        System.out.println(("学号" + userInputStrIDName + ",\t" + "姓名" + (stuArrayList.get(listIndex))[1]));
                    } else {
    
    
                        System.out.println("学生ID不存在!");
                    }
                }
                break;
            default:
                System.out.println("输入有误!请重新输入!");
        }
        selectStudentInfo(sc);
    }

A set is relatively simple. For convenience, the individual elements of the collection are lists of String type, which is convenient for management.
It should be noted that if theback to previous levelIf the function is nested into the case, it may penetrate, so use return directly outside the switch to determine in advance whether the user needs to log out.

  • Add student information
//3、添加学生信息主方法
    public static void addStudentInfo(Scanner sc) {
    
    
        //先判断一下集合是否为空,为空直接调用方法
        if (stuArrayList.isEmpty()) {
    
    
            System.out.println("学生集合为空!直接添加!");
            addInfoFunc(sc);
        } else {
    
     //非空,先判断ID是否重复
            System.out.print("请输入学生ID:");
            String userString = sc.next();
            //先把所有的学号取出来存到列表当中
            String[] stuIDList = new String[stuArrayList.size()];
            for (int listIndex = 0; listIndex < stuArrayList.size(); listIndex++) {
    
    
                stuIDList[listIndex] = (stuArrayList.get(listIndex))[0];
            }
            //判断输入的ID是否已存在
            if (Arrays.asList(stuIDList).contains(userString)) {
    
    
                System.out.println("此ID" + userString + "已存在,请重新输入!");
                addStudentInfo(sc);
            } else {
    
    
                System.out.println("ID校验不冲突,请添加学生信息!");
                addInfoFunc(sc);
            }
        }
    }

    //3.1 添加学生信息副方法,抽取的重复代码
    public static void addInfoFunc(Scanner sc) {
    
    
        String[] tmpStuInfoList = new String[8];//临时数组,用来存储,然后装到集合
        System.out.print("请输入学生ID:");
        String stuID = sc.next();
        tmpStuInfoList[0] = stuID;
        //name
        System.out.print("请输入学生姓名:");
        String stuName = sc.next();
        tmpStuInfoList[1] = stuName;

        System.out.print("请输入学生性别:");
        String stuSex = sc.next();
        tmpStuInfoList[2] = stuSex;

        System.out.print("请输入学生年龄:");
        String stuAge = sc.next();
        tmpStuInfoList[3] = stuAge;

        System.out.print("请输入学生所属年级(只能初级、中级、高级):");
        String stuClass = sc.next();
        tmpStuInfoList[4] = stuClass;

        System.out.print("请输入学生地址:");
        String stuAddress = sc.next();
        tmpStuInfoList[5] = stuAddress;

        System.out.print("请输入学生联系方式(11位手机号):");
        String stuPhone = sc.next();
        tmpStuInfoList[6] = stuPhone;

        System.out.print("请输入学生电子邮箱:(包含@和.com)");
        String stuEmail = sc.next();
        tmpStuInfoList[7] = stuEmail;

        stuArrayList.add(tmpStuInfoList);
        System.out.println("写入成功!系统将自动返回上层目录~");
    }

Every time it runs, the student collection is always initialized to an empty collection. If no judgment is made, the system cannot enter the adding process, so judge in advance whether it is an empty collection, and add it directly if it is empty.
The logic of this method: first judge whether it is empty, if it is empty, add it directly; if it is not empty, first judge whether the ID is repeated, if it is repeated, recurse this method, and re-enter; if it is not repeated, start adding student information.


  • Modify student information
    //6、修改学生信息
    public static int modifyStudentInfo(Scanner sc) {
    
    
        System.out.println("---------------------------请选择操作的信息对应的数字--------------------------");
        System.out.println("1.根据ID修改学生全部信息   2.根据ID修改学生部分信息   3.返回上级目录    4.退出系统");
        System.out.println("-----------------------------------------------------------------------------");
        System.out.print("请选择:");
        int userInputInt = sc.nextInt();
        switch (userInputInt) {
    
    
            case 1:
                System.out.println("建议直接删除学生信息,再重新录入信息!");
                System.out.println("请输入您要修改(删除重录)的学生ID:");
                //先调用删除方法,删除学生信息
                delStudentInfo(sc);
                //添加学生信息:
                addStudentInfo(sc);
                break;
            case 2:
                System.out.print("请输入您要修改的学生ID:");
                String userInputStr = sc.next();
                for (int listIndex = 0; listIndex < stuArrayList.size(); listIndex++) {
    
    
                    if (stuArrayList.get(listIndex)[0].equals(userInputStr)) {
    
    
                        System.out.print("请输入您要修改的属性:");
                        String userInputStuInfo = sc.next();
                        System.out.print("请输入修改后的值:");
                        String userInputStuFinalInfo = sc.next();
                        //用一个列表存储所有的属性
                        String[] tmpInfoList = {
    
    "id", "name", "age", "sex", "class", "phone", "Email", "address"};
                        if (Arrays.asList(tmpInfoList).contains(userInputStuInfo)) {
    
    
                            //索引值
                            int tmpIndex = Arrays.asList(tmpInfoList).indexOf(userInputStuInfo);
                            //修改集合当中对应的索引值的属性
                            (stuArrayList.get(listIndex))[tmpIndex] = userInputStuFinalInfo;
                            System.out.println("修改成功!修改属性" + userInputStr + "为:" + userInputStuFinalInfo);
                        } else {
    
    
                            System.out.println("您输入的学生属性不存在,请重新输入!");
                        }
                    } else {
    
    
                        System.out.println("您输入的学生ID不存在,请重新输入!");
                    }
                }
                break;
            //返回上级目录 == 退出本函数
            case 3:
                break;
            //退出本函数,并在调用函数的代码后面执行退出函数
            case 4:
                return -1;
        }
        return 0;
    }

Method logic: When the user chooses to modify all student information according to the ID, the delete method is called first, and then the add method is called; when the user chooses the ID to modify part of the student information, the collection is traversed, and the memory address is converted into data for judgment, through one-to-one correspondence If the user chooses to return to the previous layer, it will directly break (wonder why it will not penetrate here...), if it is necessary to exit the system, first exit this method, and then return to the main method to judge the return value, if it is -1, the exit method is called.
It should be noted that when we judge which attribute the user wants to modify, we use a list to store all the attributes. This attribute needs to be in the same order as the list we store in the collection. This is for the convenience of directly modifying through the index .

Revise

List order in collection


  • delete student information
	//5、删除学生信息
    public static void delStudentInfo(Scanner sc) {
    
    
        if (stuArrayList.size() == 0) {
    
    
            System.out.println("学生集合为空,请先添加信息!");
            return;
        } else if (stuArrayList.size() != 0) {
    
    
            System.out.print("请输入要删除的学生ID:");
            String userInpStuID = sc.next();
            for (int listIndex = 0; listIndex < stuArrayList.size(); listIndex++) {
    
    
                if ((stuArrayList.get(listIndex))[0].equals(userInpStuID)) {
    
    
                    //remove传入值,故先将查询值    *其实是内存地址
                    String[] tmpStrList = stuArrayList.get(listIndex);
                    //删除
                    stuArrayList.remove(tmpStrList);
                    System.out.println("删除成功!系统即将返回上一层!");
                } else {
    
    
                    System.out.println("学生ID不存在,请重新输入!");
                }
            }
        }
    }

Method logic: traverse the loop student data collection, find the ID value passed in by the user, if it exists, use a temporary variable to store the value of the Array, and then pass it to the remove method of ArrayList; if it does not exist, then prompt.


have a test:

final test


The complete code has been sent to personal resources, you can find and download or splice the code of this article by yourself...it is not difficult, it is recommended to type by hand


End

Guess you like

Origin blog.csdn.net/qq_44491709/article/details/108908597