Skip to content

Instantly share code, notes, and snippets.

@ntijoh-daniel-berg
Created March 4, 2016 09:18
Show Gist options
  • Save ntijoh-daniel-berg/de9786b9e36e4ed724bf to your computer and use it in GitHub Desktop.
Save ntijoh-daniel-berg/de9786b9e36e4ed724bf to your computer and use it in GitHub Desktop.
class NegativeAmountPaidError < ArgumentError; end
class NotEnoughPaidError < ArgumentError; end
def change_o_matic(price:, amount_paid:)
if amount_paid <= 0
raise NegativeAmountPaidError, "amount_paid (#{amount_paid}) must not be negative or zero"
elsif price > amount_paid
raise NotEnoughPaidError, "amount paid (#{amount_paid}) must not be less than price (#{price})"
end
change = {1000 => 0, 500 => 0, 200 => 0, 100 => 0, 50 => 0, 20 => 0, 10 => 0, 5 => 0, 2 => 0, 1 => 0 }
denominations = change.keys.sort.reverse
amount_due = amount_paid - price
denominations.each do |den|
while amount_due >= den do
change[den] += 1
amount_due -= den
end
end
return change
end
def process_payment(price:)
puts "That will be #{price}, thanks"
change = change_o_matic(price: price, amount_paid: 500)
puts "Att returnera"
change.each_pair do |denomination, amount|
if amount > 0
puts "#{denomination}: #{amount}"
end
end
puts "Thanks, have a nice day!"
end
process_payment(price: 123)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment