Finishing the Shopping Cart with TDD and Rust
Completing the Shopping Cart Module with Rust
Welcome to your fifth and final unit for this course, focused on practicing the principles of Test Driven Development (TDD) using Rust and its built-in testing framework. We're going to continue building our ShoppingCart system by integrating more sophisticated features into our module.
In this course, the 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 apply the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. Ask questions if you need any clarification!
11. Quantity Limit for Single Product
- Description: The cart should enforce a maximum quantity limit of 10 for a single type of product, preventing more than this allowed amount from being added.
- Details
- Utilize the
add_item(product: Product, quantity: usize) -> Result<(), String>function to add products to the cart. - Ensure an error is returned when adding a quantity that exceeds the limit of 10 for a single product.
- The error message should clearly state,
'Maximum quantity exceeded'when the limit is breached.
- Utilize the
- Examples: Attempting to add
11units of a product should return an error with the message'Maximum quantity exceeded'.
12. Retrieving Product Details by ID
- Description: When a product is added to the cart, it should be possible to retrieve that cart entry using its ID, including the product information and the quantity in the cart.
- Details
- Enable products to be added using the
add_item()function with specific IDs. - Ensure
get_item(id: &str)returns the correct cart item details, including the quantity, after being added to the cart.
- Enable products to be added using the
- Examples: Adding a
Bookwith ID"1"and price10.0, and retrieving by ID"1", should return a cart item whose product has ID"1", name"Book", price10.0, and quantity1.
13. Applying Discount Codes
- Description: Applying a valid discount code should reduce the total price of products in the cart by the specified discount percentage.
- Details
- Use the
apply_discount_code(code: &str)function to apply a discount. - Support valid discount codes such as
'HOLIDAY25'for a 25% discount. - Update
get_total() -> f64to reflect the discounted price.
- Use the
- Examples: Applying the discount code
'HOLIDAY25'to a productBookwith a price of100.0should reduce the total to75.0.
