korobushk

ShoppingCart Class

Aug 3rd, 2020 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class ShoppingCart {
  5.  
  6.     private Map<String, Item> cart = new HashMap<>();
  7.  
  8.     public ShoppingCart() {
  9.         this.cart = new HashMap<>();
  10.  
  11.     }
  12.  
  13.     public void add(String product, int price) {
  14.         if(cart.containsKey(product)){
  15.             this.cart.get(product).increaseQuantity();
  16.         }else
  17.        
  18.         this.cart.put(product, new Item(product, 1, price));
  19.  
  20.     }
  21.  
  22.     public int price() {
  23.         int sum = 0;
  24.         for (String getItems : cart.keySet()) {
  25.             sum = cart.get(getItems).price() + sum;
  26.         }
  27.  
  28.         return sum;
  29.     }
  30.  
  31.     public void print() {
  32.         for (String getItemsPrint : cart.keySet()) {
  33.             System.out.println(cart.get(getItemsPrint));
  34.         }
  35.     }
  36.  
  37. }
  38.  
Add Comment
Please, Sign In to add comment