Last active
January 18, 2016 22:02
-
-
Save spanuska/d6755186084872716ecb 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 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