From 0fd8d966d9cfb1d247832ab290a669a04a7daf2b Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Wed, 28 Jan 2026 00:11:12 +0100 Subject: [PATCH 01/16] updated version of junit, assertj, mockito to latest version --- pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 From 826e7a4f1c7ca0be6dc7441908796bbd035d1da5 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 21:09:12 +0100 Subject: [PATCH 02/16] Initial commit, added ShoppingCart + ShoppingCartTest --- src/main/java/com/example/shop/ShoppingCart.java | 4 ++++ src/test/java/com/example/shop/ShoppingCartTest.java | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 src/main/java/com/example/shop/ShoppingCart.java create mode 100644 src/test/java/com/example/shop/ShoppingCartTest.java 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..9a822158f --- /dev/null +++ b/src/main/java/com/example/shop/ShoppingCart.java @@ -0,0 +1,4 @@ +package com.example.shop; + +public class ShoppingCart { +} 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..c3abc92ec --- /dev/null +++ b/src/test/java/com/example/shop/ShoppingCartTest.java @@ -0,0 +1,7 @@ +package com.example.shop; + +import static org.junit.jupiter.api.Assertions.*; + +class ShoppingCartTest { + +} \ No newline at end of file From 10719092bf288e3a64710b8246d4ec1c03430dcd Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 21:11:05 +0100 Subject: [PATCH 03/16] added test: addItem returns true when item is added --- src/test/java/com/example/shop/ShoppingCartTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/com/example/shop/ShoppingCartTest.java b/src/test/java/com/example/shop/ShoppingCartTest.java index c3abc92ec..0e5cb9bfe 100644 --- a/src/test/java/com/example/shop/ShoppingCartTest.java +++ b/src/test/java/com/example/shop/ShoppingCartTest.java @@ -1,7 +1,17 @@ package com.example.shop; +import org.junit.jupiter.api.Test; + 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); + } } \ No newline at end of file From f57303ac96e0f869f3c819c59c64ce68540cc504 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 21:16:47 +0100 Subject: [PATCH 04/16] added method addItem in ShoppingCart that returns true --- src/main/java/com/example/shop/ShoppingCart.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/com/example/shop/ShoppingCart.java b/src/main/java/com/example/shop/ShoppingCart.java index 9a822158f..c9d6f82ec 100644 --- a/src/main/java/com/example/shop/ShoppingCart.java +++ b/src/main/java/com/example/shop/ShoppingCart.java @@ -1,4 +1,14 @@ package com.example.shop; +import java.util.ArrayList; +import java.util.List; + public class ShoppingCart { + private List items = new ArrayList<>(); + + public boolean addItem(String name, double price, int quantity) { + items.add(name); + return true; + } + } From 94c79412d6f7d7b57e3055b17c053db405ff1214 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 21:19:52 +0100 Subject: [PATCH 05/16] test case: removeItem returns true when item exists --- src/test/java/com/example/shop/ShoppingCartTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/com/example/shop/ShoppingCartTest.java b/src/test/java/com/example/shop/ShoppingCartTest.java index 0e5cb9bfe..180607eaf 100644 --- a/src/test/java/com/example/shop/ShoppingCartTest.java +++ b/src/test/java/com/example/shop/ShoppingCartTest.java @@ -14,4 +14,14 @@ void addItem_returnsTrue_whenItemIsAdded() { assertTrue(result); } + @Test + void removeItem_returnsTrue_whenItemExists() { + ShoppingCart cart = new ShoppingCart(); + cart.addItem("T-Shirt", 129.0, 1); + + boolean result = cart.removeItem("T-Shirt"); + + assertTrue(result); + } + } \ No newline at end of file From af3fd0cfeea15faf75cf89c093270070a330984e Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 21:22:00 +0100 Subject: [PATCH 06/16] added removeItem to ShoppingCart returns boolean --- src/main/java/com/example/shop/ShoppingCart.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/example/shop/ShoppingCart.java b/src/main/java/com/example/shop/ShoppingCart.java index c9d6f82ec..e19f7b111 100644 --- a/src/main/java/com/example/shop/ShoppingCart.java +++ b/src/main/java/com/example/shop/ShoppingCart.java @@ -11,4 +11,8 @@ public boolean addItem(String name, double price, int quantity) { return true; } + public boolean removeItem(String name) { + return items.remove(name); + } + } From d4e493fd5d26863bbf1e6a85b90000cd72626b55 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 21:26:34 +0100 Subject: [PATCH 07/16] test case: getTotalPrice returns total sum of items in cart --- src/test/java/com/example/shop/ShoppingCartTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/com/example/shop/ShoppingCartTest.java b/src/test/java/com/example/shop/ShoppingCartTest.java index 180607eaf..f28d6b930 100644 --- a/src/test/java/com/example/shop/ShoppingCartTest.java +++ b/src/test/java/com/example/shop/ShoppingCartTest.java @@ -23,5 +23,14 @@ void removeItem_returnsTrue_whenItemExists() { assertTrue(result); } + @Test + void getTotalPrice_returnsSumOfPrices() { + ShoppingCart cart = new ShoppingCart(); + + cart.addItem("T-Shirt", 129.0, 2); + cart.addItem("Jeans", 299, 1); + + assertEquals(557, cart.getTotalPrice()); + } } \ No newline at end of file From b05e2b12647abbb194fe855c40ba55c7455097c3 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 21:30:43 +0100 Subject: [PATCH 08/16] added getTotalPrice method in ShoppingCart to return sum of all items prices in list --- src/main/java/com/example/shop/ShoppingCart.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/example/shop/ShoppingCart.java b/src/main/java/com/example/shop/ShoppingCart.java index e19f7b111..16c20e68a 100644 --- a/src/main/java/com/example/shop/ShoppingCart.java +++ b/src/main/java/com/example/shop/ShoppingCart.java @@ -5,14 +5,19 @@ public class ShoppingCart { private List items = new ArrayList<>(); + private double totalPrice; public boolean addItem(String name, double price, int quantity) { items.add(name); + totalPrice += price*quantity; return true; } public boolean removeItem(String name) { return items.remove(name); } + public double getTotalPrice() { + return totalPrice; + } } From 9c79ec49b714eb37034b922bca5db958773f64a1 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 21:35:35 +0100 Subject: [PATCH 09/16] test case: updateQuantity returns true and updates totalPrice --- src/test/java/com/example/shop/ShoppingCartTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/com/example/shop/ShoppingCartTest.java b/src/test/java/com/example/shop/ShoppingCartTest.java index f28d6b930..278cd2bba 100644 --- a/src/test/java/com/example/shop/ShoppingCartTest.java +++ b/src/test/java/com/example/shop/ShoppingCartTest.java @@ -32,5 +32,15 @@ void getTotalPrice_returnsSumOfPrices() { assertEquals(557, cart.getTotalPrice()); } + @Test + void updateQuantity_returnsTrue_andUpdatesTotalPrice() { + ShoppingCart cart = new ShoppingCart(); + cart.addItem("T-Shirt", 129, 1); + + boolean result = cart.updateQuantity("T-Shirt", 3); + + assertTrue(result); + assertEquals(387.0, cart.getTotalPrice()); + } } \ No newline at end of file From 8b9fd99e2362001f8116c7feb3ea8bb5d89e7bea Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 21:49:37 +0100 Subject: [PATCH 10/16] added updateQuantity with hardcoded parameters. will refactor at later stage in TDD --- src/main/java/com/example/shop/ShoppingCart.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/com/example/shop/ShoppingCart.java b/src/main/java/com/example/shop/ShoppingCart.java index 16c20e68a..9c6607d1f 100644 --- a/src/main/java/com/example/shop/ShoppingCart.java +++ b/src/main/java/com/example/shop/ShoppingCart.java @@ -6,6 +6,7 @@ public class ShoppingCart { private List items = new ArrayList<>(); private double totalPrice; + private double tshirtPrice = 129.0; public boolean addItem(String name, double price, int quantity) { items.add(name); @@ -19,5 +20,12 @@ public boolean removeItem(String name) { public double getTotalPrice() { return totalPrice; } + public boolean updateQuantity(String name, int quantity) { + if (name.equals("T-Shirt")) { + totalPrice = tshirtPrice * quantity; + return true; + } + return false; + } } From aaa09c4836fe19f74a804e98d20bc8f6c056f135 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 21:58:00 +0100 Subject: [PATCH 11/16] test case: applyDiscount returns true when discounted. expects T-Shirt price to be 70.0 after discount --- src/test/java/com/example/shop/ShoppingCartTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/com/example/shop/ShoppingCartTest.java b/src/test/java/com/example/shop/ShoppingCartTest.java index 278cd2bba..fd2572193 100644 --- a/src/test/java/com/example/shop/ShoppingCartTest.java +++ b/src/test/java/com/example/shop/ShoppingCartTest.java @@ -42,5 +42,13 @@ void updateQuantity_returnsTrue_andUpdatesTotalPrice() { assertTrue(result); assertEquals(387.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()); + } } \ No newline at end of file From 2e8f3bd41eef74052484b82afffa3b26ab851cd2 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 22:10:22 +0100 Subject: [PATCH 12/16] added applyDiscount to ShoppingCart. discount must be between 0-1. updated getTotalPrice to apply discount to totalPrice --- src/main/java/com/example/shop/ShoppingCart.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/shop/ShoppingCart.java b/src/main/java/com/example/shop/ShoppingCart.java index 9c6607d1f..fb62256a7 100644 --- a/src/main/java/com/example/shop/ShoppingCart.java +++ b/src/main/java/com/example/shop/ShoppingCart.java @@ -7,6 +7,7 @@ public class ShoppingCart { private List items = new ArrayList<>(); private double totalPrice; private double tshirtPrice = 129.0; + private double discount = 0.0; public boolean addItem(String name, double price, int quantity) { items.add(name); @@ -18,7 +19,7 @@ public boolean removeItem(String name) { return items.remove(name); } public double getTotalPrice() { - return totalPrice; + return totalPrice * (1 - discount); } public boolean updateQuantity(String name, int quantity) { if (name.equals("T-Shirt")) { @@ -27,5 +28,12 @@ public boolean updateQuantity(String name, int quantity) { } return false; } + public boolean applyDiscount(double discount) { + if (discount < 0 || discount > 1) { + return false; + } + this.discount = discount; + return true; + } } From bc11d1635646292e079f163acb7504fb8a754da1 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 22:14:09 +0100 Subject: [PATCH 13/16] added item class --- src/main/java/com/example/shop/Item.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/com/example/shop/Item.java 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..a7590bc22 --- /dev/null +++ b/src/main/java/com/example/shop/Item.java @@ -0,0 +1,4 @@ +package com.example.shop; + +public class Item { +} From a02ca27b6b91075d953bbca3d3def7ebbbca823c Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 23:15:10 +0100 Subject: [PATCH 14/16] refactored ShoppingCart methods to Items object. changed prices in testcases for easier calculation. --- src/main/java/com/example/shop/Item.java | 33 +++++++++++++++++++ .../java/com/example/shop/ShoppingCart.java | 24 +++++++++----- .../com/example/shop/ShoppingCartTest.java | 13 ++++---- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/example/shop/Item.java b/src/main/java/com/example/shop/Item.java index a7590bc22..af5315108 100644 --- a/src/main/java/com/example/shop/Item.java +++ b/src/main/java/com/example/shop/Item.java @@ -1,4 +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 index fb62256a7..e0fb43678 100644 --- a/src/main/java/com/example/shop/ShoppingCart.java +++ b/src/main/java/com/example/shop/ShoppingCart.java @@ -4,29 +4,35 @@ import java.util.List; public class ShoppingCart { - private List items = new ArrayList<>(); + private List items = new ArrayList<>(); private double totalPrice; - private double tshirtPrice = 129.0; + private double tshirtPrice = 100.0; private double discount = 0.0; public boolean addItem(String name, double price, int quantity) { - items.add(name); - totalPrice += price*quantity; + items.add(new Item(name, price, quantity)); return true; } public boolean removeItem(String name) { - return items.remove(name); + return items.removeIf(item -> item.getName().equals(name)); } public double getTotalPrice() { + totalPrice = 0.0; + for(Item item : items) { + totalPrice += item.getPrice() * item.getQuantity(); + } return totalPrice * (1 - discount); } public boolean updateQuantity(String name, int quantity) { - if (name.equals("T-Shirt")) { - totalPrice = tshirtPrice * quantity; - return true; + boolean changedQty = false; + for(Item item : items) { + if(item.getName().equals(name)) { + changedQty = true; + item.setQuantity(quantity); + } } - return false; + return changedQty; } public boolean applyDiscount(double discount) { if (discount < 0 || discount > 1) { diff --git a/src/test/java/com/example/shop/ShoppingCartTest.java b/src/test/java/com/example/shop/ShoppingCartTest.java index fd2572193..467a200ac 100644 --- a/src/test/java/com/example/shop/ShoppingCartTest.java +++ b/src/test/java/com/example/shop/ShoppingCartTest.java @@ -17,7 +17,7 @@ void addItem_returnsTrue_whenItemIsAdded() { @Test void removeItem_returnsTrue_whenItemExists() { ShoppingCart cart = new ShoppingCart(); - cart.addItem("T-Shirt", 129.0, 1); + cart.addItem("T-Shirt", 100.0, 1); boolean result = cart.removeItem("T-Shirt"); @@ -27,20 +27,19 @@ void removeItem_returnsTrue_whenItemExists() { void getTotalPrice_returnsSumOfPrices() { ShoppingCart cart = new ShoppingCart(); - cart.addItem("T-Shirt", 129.0, 2); - cart.addItem("Jeans", 299, 1); + cart.addItem("T-Shirt", 100.0, 2); + cart.addItem("Jeans", 300, 1); - assertEquals(557, cart.getTotalPrice()); + assertEquals(500, cart.getTotalPrice()); } @Test void updateQuantity_returnsTrue_andUpdatesTotalPrice() { ShoppingCart cart = new ShoppingCart(); - cart.addItem("T-Shirt", 129, 1); + cart.addItem("T-Shirt", 100, 1); boolean result = cart.updateQuantity("T-Shirt", 3); - assertTrue(result); - assertEquals(387.0, cart.getTotalPrice()); + assertEquals(300.0, cart.getTotalPrice()); } @Test void applyDiscount_returnsTrue_andReducesTotal() { From a290bae3ab9260ec6d8ad492c046b7b50b4ffd30 Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 23:17:21 +0100 Subject: [PATCH 15/16] clean up --- src/main/java/com/example/shop/ShoppingCart.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/example/shop/ShoppingCart.java b/src/main/java/com/example/shop/ShoppingCart.java index e0fb43678..2fa2e71a9 100644 --- a/src/main/java/com/example/shop/ShoppingCart.java +++ b/src/main/java/com/example/shop/ShoppingCart.java @@ -5,8 +5,6 @@ public class ShoppingCart { private List items = new ArrayList<>(); - private double totalPrice; - private double tshirtPrice = 100.0; private double discount = 0.0; public boolean addItem(String name, double price, int quantity) { @@ -18,7 +16,7 @@ public boolean removeItem(String name) { return items.removeIf(item -> item.getName().equals(name)); } public double getTotalPrice() { - totalPrice = 0.0; + double totalPrice = 0.0; for(Item item : items) { totalPrice += item.getPrice() * item.getQuantity(); } From 7045e70889ef397ffeffb75f37833a7ce86e110d Mon Sep 17 00:00:00 2001 From: Eric Phu Date: Mon, 9 Feb 2026 23:48:37 +0100 Subject: [PATCH 16/16] added edge cases --- .../java/com/example/shop/ShoppingCart.java | 3 + .../com/example/shop/ShoppingCartTest.java | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/main/java/com/example/shop/ShoppingCart.java b/src/main/java/com/example/shop/ShoppingCart.java index 2fa2e71a9..e5c96e928 100644 --- a/src/main/java/com/example/shop/ShoppingCart.java +++ b/src/main/java/com/example/shop/ShoppingCart.java @@ -8,6 +8,9 @@ public class ShoppingCart { 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; } diff --git a/src/test/java/com/example/shop/ShoppingCartTest.java b/src/test/java/com/example/shop/ShoppingCartTest.java index 467a200ac..3047c232b 100644 --- a/src/test/java/com/example/shop/ShoppingCartTest.java +++ b/src/test/java/com/example/shop/ShoppingCartTest.java @@ -1,6 +1,10 @@ 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.*; @@ -49,5 +53,56 @@ void applyDiscount_returnsTrue_andReducesTotal() { 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