0

I'm new to java and I'm trying to create a customer database program. There are some Customers with different firstName but same lastName (and visa-versa). If user inputs Customer lastName to search for and lastName matches multiple Customers, how can I show a list of Customers who matched the user's input and then be prompted to select which Customer is to be used?

Here is the code I have so far:

private Customer searchCustomer(String search) {
Customer customer = null;
for (Customer cust : mockCustomerDatabase) {
    if (cust.getLastName().toLowerCase().indexOf(search.toLowerCase()) > -1) 
    return cust;
    }
}
return customer;
}

Customer database:

private void createMockData() {
Customer cust = new Customer("Brain", "Holtz", "[email protected]");
mockCustomerDatabase.add(cust);
cust = new Customer("Bruce", "Bagley", "[email protected]");
mockCustomerDatabase.add(cust);
cust = new Customer("Courtney", "Lee", "[email protected]");
mockCustomerDatabase.add(cust);
cust = new Customer("Jacob", "Graf", "[email protected]");
mockCustomerDatabase.add(cust);
cust = new Customer("Jacob", "Brown", "[email protected]");
mockCustomerDatabase.add(cust);
cust = new Customer("Kevin", "Brown", "[email protected]");
mockCustomerDatabase.add(cust);

Customer Class:

public class Customer {
public String firstName;
public String lastName;
public String email;
public Customer(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

public Customer() {
}

//Getter's and Setter's 
@Override
public String toString() {
return "Customer [firstName=" + firstName + ", lastName=" + lastName       + ",email=" + email + "]";
}

}
2
  • You need to start by making the searchCustomer method return a List of Customer objects. You are already looping through the main list of customers so you have most of the logic done. Commented Apr 27, 2016 at 18:35
  • @Austin: I figured things out. thank you Commented Apr 29, 2016 at 17:34

1 Answer 1

1

Use lambda Expressions:

public Customer  findPersonByName(final String name) {
    return mockCustomerDatabase.stream().filter(p -> p.getName().equals(name)).findAny();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.