Created
May 21, 2010 03:05
-
-
Save fudanchii/408405 to your computer and use it in GitHub Desktop.
This file contains 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 Cashier | |
@@tokens = {100 => 1, 200 => 4, 500 => 2, 1000 => 2, 2000 => 2, 5000 => 2, 10000 => 2, 20000 => 1, 50000 => 0, 100000 => 0} | |
def givemychange(ch) | |
@change = [] | |
p = @@tokens.keys.sort.reverse | |
p.each do |money| | |
temp = ch / money | |
next if temp == 0 or @@tokens[money] == 0 | |
while temp > 0 | |
@change << money | |
puts "ambil " + money.to_s | |
temp -= 1 | |
@@tokens[money] -= 1 | |
if @@tokens[money] == 0 | |
puts "lembar "+ money.to_s + " habis!!!" | |
t = temp * money | |
temp = 0 | |
end | |
end | |
ch %= money | |
ch = ch.to_i + t.to_i | |
puts "tinggal " + ch.to_s | |
break if ch == 0 | |
retry | |
end | |
@change | |
end | |
def ipaywith(h,total) | |
payment = 0 | |
puts "tagihan = " + total.to_s | |
puts "dibayar dengan uang : " | |
h.each do |money, sum| | |
puts money.to_s + ": " + sum.to_s + " lembar" | |
payment += money * sum | |
end | |
puts "= "+ payment.to_s | |
ch = payment - total | |
return "bayarnya kurang..." if ch < 0 | |
puts "kembali = " + ch.to_s | |
h.each do |m, s| | |
@@tokens[m] += s | |
end | |
givemychange ch | |
end | |
def tokens | |
@@tokens | |
end | |
end | |
cash = Cashier.new | |
puts cash.ipaywith({5000 => 4}, 18000) | |
puts cash.ipaywith({2000 => 3, 5000 => 1}, 10000) | |
puts cash.ipaywith({50000 => 1}, 2300) | |
puts cash.ipaywith({100000 => 1}, 13500) | |
puts cash.ipaywith({50000 => 1, 10000 =>2, 1000 => 5}, 75000) | |
puts cash.ipaywith({20000 => 2}, 34300) | |
puts "sisa " | |
cash.tokens.each do |m,s| | |
puts m.to_s + " = " + s.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment