Completing the Shopping Cart with TDD in Kotlin

Completing the Shopping Cart

Welcome to the fifth and final unit of this course, focused on practicing the principles of Test Driven Development (TDD) using Kotlin and JUnit. We're going to continue building our ShoppingCart system by integrating more sophisticated 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.

Remember to apply the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. Ask questions if you need any clarification!

Final Requirements for ShoppingCart Class

The following requirements introduce more sophisticated capabilities for the ShoppingCart class, focusing on robust item management, discount application, and quantity handling.

These features will challenge you to apply TDD principles effectively and enhance the system's functionality.

11. Quantity Limit for Single Item

  • Description: The cart should enforce a maximum quantity limit of 10 for a single type of item, preventing more than this allowed amount from being added.

  • Details

    • Utilize the addItem(item: Item, quantity: Int) method to add items to the cart.
    • Ensure an exception is raised when adding a quantity that exceeds the limit of 10 for a single item.
    • The exception's message should clearly state "Maximum quantity exceeded" when the limit is breached.
    class InvalidCartOperationException(message: String) : Exception(message)
  • Examples: Attempting to add 11 units of an item should throw a custom InvalidCartOperationException with the message "Maximum quantity exceeded".

12. Retrieving Item Details by ID

  • Description: When an item is added to the cart, it should be possible to retrieve that item's details using its ID, including the product information and the quantity in the cart.

  • Details

    • Enable items to be added using the addItem() method with specific IDs.
    • Ensure that getItem(id: String): ItemDetails? returns the correct item details, including the quantity, after being added to the cart.
  • Examples: Adding a "Book" with ID "1" priced at 10 and retrieving by ID 1 should return { Id: '1', Name: 'Book', Price: 10, Quantity: 1 }.

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