Teach you how to learn the student management system in JAVA in ten minutes

 

Table of contents

  1. Create student class

  2. Writing code for the main interface in the test class

  3. Code writing to add student information (addStuent)

  4. Writing code to delete student information (deleteStudent)

  5. Code writing to modify student information (updateStudent)

  6. Code writing to view student information (findAllStudent)

  7. Retrieve the student number code writing in each class (nameUsed)


A brief summary of the student system: a collection of codes that stores student classes in an Array List (collection) and allows for adding, deleting, modifying, and viewing.

The member variables and methods in the student class used in this article, as well as the methods that come with the collection:

Student number: sid

Student name: name

Student age: age

Student address: adress

Method to add student information: addStudent(ArrayList<Student> arr)

Method to delete student information: deleteStudent(ArrayList<Student> arr)

Method to modify student information: updateStudent(ArrayList<Student> arr)

Method to query student information: findAllStudent(ArrayList<Student> arr)

Code to retrieve the student number in each class: boolean nameUsed(ArrayList<Student>arr,String sid)

The methods used in the ArrayList collection are: add (add), remove (remove), set (index), get, and size

1: Create student class

First, use private to modify the member variables in the student class to ensure the security of the class. Create a constructor method for the parameterless variable and a constructor method for the four member variables. Create Set and Get methods for each member variable to facilitate subsequent modification of individual variables. Assignment and access of member variables.

The code for the student class is as follows:

public class Student {
    private String sid;
    private String name;
    private String age;
    private  String address;
    public Student(){
    }

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

    public void setSid(String sid) {
        this.sid = sid;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(String age) {
        this.age = age;
    }

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

    public String getSid() {
        return sid;
    }

    public String getName() {
        return name;
    }

    public String getAge() {
        return age;
    }

    public String getAddress() {
        return address;
    }
    public void show(){
        System.out.println(this.sid+" "+this.name+this.age+this.address);
    }
}

2: Code writing of the main interface in the test class

Summary: The code implementation of the main page is mainly to complete the interface output when executing different methods, and to define a collection to facilitate subsequent method parameter transfer (ArrayList<Student> arr = new ArrayList<Student>()), and to use System.exit( 0) to directly end the operation of the JVM virtual machine.

The code to implement the interface is as follows:

ArrayList<Student> arr = new ArrayList<Student>();
        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.print("请输入你的选择:");
            System.out.println();
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();
            switch (line) {
                case "1":
                    addStudent(arr);
                    break;
                case "2":
                    deleteStudent(arr);
                    break;
                case "3":
                    updateStudent(arr);
                    break;
                case "4":
                    findAllStudent(arr);
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    System.exit(0);
                default:
                    System.out.println("输入非法,请重新输入");
                    break;
            }
        }

3. Code writing to add student information (addStuent)

Summary: First use the nameUsed method to determine whether there is a duplicate name, then use a separate set method in the student class to assign a value to each member variable, and finally use the add function in the set to fill it.

The implementation code is as follows:

//添加学生的方法
    public static void addStudent(ArrayList<Student> arr) {
        Scanner sc = new Scanner(System.in);
        String sid;
       while(true){
           System.out.println("请输入学生学号");
            sid = sc.nextLine();
           boolean flag=nameUsed(arr,sid);
           if(flag){
               System.out.println("该学号已被占用,请重新输入");
           }else {
               break;
           }
       }
        System.out.println("请输入学生姓名");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄");
        String age = sc.nextLine();
        System.out.println("请输入学生家庭住址");
        String adress = sc.nextLine();

        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(adress);
        arr.add(s);
        System.out.println("添加成功");
    }

4. Code writing to delete student information (deleteStudent)

Summary: First use equals and loops to determine whether the student number exists, and then use the remove method in the ArrayList collection to delete it.

The implementation code is as follows:

//删除学生的方法
    public static void deleteStudent(ArrayList<Student> arr) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生的学号");
        String sid = sc.nextLine();
        int flag=-1;
        for (int i = 0; i < arr.size(); i++) {
            Student s = arr.get(i);
            if (s.getSid().equals(sid)) {
                arr.remove(i);
                flag=1;
                break;
            }
        }
        if(flag==-1){
            System.out.println("该学号不存在,请重新输入");
            return ;
        }
        System.out.println("删除成功");
    }

5. Code writing to modify student information (updateStudent)

Summary: The codes for modifying information and deleting information are similar. They both first determine whether the student number exists. The next operation of modifying the information is to repackage the information you want to modify into a student class, and then search for the required information in the collection. For the modified subscript position, use the set method in ArrayList to replace the index subscript at the same time.

The implemented code is as follows:

 //修改学生的方法
    public static void updateStudent(ArrayList<Student> arr) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生的学号");
        String sid = sc.nextLine();

        System.out.println("请输入学生的新姓名");
        String name = sc.nextLine();
        System.out.println("请输入学生新年龄");
        String age = sc.nextLine();
        System.out.println("请输入学生家庭住址");
        String adress = sc.nextLine();
        Student s=new Student();

        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(adress);

        int flag=-1;
        for (int i = 0; i < arr.size(); i++) {
            Student s1 = arr.get(i);
            if (s1.getSid().equals(sid)) {
                 arr.set(i,s);
                 flag=1;
                break;
            }
        }

        if(flag==-1){
            System.out.println("输入的学号不存在,请重新输入");
            return ;
        }
        System.out.println("修改信息成功");
    }

6. Code writing to view student information (findAllStudent)

Summary: Determine the format of the output information, and use a loop to traverse the stored information of each type in the collection.

The implementation code is as follows:

//显示所有学生
    public static void findAllStudent(ArrayList<Student> arr) {
        if (arr.size() == 0) {

            System.out.println("无数据,请先输入数据在进行查看");
            return;
        }

        System.out.println("学号" + "\t\t\t" + "姓名" + "\t\t" + "年龄" + "\t\t" + "地址");
        for (int i = 0; i < arr.size(); i++) {

            Student s = arr.get(i);

            System.out.println(s.getSid() + "\t\t" + s.getName() + "\t" + s.getAge() + "岁

\t" + s.getAddress());

        }
    }

7. Retrieve the student number code writing in each category (nameUsed)

Summary: Use the get method in the student class and the equals method in the ArrayList to determine whether there are duplicate names. The return value is received as boolean .

The implementation code is as follows:

 public static boolean nameUsed(ArrayList<Student>arr,String sid){
        boolean flag=false;

        for(int i=0;i<arr.size();i++){

            Student s=arr.get(i);

            if(s.getSid().equals(sid)){

                flag=true;
            }

        }

        return flag;
    }

Summarize:

The result of the code:

If you think the article is good, I look forward to your one-click triple link. Your encouragement is the source of motivation for my creation. Let's work hard together and see you at the top! ! !

Guess you like

Origin blog.csdn.net/smile_sundays/article/details/132239391