Completing the Shopping Cart Module with C++ and Google Test

Welcome to the fifth and final unit of this course, focused on practicing the principles of Test Driven Development (TDD) using C++ and Google Test. We're going to continue building our ShoppingCart system by integrating even more sophisticated features into 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. In previous lessons, tests were provided to guide you, but now it's your turn to define them!

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
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, quantity) 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 should be of type std::invalid_argument with the message "Maximum quantity exceeded."
  • Examples: Attempting to add 11 units of an item should throw a std::invalid_argument exception indicating "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) returns the correct item details, including the quantity, after being added to the cart.
  • Examples: Adding a "Book" with ID 1 and price 10 and retrieving by ID 1 should return an object with Id: 1, Name: 'Book', Price: 10, Quantity: 1.
13. Applying Discount Codes
  • Description: Applying a valid discount code should reduce the total price of items in the cart by the specified discount percentage.
  • Details
    • Use the ApplyDiscountCode(code) method to apply a discount.
    • Support valid discount codes stored in a std::map, such as HOLIDAY25 for a 25% discount.
    • Update GetTotal() to reflect the discounted price.
  • Examples: Applying the discount code 'HOLIDAY25' to a product "Book" with a price of 100 should reduce the total to 75.
14. Invalid Discount Code
  • Description: The system should not accept invalid discount codes and should throw an appropriate exception when such a code is applied.
  • Details
    • Ensure ApplyDiscountCode(code) checks against a list of valid codes.
    • Raise an exception of type std::invalid_argument with the message "Invalid discount code" if the code is not valid.
  • Examples: Applying the discount code "INVALID" after adding an item should throw an error indicating the code is invalid.
15. Adding an Existing Item
  • Description: When an item that already exists in the cart is added again, its quantity should increase without creating duplicates, and the total price should reflect the cumulative quantity.
  • Details
    • Allow items to be added again using the AddItem() method without creating duplicates in the cart.
    • Ensure GetItem(id) returns the updated quantity after adding the same item.
    • Update the total price to reflect the cumulative quantity of the added items.
  • Examples: Adding a "Book" priced at 200 twice should result in a quantity of 2 for that item, with the total updated price reflecting the double addition.
16. Adding to an Existing Item Respects Maximum Quantity
  • Description: When adding more units to existing items in the cart, the total quantity should not exceed the predefined maximum limit.
  • Details
    • Use the AddItem(item) method to add units to an existing item.
    • Ensure that adding a quantity which causes the total to exceed the maximum allowed quantity throws an exception of type std::runtime_error.
    • The exception message should be "Maximum quantity exceeded" when the quantity limit is breached.
  • Example: Adding 3 units of an item with ID 1, named Book, priced at 200 to an existing 8 units should throw an error, as it exceeds the limit.
Summary and Preparation for Practice Exercises

In this unit, you reviewed descriptions for more advanced test cases to expand the functionality of the ShoppingCart class using C++ and Google Test. The focus was on utilizing TDD practices for advanced features like handling non-existent items, applying discounts, and updating item quantities. With solidified TDD skills, let's proceed to write targeted test cases that capture essential functionality for a robust ShoppingCart class.

As you undertake these exercises, remember to engage in the Red-Green-Refactor cycle.

Great work! You're almost finished with this practice course and well on your way to mastering TDD techniques. 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