|
|
@ -1,6 +1,8 @@ |
|
|
|
#!/usr/bin/ruby |
|
|
|
#!/usr/bin/ruby |
|
|
|
|
|
|
|
|
|
|
|
class CoffeMachine |
|
|
|
class CoffeMachine |
|
|
|
|
|
|
|
attr_accessor :location, :change, :coffees, :price, :number_served |
|
|
|
|
|
|
|
|
|
|
|
def initialize(location, change, coffees, price) |
|
|
|
def initialize(location, change, coffees, price) |
|
|
|
@location = location |
|
|
|
@location = location |
|
|
|
@change = change |
|
|
|
@change = change |
|
|
@ -16,7 +18,7 @@ class CoffeMachine |
|
|
|
puts("Total amount served: #{@number_served}") |
|
|
|
puts("Total amount served: #{@number_served}") |
|
|
|
|
|
|
|
|
|
|
|
print("Insert coins: ") |
|
|
|
print("Insert coins: ") |
|
|
|
coins = await_payment() |
|
|
|
await_payment() |
|
|
|
else |
|
|
|
else |
|
|
|
puts("Inget kaffe kvar :(") |
|
|
|
puts("Inget kaffe kvar :(") |
|
|
|
end |
|
|
|
end |
|
|
@ -34,16 +36,7 @@ class CoffeMachine |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@change = @change.merge(coin_hash) |
|
|
|
@change = @change.merge(coin_hash) |
|
|
|
sum, d = verify_payment(coin_hash) |
|
|
|
verify_payment(coin_hash) |
|
|
|
|
|
|
|
|
|
|
|
if( d > 0 ) then |
|
|
|
|
|
|
|
dispense_change(d) |
|
|
|
|
|
|
|
dispense_coffe() |
|
|
|
|
|
|
|
elsif( d < 0 ) then |
|
|
|
|
|
|
|
puts("Not enough money") |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
dispense_coffe() |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
private def verify_payment(coin_hash) |
|
|
|
private def verify_payment(coin_hash) |
|
|
@ -52,31 +45,38 @@ class CoffeMachine |
|
|
|
sum += value*key |
|
|
|
sum += value*key |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
return sum, sum - @price |
|
|
|
delta = sum - @price |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( delta > 0 ) then |
|
|
|
|
|
|
|
dispense_change(delta) |
|
|
|
|
|
|
|
dispense_coffe() |
|
|
|
|
|
|
|
elsif( delta < 0 ) then |
|
|
|
|
|
|
|
puts("Not enough money") |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
dispense_coffe() |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
private def calculate_change(d) |
|
|
|
|
|
|
|
@change.each do |type, amount| |
|
|
|
|
|
|
|
amount = d.to_f/type |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( amount.to_i == amount && amount <= @change[type] ) then |
|
|
|
private def calculate_change(delta) |
|
|
|
return type, amount.to_i |
|
|
|
coin_return = {} |
|
|
|
else |
|
|
|
@change.each do |type, count| |
|
|
|
|
|
|
|
puts("adhfio: #{type} #{count}") |
|
|
|
|
|
|
|
amount = delta/type |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
coin_return[type] = amount |
|
|
|
|
|
|
|
@change[type] -= amount |
|
|
|
|
|
|
|
delta %= type |
|
|
|
next |
|
|
|
next |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
return nil, nil |
|
|
|
return coin_return |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
private def dispense_change(d) |
|
|
|
private def dispense_change(delta) |
|
|
|
cointype, amount = calculate_change(d) |
|
|
|
coin_return = calculate_change(delta) |
|
|
|
if( cointype ) then |
|
|
|
|
|
|
|
puts("Här är din change:") |
|
|
|
puts("Här är din change:") |
|
|
|
@change[cointype] -= amount |
|
|
|
puts("-> #{coin_return}") |
|
|
|
puts("-> #{amount}st #{cointype} kr") |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
puts("Kan ej ge tillbaka någon change, tack för pengarna.") |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
private def dispense_coffe() |
|
|
|
private def dispense_coffe() |
|
|
@ -90,5 +90,5 @@ class CoffeMachine |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
x = CoffeMachine.new("hej", {10=>2, 1=>100}, 5, 10) |
|
|
|
x = CoffeMachine.new("hej", {10=>2, 1=>100}, 5, 15) |
|
|
|
x.prompt_for_payment |
|
|
|
x.prompt_for_payment |
|
|
|