summaryrefslogtreecommitdiff
path: root/lib/cart.rb
blob: c9f94837234e495500ee6ced5a4e498cc41288a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Cart
  def initialize(items = [])
    @items = items
  end

  def add(product)
    @items.push(product)
  end

  def includes?(product)
    @items.include?(product)
  end

  def quantity_of(product)
    @items.find_all do |item|
      item == product
    end.count
  end

  def total_items
    @items.count
  end

  def total_price
    @items.reduce(Money.new(0.00)) do |total, item|
      total + item.price
    end
  end
end