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.
- 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()
returns0
for an empty cart. - Verify the method
GetTotal()
returns0
for the initial state.
- Initialize a new shopping cart using the
- Examples: A newly created cart should have an item count of
0
and a total price of0
.
- 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.
- Add an item to the cart using the
- Examples: Adding a Product
{ Id = "1", Name = "Book", Price = 10 }
should result in an item count of1
and a total of10
.
- 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.
- Utilize the
- 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 of2
and a total price of15
.
- 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.
- Implement adding items with a specified quantity using the
- Examples: Adding adding a
Book
with a price of10
with a quantity of3
should result in an item count of3
and a total price of30
.
- 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.
- Implement a
- Examples: Adding an Book with a price of
10
and a quantity of2
and then removing one should result in a count of1
and a total of10
.
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!