-
-
Save alishersadikov/5119525d4bf74e575d0b992eb0ecd7b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order | |
def initialize | |
@line_items = [] | |
end | |
def add_line_item(line_item) | |
@line_items << line_item | |
end | |
def total | |
subtotals = @line_items.each { |li| li.quantity * li.price } | |
subtotals.reduce(:+) | |
end | |
end | |
class LineItem | |
attr_reader :quantity, :price | |
def initialize(quantity, price) | |
@price = price | |
@quantity = quantity | |
end | |
end | |
order = Order.new | |
order.add_line_item LineItem.new(2, 3.00) | |
order.add_line_item LineItem.new(4, 1.00) | |
puts order.total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment