Welcome to the fourth unit of our course, dedicated to practicing Test-Driven Development (TDD) with Scala 3 and ScalaTest. We'll continue expanding our ShoppingCart
system by adding additional features.
This hands-on course emphasizes receiving requirements through tests, one at a time. Your task is to write tests AND implement the code to pass each test, simulating a real-world TDD environment.
Remember to employ the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. I'm here to assist! Just ask if needed.
The following requirements will introduce additional features for the ShoppingCart
class, enabling robust handling of discounts, item management, and overall cart operations.
These enhancements will help solidify your TDD skills while building a more versatile system!
-
Description: Trying to remove an item that is not present in the cart should throw an
ItemNotFoundException
. -
Details
- Implement the removal through a
removeItem(id: String): Unit
method. - Ensure the method throws
ItemNotFoundException
with a message "Item not found" if the item is not in the cart.
- Implement the removal through a
-
Example: Attempting to remove an item with
Id: "999"
should throwItemNotFoundException
.
-
Description: Applying a percentage discount should adjust the total price of the items in the cart accordingly.
-
Details
- Use the
applyDiscount(percentage: Double): Unit
method to apply a percentage discount to the total. - Ensure
getTotal: Int
returns the adjusted price after applying the discount.
- Use the
-
Example: Applying a 10% discount to a total price of
100
should result in a new total of90
.
-
Description: Automatically apply a 10% discount when the total price of items in the cart exceeds $150.
-
Details
- If the total exceeds 150, the 10% discount is automatically applied in the
getTotal: Int
method.
- If the total exceeds 150, the 10% discount is automatically applied in the
-
Example: Adding a
"Book"
with a price of200
should result in a total of180
after applying the bulk discount.
-
Description: The cart should be able to remove all items, resetting both the item count and the total price to zero.
-
Details
- Implement a
clear(): Unit
method to remove all items from the cart. - Ensure
getItemCount: Int
returns0
after clearing. - Verify
getTotal: Int
is0
after clearing.
- Implement a
-
Example: If there are multiple items in the cart, calling
clear()
should leave the count and the total as0
.
-
Description: When the quantity of an item in the cart is changed, both the item count and total price should accurately reflect the new quantity.
-
Details
- Allow item quantities to be updated using an
updateQuantity(id: String, quantity: Int): Unit
method. - Ensure
getItemCount: Int
returns the correct total item count after the quantity is updated. - Ensure
getTotal: Int
returns the correct total price after the quantity is updated.
- Allow item quantities to be updated using an
-
Example: Updating the quantity of a
"Book"
with a price of10
from2
to3
should result in a count of3
and a total of30
.
In this unit, you explored designing test cases for an enhanced ShoppingCart
class, focusing on advanced features such as handling the removal of non-existent items, applying discounts, clearing the cart, and updating item quantities. Now it's your turn to ensure that the described functionality is implemented by writing comprehensive test cases and ensuring that the tests pass with the least amount of code needed.
Engage fully in the Red-Green-Refactor cycle. Practice writing tests first and implement code only when a test requires it.
Red! Green! Refactor!
