java - Calculating total price -
i have abstract class transaction, calculate total price of each transaction. total price calculated getting price of each product in map , multiply price quantity of each product. don't know how multiply these prices quantities values in map. can me, please? tried , nothing works.
public abstract class transaction { //attributes ... //links map<product,integer> products; //constructor transaction() { id = newtrid.incrementandget(); date = new date(); products = new hashmap<>(); } abstract void addproduct(product aproduct, int aquantity); bigdecimal calculatetotal() { bigdecimal total = new bigdecimal(0); for(product eachproduct : products.keyset()) { total.add(eachproduct.getprice()); } (integer eachproduct : products.values()) { } return total; } }
bigdecimal immutable , adddoes not change object called. need reassign result of add:
bigdecimal calculatetotal() { bigdecimal total = new bigdecimal(0); (map.entry<product, integer> entry : products.entryset()) { total = total.add(bigdecimal.valueof(entry.getkey().getprice() * entry.getvalue())); } return total; }
Comments
Post a Comment