Add ShoppingCard
parent
6bc783877c
commit
85eabab106
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue