Welcome to the fourth unit of our course, focusing on enhancing your Test Driven Development (TDD) skills with Kotlin and JUnit. We will be expanding our ShoppingCart
system by adding additional features.
This hands-on lesson 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 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 exception indicating that the item was not found.
-
Details:
- Implement the removal through a
removeItem(id: Int)
method. - Ensure the method throws an exception with an appropriate message if the item is not found in the cart.
- Implement the removal through a
-
Examples: Attempting to remove an item with
Id: 999
should throw an exception with the message"Item not found"
.
Kotlin1class ShoppingCart { 2 private val items = mutableMapOf<Int, Int>() 3 4 fun removeItem(id: Int) { 5 if (!items.containsKey(id)) { 6 throw Exception("Item not found") 7 } 8 items.remove(id) 9 } 10}
-
Description: Applying a percentage discount should adjust the total price of the items in the cart accordingly.
-
Details:
- Use the
applyDiscount(percentage: Double)
method to apply a percentage discount to the total. - Ensure
getTotal()
returns the adjusted price after applying the discount.
- Use the
-
Examples: Applying a 10% discount to a total price of
100
should result in a new total of90
.
Kotlin1class ShoppingCart { 2 private var total: Double = 0.0 3 4 fun applyDiscount(percentage: Double) { 5 total -= total * (percentage / 100) 6 } 7 8 fun getTotal(): Double { 9 return total 10 } 11}
-
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 should be automatically applied.
- The
getTotal()
method should return the discounted total when the discount is applicable.
-
Examples: Adding a
"Book"
with a price of200
should result in a total of180
after applying the bulk discount.
Kotlin1class ShoppingCart { 2 private var total: Double = 0.0 3 4 private fun applyBulkDiscount() { 5 if (total > 150) { 6 total -= total * 0.10 7 } 8 } 9 10 fun getTotal(): Double { 11 applyBulkDiscount() 12 return total 13 } 14}
-
Description: The cart should be able to remove all items, resetting both the item count and total price to zero.
-
Details:
- Implement a
clear()
method to remove all items from the cart. - Ensure
getItemCount()
returns0
after clearing. - Verify
getTotal()
is0
after clearing.
- Implement a
-
Examples: If there are multiple items in the cart, calling
clear
should leave the count as0
and the total as0
.
Kotlin1class ShoppingCart { 2 private val items = mutableMapOf<Int, Int>() 3 private var total: Double = 0.0 4 5 fun clear() { 6 items.clear() 7 total = 0.0 8 } 9 10 fun getItemCount(): Int { 11 return items.size 12 } 13 14 fun getTotal(): Double { 15 return total 16 } 17}
-
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: Int, quantity: Int)
method. - Ensure
getItemCount()
returns the correct total item count after the quantity is updated. - Ensure
getTotal()
returns the correct total price after the quantity is updated.
- Allow item quantities to be updated using an
-
Examples: Updating the quantity of a
"Book"
with a price of10
from2
to3
should result in a count of3
and a total of30
.
Kotlin1class ShoppingCart { 2 private val items = mutableMapOf<Int, Int>() 3 private val itemPrices = mutableMapOf<Int, Double>() 4 5 fun updateQuantity(id: Int, quantity: Int) { 6 if (items.containsKey(id)) { 7 items[id] = quantity 8 } 9 } 10 11 fun getItemCount(): Int { 12 return items.values.sum() 13 } 14 15 fun getTotal(): Double { 16 return items.entries.sumByDouble { (id, quantity) -> itemPrices[id]!! * quantity } 17 } 18}
In this unit, you explored designing test cases for an enhanced ShoppingCart
class, focusing on advanced features such as removing 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!
