Skip to content

Instantly share code, notes, and snippets.

@spanuska
Last active January 18, 2016 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spanuska/d6755186084872716ecb to your computer and use it in GitHub Desktop.
Save spanuska/d6755186084872716ecb to your computer and use it in GitHub Desktop.
class ChangeDispenserCashregister
attr_accessor :total, :payment, :change_to_dispense
COINS = { quarter: 25, dime: 10, nickel: 5, penny: 1 }
def initialize
get_total
get_payment
dispense_change
end
def get_total
puts "What is the total cost, with 2 decimal places, like '12.50'?"
@total = gets.chomp.to_f * 100
puts @total
end
def get_payment
puts "Enter payment with 2 decimal places, like '1.99'."
@payment = gets.chomp.to_f * 100 # converts into cents
puts @payment
get_change_due
end
def get_change_due
@change_to_dispense = {}
cents_due = @payment - @total
COINS.map do |coin, value|
num_coins = (cents_due/value).floor
change_to_dispense[coin] = num_coins
cents_due -= (value * num_coins)
end
end
def dispense_change
puts "Your change is #{@change_to_dispense}."
end
end
ChangeDispenserCashregister.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment