RedHawk87
RedHawk87

Reputation: 3

choose from array list elements that match user input

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 + "]";
}

}

Upvotes: 0

Views: 280

Answers (1)

Use lambda Expressions:

public Customer  findPersonByName(final String name) {
    return mockCustomerDatabase.stream().filter(p -> p.getName().equals(name)).findAny();
}

Upvotes: 1

Related Questions