Implementing More Features for the Shopping Cart

More about the Shopping Cart Module with Rust and Testing

Welcome to the fourth unit of this course dedicated to practicing Test Driven Development (TDD). In this lesson, we will continue building our ShoppingCart system by adding more features using Rust and its built-in testing framework.

This course places emphasis on hands-on practice, where you will receive requirements through tests one at a time. Your task is to write tests and implement the code that makes each test pass, simulating a real-world TDD environment.

Remember to use the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. I'm here to help if needed!

6. Removing a Non-Existent Item

  • Description: Attempting to remove a product that is not present in the cart should return a result indicating that the product was not found.
  • Details:
    • Implement the removal via a fn remove_item(&mut self, id: &str) -> Result<(), String> function.
    • Ensure the function returns an Err with the message "Item not found" if the product is not in the cart.
  • Examples: Attempting to remove a product with id: "999" should return an error with the message "Item not found".

7. Applying a Percentage Discount

  • Description: Applying a percentage discount should adjust the total price of the products in the cart accordingly.
  • Details:
    • Implement a fn apply_discount(&mut self, percentage: f64) method to apply a percentage discount to the total.
    • Ensure get_total() returns the adjusted price after applying the discount.
  • Examples: Applying a 10% discount to a total price of 100.0 should result in a new total of 90.0.

8. Applying a Bulk Discount

  • Description: Automatically apply a 10% discount when the total price of products in the cart exceeds $150.
  • Details:
    • If the total exceeds 150, the 10% discount should be automatically applied.
    • The get_total() method should return the discounted total when the discount is applicable.
  • Examples: Adding a "Book" with a price of 200.0 should result in a total of 180.0 after applying the bulk discount.

9. Clearing All Items

  • Description: The cart should have the functionality to remove all products, resetting both the item count and the total price to zero.
  • Details:
    • Implement a fn clear(&mut self) method to remove all products from the cart.
    • Ensure get_item_count() returns 0 after clearing.
    • Verify get_total() is 0.0 after clearing.
  • Examples: If there are multiple products in the cart, calling clear should leave the count as 0 and the total as 0.0.

10. Updating Item Quantity

  • Description: When the quantity of a product in the cart is changed, both the item count and total price should accurately reflect the new quantity.
  • Details:
    • Allow item quantities to be updated using a fn update_quantity(&mut self, id: &str, quantity: usize) function.
    • Ensure get_item_count() returns the correct total item count after the quantity is updated.
    • Ensure get_total() returns the correct total price after the quantity is updated.
  • Examples: Updating the quantity of a "Book" with a price per unit of 10.0 from 2 to 3 should result in a total count of 3 and a total price of 30.0.

Summary and Preparation for Practice Exercises

In this unit, you explored the design of test cases for a ShoppingCart struct, focusing on essential features like handling an empty cart, adding items, managing item quantities, and removing items. Now it's your turn to ensure that the described functionality is implemented by writing comprehensive test cases and ensuring that the tests pass with the least amount of code needed.

As you undertake these exercises, remember to engage fully in the Red-Green-Refactor cycle. Be sure to practice writing tests first and do not write implementation code unless the test asks for it.

Red! Green! Refactor!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal