Untitled

mail@pastecode.io avatar
unknown
java
a year ago
613 B
3
Indexable
Never
public static double order_cost(HashMap<Integer, Double> chemicals, ArrayList<Integer> glassware) {
        double totalCost = 0;
        Collections.sort(glassware);
        for (Integer quantity : chemicals.keySet()) {
            double price = chemicals.get(quantity);
            int kolb = Collections.binarySearch(glassware, quantity);
            if (kolb < 0) {
                kolb = -(kolb + 1);
            }
            if (kolb == glassware.size()) {
                kolb--;
            }
            totalCost += glassware.get(kolb) * price;
        }
        return totalCost;
    }