Welcome to your fourth unit for this course, dedicated to practicing Test Driven Development (TDD). We're going to continue building our ShoppingCart system by adding even more features.
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.
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: Attempting to remove an item that is not present in the cart should be handled through exception handling in C++.
- Details
- Implement the removal through a
RemoveItem(int id)method. - Ensure the method uses
throwto generate an exception if the item is not found. - When testing, use Google Test's
EXPECT_THROWmacro to verify that the correct exception is thrown with the expected message. - Use
std::runtime_errorto represent the exception with the message"Item not found".
- Implement the removal through a
- Examples: Attempting to remove an item with
Id: 999should result instd::runtime_errorbeing thrown with the message"Item not found".
- Description: Applying a percentage discount should adjust the total price of the items in the cart accordingly.
- Details
- Use the
ApplyDiscount(double percentage)method to apply a percentage discount to the total. - Ensure
GetTotal()returns the adjusted price after applying the discount.
- Use the
- Examples: Applying a 10% discount to a total price of
100should result in a new total of90.
- Description: Automatically apply a 10% discount when the total price of items in the cart exceeds $150.
- Details
- If the total exceeds 150, the 10% discount should be automatically applied.
- The
GetTotal()method should return the discounted total when the discount is applicable.
- Examples: Adding a
"Book"with a price of200should result in a total of180after applying the bulk discount.
