第一个参数是一个HashMap,代表我们商店中每件商品的售价(给定)。
第二个参数也是HashMap,但是这个参数代表了我们为库存购买每件商品的成本。
第三个参数也是一个HashMap,但是这个参数代表了一个客户购物车。
此方法计算并返回当天的总利润。请注意,销售商品的利润是客户的成本(第一个参数中的价格)减去购买我们库存商品的成本(第二个参数)。在所有计算中,使用以下价格。
这就是我所做的,不确定我做错了什么。还有一种方法可以在不使用entryset的情况下完成此操作吗?
public static double computeProfit(HashMap<String, Double> sellingPrice, HashMap<String, Double> inventory, ArrayList<HashMap<String, Integer>> orders) {
double ans = 0;
for (HashMap<String, Integer> order : orders) {
for (Map.Entry<String, Integer> itemToQuantity : order.entrySet()) {
String item = itemToQuantity.getKey();
Integer quantityPurchased = itemToQuantity.getValue();
inventory.put(item, inventory.get(item) * quantityPurchased);
}
}
for (Entry<String, Double> itemInInventory : inventory.entrySet()) {
String itemName = itemInInventory.getKey();
Double reQuantity = itemInInventory.getValue();
ans = ans -(sellingPrice.get(itemName)+ reQuantity);
}
return ans;
}
public static ArrayList<HashMap<String,Integer>> allOrders(){
ArrayList<HashMap<String, Integer>> allOrders = new ArrayList<>();
HashMap<String, Integer> cart0 = new HashMap<>();
cart0.put("frozen dinner", 4);
cart0.put("yogurt", 4);
cart0.put("milk", 3);
allOrders.add(cart0);
HashMap<String, Integer> cart1 = new HashMap<>();
cart1.put("yogurt", 4);
cart1.put("strawberries", 3);
cart1.put("apples", 4);
allOrders.add(cart1);
return allOrders;
}
public static HashMap<String,Double>sellingPrice(){
HashMap<String,Double>priceList = new HashMap<String,Double>();
priceList.put("eggs", 1.79);
priceList.put("orange juice",2.5);
priceList.put("yogurt",1.99);
priceList.put("bread",2.49);
priceList.put("butter",2.39);
priceList.put("peppers",1.49);
priceList.put("chips",2.95);
priceList.put("chocolate chips",2.39);
priceList.put("popcorn",1.99);
priceList.put("tomato sauce",0.99);
priceList.put("frozen pizza",5.49);
priceList.put("milk",2.09);
priceList.put("bananas",0.49);
priceList.put("hot dog",1.29);
priceList.put("relish",0.99);
priceList.put("frozen dinner",2.5);
priceList.put("cereal",3.25);
priceList.put("tuna fish",0.99);
priceList.put("coffee",2.0);
priceList.put("pasta",0.99);
priceList.put("strawberries",3.5);
priceList.put("apples",1.29);
priceList.put("sugar",1.99);
priceList.put("ketchup",2.89);
return sellingPrice();
}
public static HashMap<String, Double> purchasePrice(){
HashMap<String,Double>cart = new HashMap <String,Double>();
cart.put("eggs", 1.2);
cart.put("orange juice", 1.0);
cart.put("yogurt", 1.0);
cart.put("bread", 1.5);
cart.put("butter", 1.95);
cart.put("peppers", 0.99);
cart.put("chips", 0.9);
cart.put("chocolate chips", 1.79);
cart.put("popcorn", 0.99);
cart.put("tomato sauce", 0.4);
cart.put("frozen pizza", 2.6);
cart.put("milk", 1.89);
cart.put("bananas", 0.39);
cart.put("hot dog", 0.79);
cart.put("relish", 0.49);
cart.put("frozen dinner", 1.5);
cart.put("cereal", 1.55);
cart.put("tuna fish", 0.49);
cart.put("coffee", 0.6);
cart.put("pasta", 0.5);
cart.put("strawberries", 1.99);
cart.put("apples", 0.99);
cart.put("sugar", 1.5);
cart.put("sugar", 0.98);
return purchasePrice();
}
public static void main(String [] args){
computeProfit(sellingPrice(),purchasePrice(),allOrders());
}
我也试过这个,我在这里做错了什么?
public static double ComputeProfit(HashMap sellingPrice,HashMap inventory,ArrayList&gt; orders){
double ans = 0;
int i =0, j = 0;
for(i=0;i< orders.size();i++) {
HashMap<String,Integer> temp = orders.get(i);
for (j=0;j < temp.size();j++) {
String item = temp.getKey(j);
Integer quantityPurchased = temp.getValue(j);
inventory.put(item, inventory.get(item) * quantityPurchased);
}
}
int k=0 ;
for (k =0 ; k< inventory.size();k++) {
HashMap <String,Integer>itemInInventory = inventory.get(k);
String itemName = itemInInventory.getKey(k);
Double reQuantity = itemInInventory.getValue(k);
ans = ans -(sellingPrice.get(itemName)+ reQuantity);
}
return ans;
}
public static ArrayList<HashMap<String,Integer>> allOrders(){
ArrayList<HashMap<String, Integer>> allOrders = new ArrayList<>();
HashMap<String, Integer> cart0 = new HashMap<>();
cart0.put("frozen dinner", 4);
cart0.put("yogurt", 4);
cart0.put("milk", 3);
allOrders.add(cart0);
HashMap<String, Integer> cart1 = new HashMap<>();
cart1.put("yogurt", 4);
cart1.put("strawberries", 3);
cart1.put("apples", 4);
allOrders.add(cart1);
return allOrders;
}
public static HashMap<String,Double>sellingPrice(){
HashMap<String,Double>priceList = new HashMap<String,Double>();
priceList.put("eggs", 1.79);
priceList.put("orange juice",2.5);
priceList.put("yogurt",1.99);
priceList.put("bread",2.49);
priceList.put("butter",2.39);
priceList.put("peppers",1.49);
priceList.put("chips",2.95);
priceList.put("chocolate chips",2.39);
priceList.put("popcorn",1.99);
priceList.put("tomato sauce",0.99);
priceList.put("frozen pizza",5.49);
priceList.put("milk",2.09);
priceList.put("bananas",0.49);
priceList.put("hot dog",1.29);
priceList.put("relish",0.99);
priceList.put("frozen dinner",2.5);
priceList.put("cereal",3.25);
priceList.put("tuna fish",0.99);
priceList.put("coffee",2.0);
priceList.put("pasta",0.99);
priceList.put("strawberries",3.5);
priceList.put("apples",1.29);
priceList.put("sugar",1.99);
priceList.put("ketchup",2.89);
return sellingPrice();
}
public static HashMap<String, Double> purchasePrice(){
HashMap<String,Double>cart = new HashMap <String,Double>();
cart.put("eggs", 1.2);
cart.put("orange juice", 1.0);
cart.put("yogurt", 1.0);
cart.put("bread", 1.5);
cart.put("butter", 1.95);
cart.put("peppers", 0.99);
cart.put("chips", 0.9);
cart.put("chocolate chips", 1.79);
cart.put("popcorn", 0.99);
cart.put("tomato sauce", 0.4);
cart.put("frozen pizza", 2.6);
cart.put("milk", 1.89);
cart.put("bananas", 0.39);
cart.put("hot dog", 0.79);
cart.put("relish", 0.49);
cart.put("frozen dinner", 1.5);
cart.put("cereal", 1.55);
cart.put("tuna fish", 0.49);
cart.put("coffee", 0.6);
cart.put("pasta", 0.5);
cart.put("strawberries", 1.99);
cart.put("apples", 0.99);
cart.put("sugar", 1.5);
cart.put("sugar", 0.98);
return purchasePrice();
}
public static void main(String [] args){
computeProfit(sellingPrice(),purchasePrice(),allOrders());
}
}
答案 0 :(得分:-1)
你的答案看起来大概是正确的,你的类型命名只有几个问题(Integer应该是int,Double应该是double),并且稍后在代码的一部分和Entry中使用Map.Entry。下面的代码修复了computeProfit函数中的问题,应该可以解决您的问题。如果您有任何问题,请告诉我们!
public static double computeProfit(HashMap<String, double> sellingPrice, HashMap<String, double> inventory, ArrayList<HashMap<String, int>> orders) {
double profit = 0;
for (HashMap<String, int> order: Orders) {
for (Map.Entry<String, int> orderEntry: order.entrySet()) {
double purchasePrice = inventory.get(orderEntry.getKey());
double salePrice = sellingPrice.get(orderEntry.getKey());
int purchaseCount = orderEntry.getValue();
profit += ( salePrice - purchasePrice ) * purchaseCount;
}
}
return profit;
}