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 ShoppingCart is created, it should start without any products, and the total price should be zero.

  • Details:

    • Define a new struct ShoppingCart in Rust.
    • Implement an impl block with methods get_item_count() returning an integer and get_total() returning a float for the ShoppingCart struct.
    • Ensure the function get_item_count() returns 0 for an empty cart.
    • Verify the function get_total() returns 0.0 for the initial state.
  • Examples:

    pub struct ShoppingCart {
        // Define necessary fields
    }
    
    impl ShoppingCart {
        pub fn new() -> Self {
            // Initialize a new ShoppingCart
        }
        
        pub fn get_item_count(&self) -> usize {
            // Return the count of cart items
        }
        
        pub fn get_total(&self) -> f64 {
            // Return the total price
        }
    }
    #[cfg(test)]
    mod tests {
        use project::*;
    
        #[test]
        fn test_empty_cart() {
            let cart = ShoppingCart::new();
            assert_eq!(cart.get_item_count(), 0);
            assert_eq!(cart.get_total(), 0.0);
        }
    }

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 Product and implement the add_item() method in the impl block for ShoppingCart.
    • 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.
  • Examples:

    pub struct Product {
        pub id: String,
        pub name: String,
        pub price: f64,
    }
    
    impl ShoppingCart {
        pub fn add_item(&mut self, product: Product) {
            // Add the product to the cart
        }
    }
    #[cfg(test)]
    mod tests {
        use project::*;
    
        #[test]
        fn test_adding_single_product() {
            let mut cart = ShoppingCart::new();
            let book = Product {
                id: "1".to_string(),
                name: "Book".to_string(),
                price: 10.0,
            };
            cart.add_item(book);
            assert_eq!(cart.get_item_count(), 1);
            assert_eq!(cart.get_total(), 10.0);
        }
    }
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