From e7f6a9620f33d0308b3bdb0d2846e99c7b7ab488 Mon Sep 17 00:00:00 2001 From: David Underwood Date: Wed, 15 Jul 2026 11:50:49 -0400 Subject: [PATCH] Item removal --- README.md | 1 - checkout.rb | 8 ++++++++ tests.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 20109d6..3899d36 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,6 @@ Discounts are simple calculations done in-line, again for simplicity ## Future Work Some basic stuff is missing. -* Items cannot be individually removed from the checkout * Checkout items are not grouped when listed * Subtotals are not listed anywhere * Discount totals are not assoiated with the items they apply to (or available to the user at all, they're just applied to the total silently) diff --git a/checkout.rb b/checkout.rb index 9b12081..d7c6252 100644 --- a/checkout.rb +++ b/checkout.rb @@ -16,6 +16,14 @@ class Checkout @items.push item end + def remove(item) + if index = @items.index(item) + @items.delete_at(index) + end + + @items + end + def items @items end diff --git a/tests.rb b/tests.rb index 2622192..4f18bf0 100644 --- a/tests.rb +++ b/tests.rb @@ -108,6 +108,41 @@ class TestCheckout < Minitest::Test assert_equal (30.00 + 10.50), @checkout.total end + # Item removal + + def test_that_removing_an_item_not_in_the_checkout_removes_nothing + @checkout.add(:PICO2) + @checkout.remove(:PI5) + + assert_equal [:PICO2], @checkout.items + end + + def test_that_items_are_removed_one_by_one + 4.times do + @checkout.add(:ZERO2W) + end + + 3.times do + @checkout.add(:PICO2) + end + + assert_equal [:ZERO2W, :ZERO2W, :ZERO2W, :ZERO2W, :PICO2, :PICO2, :PICO2].sort, @checkout.items.sort + + @checkout.remove(:ZERO2W) + + assert_equal [:ZERO2W, :ZERO2W, :ZERO2W, :PICO2, :PICO2, :PICO2].sort, @checkout.items.sort + + @checkout.remove(:PICO2) + + assert_equal [:ZERO2W, :ZERO2W, :ZERO2W, :PICO2, :PICO2].sort, @checkout.items.sort + + 2.times do + @checkout.remove(:PICO2) + end + + assert_equal [:ZERO2W, :ZERO2W, :ZERO2W].sort, @checkout.items.sort + end + def test_that_total_is_correctly_recalculated_when_items_are_added 2.times do @checkout.add(:PICO2) @@ -125,4 +160,16 @@ class TestCheckout < Minitest::Test assert_equal 40.50, @checkout.total end + + def test_that_total_is_correctly_recalculated_when_items_are_removed + 3.times do + @checkout.add(:PICO2) # Bulk discount applies + end + + assert_equal 10.50, @checkout.total + + @checkout.remove(:PICO2) # Bulk discount no longer applies + + assert_equal 8.00, @checkout.total + end end