简体   繁体   中英

Big Decimal in Java

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

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM