Student Management System (ArrayList Simple Edition)

Posted by cachemony on Sun, 13 Feb 2022 19:00:03 +0100

Student Management System (ArrayList Simple Edition)

Ideas for Implementing Student Management System

(1) Defining student classes

(2) Coding of the main interface

(3) Code writing for adding students

(4) View students'code writing

Delete student code writing

Modify students'code writing

Solve the problem of adding duplicate student numbers

Resolve the problem of deleting/modifying student numbers that do not exist

Define Student Classes

  • Student Class:

    • Student
  • Member variables:

    • School number: sid
    • Name:name
    • age:
    • Residence:address
  • Construction method:

    • Parametric construction
    • Construction with four parameters
  • Membership method:

    • get/set method for each member variable

Here the construction and membership methods can be generated using the Alt+Ins key combination

package cn.wolfcode;

public class Student {
    //School Number
    private String sid;
    //Full name
    private String name;
    //Age
    private Integer age;
    //Residence
    private String address;

    public Student() {
    }

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

    public String getSid() {
        return sid;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getAddress() {
        return address;
    }

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

Here's the module-by-module code, with the complete StudentManager code at the bottom

Coding for Main Interface

Ideas:

1. Complete the writing of the main interface with output statements

(2) Using Scanner to Implement Data Entry for Naval Gun

(3) Selection of complete operation with switch statement

(4) Complete the cycle and return to the main interface again

Sample diagram of the main interface:

package cn.wolfcode;

import java.util.Scanner;

public class StudentManager {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        List<Student> array = new ArrayList<>();
        a : while(true) {
            System.out.println("--------Welcome to the Student Management System--------");
            System.out.println("1 Add Students");
            System.out.println("2 Delete Students");
            System.out.println("3 Modify Students");
            System.out.println("4 View all students");
            System.out.println("5 Sign out");
            System.out.println("Please enter your choice:");
            int i = sc.nextInt();
            /*
             * Replace the method with text here, wait until the method is finished, delete this, and call the method instead
             */
            switch (i) {
                case 1:
                    System.out.println("Add Students");
                    break;
                case 2:
                    System.out.println("Delete Students");
                    break;
                case 3:
                    System.out.println("Modify Students");
                    break;
                case 4:
                    System.out.println("View all students");
                    break;
                case 5:
                    System.out.println("Thank you for using");
                    break a;
            }
        }
    }
}

Add Student Coding

Ideas:

  • Select Add Student by Keyboard Input
  • Define a method for adding students
    • Show prompt message, prompt to enter is it?
    • Keyboard Entry Data Required by Student Objects
    • Create a student object and assign keyboard data to member variables of the student object
    • Add Student Object to Collection (Save)
    • Success hints for adding
  • Call Method
//Add Students
private static void add(List<Student> array) {
    System.out.println("Please enter the student's number:");
    String sid = sc.next();
    System.out.println("Please enter the name of the student:");
    String name = sc.next();
    System.out.println("Please enter the student's age:");
    int age = sc.nextInt();
    System.out.println("Please enter the address of the student:");
    String address = sc.next();
    Student student = new Student(sid, name, age, address);
    array.add(student);
    System.out.println("Successful addition of student information");
}

Change console printing in case1 to calling methods

case 1:
    add(array);
    break;

View student code writing

Ideas:

  • Use keyboard input to select to view all student information
  • Define a method for viewing student learning
    • Determine if there is student information in the collection, if no prompt is displayed
    • Display header information
    • Remove data from the set to display student information in the corresponding format
  • Call Method
//View Students
private static void selectAll(List<Student> array) {
    if(array.isEmpty()) {
            System.err.println("No information, Please add message and query again!");
            return;
        }
    //Show Header
    System.out.println("School Number\t\t Full name\t\t Age\t\t Residence");
    //for loop traverses array set
    for(int i=0; i<array.size(); i++) {
        Student s = array.get(i);
        System.out.println(s.getSid()+ "\t\t" + s.getName() + "\t\t" + s.getAge() + "year\t" + s.getAddress());
    }
}

Change console printing in case4 to calling method

case 4:
    selectAll(array);
    break;

Delete student code writing

Ideas:

