Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Cart
- include Reloadable
- attr_reader :items
- def initialize
- @items = []
- end
- def total_price
- @items.inject(0) { |sum, item| sum + item.price }
- end
- def total_items
- @items.inject(0) { |sum, item| sum + item.quantity }
- end
- def add_product(product)
- current_item = @items.find{|item| item.product == product}
- if current_item
- current_item.increment_quantity
- else
- current_item = CartItem.new(product)
- @items << current_item
- end
- current_item
- end
- def remove_product(product)
- current_item = @items.find{|item| item.product == product}
- if current_item.quantity > 1
- current_item.quantity = current_item.quantity - 1
- elsif current_item.quantity == 1
- @items.delete(current_item)
- end
- current_item
- end
- end
Add Comment
Please, Sign In to add comment