Skip to content

Instantly share code, notes, and snippets.

@werdyii
Last active December 16, 2015 02:09
Show Gist options
  • Save werdyii/5360534 to your computer and use it in GitHub Desktop.
Save werdyii/5360534 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# encoding: utf-8
# Barmanov účet
class Bill
def initialize
@items = []
@total = 0.0
end
def add_item name, price, quantity = 1
item = @items.find{|i| i["name"] == name}
if item.nil?
item = {"name" => name, "price" => price, "quantity" => quantity.to_i, "total" => price*quantity }
@items << item
else
item["quantity"] += 1
item["total"] = item["quantity"]*item["price"]
end
end
def display
@items.each do |i|
printf "%15s %.2fx%i -> %.2f\n", i["name"], i["price"], i["quantity"], i["total"]
end
puts "==============================================="
printf "%15s : \e[31m %.2f\e[0m\n", "Celkom", total
end
private
def total
@items.inject(0){| sum, i| sum + i["total"] }
end
end
u = Bill.new
u.add_item "Káva", 1.8
u.add_item "Soletky", 0.5
u.add_item "Minerálka", 1.1
u.add_item "Káva", 1.8
u.add_item "Lupienky", 0.8, 3
u.add_item "Kavenka", 0.3
u.add_item "Kavenka", 0.3
u.add_item "Kavenka", 0.3
u.display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment