0

I am creating a customer manager application for a Java course. I have it separated as per the requirements into 3 packages. The first package has a class called Customer, which models a customer and it's instance variables, such as customerID. The second package is a customer database that includes an ArrayList. The third package is going to be a menu driven UI that will allow the user to choose between 4 options. Currently, I am stuck trying to write a method that will search through the list for a given customerID and return a Customer object.

In the customer database class, I am getting the customerID from the user within the method. Then, I am running a for loop that should traverse the method to see if the customerID is found. I am having issues on how to return a customer object if the id is a match.

public Customer searchCustomer(String customerID) {

     System.out.println("Enter customer ID you want to find:");
     customerID = scnr.next();
     Customer c;

     for (int i = 0, i < customerList.size(); i++ {
          c = customerList.get(i);
          if (customerList.get(i).equals(customerID) {
          String foundID = customerID;
          }
     }
}         

I want to return Customer c at the end of the method, but cannot figure out how to do this.

noobCoder
  • 1
  • 1
  • Why you need to reassign variable customerId customerID = scnr.next(); in this method? – Łukasz Gawron Apr 30 '19 at 20:54
  • I would recommend storing it in a Map instead of List, you would have key customerId and value Customer object. In that case searching would be easy Customer customer = customersMap.get(customerId) – Łukasz Gawron Apr 30 '19 at 20:57
  • Here is similiar topic you may want to explore https://stackoverflow.com/questions/19774374/arraylist-retrieve-object-by-id – Łukasz Gawron Apr 30 '19 at 21:09

3 Answers3

0

In the if statement one can just write return c. This will return the first Customer that matches. At the end of the method one can return null or throw an exception if the Customer wasn’t found.

Taylor Hansen
  • 170
  • 1
  • 9
0

You compare Customer object with CustomerId.

Change code to

if (customerList.get(i).getId().equals(customerID) {
    return customerList.get(i);
}
Bor Laze
  • 2,458
  • 12
  • 20
0

A few of the potential mistakes you're making are:

  1. Passing in an parameter to your method that you're not using. Either do the Scanner stuff outside of the method and then pass the ID to the method, or do the Scanner part in the method and have no parameters. The former approach is generally the preferred one, though.

  2. Comparing the customer to a String. You need to compare the user-entered ID to the Customer object's ID field. The String the user enters will never be equal to the entire Customer object.

  3. You're not returning anything from the method. Once you find the Customer you're looking for, you need a return statement.

Also, you can use Java's "enhanced for loop" to make the code a bit cleaner. Here's some code that assumes that your Customer objects use a String as their ID, and have a .getID() method on them to get their ID.

Here's some code that needs to search for a customer. This can be in another method.

System.out.println("Enter customer ID you want to find: ");
customerID = scnr.next();
Customer customer = searchCustomer(customerID);

And here's the search method that loops through the customerList

public Customer searchCustomer(String customerID) {
    for(Customer customer : customerList) {
        if (customer.getId().equals(customerID) ) {
            return customer;
        }
    }
    return null;  // Or perhaps throw an exception.
}  

Note that I'm making a lot of assumptions about how the other parts of your code that I haven't seen are structured, so you very probably will have to modify this code sample if you want to use it, but hopefully it puts you on the right track.

Jordan
  • 2,273
  • 9
  • 16
  • This seems very helpful! Now I just have to figure how to go about calling this in my application - this teacher is throwing some curveballs on this final project. – noobCoder Apr 30 '19 at 21:37