Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RubyATM
- ACCEPTED_NOTES = [100, 200, 500, 2000]
- DEFAULT_BALANCE = { 100 => 10, 200 => 10, 500 => 1, 2000 => 50}
- MAX_WITHDRAWAL = 20000
- EVIL_ATM = false # Always return the smallest possible notes
- def initialize(notes = DEFAULT_BALANCE)
- @balance = {}
- deposit(notes)
- end
- def withdraw(amount)
- check_withdrawal(amount)
- withdrawal = fetch_notes(amount)
- @balance.merge!(withdrawal){ |key, existing, withdrawal| existing - withdrawal }
- return withdrawal, @balance
- end
- def deposit(notes)
- check_deposit(notes)
- @balance.merge!(notes){ |key, existing, deposit| existing + deposit }
- end
- private
- def fetch_notes(amount)
- withdrawn_notes = {}
- withdrawn_amount = 0
- notes = EVIL_ATM ? @balance.keys.sort : @balance.keys.sort.reverse
- notes.each do |note|
- max_notes = (amount - withdrawn_amount) / note
- actual_notes = [@balance[note], max_notes].min
- if actual_notes > 0
- withdrawn_amount += actual_notes * note
- withdrawn_notes[note] = actual_notes
- end
- end
- raise WithdrawalAmountInvalid, 'Amount cannot be withdrawn. Please specify another amount.' if withdrawn_amount < amount
- withdrawn_notes
- end
- def check_withdrawal(amount)
- raise WithdrawalAmountInvalid, 'Please round to the nearest tenth.' if amount % 10 > 0
- raise WithdrawalAmountTooLow, 'Amount too low.' if amount < 0
- raise WithdrawalAmountTooHigh, "Amount too high. Max withdrawal can be #{max_withdrawal}." if amount > max_withdrawal
- end
- def check_deposit(notes)
- raise DepositAmountInvalid, "This ATM accepts only these notes: #{ACCEPTED_NOTES.join(", ")}" unless (notes.keys - ACCEPTED_NOTES).empty?
- end
- def balance
- @balance.map{ |note, count| note * count }.reduce(:+)
- end
- def max_withdrawal
- [MAX_WITHDRAWAL, balance].min
- end
- end
- class WithdrawalAmountTooHigh < StandardError; end
- class WithdrawalAmountTooLow < StandardError; end
- class WithdrawalAmountInvalid < StandardError; end
- class DepositAmountInvalid < StandardError; end
- module Run
- loop do
- puts"\n"
- puts"-----------------------------------"
- print"Enter the amount you need to withdrawl: "
- amount = gets.chomp.to_i
- puts"-----------------------------------"
- atm = RubyATM.new
- currency_calculation, balance = atm.withdraw(amount)
- p "Currency calculation=> ",currency_calculation
- p "Balance remaining in ATM: #{balance}"
- break
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement