Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In a class named \"Lab\" write a public static method named \"computeProfit\" th

ID: 3714995 • Letter: I

Question

In a class named "Lab" write a public static method named "computeProfit" that takes three parameters and returns a double. The first parameter is a HashMap<String, Double> representing the selling price of each item in our store that we have been using throughout this lab. The second parameter is also a HashMap<String, Double> but this one represents our cost for us to purchase each type of item for our inventory. The third parameter is an ArrayList<HashMap<String, Integer» representing all the orders that have been made in a day. This is an ArrayList where each element is a HashMap<String, Integer> representing a customer's cart in the same format as the previous part. This method computes and returns our total profit for this day. Note that the profit of selling an item is the cost to the customer (prices in the first parameter) minus our cost to purchase the item for our inventory (second parameter). In all computation, use the following prices.


Selling prices: {eggs=2.50, orange juice=2.75, yogurt=1.50, bread=2.10, butter=2.0, peppers=1.99, chips=2.55, chocolate chips=3.95, popcorn=5.99, tomato sauce=2.49, frozen pizza=6.99, milk=2.09, bananas=0.49, hot dog=1.29, relish=0.99, frozen dinner=2.5, cereal=3.25, tuna fish=0.99, coffee=2.0, pasta=0.99}


Purchase prices: {eggs=1.2, orange juice=2.3, yogurt=2.0, bread3.15, butter=1.5, peppers=1.05, chips=0.9, chocolate chips=1.79, popcorn=0.99, tomato sauce=0.4. frozen pizza=2.6, milk=1.89, bananas=0.39. hot dog=0.79, relish=0.49, frozen dinner=1.5, cereal=1.55, tuna fish=0.49, coffee=4.0, pasta=0.5}

Explanation / Answer

import java.util.ArrayList; import java.util.HashMap; public class Lab { public static double computeProfit(HashMap sp, HashMap cp, ArrayList orders) { double profit = 0; for (HashMap order: orders) { for (String item: order.keySet()) { int quantity = order.get(item); double itemSp = sp.get(item); double itemCp = cp.get(item); double itemProfit = (itemSp - itemCp) * quantity; profit += itemProfit; } } return profit; } public static void main(String args[]) { // I have added only two items, you can add more HashMap sp = new HashMap() {{put("eggs", 2.5);put("orange juice", 2.75);}}; HashMap cp = new HashMap() {{put("eggs", 1.2);put("orange juice", 2.3);}}; HashMap order1 = new HashMap(){{put("eggs", 5); put("orange juice", 1);}}; HashMap order2 = new HashMap(){{put("eggs", 10); put("orange juice", 20);}}; ArrayList orders = new ArrayList(); orders.add(order1); orders.add(order2); System.out.println(computeProfit(sp, cp, orders)); } }