From 9d0e17c02d9d0b1044dcf04cf51c21a94bbd857c Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 29 Apr 2013 17:15:53 -0600 Subject: calculate price for multiple items in the cart' --- lib/cart.rb | 5 +++-- spec/unit/cart_spec.rb | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/cart.rb b/lib/cart.rb index 81d6971..c9f9483 100644 --- a/lib/cart.rb +++ b/lib/cart.rb @@ -22,7 +22,8 @@ class Cart end def total_price - return Money.new(0.00) if @items.empty? - @items.first.price + @items.reduce(Money.new(0.00)) do |total, item| + total + item.price + end end end diff --git a/spec/unit/cart_spec.rb b/spec/unit/cart_spec.rb index eb77a36..3459048 100644 --- a/spec/unit/cart_spec.rb +++ b/spec/unit/cart_spec.rb @@ -38,7 +38,7 @@ describe Cart do sut.total_items.should == 1 end - it "should calculate a total price of $0.00" do + it "should calculate a total price" do sut.total_price.should == crayons.price end end @@ -56,6 +56,10 @@ describe Cart do it "should indicate the total number of items in the cart" do sut.total_items.should == 2 end + + it "should calculate the total price" do + sut.total_price.should == crayons.price + crayons.price + end end context "when there is multiple products" do @@ -68,6 +72,10 @@ describe Cart do it "should indicate the total number of items in the cart" do sut.total_items.should == 3 end + + it "should calculate the total price" do + sut.total_price.should == crayons.price + phone.price + laptop.price + end end end -- cgit v1.2.3