Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I search a Java ArrayList for an already existing customer ID? I am not

ID: 3529844 • Letter: H

Question

How can I search a Java ArrayList for an already existing customer ID? I am not supposed to know if the customer ID has already been entered. I am to enter in the customer ID when I am entering in a new transaction for the customer. // Data structure to store customers private static ArrayList customers = new ArrayList(); Customer equals the Array List. The customer ID is store in the Customer Class which I call to using Customer generalC = new Customer(); How can I fix the code below to get this to work? // Helper method to determine if a customer exists public static Customer findCustomer(long customerID) { for (int i = 0; i < customers.size(); i++) { this is where I get lost.... } } return

Explanation / Answer

Hey the most easy way to search a customer in a arraylist is to use a for each loop.But you can also do it using your simple for loop approch And this is the right answer not the above one..I have explain both the approches..Check both of them.and rate

public static Customer findCustomer(long customerID) {

for (int i = 0; i < customers.size(); i++) {

if(customers.get(i).getID()==customerID)//or however the structure is to get ID

{

return customers.get(i); //returns the customer object

}

}

return null; //not found

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote