Building a Shopping Cart with Many Requirements through TDD
Introduction to the Shopping Cart Module
Welcome to the third unit of this course dedicated to practicing Test Driven Development (TDD) utilizing Rust. In this module, we will focus on creating a shopping cart with a variety of features. Throughout 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.
1. Starting with an Empty Cart
-
Description: When a new
ShoppingCartis created, it should start without any products, and the total price should be zero. -
Details:
- Define a new struct
ShoppingCartin Rust. - Implement an
implblock with methodsget_item_count()returning an integer andget_total()returning a float for theShoppingCartstruct. - Ensure the function
get_item_count()returns0for an empty cart. - Verify the function
get_total()returns0.0for the initial state.
- Define a new struct
-
Examples:
2. Adding a Single Product
-
Description: Verify that adding a single product to the cart increases the item count and adjusts the total price to include the price of the added product.
-
Details:
- Define a struct
Productand implement theadd_item()method in theimplblock forShoppingCart. - Confirm that
get_item_count()reflects the change in the number of items. - Ensure
get_total()accurately accounts for the total price of products in the cart.
- Define a struct
-
Examples:
3. Adding Multiple Products
-
Description: Adding multiple distinct products to the shopping cart should update the item count to reflect the total number of unique products, and the total price should equal the sum of the prices of all products added.
-
Details:
- Utilize a
Vec<Product>to store products in theShoppingCartand implement the handling of multiple products throughadd_item(). - Confirm that
get_item_count()correctly represents the total number of unique products added to the cart. - Verify that
get_total()accurately calculates the sum of the prices for all individual products added.
- Utilize a
-
Examples:
4. Handling Multiple Quantities of the Same Product
-
Description: Adding multiple quantities of the same product should adjust the item count to reflect the total quantity and set the total price to be the product of the unit price and the quantity.
-
Details:
- Evolve the
add_item()method to handle adding a product with a specified quantity. - Ensure that
get_item_count()shows the sum of all product quantities. - Confirm that
get_total()accurately calculates the total price by multiplying the unit price by the total quantity.
- Evolve the
-
Examples:
5. Removing a Product
-
Description: Removing a product from the cart should result in a decreased item count and an updated total price.
-
Details:
- Implement a
remove_item(id: &str)method to remove products from the cart by their ID. - Ensure that
get_item_count()correctly reflects the number of items after removal. - Modify
get_total()to return the updated total price after the product is removed.
- Implement a
-
Examples:
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!
Make sure to include clear and concise tests for each requirement using Rust’s built-in testing framework to validate that your implementations are functioning as expected.
