Welcome to your fourth unit for this course dedicated to practicing Test-Driven Development (TDD) utilizing Swift and XCTest. We're going to continue building our ShoppingCart system by integrating even more features into our class.
In this course, the emphasis is placed on hands-on practice, where you'll receive requirements through tests, one at a time. Your task is to write tests AND implement code that makes each test pass, simulating a real-world TDD environment. Whereas I wrote the tests for you last time, this time it's all up to you!
Remember to use the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. I'm still here to help! Just ask.
- Description: Removing an item that does not exist in the cart should raise an exception, indicating the item is not found.
- Details
- Support item removal through a
removeItem(itemId: String)method. - Ensure the method raises an appropriate error message if an item does not exist in the cart.
- Support item removal through a
- Examples: Attempting to remove an item with
itemId: "999"from the cart should raise an exception with the message"Item not found".
- Description: When a percentage discount is applied, the total price of the items in the cart should reflect this discount.
- Details
- Use the
applyDiscount(percentage: Double)method to apply a percentage discount to the total. - Ensure
getTotal()returns the discounted price after applying the discount.
- Use the
- Examples: Applying a 10% discount to an item with a total price of
100should result in a new total of90.
- Description: When the total price of items in the cart exceeds $150, a bulk discount of 10% should be applied to the total.
- Details
- If the total exceeds 150, a 10% discount should be applied automatically.
- The
getTotal()method should return the discounted total when applicable.
- Examples: Adding an item
{ "itemId": "1", "name": "Book", "price": 200 }should result in a total of180after applying the bulk discount.
