As trainee in Java I need to find how to reduce the value of a BigDecimal variable with the value of a Map, thats also BigDecimal.. OK, for example: I have a restaurant with wareHose where I save my productAvailabilities and a menuMap, where I create my products, SO: when I have an order (Pizza), I have to reduce the available quantity from the wareHouse, with the quantities I need for create my order. Every single Pizza has its map : Map<Product.Type, Quantity> pizza
Asked
Active
Viewed 47 times
0
-
Does this answer your question? [How to update a value, given a key in a hashmap?](https://stackoverflow.com/questions/4157972/how-to-update-a-value-given-a-key-in-a-hashmap) – Chaosfire Aug 31 '22 at 15:31
-
Vague title. Rewrite to summarize your specific technical issue. – Basil Bourque Aug 31 '22 at 15:37
2 Answers
1
If you want to update value of Quantity
, then you can use put
method
pizza.put(key, val);
to add a new key/value pair or overwrite an existing key's value.

StepUp
- 36,391
- 15
- 88
- 148
1
class Recipe:
pizza.put(Product.Type.CHEDDAR, new Quantity(BigDecimal.valueOf(40), Quantity.Unit.G));
pizza.put(Product.Type.ONION, new Quantity(BigDecimal.valueOf(20), Quantity.Unit.G));
pizzaList.add(new Pizza(Pizza.PizzaType.CLASSIC, Pizza.PizzaSize.LARGE, pizza));
class WareHouse :
foodQuantities.put(Product.Type.ONION, new Product(Product.Type.ONION, BigDecimal.valueOf(5)));
foodQuantities.put(Product.Type.CHEDDAR, new Product(Product.Type.CHEDDAR, BigDecimal.valueOf(5)));
class Chef:
//The chef cooks, so he reduce the available quantity from ware house by using the neededIngredients from the Recipe.
avilableProducts.computeIfPresent(Product.Type.TOMATO, (k, v) -> v - 0.100);
avilableProducts.computeIfPresent(Product.Type.CUCUMBER, (k, v) -> v - 0.100);

S.B
- 13,077
- 10
- 22
- 49

Veselin Zahariev
- 29
- 3
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 05 '22 at 09:38