Practicing Advanced TDD with Ruby through ShoppingCart Enhancements
Introduction to the Shopping Cart Module
Welcome to your fifth and final unit of this course, dedicated to practicing Test-Driven Development (TDD) utilizing Ruby and RSpec. We're going to finish building our ShoppingCart system by adding even more features to our class.
In 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. Previously, I wrote the tests for you; this time, it's all up to you!
Remember to use the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. I'm still here to help! Just ask.
Final Requirements for `ShoppingCart` Class
In this section, you'll learn how to implement the following features:
Enforcing a Quantity Limit for a Single Item
Retrieving Item Details by ID
Applying and Validating Discount Codes
Handling the Addition of Existing Items and Ensuring Quantity Limits
11. Quantity Limit for a Single Item
Description: The cart should enforce a maximum quantity limit of 10 for a single type of item, preventing more than the allowed amount from being added.
Details
Utilize the add_item(item, quantity) method to add items to the cart.
Ensure an exception is raised when adding a quantity that exceeds a limit of 10 for a single item.
The exception message should clearly state, "Maximum quantity exceeded" when the limit is breached.
Examples: Attempting to add 11 units of Product.new('1', 'Book', 10) should raise an exception indicating "Maximum quantity exceeded."
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
12. Retrieving Item Details by ID
Description: When an item is added to the cart, it should be possible to retrieve the item details using its ID, which includes the product information and its quantity in the cart.
Details
Enable items to be added using an add_item() method with specific IDs.
Ensure get_item(id) returns the correct item details, including the quantity after being added to the cart.
Examples: Adding an item with Product.new('1', 'Book', 10) and retrieving it by ID 1 should return {'id' => '1', 'name' => 'Book', 'price' => 10, 'quantity' => 1}.
Description: When an item that already exists in the cart is added again, its quantity should increase without duplicates, and the total price should reflect the cumulative price.
Details
Allow items to be added again using the add_item() method without creating duplicates in the cart.
Ensure get_item(id) returns the updated quantity after adding the same item.
Update the total price to reflect the price of the added items' cumulative quantities.
Examples: Adding Product.new('1', 'Book', 200) twice should result in a quantity of 2 for that item, with the total updated price reflecting the double addition.
16. Adding to an Existing Item Respects Maximum Quantity
Description: When an existing item in the cart has more units added to it, the total quantity should not exceed the predefined maximum limit.
Details
Utilize the add_item(item) method to add to an existing item.
Ensure that adding a quantity that results in exceeding the maximum allowed quantity raises an exception.
The exception message should be "Maximum quantity exceeded" when the quantity limit is breached.
Example: Adding 3 units of Product.new('1', 'Book', 200) to an existing 8 units should raise an exception, as it exceeds the limit.
class ShoppingCart attr_reader :items def initialize @items = [] end def add_item(item, quantity = 1) existing_item = @items.find { |i| i[:id] == item[:id] } if existing_item new_quantity = existing_item[:quantity] + quantity raise "Maximum quantity exceeded" if new_quantity > 10 existing_item[:quantity] = new_quantity else raise "Maximum quantity exceeded" if quantity > 10 @items << item.merge(quantity: quantity) end end def total @items.sum { |item| item[:price] * item[:quantity] } endend# Example Usagecart = ShoppingCart.newcart.add_item({ id: '1', name: 'Book', price: 200 }, 8)begin cart.add_item({ id: '1', name: 'Book', price: 200 }, 3)rescue => e puts e.message # Output: "Maximum quantity exceeded"end
Summary and Preparation for Practice Exercises
In this unit, you reviewed descriptions for more advanced test cases to expand the functionality of the ShoppingCart class, covering features like handling non-existent items, applying discounts, and updating item quantities. With your solidified TDD skills, let's proceed to writing targeted test cases that capture essential functionality for a robust ShoppingCart class.
As you undertake these exercises, remember to engage 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.
And you're almost done with this practice course! Great work! You're almost there.