summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2013-04-29 17:15:53 -0600
committermo khan <mo@mokhan.ca>2013-04-29 17:15:53 -0600
commit9d0e17c02d9d0b1044dcf04cf51c21a94bbd857c (patch)
treedda513e2f31bc9e89ab23b95223911006d3dabac
parent804c2557b957bcf81349f95cf334c80904fda1f6 (diff)
calculate price for multiple items in the cart'
-rw-r--r--lib/cart.rb5
-rw-r--r--spec/unit/cart_spec.rb10
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