The first parameter is a HashMap representing the selling price of each item in our store (that is given).
The second parameter is also a HashMap but this one represents our cost for us to purchase each item for our inventory.
The third parameter is also a HashMap but this one representing a customers cart.
This method computes and returns total profit for this day. Note that the profit of selling an item is the cost of a customer (prices in the first parameter) minus our cost to purchase the item our inventory (second parameter). In all computation, use the following prices.
This is what I did, not sure what I did wrong. Also is there a way I can do this without using 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());
}
I also tried this , what am I doing wrong here?
public static double ComputeProfit(HashMap sellingPrice, HashMap inventory, ArrayList> 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());
}
}
Your answer looks mostly right, there are just a few issues with your type naming (Integer should be int, Double should be double) and you use Map.Entry in one portion of the code and Entry later on. The code below fixes the issues in your computeProfit function and should solve your problem. Let me know if you have any questions!
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;
}