  • Use keyboard input to select delete student information
  • Define a method for deleting student information
    • Show prompt information
    • Type the student number you want to delete
    • Traversing through a collection removes the corresponding student object from the collection
    • Success hint for deletion
  • Call Method
//Delete Students
private static void delete(List<Student> array) {
    System.out.println("Please enter the student number to delete");
    String sid = sc.next();
    for(int i=0; i<array.size(); i++) {
        if(sid.equals(array.get(i).getSid())) {
            array.remove(i);
        }
    }
    System.out.println("Student Information Deleted Successfully");
}

Change console printing in case2 to calling method

case 2:
    delete(array);
    break;

Modify student code writing

Ideas:

  • Use keyboard input selection to modify student information
  • Define a method for modifying student information
    • Show prompt information
    • Type the student number you want to modify
    • Type the student information you want to modify
    • Traverse the collection to modify the corresponding student information
    • Give Successful Modification Tips
  • Call Method
//Modify Student Information
private static void update(List<Student> array) {
    System.out.println("Please enter the student number to modify the information");
    String sid = sc.next();
    System.out.println("Please enter the new name of the student:");
    String name = sc.next();
    System.out.println("Please enter the new age of the student:");
    int age = sc.nextInt();
    System.out.println("Please enter the new address of the student:");
    String address = sc.next();
    Student student = new Student(sid, name, age, address);
    for(int i=0; i<array.size(); i++) {
        if(sid.equals(array.get(i).getSid())) {
            array.set(i,student);
        }
    }
    System.out.println("Successful Student Information Modification");
}

Change console printing in case3 to calling methods

case 3:
    update(array);
    break;

Solve the problem of adding duplicate student numbers

Ideas:

  • Define a method to determine if a school number is used
    • Returns true if it is the same number as a student in the collection
    • Otherwise, return false
  • Call this method after adding the student enrollment number
    • If true is returned, pop up a prompt to re-enter the school number
    • Otherwise the student object can be added normally
//Determine if the number is repeated
private static boolean hasSid(List<Student> array, String sid) {
    for(int i=0; i<array.size(); i++) {
        if(sid.equals(array.get(i).getSid())) {
            return true;
        }
    }
    return false;
}

Modify the code to add the student object after writing the method

//Add Students
private static void add(List<Student> array) {
    String sid = null;
    while (true) {
        System.out.println("Please enter the student's number:");
        sid = sc.next();
        if (hasSid(array, sid)) {
            System.out.println("The number you entered has been occupied. Please re-enter the number");
        } else {
            break;
        }
    }
    System.out.println("Please enter the name of the student:");
    String name = sc.next();
    System.out.println("Please enter the student's age:");
    int age = sc.nextInt();
    System.out.println("Please enter the address of the student:");
    String address = sc.next();
    Student student = new Student(sid, name, age, address);
    array.add(student);
    System.out.println("Successful addition of student information");
}

Solve the problem of deleting/modifying student numbers that do not exist

Ideas:

  • Call hasSid(array, sid) to determine if a pupil number exists
    • If false is returned, pop up the prompt and re-enter the school number
    • Otherwise the student object can be deleted/modified normally

Since modifying deletion is the same as modifying the method's code, only the deletion method is shown here. To see how the modification method can be viewed in the full code below

//Delete Students
private static void delete(List<Student> array) {
    String sid;
    while(true) {
        System.out.println("Please enter the student number to delete");
        sid = sc.next();
        if (!hasSid(array, sid)) {
            System.err.println("This information does not exist, please re-enter it");
        } else {
            break;
        }
    }
    for(int i=0; i<array.size(); i++) {
        if(sid.equals(array.get(i).getSid())) {
            array.remove(i);
        }
    }
    System.out.println("Student Information Deleted Successfully");
}

Since it's a simple version of the student management system, it's almost like completing the basic CRUD. There may be an advanced version of the student management system in the future. Anxious students can also trust me to find out

Here's the complete StudentManager code

package cn.wolfcode;

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

public class StudentManager {
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        List<Student> array = new ArrayList<>();

