有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java方法来查找要添加对象的ArrayList索引

我有一个ArrayList使用Customer类填充客户信息。在我的addCustomerRecord方法中,我在addCustomerRecord方法中调用findAddIndex,以便在显示数据之前对输入的数据进行排序。这是我的代码,不要介意fileWhatever方法,我不使用它

public class CustomerDemo
{
    //arrayList of customer objects
    public static ArrayList<Customer> customerAL = new ArrayList<>();



    public static void main (String[] args)
    {        
        //to hold menu choice
        String menuChoice = "";

        Scanner kb = new Scanner(System.in);

        System.out.println("To add a record press 'A': \n"
                + "to display all records press 'D': \n"
                + "to exit press 'Q': \n");

        //loop priming read
        menuChoice = kb.nextLine();

        //make input case insensitive
        menuChoice = menuChoice.toLowerCase();


        do
        {
            if(menuChoice.equals("a"))
                addCustomerRecord(kb);
            else if(menuChoice.equals("d"))
            {
                displayCustomerRecords();
            }
            else if(menuChoice.equals("q"))
            {
                System.out.println("Program exiting..");
                System.exit(0);
            }

            else
            {
                System.out.println("incorrect entry. Please re-enter a valid entry: \n");
                menuChoice = kb.nextLine();
                menuChoice = menuChoice.toLowerCase();
            }

            System.out.println("To add a record press 'A': \n"
                    + "to display all records press 'D': \n"
                    + "to exit press 'Q': \n");
            menuChoice = kb.nextLine();
            menuChoice = menuChoice.toLowerCase();
        }while(menuChoice.equals("a") || menuChoice.equals("d") || menuChoice.equals("q"));


        kb.close();
    }

   /*     public static void displayCustomerRecords()
    {
        System.out.println();
        for (int i = 0; i < customerAL.size(); ++i)
        {
            System.out.printf("%-15s", customerAL.get(i).getLastName());
            System.out.printf("%-15s", customerAL.get(i).getFirstName());
            System.out.printf("%-6s", customerAL.get(i).getCustID());
            System.out.printf("%15s\n", customerAL.get(i).getPhoneNumber());
        }
        System.out.println();
    }


    /**
     * prompts to enter customer data and mutator methods called 
     * with a Scanner object passed as an argument to set data
     * @param location index position of where the element will be added. 
     * @param kb a Scanner object to accept input
     */

    public static void addCustomerRecord(Scanner kb)
    {

        Customer currentCustomerMemoryAddress = new Customer();

        System.out.println("Enter first name: \n");
        String fName = kb.nextLine();
        currentCustomerMemoryAddress.setFirstName(fName);

        System.out.println("Enter last name: \n");
        String lName = kb.nextLine();
        currentCustomerMemoryAddress.setLastName(lName);

        System.out.println("Enter customer phone number: \n");
        String pNum = kb.nextLine();
        currentCustomerMemoryAddress.setPhoneNumber(pNum);

        System.out.println("Enter customer ID number: \n");
        String ID = kb.nextLine();
        currentCustomerMemoryAddress.setCustID(ID);

        int addLocation = findAddLocation(currentCustomerMemoryAddress);

        customerAL.add(addLocation, currentCustomerMemoryAddress);

        currentCustomerMemoryAddress = null;
    }

    public static int findAddLocation(Customer cust)
    {
        int location = 0;

        if(!customerAL.isEmpty())
        {
            for(int i = 0; i < customerAL.size(); i++)
            {
                //Stumped here
            }
        }
        else
            return location;
        return location;
    }
}    

共 (0) 个答案