我正在尝试从主要信息中检索所有客户的信息。我有一个CustomerManager类和一个类main(它是:main(String [] args))这是CustomerManager类的代码,它使用hashmap作为集合
公共类CustomerManager {
private static final Map<String, Customer> customersManager = new HashMap<> ();
public static void addCustomer(Customer c){
// Customers
customersManager.put(c.getID(), c);
customersManager.put ("ID1", new Customer ("ID1","Jonathan", "Mifsud", "Test Address", 21345678, "L001"));
customersManager.put ("ID2", new Customer ("ID2","David", "Aguis", "2nd Address", 21456778, "L002"));
customersManager.put ("ID3", new Customer ("ID3","Frank", "Mamo", "example Address", 21987653, "L003"));
}
public static void findCustomer(Customer c){
customersManager.put(c.getID(), c);
}
public static void deleteCustomer(Customer c){
customersManager.remove(c.getID());
}
}
现在如何从CustomerManagement类检索客户信息到main。我试过这个:
System.out.println(“插入客户ID:”);
customersManager.findCustomer(C);
但它给了我一个错误:
变量c可能没有被初始化
答案 0 :(得分:1)
在toString()
类中添加以下Customer
方法以打印其内容:
@Override
public String toString() {
return "Customer [fname=" + fname
+", lname=" + lname
+", address=" + address
+", phone=" + phone
+", loyaltyCard=" + loyaltyCard + "]";
}