From dadfeed768bdc5eacce52aa8208dd43ff5dfab45 Mon Sep 17 00:00:00 2001 From: David Underwood Date: Wed, 15 Jul 2026 10:44:00 -0400 Subject: [PATCH] Basic total generation, no discounts for now --- checkout.rb | 12 +++++++++--- main.rb | 4 ++-- tests.rb | 19 +++++++++++++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/checkout.rb b/checkout.rb index bae0ff4..dd0116d 100644 --- a/checkout.rb +++ b/checkout.rb @@ -12,7 +12,7 @@ class Checkout end def add(item) - raise InvalidItem unless valid_item(item) + raise InvalidItem unless valid_item?(item) @items.push item end @@ -20,9 +20,15 @@ class Checkout @items end + def total + @items.reduce(0.00) do |running_total, item| + running_total + AVAILABLE_ITEMS[item] + end + end + private - def valid_item(item) + def valid_item?(item) return AVAILABLE_ITEMS.keys.include?(item) end -end \ No newline at end of file +end diff --git a/main.rb b/main.rb index 93acc16..9129c44 100644 --- a/main.rb +++ b/main.rb @@ -5,5 +5,5 @@ checkout = Checkout.new() checkout.add(:PI5) checkout.add(:ZERO2W) -checkout.items # ["PI5", "ZERO2W"] -checkout.total # 75.00 +puts checkout.items # ["PI5", "ZERO2W"] +puts checkout.total # 75.00 diff --git a/tests.rb b/tests.rb index e5c422e..a52ddf7 100644 --- a/tests.rb +++ b/tests.rb @@ -16,7 +16,22 @@ class TestCheckout < Minitest::Test def test_that_allowed_items_are_added @checkout.add(:PI5) @checkout.add(:ZERO2W) + @checkout.add(:PICO2) - assert_equal [:PI5, :ZERO2W].sort, @checkout.items.sort + assert_equal [:PI5, :ZERO2W, :PICO2].sort, @checkout.items.sort end -end \ No newline at end of file + + def test_that_total_is_the_sum_of_all_items # Not accounting for discounts at the moment + @checkout.add(:PI5) # 60.00 + @checkout.add(:ZERO2W) # 15.00 + @checkout.add(:PICO2) # 4.00 + # ----- + # 79.00 total + + assert_equal 79.00, @checkout.total + end + + def test_that_an_empty_checkout_total_is_zero + assert_equal 0.00, @checkout.total + end +end