Implementing More Features for the Shopping Cart

More about the Shopping Cart Module with Go and Testify

Welcome to your fourth unit of this course dedicated to practicing Test Driven Development (TDD). We're going to continue building our ShoppingCart system by adding even more features using Go and Testify.

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.

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` Struct

6. Removing a Non-Existent Item

  • Description: Trying to remove an item that is not present in the cart should return an error, indicating that the item was not found.
  • Details:
    • Implement the removal through a RemoveItem(id int) error function.
    • Ensure the function returns an error with an appropriate message if the item is not found in the cart.
  • Examples: Attempting to remove an item with Id: 999 should return an error with the message "Item not found".

7. Applying Percentage Discount

  • Description: Applying a percentage discount should adjust the total price of the items in the cart accordingly.
  • Details:
    • Use the ApplyDiscount(percentage float64) float64 method to apply a percentage discount to the total.
    • Ensure GetTotal() returns the adjusted price after applying the discount.
  • Examples: Applying a 10% discount to a total price of 100 should result in a new total of 90.

8. Applying a Bulk Discount

  • Description: Automatically apply a 10% discount when the total price of items in the cart exceeds $150.
  • Details:
    • If the total exceeds 150, the 10% discount should be automatically applied.
    • The GetTotal() method should return the discounted total when the discount is applicable.
  • Examples: Adding a "Book" with a price of 200 should result in a total of 180 after applying the bulk discount.
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