diff --git a/pom.xml b/pom.xml index 543c1aa50..7d8ef717a 100644 --- a/pom.xml +++ b/pom.xml @@ -16,19 +16,19 @@ org.junit.jupiter junit-jupiter - 5.11.4 + 6.0.1 test org.assertj assertj-core - 3.27.3 + 3.27.6 test org.mockito mockito-junit-jupiter - 5.15.2 + 5.21.0 test diff --git a/src/main/java/com/example/shop/Item.java b/src/main/java/com/example/shop/Item.java new file mode 100644 index 000000000..af5315108 --- /dev/null +++ b/src/main/java/com/example/shop/Item.java @@ -0,0 +1,37 @@ +package com.example.shop; + +public class Item { + private String name; + private double price; + private int quantity; + + Item(String name, double price, int quantity) { + this.name = name; + this.price = price; + this.quantity = quantity; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } +} diff --git a/src/main/java/com/example/shop/ShoppingCart.java b/src/main/java/com/example/shop/ShoppingCart.java new file mode 100644 index 000000000..e5c96e928 --- /dev/null +++ b/src/main/java/com/example/shop/ShoppingCart.java @@ -0,0 +1,46 @@ +package com.example.shop; + +import java.util.ArrayList; +import java.util.List; + +public class ShoppingCart { + private List items = new ArrayList<>(); + private double discount = 0.0; + + public boolean addItem(String name, double price, int quantity) { + if (name == null || price < 0 || quantity <=0) { + return false; + } + items.add(new Item(name, price, quantity)); + return true; + } + + public boolean removeItem(String name) { + return items.removeIf(item -> item.getName().equals(name)); + } + public double getTotalPrice() { + double totalPrice = 0.0; + for(Item item : items) { + totalPrice += item.getPrice() * item.getQuantity(); + } + return totalPrice * (1 - discount); + } + public boolean updateQuantity(String name, int quantity) { + boolean changedQty = false; + for(Item item : items) { + if(item.getName().equals(name)) { + changedQty = true; + item.setQuantity(quantity); + } + } + return changedQty; + } + public boolean applyDiscount(double discount) { + if (discount < 0 || discount > 1) { + return false; + } + this.discount = discount; + return true; + } + +} diff --git a/src/test/java/com/example/shop/ShoppingCartTest.java b/src/test/java/com/example/shop/ShoppingCartTest.java new file mode 100644 index 000000000..3047c232b --- /dev/null +++ b/src/test/java/com/example/shop/ShoppingCartTest.java @@ -0,0 +1,108 @@ +package com.example.shop; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.*; + +class ShoppingCartTest { + @Test + void addItem_returnsTrue_whenItemIsAdded() { + ShoppingCart cart = new ShoppingCart(); + + boolean result = cart.addItem("T-Shirt", 129.0, 1); + + assertTrue(result); + } + + @Test + void removeItem_returnsTrue_whenItemExists() { + ShoppingCart cart = new ShoppingCart(); + cart.addItem("T-Shirt", 100.0, 1); + + boolean result = cart.removeItem("T-Shirt"); + + assertTrue(result); + } + @Test + void getTotalPrice_returnsSumOfPrices() { + ShoppingCart cart = new ShoppingCart(); + + cart.addItem("T-Shirt", 100.0, 2); + cart.addItem("Jeans", 300, 1); + + assertEquals(500, cart.getTotalPrice()); + } + @Test + void updateQuantity_returnsTrue_andUpdatesTotalPrice() { + ShoppingCart cart = new ShoppingCart(); + cart.addItem("T-Shirt", 100, 1); + + boolean result = cart.updateQuantity("T-Shirt", 3); + assertTrue(result); + assertEquals(300.0, cart.getTotalPrice()); + } + @Test + void applyDiscount_returnsTrue_andReducesTotal() { + ShoppingCart cart = new ShoppingCart(); + cart.addItem("T-Shirt", 100.0, 1); + boolean result = cart.applyDiscount(0.3); + assertTrue(result); + assertEquals(70.0, cart.getTotalPrice()); + } + @Test + void removeItemThatDoesNotExist_returnsFalse() { + ShoppingCart cart = new ShoppingCart(); + + Assertions.assertFalse(cart.removeItem("Hat")); + } + @Test + void removeItemWhenNameIsNull_returnsFalse() { + ShoppingCart cart = new ShoppingCart(); + Assertions.assertFalse(cart.removeItem(null)); + } + @ParameterizedTest + @CsvSource({ + "-1.0, 1", + "10.0, -1", + "10.0, 0", + "-10.0, -1" + }) + void addItemWithNegativePriceOrQty_returnsFalse(double price, int quantity) { + ShoppingCart cart = new ShoppingCart(); + Assertions.assertFalse(cart.addItem("T-Shirt", price, quantity)); + } + @Test + void addItemWhereNameIsNull_returnsFalse() { + ShoppingCart cart = new ShoppingCart(); + Assertions.assertFalse(cart.addItem(null, 100,1)); + } + + @Test + void updateQuantityWhereNameIsNull_returnsFalse() { + ShoppingCart cart = new ShoppingCart(); + Assertions.assertFalse(cart.updateQuantity(null, 1)); + } + @ParameterizedTest + @CsvSource({ + "-1", + "0" + }) + void updateQuantityWhereQtyIsNull_returnsFalse(int quantity) { + ShoppingCart cart = new ShoppingCart(); + Assertions.assertFalse(cart.updateQuantity("T-Shirt", quantity )); + } + + @ParameterizedTest + @CsvSource({ + "-0.1", + "1.1" + }) + void applyDiscountWhereDiscountIsNotAllowed_returnsFalse(double discount) { + ShoppingCart cart = new ShoppingCart(); + Assertions.assertFalse(cart.applyDiscount(discount)); + } +} \ No newline at end of file