从匹配用户输入的数组列表元素中进行选择

时间:2016-04-27 18:26:57

标签: java

我是java的新手,我正在尝试创建一个客户数据库程序。有些Customers具有不同的firstName但相同的lastName(反之亦然)。如果用户输入Customer lastName来搜索并lastName匹配多个Customer,我该如何显示与用户输入相匹配的Customer列表然后会提示您选择要使用哪个Customer

这是我到目前为止的代码:

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

客户数据库:

private void createMockData() {
Customer cust = new Customer("Brain", "Holtz", "Brian@Holtz.Com");
mockCustomerDatabase.add(cust);
cust = new Customer("Bruce", "Bagley", "Bruce@Bagley.com");
mockCustomerDatabase.add(cust);
cust = new Customer("Courtney", "Lee", "Courtney@lee.com");
mockCustomerDatabase.add(cust);
cust = new Customer("Jacob", "Graf", "Jacob@graf.com");
mockCustomerDatabase.add(cust);
cust = new Customer("Jacob", "Brown", "Jacob@Brown.com");
mockCustomerDatabase.add(cust);
cust = new Customer("Kevin", "Brown", "Kevin@Brown.com");
mockCustomerDatabase.add(cust);

客户类:

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

}

1 个答案:

答案 0 :(得分:1)

使用lambda表达式:

clang++