Array Storage Names and Added, Deleted, and Alterated Checks

Posted by jayarsee on Mon, 14 Oct 2019 18:18:11 +0200

  1. Implementation of Student Management System Version V1.0 (Storage String in Array)
    Array student management requires the management of the student name information stored in it. It can add, delete and query data.
    The functions are shown as follows:
    Student management system:
    1. Adding trainee information
    2. Delete trainee information
    3. Search for trainee information
    4. Modifying Student Information
    5. exit
package com.qf.v1;

import java.util.Scanner;

/**
 * @author Zhouzilong
 * @date 2019/7/26
 */
public class Test {
    // Static properties for all method calls
    private static String[] names = new String[100];

    public static void main(String[] args) {
        do {
            System.out.println("Student Management System:\n" + 
                    "1.Adding Student Information\n" + 
                    "2.Delete student information\n" + 
                    "3.Inquiry for trainee information\n" + 
                    "4.Sign out\n"+"Please enter your choice:");
            Scanner sc = new Scanner(System.in);
            int num = sc.nextInt();
            switch (num) {
            case 1:
                System.out.println("Please enter the name of the trainee:");
                String name = sc.next();
                add(name);
                break;
            case 2:
                System.out.println("Please enter the name of the trainee:");
                String delName = sc.next();
                delete(delName);
                break;
            case 3:
                queryAll();
                break;
            case 4:
                System.out.println("Please enter the name of the trainee:");
                name = sc.next();
                System.out.println("Please enter the revised student name.");
                String name1 = sc.next();
                modify(name, name1);
                break;   
            case 5:
                System.out.println("Exit completed.");
                return;

            default:
                break;
            }
        } while (true);
    }
    
    //The deletion method is to judge whether the student exists or not, and delete if it exists.
    //Deletion logic: for loop scans the target to determine whether the next element of the target is empty, and if it is empty, empty the target.
    //If it is not empty, the next cycle continues to determine whether the next element of the target is empty by exchanging the values of the target and the next element.
    private static void delete(String name) {
        if (ifExist(name) == -1) {
                System.out.println("The student was not found.");
            }
        for (int i = 0; i < names.length; i++) {
            if (name.equals(names[i])) {
               if (names[i + 1] == null) {
                        names[i] = null;
                        System.out.println("Delete successfully!");
                        return;
                    }
                    String temp = names[i];
                    names[i] = names[i + 1];
                    names[i + 1] = temp;
            }
        }
    }
    
        private static void modify(String name, String name1) {
        if (ifExist(name) == -1) {
            System.out.println("The student was not found!");
        }
        for (int i = 0; i < names.length; i++) {
            if (names[i] == null) {
                break;
            }
            if (names[i].equals(name)) {
                names[i] = name1;
            }
        }
    }


    private static void queryAll() {
        for (String name:names) {
            //When String judges null, instead of equals, use==
            if (name == null) {
                break;
            }
            System.out.println(name);
        }
    }

    private static void add(String name) {
        // Judge whether there is a rename in the array, return - 1, no rename; return other subscripts, have a rename
        int index = ifExist(name);
        if (index == -1) {
            System.out.println("Add success");
        } else {
            System.out.println("The name already exists and cannot be added");
        }
    }

    private static int ifExist(String name) {
        // Arrays are contiguous memory spaces and stored values are contiguous.
        for (int i = 0; i < names.length; i++) {
            if (names[i] == null) {
                names[i] = name;
                return -1;
            }
            // Null pointer exception NPE: null call method, null pointer exception will appear
            if (names[i].equals(name)) {
                // Existing, return subscript
                return i;
            }
        }
        return -1;
    }
}

Key points:
1. The address space of an array is continuous and can't skip a space assignment, that is, there can't be null between any two elements of an array. If one element in the middle is null, the latter element will become null.
2. When judging whether a string is empty, use the "==" symbol directly instead of the equals method.
3. Notice the null pointer exception. When using the equals method, it is important to avoid null on the left, otherwise null pointer exceptions (NPE) will occur.

  1. Implementation of Student Management System Version V2.0
  2. Array is used as the data structure to store the information of trainees. The information of trainees only needs to contain their names and telephone numbers. The trainees'names are required to be unique.
    Class Student
