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:
