2 回答

TA貢獻1797條經驗 獲得超6個贊
下面一行是錯誤的。
ellicenses.add(new ElvisLicense("TestCompany","VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));
由于許可證需要客戶反對一個參數。相反,您應該首先創建客戶對象。
ellicenses.add(new ElvisLicense(new Customer("TestCompany","VendorA",1234,"1234-A"),"Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));
重復使用它customer list以避免創建公司。
for(Customer customer : customers){
// here you need some way to offer other parameters except customer parameter.
License license = new new ElvisLicense(customer,"Solutions Server gold","1234-dtbk-87654-nlof",10, true , true);
ellicenses.add(license);
}

TA貢獻1794條經驗 獲得超7個贊
您需要做的是在創建對象時使用您已經創建的 Customer 對象之一ElvisLicense。為了更容易地按姓名找到該客戶,我建議您將它們存儲在地圖中,而不是將名稱作為鍵的列表。
Map<String, Customer> customerMap = new HashMap<>();
Customer customer = new Customer("TestCompany","John Doe",1234567890,"[email protected]"));
customerMap.put(customer.getcCompany(), customer);
所以在創建許可證時你要查找客戶
List <ElvisLicense> ellicenses = new ArrayList <> (10);
Customer customer = customerMap.get("TestCompany");
if (customer != null) {
ElvisLicense license = new ElvisLicense(customer,"VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));
ellicenses.add(license);
} else {
//If the customer isn't found you need some kind of error handling, better than below :)
System.out.println("Can't create a license, no customer found");
}
添加回答
舉報