Item removal

This commit is contained in:
David Underwood
2026-07-15 11:50:49 -04:00
parent 07a9c25d9e
commit e7f6a9620f
3 changed files with 55 additions and 1 deletions
-1
View File
@@ -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)
+8
View File
@@ -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
+47
View File
@@ -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