diff --git a/src/main/java/com/example/shoppingcart/Product.java b/src/main/java/com/example/shoppingcart/Product.java new file mode 100644 index 00000000..d697ddc7 --- /dev/null +++ b/src/main/java/com/example/shoppingcart/Product.java @@ -0,0 +1,20 @@ +package com.example.shoppingcart; + +public class Product { + private final String name; + private final double price; + + public Product(String name, double price) { + this.name = name; + this.price = price; + } + + public String getName() { + return name; + } + + public double getPrice() { + return price; + } +} + diff --git a/src/main/java/com/example/shoppingcart/ShoppingCart.java b/src/main/java/com/example/shoppingcart/ShoppingCart.java new file mode 100644 index 00000000..ad5c9a8b --- /dev/null +++ b/src/main/java/com/example/shoppingcart/ShoppingCart.java @@ -0,0 +1,42 @@ +package com.example.shoppingcart; + +import java.util.HashMap; +import java.util.Map; + +public class ShoppingCart { + + private final Map items = new HashMap<>(); + private double discount = 0.0; + + public void addProduct(Product product, int quantity) { + items.merge(product, quantity, Integer::sum); + } + + public void removeProduct(Product product) { + items.remove(product); + } + + public Map getItems() { + return items; + } + + public void applyDiscount(double discount) { + this.discount = discount; + } + + public double calculateTotal() { + double total = 0.0; + for (Map.Entry entry : items.entrySet()) { + total += entry.getKey().getPrice() * entry.getValue(); + } + return total * (1 - discount); // stöd för rabatt + } + public void updateQuantity(Product product, int newQuantity) { + if (newQuantity <= 0) { + items.remove(product); + } else { + items.put(product, newQuantity); + } + } + +} diff --git a/src/test/java/com/example/shoppingcart/ShoppingCartTest.java b/src/test/java/com/example/shoppingcart/ShoppingCartTest.java new file mode 100644 index 00000000..ada50501 --- /dev/null +++ b/src/test/java/com/example/shoppingcart/ShoppingCartTest.java @@ -0,0 +1,92 @@ +package com.example.shoppingcart; + +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.*; + +public class ShoppingCartTest { + + @Test + void shouldAddProductToCart() { + ShoppingCart cart = new ShoppingCart(); + Product product = new Product("Apple", 10.0); + + cart.addProduct(product, 1); + + assertThat(cart.getItems()).containsEntry(product, 1); + } + + @Test + void shouldRemoveProductFromCart() { + ShoppingCart cart = new ShoppingCart(); + Product product = new Product("Banana", 5.0); + + cart.addProduct(product, 2); + cart.removeProduct(product); + + assertThat(cart.getItems()).doesNotContainKey(product); + } + + @Test + void shouldCalculateTotalPrice() { + ShoppingCart cart = new ShoppingCart(); + Product apple = new Product("Apple", 10.0); + Product banana = new Product("Banana", 5.0); + + cart.addProduct(apple, 2); // 2 x 10 = 20 + cart.addProduct(banana, 3); // 3 x 5 = 15 + + double total = cart.calculateTotal(); // FIXAT HÄR + + assertThat(total).isEqualTo(35.0); + } + + @Test + void shouldApplyPercentageDiscountToTotalPrice() { + ShoppingCart cart = new ShoppingCart(); + Product product1 = new Product("Apple", 10.0); + Product product2 = new Product("Banana", 5.0); + + cart.addProduct(product1, 2); // 20 kr + cart.addProduct(product2, 4); // 20 kr + cart.applyDiscount(0.10); // 10% rabatt + + double total = cart.calculateTotal(); + + assertThat(total).isEqualTo(36.0); // 40 - 10% = 36 + } + + @Test + void shouldCalculateTotalPriceWithNoDiscount() { + ShoppingCart cart = new ShoppingCart(); + Product apple = new Product("Apple", 10.0); + Product banana = new Product("Banana", 5.0); + + cart.addProduct(apple, 2); // 20.0 + cart.addProduct(banana, 3); // 15.0 + + double total = cart.calculateTotal(); + + assertThat(total).isEqualTo(35.0); + } + @Test + void shouldUpdateQuantityOfExistingProduct() { + ShoppingCart cart = new ShoppingCart(); + Product apple = new Product("Apple", 10.0); + + cart.addProduct(apple, 2); + cart.updateQuantity(apple, 5); // ändra direkt till 5 + + assertThat(cart.getItems()).containsEntry(apple, 5); + } + @Test + void shouldRemoveProductWhenQuantityIsZero() { + ShoppingCart cart = new ShoppingCart(); + Product apple = new Product("Apple", 10.0); + cart.addProduct(apple, 2); + + cart.updateQuantity(apple, 0); + + assertThat(cart.getItems()).doesNotContainKey(apple); + } + +}