maahi
maahi

Reputation: 5

How to calculate the inventory amount at the end of the day in java

The first parameter is a HashMap<String, Double> representing the selling price of each item in our store . 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.This method should compute and return 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.

Can someone help me out with this problem? Thanks!

So far I have this

import java.util.ArrayList;
import java.util.HashMap;

public class thi {  

public static double totalCostOfInventory(HashMap<String, Double> sellingPrice, HashMap<String, Integer> inventory, ArrayList<HashMap<String, Integer>> 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 key = temp.get(j);
         }
    }
    return ans;
  }
}

Upvotes: 1

Views: 1314

Answers (4)

Kirill Simonov
Kirill Simonov

Reputation: 8481

Using Java 8 it is possible to solve it like this (see comments for some explanation):

public static double totalCostOfInventory(HashMap<String, Double> sellingPrice,
                                          HashMap<String, Integer> inventory,
                                          ArrayList<HashMap<String, Integer>> orders) {

    //computing the total cost of inventory at the start of the day
    double totalStart = totalPrice(inventory, sellingPrice);

    //for each cart we calculate the total price and sum all prices
    double totalSpent = orders.stream()
            .map(map -> totalPrice(map, sellingPrice))
            .reduce(Double::sum).orElse(0d);

    return totalStart - totalSpent;
}

//this method takes a map of prices and a map of quantities and calculates the total price
private static double totalPrice(Map<String, Integer> inventory, HashMap<String, Double> prices) {
    return inventory.entrySet()
            //creating a stream of map entries <String, Integer>
            .stream()
            .reduce(0d,
                    //for each entry we take the item's price, multiply it by quantity
                    //and add it to the total result
                    (acc, entry) -> acc + prices.get(entry.getKey()) * entry.getValue(),
                    Double::sum);
}

Upvotes: 0

Johannes Kuhn
Johannes Kuhn

Reputation: 15163

"Single-Line" Version using Java Streams:

public static double totalCostOfInventory(HashMap<String, Double> sellingPrice,
        HashMap<String, Integer> inventory,
        ArrayList<HashMap<String, Integer>> orders) {
    return orders.stream().flatMap(k -> k.entrySet().stream())
            .collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.summingInt(Map.Entry::getValue)))
            .entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, e -> (inventory.get(e.getKey()) - e.getValue()) * sellingPrice.get(e.getKey())))
            .values().stream().mapToDouble(Double::doubleValue)
            .sum();
}

Yeah, it gets very nasty when you do that, but you can do it with streams.

Upvotes: 0

Arseniy
Arseniy

Reputation: 19

This seems as though it's a homework assignment, and stack overflow is not here to solve homework, it's to get answers to problems that you are having in a certain chunk of code. That said everyone made these types of mistakes when they were new(at least i did) so i'll explain the solution without giving you the code.

Programming is about breaking down problems, so let's break down what we have to do:

Step 1. Iterate over the orders arraylist, then we do inventory.get([currentOrder].getKey()) from which we remove the amount of items that were purchased

Step 2. After that iterate over the inventory hashmap and simply add the amount of goods remaining * the price of the given good to the total.

And that's all there is to it. A rather simple program, if you can get this working with the loop method i would recommend looking into doing this with streams as well to increase your data structure manipulation knowledge.

Upvotes: 1

Selvaram G
Selvaram G

Reputation: 748

Try this

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Inventory {

    public static double totalCostOfInventory(HashMap<String, Double> sellingPrice,
                                              HashMap<String, Integer> inventory,
                                              ArrayList<HashMap<String, Integer>> orders) {

        double ans = 0;

        for (HashMap<String, Integer> order : orders) {

            // Removing the purchased items from inventory
            for (Map.Entry<String, Integer> itemToQuantity : order.entrySet()) {
                String item = itemToQuantity.getKey();
                Integer quantityPurchased = itemToQuantity.getValue();
                inventory.put(item, inventory.get(item) - quantityPurchased);
            }
        }

        // Computing cost of inventory by iterating over remaining items
        for (Map.Entry<String, Integer> itemInInventory : inventory.entrySet()) {
            String itemName = itemInInventory.getKey();
            Integer remainingQuantity = itemInInventory.getValue();

            ans = ans + (remainingQuantity * sellingPrice.get(itemName));
        }

        return ans;
    }
}

Upvotes: 0

Related Questions