Introduction to the Shopping Cart Module

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.

More Requirements for `ShoppingCart` Class
6. Removing a Non-Existent Item
  • 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.
  • Examples: Attempting to remove an item with itemId: "999" from the cart should raise an exception with the message "Item not found".
7. Applying Percentage Discount
  • 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.
  • Examples: Applying a 10% discount to an item with a total price of 100 should result in a new total of 90.
8. Applying a Bulk Discount
  • 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 of 180 after applying the bulk discount.
9. Clearing All Items
  • Description: The cart should be able to remove all items, resetting the item count and total price to zero.
  • Details
    • Implement a clear() method to remove all items from the cart.
    • Ensure getItemCount() returns 0 after clearing.
    • Verify getTotal() is 0 after clearing.
  • Examples: If there are multiple items in the cart, calling clear() should leave the count as 0 and the total as 0.
10. Updating Item Quantity
  • Description: When the quantity of an item in the cart is updated, the item count and total price should reflect the updated quantity.
  • Details
    • Allow item quantities to be updated using an updateQuantity(itemId: String, quantity: Int) method.
    • Ensure getItemCount() returns the correct count after updating the quantity.
    • Update getTotal() to return the correct total price after updating the quantity.
  • Examples: Updating the quantity of { "itemId": "1", "name": "Book", "price": 10 } from 2 to 3 should result in a count of 3 and a total of 30.
Summary and Preparation for Practice Exercises

In this unit, you explored the design of test cases for a new ShoppingCart class, focusing on essential features like handling an empty cart, adding items, managing item quantities, and removing items. 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 using Swift and XCTest.

As you undertake these exercises, remember to engage in the Red-Green-Refactor cycle. Be sure to practice writing tests first and do not write implementation code unless the test asks for it.

Red! Green! Refactor!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal