Add setting of product quantity, add product id
parent
6960857aee
commit
83d0262e73
|
|
@ -2,12 +2,14 @@ package org.example;
|
|||
|
||||
public class Product {
|
||||
|
||||
int id;
|
||||
String name;
|
||||
float weight;
|
||||
float price;
|
||||
float vatPortion;
|
||||
|
||||
public Product(String name, float weight, float price, float vatPortion) {
|
||||
public Product(int id, String name, float weight, float price, float vatPortion) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.weight = weight;
|
||||
this.price = price;
|
||||
|
|
|
|||
|
|
@ -19,6 +19,19 @@ public class Shop {
|
|||
cart.removeProduct(product);
|
||||
}
|
||||
|
||||
public void setProductQuantityInCart(Product product, int quantity) {
|
||||
cart.setProductQuantity(product, quantity);
|
||||
}
|
||||
|
||||
public Product getProductById(int id) {
|
||||
for (Product product : products) {
|
||||
if (product.id == id) {
|
||||
return product;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Order checkout() {
|
||||
Order order = cart.toOrder();
|
||||
cart.clearProducts();
|
||||
|
|
|
|||
|
|
@ -10,18 +10,26 @@ public class ShoppingCart {
|
|||
|
||||
public void addProduct(Product product) {
|
||||
int currentQuantity = products.getOrDefault(product, 0);
|
||||
products.put(product, currentQuantity + 1);
|
||||
setProductQuantity(product, currentQuantity + 1);
|
||||
}
|
||||
|
||||
public void removeProduct(Product product) {
|
||||
int currentQuantity = products.getOrDefault(product, 0);
|
||||
if (currentQuantity > 1) {
|
||||
products.put(product, currentQuantity - 1);
|
||||
setProductQuantity(product, currentQuantity == 0 ? 0 : currentQuantity - 1);
|
||||
}
|
||||
|
||||
public void setProductQuantity(Product product, int quantity) {
|
||||
if (quantity > 0) {
|
||||
products.put(product, quantity);
|
||||
} else {
|
||||
products.remove(product);
|
||||
}
|
||||
}
|
||||
|
||||
public int getProductQuantity(Product product) {
|
||||
return products.getOrDefault(product, 0);
|
||||
}
|
||||
|
||||
public void clearProducts() {
|
||||
products.clear();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue