Lesson 3
Building a Shopping Cart with Many Requirements through TDD
Introduction to the Shopping Cart Module

Welcome to your third unit of this course dedicated to practicing Test Driven Development (TDD) utilizing C# and xUnit. We will begin building a new system; this time, we’ll focus on creating a shopping cart module with a variety of 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. Unlike previous exercises, where tests were supplied, this time you're in charge!

Remember to utilize the core concepts of the Red-Green-Refactor cycle while working through these exercises. Assistance is available, so don't hesitate to ask for help.

Requirements for `ShoppingCart` Class
1. Starting with an Empty Cart
  • Description: When a new ShoppingCart is created in C#, it should start without any items, and the total price should be zero.
  • Details
    • Initialize a new shopping cart using the ShoppingCart constructor.
    • Ensure the method GetItemCount() returns 0 for an empty cart.
    • Verify the method GetTotal() returns 0 for the initial state.
  • Examples: A newly created cart should have an item count of 0 and a total price of 0.
2. Adding a Single Item
  • Description: Verify that adding a single item to the cart increases the item count and adjusts the total price to include the price of the added item.
  • Details
    • Add an item to the cart using the AddItem(Item item) method.
    • Confirm that GetItemCount() reflects the change in the number of items.
    • Ensure GetTotal() accurately accounts for the total price of items in the cart.
  • Examples: Adding a Product { Id = "1", Name = "Book", Price = 10 } should result in an item count of 1 and a total of 10.
3. Adding Multiple Items
  • Description: Adding multiple distinct items to the shopping cart should update the item count to reflect the total number of unique items, and the total price should equal the sum of the prices of all items added.
  • Details
    • Utilize the AddItem() method to insert different items into the shopping cart.
    • Confirm that GetItemCount() correctly represents the total number of unique items added to the cart.
    • Verify that GetTotal() accurately calculates the sum of the prices for all individual items added.
  • Examples: Adding an item with adding { Id = "1", Name = "Book", Price = 10 } and { Id = "2", Name = "Pen", Price = 5 } should lead to an item count of 2 and a total price of 15.
4. Handling Multiple Quantities of the Same Item
  • Description: Adding multiple quantities of the same item should adjust the item count to reflect the total quantity and set the total price to be the product of the item's price and the quantity.
  • Details
    • Implement adding items with a specified quantity using the AddItem(Item item, int quantity) method.
    • Ensure that GetItemCount() shows the sum of all item quantities.
    • Confirm that GetTotal() accurately calculates the total price by multiplying the unit price by the total quantity.
  • Examples: Adding adding a Book with a price of 10 with a quantity of 3 should result in an item count of 3 and a total price of 30.
5. Removing an Item
  • Description: Removing an item from the cart should result in a decreased item count and updated total price.
  • Details
    • Implement a RemoveItem(string id) method to remove items from the cart.
    • Ensure that GetItemCount() correctly reflects the number of items after removal.
    • Modify GetTotal() to return the updated total price after the item is removed.
  • Examples: Adding an Book with a price of 10 and a quantity of 2 and then removing one should result in a count of 1 and a total of 10.
Summary and Preparation for Practice Exercises

Looking ahead, you'll engage in practice sessions where you'll write tests and ensure their successful execution while employing the Red-Green-Refactor cycle. Your implementations may differ from expected solutions, and that's perfectly fine. Each session starts from a basic foundation, offering you the opportunity to compare approaches and refine your skills in ensuring test success.

As you progress through these exercises, remain focused on the Red-Green-Refactor principles. Begin by writing tests and execute only those implementation steps that the tests request.

Red! Green! Refactor!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.