ashishps1 / awesome-low-level-design

Learn Low Level Design (LLD) and prepare for interviews using free resources.
https://blog.algomaster.io
GNU General Public License v3.0
8.76k stars 2.27k forks source link

Updating the ingredients for coffee vending machine #17

Open naman-ajmera opened 5 months ago

naman-ajmera commented 5 months ago

private void updateIngredients(Coffee coffee) { for (Map.Entry<Ingredient, Integer> entry : coffee.getRecipe().entrySet()) { Ingredient ingredient = entry.getKey(); int requiredQuantity = entry.getValue(); ingredient.updateQuantity(-requiredQuantity); if (ingredient.getQuantity() < 3) { System.out.println("Low inventory alert: " + ingredient.getName()); } } }

The UpdateIngredients method is updating the ingredients used by that coffee, shouldn't it update the ingredients map and its quantity after dispensing coffee.

lokeshsk1 commented 3 months ago

No, the code is correct, Let me explain

private void updateIngredients(Coffee coffee) {
        for(Map.Entry<Ingredient, Integer> ingredientMap : coffee.getRecipe().entrySet()){
            Ingredient ingredient = ingredientMap.getKey();
            int quantity = ingredientMap.getValue(); //this is the quantity of the ingredient in the coffee's recipe
            ingredient.updateQuantity(-quantity); //here, we update the overall quantity of the ingredient in the vending machine
        }
    }

For example, let's assume espresso, Recipe contains Ingredient and quantity here, the Ingredient has a quantity, which is the overall quantity and the quantity is the quantity in of the ingredient in espresso

dhanushkumar-amk commented 1 week ago

private void updateIngredients(Coffee coffee) { coffee.getRecipe().forEach((ingredient, requiredQuantity) -> { ingredient.updateQuantity(-requiredQuantity);

    if (ingredient.getQuantity() < 3) {
        System.out.println("Low inventory alert: " + ingredient.getName());
    }
});

} pls check this