        //Here's where I do the test. Unwanted children's shoes can delete this paragraph
        Student s1 = new Student("001", "xhm", 12, "blw");
        Student s2 = new Student("002", "pdx", 12, "dst");
        Student s3 = new Student("003", "zyg", 21, "rlx");
        array.add(s1);
        array.add(s2);
        array.add(s3);
        
        a:
        while (true) {
            System.out.println("--------Welcome to the Student Management System--------");
            System.out.println("1 Add Students");
            System.out.println("2 Delete Students");
            System.out.println("3 Modify Students");
            System.out.println("4 View all students");
            System.out.println("5 Sign out");
            System.out.println("Please enter your choice:");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    add(array);
                    break;
                case 2:
                    delete(array);
                    break;
                case 3:
                    update(array);
                    break;
                case 4:
                    selectAll(array);
                    break;
                case 5:
                    System.out.println("Thank you for using");
                    break a;
            }
        }
    }

    //Add Students
    private static void add(List<Student> array) {
        String sid = null;
        while (true) {
            System.out.println("Please enter the student's number:");
            sid = sc.next();
            if (hasSid(array, sid)) {
                System.err.println("The number you entered has been occupied. Please re-enter the number");
            } else {
                break;
            }
        }
        System.out.println("Please enter the name of the student:");
        String name = sc.next();
        System.out.println("Please enter the student's age:");
        int age = sc.nextInt();
        System.out.println("Please enter the address of the student:");
        String address = sc.next();
        Student student = new Student(sid, name, age, address);
        array.add(student);
        System.out.println("Successful addition of student information");
    }

    //View Students
    private static void selectAll(List<Student> array) {
        if (array.isEmpty()) {
            System.err.println("No information, Please add message and query again!");
            return;
        }
        //Show Header
        System.out.println("School Number\t\t Full name\t\t Age\t\t Residence");
        //for loop traverses array set
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            System.out.println(s.getSid() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "year\t" + s.getAddress());
        }
    }

    //Delete Students
    private static void delete(List<Student> array) {
        String sid;
        while(true) {
            System.out.println("Please enter the student number to delete");
            sid = sc.next();
            if (!hasSid(array, sid)) {
                System.err.println("This information does not exist, please re-enter it");
            } else {
                break;
            }
        }
        for(int i=0; i<array.size(); i++) {
            if(sid.equals(array.get(i).getSid())) {
                array.remove(i);
            }
        }
        System.out.println("Student Information Deleted Successfully");
    }

    //Modify Student Information
    private static void update(List<Student> array) {
        String sid;
        while(true) {
            System.out.println("Please enter the student number to modify the information");
            sid = sc.next();
            if (!hasSid(array, sid)) {
                System.err.println("This information does not exist, please re-enter it");
            } else {
                break;
            }
        }
        System.out.println("Please enter the new name of the student:");
        String name = sc.next();
        System.out.println("Please enter the new age of the student:");
        int age = sc.nextInt();
        System.out.println("Please enter the new address of the student:");
        String address = sc.next();
        Student student = new Student(sid, name, age, address);
        for(int i=0; i<array.size(); i++) {
            if(sid.equals(array.get(i).getSid())) {
                array.set(i,student);
            }
        }
        System.out.println("Successful Student Information Modification");
    }

    //Determine if the number is repeated
    private static boolean hasSid(List<Student> array, String sid) {
        for(int i=0; i<array.size(); i++) {
            if(sid.equals(array.get(i).getSid())) {
                return true;
            }
        }
        return false;
    }
}

Topics: Java Back-end arraylist