package com.qf.v2;

/**
 * @author Zhouzilong
 * @date 2019/7/26
 */
public class Student {
    private String name;
    private String phoneNum;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhoneNum() {
        return phoneNum;
    }
    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }
    public Student() {}
    
    public String toString() {
        return "name: "+name+"  phonenumber: "+phoneNum;
    }
}

Test class

package com.qf.v2;

import java.util.Scanner;

/**
 * @author Zhouzilong
 * @date 2019/7/26
 */
public class Test {
    private static Student[] sts = new Student[100];

    public static void main(String[] args) {
        do {
            System.out.println("Student Management System:\n" + "1.Adding Student Information\n" + "2.Delete student information\n" + "3.Inquiry for trainee information\n" + "4.Sign out\n" + "Please enter your choice:");
            Scanner sc = new Scanner(System.in);
            int num = sc.nextInt();
            switch (num) {
            case 1:
                System.out.println("Please enter the name of the trainee.");
                String name = sc.next();
                System.out.println("Please enter the number of the trainee.");
                String phoneNum = sc.next();
                add(name, phoneNum);
                break;
            case 2:
                System.out.println("Please enter the name of the trainee:");
                name = sc.next();
                delete(name);
                break;
            case 3:
                queryAll();
                break;
            case 4:
                System.out.println("Please enter the name of the trainee:");
                name = sc.next();
                System.out.println("Please enter the revised student name.");
                String name1 = sc.next();
                System.out.println("Please enter the modified mobile phone number.");
                phoneNum = sc.next();
                modify(name, name1,phoneNum);
                break;
            case 5:
                System.out.println("Exit completed.");
                return;

            default:
                System.out.println("Please enter the correct number!");
                break;
            }
        } while (true);
    }
    
    private static void modify(String name, String name1, String phoneNum) {
        if (isExsit(name)) {
            for (int i = 0; i < sts.length; i++) {
                if (sts[i] == null) {
                    break;
                }
                if (sts[i].getName().equals(name)) {
                    sts[i].setName(name1);
                    sts[i].setPhoneNum(phoneNum);
                    System.out.println("Successful revision!");
                }
            }
        }else {
            System.out.println("The student was not found!");
        }
    }

    private static void queryAll() {
        for (Student s : sts) {
            if (s != null) {
                String result = s.toString();
                System.out.println(result + "\n");
            }
        }
    }

    private static void delete(String name) {
        if (isExsit(name)) {
            for (int j = 0; j < sts.length; j++) {
                if (sts[j + 1] == null) {
                    sts[j] = null;
                    System.out.println("Delete successfully!");
                    return;
                }
                //Corresponding
                if (name.equals(sts[j].getName())) {
                    Student temp = sts[j];
                    sts[j] = sts[j + 1];
                    sts[j + 1] = temp;
                }
            }
        } else {
            System.out.println("The trainee does not exist");
        }
    }

    private static void add(String name, String phoneNum) {
        if (isExsit(name, phoneNum) != -1) {
            System.out.println("The student already exists!");
        } else {
            System.out.println("Successful addition!");
        }
    }

    private static int isExsit(String name, String phoneNum) {
        for (int i = 0; i < sts.length; i++) {
            if (sts[i] == null) {
                sts[i] = new Student();
                sts[i].setName(name);
                sts[i].setPhoneNum(phoneNum);
                return -1;
            }

            if (name.equals(sts[i].getName())) {
                return i;
            }
        }
        return -1;
    }

    private static boolean isExsit(String name) {
        for (int i = 0; i < sts.length; i++) {
            if (name.equals(sts[i].getName())) {
                return true;
            }
        }
        return false;
    }
}

Topics: Java Mobile