Commit 9d0e17c

mo khan <mo@mokhan.ca>
2013-04-29 23:15:53
calculate price for multiple items in the cart'
1 parent 804c255
Changed files (2)
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
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