Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the ingredients for coffee vending machine #17

Open
naman-ajmera opened this issue Jun 23, 2024 · 2 comments
Open

Updating the ingredients for coffee vending machine #17

naman-ajmera opened this issue Jun 23, 2024 · 2 comments

Comments

@naman-ajmera
Copy link

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
Copy link
Contributor

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants