Explain in detail how Java uses collections to implement a customer information management system

  • 2021-12-11 18:02:56
  • OfStack

Directory 1 Customer Class 2 Main Interface 3 Methods (1) Add customers (2) Judge whether the number is occupied (3) Modify customer information (4) Delete customers (5) Customer list (6) Exit 4 Problem summary (1) String comparison problem (2) Unsuccessful customer modification (3) Doubts when using get and set methods (why use set here and get there?)

1 Customer category


public class Customers {
    private String cid;
    private String name;
    private String sex;
    private String age;
    private String call;
    private String email;

    public Customers() {
    }

    public Customers(String cid,String name, String sex, String age, String call, String email) {
        this.cid=cid;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.call = call;
        this.email = email;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

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

    public String getCall() {
        return call;
    }

    public void setCall(String call) {
        this.call = call;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email){
        this.email = email;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }
}




2 main interface


public class Customermanager {
    public static void main(String[] args)   {
        ArrayList<Customers> array = new ArrayList<Customers>();

        while (true) {
            System.out.println( "----- Customer information management software -----" );
            System.out.println( "1  Add Customer " );
            System.out.println( "2  Modify customer " );
            System.out.println( "3  Delete customer " );
            System.out.println( "4  Customer list " );
            System.out.println( "5  Quit " );
            System.out.println( " Please select 1-5" );

            Scanner sc = new Scanner( System.in );
            String line = sc.nextLine();

            switch (line) {
                case "1":
                    //System.out.println( "1  Add Customer " );
                    addCustomer( array );
                    break;
                case "2":
                    //System.out.println( "2  Modify customer " );
                    modifyCustomer( array );
                    break;
                case "3":
                    //System.out.println( "3  Delete customer " );
                    deleteCustomer( array );
                    break;
                case "4":
                    //System.out.println( "4  View All Customers " );
                    findCustomer( array );
                    break;
                case "5":
                    System.out.print(" Confirm whether to exit (Y/N) : ");
                    String y = sc.nextLine();
                    if (y.equals( "Y" ))
                    {
                        System.exit( 0 );
                    }


            }
        }

    }

3 Methods

(1) Add customers


public static void addCustomer(ArrayList<Customers> array ){
        Scanner sc = new Scanner( System.in );
        String cid;

        while (true){
            System.out.println(" Please enter customer number ");
            cid = sc.nextLine();
            boolean flag =isUsed( array,cid );
            if (flag){
                System.out.println(" Number is occupied, please re-enter ");

            }else {
                break;
            }
        }




        System.out.println(" Please enter the customer name ");
        String name = sc.nextLine();
        System.out.println(" Please enter customer gender ");
        String sex = sc.nextLine();
        System.out.println(" Please enter customer age ");
        String age = sc.nextLine();
        System.out.println(" Please enter the customer phone number ");
        String call = sc.nextLine();
        System.out.println(" Please enter the customer email address ");
        String email = sc.nextLine();

        Customers c = new Customers();
        c.setCid( cid );
        c.setName( name );
        c.setSex( sex );
        c.setAge( age );
        c.setCall( call );
        c.setEmail( email );
        array.add( c);

        System.out.println(" Successful addition ");

    }

(2) Judge whether the number is occupied or not


 public static boolean isUsed(ArrayList<Customers>array ,String cid){
        boolean flag = false;
        for (int i = 0; i <array.size() ; i++) {
            Customers s = array.get( i );
            if (s.getCid().equals( cid )){
                flag = true;
                break;
            }

        }
        return flag;
    }

(3) Modify customer information


public static void modifyCustomer(ArrayList<Customers> array ){
        Scanner sc= new Scanner( System.in );
        System.out.println(" Please enter the customer number to modify ");
        String cid = sc.nextLine();
        System.out.println(" Please enter the customer name ");
        String name = sc.nextLine();
        System.out.println(" Please enter customer gender ");
        String sex = sc.nextLine();
        System.out.println(" Please enter customer age ");
        String age = sc.nextLine();
        System.out.println(" Please enter the customer phone number ");
        String call = sc.nextLine();
        System.out.println(" Please enter the customer email address ");
        String adress = sc.nextLine();
        String email = sc.nextLine();
        Customers c = new Customers();
        c.setCid( cid );
        c.setName( name );
        c.setSex( sex );
        c.setAge( age );
        c.setCall( call );
        c.setEmail( email );

        for (int i = 0; i <array.size() ; i++) {
            Customers customers = array.get( i );
            if (customers.getCid().equals( cid )){
                array.set( i,c );
                break;
            }

        }
        System.out.println(" Successful modification of customer information ");

    }

(4) Delete customers


 public static void deleteCustomer(ArrayList<Customers> array ){
        Scanner sc = new Scanner( System.in );
        System.out.println(" Please enter the customer number to delete (-1 Quit )");
        String cid = sc.nextLine();
        if (cid.equals( "-1" )){
            return;
        }
            int index = -1;
            for (int i = 0; i < array.size(); i++) {
                Customers s = array.get( i );
                if (s.getCid().equals( cid )) {
                    index = i;
                    break;
                }
            }
            if (index == -1) {
                System.out.println( " This information does not exist, please re-enter it " );
            } else {
                System.out.println(" Confirm deletion (Y/N) : ");
                String s = sc.nextLine();
                if (s.equals( "Y" )|s.equals( "y" )){
                    array.remove( index );
                    System.out.println( " Delete succeeded " );
                }
        }
    }

(5) Customer list


 public static void findCustomer(ArrayList<Customers> array ){
        if (array.size()==0){
            System.out.println(" No information, please add information before querying ");
            return;// In order that the program will not execute further, 
        }
        System.out.println(" Numbering \t Name \t Gender \t Age \t\t Telephone \t\t Mailbox ");
        for (int i = 0; i <array.size() ; i++) {
            Customers s = array.get( i );

            System.out.println(s.getCid()+"\t\t"+s.getName()+"\t"+s.getSex()+"\t"+s.getAge()+"\t\t"+s.getCall()+"\t\t"+s.getEmail());

        }
    }

(6) Exit


 System.out.print(" Confirm whether to exit (Y/N) : ");
                    String y = sc.nextLine();
                    if (y.equals( "Y" ))
                    {
                        System.exit( 0 );
                    }

4 Summary of problems

(1) String comparison problem

I encountered a problem when I entered "-1" to exit. At that time, I thought about how to compare the data of String type and int type. Later, I knew that I could directly compare the data of String type with equals method.

(2) Unsuccessful customer modification

This is an error code slice


for (int i = 0; i <array.size() ; i++) {
            Customers customers = array.get( i );
            if (customers.getCid().equals( i )){
                array.set( i,customers );
                break;
            }

The error is as follows:
First of all, if (customers. getCid (). equals ()) compares whether the traversed set is the same as the input cid, so it should be compared with cid instead of i; Secondly, array. set (); This step is to modify the element at the specified index and return the modified element; Here, the element at the index is modified with c, which stores the new customer information above, so the modified code is


for (int i = 0; i <array.size() ; i++) {
            Customers customers = array.get( i );
            if (customers.getCid().equals( cid )){
                array.set( i,c );
                break;
            }

(3) Doubts about the use of get and set (why use set here and get there?)

In this topic, set method is used when inputting data, and get method is used when outputting data. So where is the specific application scenario?
set is write data, get is get data
Some information of the class is hidden inside the class, and it is not allowed to be accessed directly by external programs. Instead, the operation and access to the hidden information are realized through the methods provided by the class

Member variable private, providing the corresponding getXxx ()/setXxx () method
Summary: Make clear the requirements of the trial scenario. After encapsulation, write data by (input) set method, and get data by (output) get method.


Related articles: