Add ShoppingCard

main
CPlaiz 2025-12-13 16:09:48 +01:00
parent 6bc783877c
commit 85eabab106
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package org.example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ShoppingCart {
HashMap<Product, Integer> products = new HashMap<>();
public void addProduct(Product product) {
int currentQuantity = products.getOrDefault(product, 0);
products.put(product, currentQuantity + 1);
}
public void removeProduct(Product product) {
int currentQuantity = products.getOrDefault(product, 0);
if (currentQuantity > 1) {
products.put(product, currentQuantity - 1);
} else {
products.remove(product);
}
}
public void clearProducts() {
products.clear();
}
}