我一直在做学校作业。
目标:
我正在提供一份超市顾客名单
每个客户都有一个邮政编码和一个包含产品名称的集合以及该客户购买的这些产品的数量。
我被要求返回一个地图(Sting = zipCode,Product = Product),其中应包含邮政编码作为密钥以及该邮政编码最畅销的产品。
我得到的代码:
/**
* (DIFFICULT!!!)
* calculates a map of most bought products per zip code that is also ordered by zip code
* if multiple products have the same maximum count, just pick one.
* @return
*/
public Map<String, Product> mostBoughtProductByZipCode() {
Map<String, Product> mostBought = null;
// TODO create an appropriate data structure for the mostBought and calculate its contents
return mostBought;
}
我一直在尝试在地图中使用地图,但在实现这一点时遇到问题。这还远未完成,根本无法编译。
/**
* (DIFFICULT!!!)
* calculates a map of most bought products per zip code that is also ordered by zip code
* if multiple products have the same maximum count, just pick one.
* @return
*/
public Map<String, Product> mostBoughtProductByZipCode() {
Map<String, Product> mostBought = null;
Map<String, Map<Product, Integer>> zipCodeProducts = new HashMap<>();
for (Customer customer : this.customers) {
String tmp = customer.getZipCode();
Map<Product, Integer> tmpMap = new HashMap<>();
for (Purchase purchase: customer.getItems()) {
tmpMap.put(purchase.getProduct(),purchase.getAmount());
}
if (!zipCodeProducts.containsKey(tmp)){
zipCodeProducts.put(tmp, tmpMap);
} else {
???
}
}
// TODO create an appropriate data structure for the mostBought and calculate its contents
return mostBought;
}
我可以采取哪些步骤来修复此实施?我只是寻求提示而不是完整的解决方案。
12345678_0001
三国纷争
相关分类