-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCart.java
More file actions
30 lines (25 loc) · 1.01 KB
/
Copy pathCart.java
File metadata and controls
30 lines (25 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.HashMap;
import java.util.Map;
public class Cart {
private Map<Product, Integer> cartItems = new HashMap<>();
public void addProduct(Product product, int quantity) {
if (product.getProductStockQuantity() >= quantity) {
cartItems.put(product, cartItems.getOrDefault(product, 0) + quantity);
} else {
System.out.println("There are not enough stock to add " + product.getProductName() + " to the cart");
}
}
public void displayCart() {
System.out.println("Products: :");
for (Map.Entry<Product, Integer> entry : cartItems.entrySet()) {
System.out.println(entry.getKey().getProductName() + " - Amount: " + entry.getValue());
}
}
public double calculateTotalPrice() {
double total = 0.0;
for (Map.Entry<Product, Integer> entry : cartItems.entrySet()) {
total += entry.getKey().getProductPrice() * entry.getValue();
}
return total;
}
}