我是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 + "]";
}
}
答案 0 :(得分:1)
使用lambda表达式:
clang++