Skip to content

Instantly share code, notes, and snippets.

@BrayanZ
Last active December 16, 2015 22:20
Show Gist options
  • Save BrayanZ/5506685 to your computer and use it in GitHub Desktop.
Save BrayanZ/5506685 to your computer and use it in GitHub Desktop.
class Invoice
def initialize *lines
@lines = lines
end
def total
@lines.inject(0){ |subtotal, line| subtotal += line.total }
end
end
class InvoiceLine
def initialize quantity, product
@quantity = quantity
@product = product
end
def total
@product.price.multiply_by @quantity
end
end
class Product
def initialize name, price
@name = name
@price = Money.new price
end
def price
return @price
end
end
class Money
def initialize money
@money = money
end
def multiply_by quantity
@money * quantity
end
end
ice_tea = Product.new "ice tea", 300
coca_cola = Product.new "Coca Cola", 1500
line1 = InvoiceLine.new 3, ice_tea
line2 = InvoiceLine.new 2, coca_cola
invoice = Invoice.new line1, line2
print invoice.total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment