ArrayList选项的开关状态?

时间:2019-05-13 16:04:25

标签: java oop methods switch-statement

    import java.util.Scanner;
    import java.util.concurrent.CopyOnWriteArrayList;

    //creates and array of the customers
    public final class customers {
    public static void main(String[] args) throws InputValidationException {

        //add new customer
        CopyOnWriteArrayList<customer> customers = new CopyOnWriteArrayList<>();
        //List will fail in case of remove due to ConcurrentModificationException

        //loop getting input
        //input 'q' to quit

        Scanner input;
        {
            //scanner to get the input

            input = new Scanner(System.in);

            {
                while (true) {
                    //ask user for input and get input
                    System.out.println("Enter customer id (press 'q' to quit): ");
                    String temp = input.nextLine();
                    if (temp.equals("q")) break;

                    int id = Integer.parseInt(temp);

                    System.out.println("Enter first name:");
                    String firstName = input.nextLine();

                    System.out.println("Enter last name:");
                    String lastName = input.nextLine();


                    //add to array list
                    customers.add(new customer(id, firstName, lastName));

                }
            }

        }




        //Display All
        System.out.println("Current List: ");
        for (customer customer : customers) {
            System.out.println(customer.toString());
        }


        // search
        System.out.println("Enter name to search and display");
        String searchString = input.nextLine();
        for (customer customer : customers) {
            if (customer.search(searchString) != null) {
                System.out.println(customer.toString());
            }
        }


        //Remove
        System.out.println("Enter name to search & remove");
        searchString = input.nextLine();
        for (customer customer : customers) {
            if (customer.search(searchString) != null) {
                System.out.println(customer.toString() + " is removed from the List");
                customers.remove(customer);
            }
        }

     }
 }

我的客户类包含变量,getter,setter和构造函数。

我的主班:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws InputValidationException {

        //run customer add/edit/remove

            Scanner scanner = new Scanner(System.in);

                    System.out.println("1. Customers");
                    System.out.println("2. products");
                    System.out.println("3. employees");

                    System.out.println("Which would you like to add/edit: ");
                    String choice = scanner.next();



        switch (choice) {
            case "1" :
                customers.main(null);
            break;
            case "2":
                products.main(null);
            break;
            case "3":
                emoloyees.main(null);
            break;
            default :
                System.out.println("not found!");
        }

    }
 }

如何在客户类中创建switch语句?我想请用户输入添加,搜索,显示或删除客户。 与我的Main类切换语句类似,尽管在Customer类中不起作用。

0 个答案:

没有答案