In a class named \"Lab\" write a public static method named \"totalCostOfRemaini
ID: 3740364 • Letter: I
Question
In a class named "Lab" write a public static method named "totalCostOfRemainingInventory" 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 a HashMap<String, Integer> representing our inventory at the start of the day (quantities of each item in the store). 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 the total cost to purchase all of our remaining inventory at the end of the day. That is, how much value we have in inventory after all the customer purchases have been processed. In all computations use the following prices and initial inventory
Selling prices: {eggs=1.79, orange juice=2.5, yogurt=1.99, bread=2.49, butter=2.39, peppers=1.49, chips=2.95, chocolate chips=2.39, popcorn=1.99, tomato sauce=0.99, frozen pizza=5.49, 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, strawberries=3.5, apples=1.29, sugar=1.99, ketchup=2.89}
Initial inventory: {eggs=41, orange juice=18, yogurt=17, bread=25, butter=23, peppers=22, chips=32, chocolate chips=28, popcorn=46, tomato sauce=49, frozen pizza=19, milk=27, bananas=22, hot dog=37, relish=19, frozen dinner=34, cereal=44, tuna fish=23, coffee=33, pasta=23, strawberries=42, apples=35, sugar=44, ketchup=35}
Explanation / Answer
Hii, I have created the required method for you. Inside the method , it will first calculate the total value of initial inventory, then calculate the combined price of all orders, which is then subtracted from the total cost to get the final value of the inventory. Defined a main method, created appropriate maps as per the question and demonstrated the method. Please find the code below. Thanks
//Lab.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Lab {
public static double totalCostOfRemainingInventory(
HashMap<String, Double> sellingPrices,
HashMap<String, Integer> inventory,
ArrayList<HashMap<String, Integer>> orders) {
double totalPrice = 0;
/**
* looping through all items in the inventory, finding the total price
*/
for (Map.Entry<String, Integer> entry : inventory.entrySet()) {
/**
* total value of one item = quantity * selling price
*/
totalPrice += entry.getValue() * sellingPrices.get(entry.getKey());
}
/**
* looping through all items in the list of orders, finding the total
* price
*/
double orderPrice = 0;
for (HashMap<String, Integer> cart : orders) {
/**
* looping through each elements in the current cart
*/
for (Map.Entry<String, Integer> entry : cart.entrySet()) {
/**
* order value of one item = quantity * selling price
*/
orderPrice += entry.getValue()
* sellingPrices.get(entry.getKey());
}
}
/**
* remaining value= total price - price of the order
*/
double remainingValue = totalPrice - orderPrice;
return remainingValue;
}
public static void main(String[] args) {
/**
* Creating a map of items and their selling prices, adding each items
* along with its price
*/
HashMap<String, Double> sellingPrices = new HashMap<String, Double>();
sellingPrices.put("eggs", 1.79);
sellingPrices.put("orange juice", 2.5);
sellingPrices.put("yogurt", 1.99);
sellingPrices.put("bread", 2.49);
sellingPrices.put("butter", 2.39);
sellingPrices.put("peppers", 1.49);
sellingPrices.put("chips", 2.95);
sellingPrices.put("chocolate chips", 2.39);
sellingPrices.put("popcorn", 1.99);
sellingPrices.put("tomato sauce", 0.99);
sellingPrices.put("frozen pizza", 5.49);
sellingPrices.put("milk", 2.09);
sellingPrices.put("bananas", 0.49);
sellingPrices.put("hot dog", 1.29);
sellingPrices.put("relish", 0.99);
sellingPrices.put("frozen dinner", 2.5);
sellingPrices.put("cereal", 3.25);
sellingPrices.put("tuna fish", 0.99);
sellingPrices.put("coffee", 2.0);
sellingPrices.put("pasta", 0.99);
sellingPrices.put("strawberries", 3.5);
sellingPrices.put("apples", 1.29);
sellingPrices.put("sugar", 1.99);
sellingPrices.put("ketchup", 2.89);
/**
* Creating a map of items and their quantities, adding each items along
* with its initial quantity
*/
HashMap<String, Integer> inventory = new HashMap<String, Integer>();
inventory.put("eggs", 41);
inventory.put("orange juice", 18);
inventory.put("yogurt", 17);
inventory.put("bread", 25);
inventory.put("butter", 23);
inventory.put("peppers", 22);
inventory.put("chips", 32);
inventory.put("chocolate chips", 28);
inventory.put("popcorn", 46);
inventory.put("tomato sauce", 49);
inventory.put("frozen pizza", 19);
inventory.put("milk", 27);
inventory.put("bananas", 22);
inventory.put("hot dog", 37);
inventory.put("relish", 19);
inventory.put("frozen dinner", 34);
inventory.put("cereal", 44);
inventory.put("tuna fish", 23);
inventory.put("coffee", 33);
inventory.put("pasta", 23);
inventory.put("strawberries", 42);
inventory.put("apples", 35);
inventory.put("sugar", 44);
inventory.put("ketchup", 35);
/**
* Creating a map of items and their quantities for the cart of customer
* 1, adding each items along with its initial quantity
*/
HashMap<String, Integer> customer1Cart = new HashMap<String, Integer>();
customer1Cart.put("eggs", 10);
customer1Cart.put("orange juice", 1);
customer1Cart.put("yogurt", 3);
customer1Cart.put("bread", 2);
customer1Cart.put("butter", 2);
/**
* Creating a map of items and their quantities for the cart of customer
* 2, adding each items along with its initial quantity
*/
HashMap<String, Integer> customer2Cart = new HashMap<String, Integer>();
customer2Cart.put("relish", 1);
customer2Cart.put("frozen dinner", 2);
customer2Cart.put("cereal", 2);
customer2Cart.put("tuna fish", 1);
/**
* creating an array list of orders (combination of carts of different
* customers)
*/
ArrayList<HashMap<String, Integer>> orders = new ArrayList<HashMap<String, Integer>>();
System.out
.println("Total value in the cart before processing all orders (Empty carts)");
/**
* Displaying the initial value of the inventory (order list is empty)
*/
System.out.println(totalCostOfRemainingInventory(sellingPrices,
inventory, orders));
orders.add(customer1Cart);
orders.add(customer2Cart);
System.out
.println("Total value in the cart after processing all orders");
/**
* Displaying the current value of the inventory (after processing two
* carts)
*/
System.out.println(totalCostOfRemainingInventory(sellingPrices,
inventory, orders));
}
}
/*OUTPUT*/
Total value in the cart before processing all orders (Empty carts)
1562.05
Total value in the cart after processing all orders
1512.44