Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- public class ShoppingCart {
- private Map<String, Item> cart = new HashMap<>();
- public ShoppingCart() {
- this.cart = new HashMap<>();
- }
- public void add(String product, int price) {
- if(cart.containsKey(product)){
- this.cart.get(product).increaseQuantity();
- }else
- this.cart.put(product, new Item(product, 1, price));
- }
- public int price() {
- int sum = 0;
- for (String getItems : cart.keySet()) {
- sum = cart.get(getItems).price() + sum;
- }
- return sum;
- }
- public void print() {
- for (String getItemsPrint : cart.keySet()) {
- System.out.println(cart.get(getItemsPrint));
- }
- }
- }
Add Comment
Please, Sign In to add comment