Guest User

Untitled

a guest
May 26th, 2018
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. class Cashier
  2.  
  3. @@tokens = {100 => 1, 200 => 4, 500 => 2, 1000 => 2, 2000 => 2, 5000 => 2, 10000 => 2, 20000 => 1, 50000 => 0, 100000 => 0}
  4.  
  5. def givemychange(ch)
  6. @change = []
  7. p = @@tokens.keys.sort.reverse
  8. p.each do |money|
  9. temp = ch / money
  10. next if temp == 0 or @@tokens[money] == 0
  11. while temp > 0
  12. @change << money
  13. puts "ambil " + money.to_s
  14. temp -= 1
  15. @@tokens[money] -= 1
  16. if @@tokens[money] == 0
  17. puts "lembar "+ money.to_s + " habis!!!"
  18. t = temp * money
  19. temp = 0
  20. end
  21. end
  22. ch %= money
  23. ch = ch.to_i + t.to_i
  24. puts "tinggal " + ch.to_s
  25. break if ch == 0
  26. retry
  27. end
  28. @change
  29. end
  30.  
  31. def ipaywith(h,total)
  32. payment = 0
  33. puts "tagihan = " + total.to_s
  34. puts "dibayar dengan uang : "
  35. h.each do |money, sum|
  36. puts money.to_s + ": " + sum.to_s + " lembar"
  37. payment += money * sum
  38. end
  39. puts "= "+ payment.to_s
  40. ch = payment - total
  41. return "bayarnya kurang..." if ch < 0
  42. puts "kembali = " + ch.to_s
  43. h.each do |m, s|
  44. @@tokens[m] += s
  45. end
  46. givemychange ch
  47. end
  48.  
  49. def tokens
  50. @@tokens
  51. end
  52.  
  53. end
  54.  
  55. cash = Cashier.new
  56. puts cash.ipaywith({5000 => 4}, 18000)
  57. puts cash.ipaywith({2000 => 3, 5000 => 1}, 10000)
  58. puts cash.ipaywith({50000 => 1}, 2300)
  59. puts cash.ipaywith({100000 => 1}, 13500)
  60. puts cash.ipaywith({50000 => 1, 10000 =>2, 1000 => 5}, 75000)
  61. puts cash.ipaywith({20000 => 2}, 34300)
  62. puts "sisa "
  63. cash.tokens.each do |m,s|
  64. puts m.to_s + " = " + s.to_s
  65. end
Add Comment
Please, Sign In to add comment