Introduction to the Shopping Cart Module

Welcome to your fifth and final unit for this course, dedicated to practicing Test-Driven Development (TDD) utilizing Swift and XCTest. We're going to finish building our ShoppingCart system by adding even more features to our class.

In this course, 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. Previously, I wrote the tests for you; 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.

Final Requirements for `ShoppingCart` Class
11. Quantity Limit for a Single Product
  • Description: The cart should enforce a maximum quantity limit of 10 for a single type of product, preventing more than the allowed amount from being added.
  • Details
    • Utilize the addItem(item: Product, quantity: Int) method to add products to the cart.
    • Ensure an exception is raised when adding a quantity that exceeds a limit of 10 for a single product.
    • The exception message should clearly state, "Maximum quantity exceeded" when the limit is breached.
  • Examples: Attempting to add 11 units of Product(id: "1", name: "Book", price: 10) should raise an exception indicating "Maximum quantity exceeded."
12. Retrieving Product Details by ID
  • Description: When a product is added to the cart, it should be possible to retrieve the product details using its ID, which includes the product information and its quantity in the cart.
  • Details
    • Enable products to be added using an addItem() method with specific IDs.
    • Ensure getItem(id: String) returns the correct product details, including the quantity after being added to the cart.
  • Examples: Adding a product with Product(id: "1", name: "Book", price: 10) and retrieving it by ID "1" should return Product(id: "1", name: "Book", price: 10, quantity: 1).
13. Applying Discount Codes
  • Description: Applying a valid discount code should reduce the total price of products in the cart by the specified discount percentage.
  • Details
    • Use the applyDiscountCode(code: String) method to apply a discount.
    • Support valid discount codes like "HOLIDAY25" for a 25% discount.
    • Update getTotal() to reflect the discounted price.
  • Examples: Applying the discount code "HOLIDAY25" to a product Product(id: "1", name: "Book", price: 100) should reduce the total to 75.
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