Integrating Advanced Features into the ShoppingCart with TDD in Ruby

Introduction to the Shopping Cart Module

Welcome to your fourth unit for this course, dedicated to practicing Test-Driven Development (TDD), utilizing Ruby and RSpec. We're going to continue building our ShoppingCart system by integrating even more features into our class.

In this course, the emphasis is 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. Where I wrote the tests for you last time, 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.

More Requirements for `ShoppingCart` Class

This section will cover additional features and enhancements for our ShoppingCart class. As you write your test cases and implement the corresponding functionality, keep the requirements below in mind:

6. Removing a Non-Existent Item

  • Description: Removing an item that does not exist in the cart should raise an exception, indicating the item is not found.
  • Details
    • Support item removal through a remove_item(item_id) method.
    • Ensure the method raises an appropriate error message if an item does not exist in the cart.
  • Examples: Attempting to remove an item with item_id: '999' from the cart should raise an exception with the message 'Item not found'.
class ShoppingCart
  attr_reader :items

  def initialize
    @items = []
  end

  # Existing methods ...

  def remove_item(id)
    item = @items.find { |i| i[:id] == id }
    raise 'Item not found' unless item

    if item[:quantity] > 1
      item[:quantity] -= 1
    else
      @items.delete(item)
    end
  end
end

# Example usage
cart = ShoppingCart.new
begin
  cart.remove_item('999') # Attempt to remove an item that doesn't exist
rescue RuntimeError => e
  puts e.message # Output: "Item not found"
end

7. Applying Percentage Discount

  • Description: When a percentage discount is applied, the total price of the items in the cart should reflect this discount.
  • Details
    • Use the apply_discount(percentage) method to apply a percentage discount to the total.
    • Ensure get_total() returns the discounted price after applying the discount.
  • Examples: Applying a 10% discount to an item with a total price of 100 should result in a new total of 90.
class ShoppingCart
  # Existing methods ...

  def apply_discount(percentage)
    discount_factor = (100 - percentage) / 100.0
    @discounted_total = total * discount_factor
  end

  def total
    @discounted_total || @items.sum { |item| item[:price] * item[:quantity] }
  end
end

# Example usage
cart = ShoppingCart.new
cart.add_item({ id: '1', name: 'Book', price: 100 })
cart.apply_discount(10)
puts cart.total # Output: 90
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