Skip to content

Instantly share code, notes, and snippets.

@vanmichael
Created November 25, 2013 12:56
Show Gist options
  • Save vanmichael/7640833 to your computer and use it in GitHub Desktop.
Save vanmichael/7640833 to your computer and use it in GitHub Desktop.
Kata Change Machine
#The Change Machine
#by van
class Change
attr_reader :dollars, :quarters, :dimes, :pennies, :change
def initialize
get_amount_due
get_amount_tendered
calculate_change
make_change
puts "Total Change Due: #{@change}"
display_change
end
def get_amount_due
puts "Enter the amount due!"
@amount_due = gets.chomp
end
def get_amount_tendered
puts "Enter the amount tendered!"
@amount_tendered = gets.chomp
end
def calculate_change
@change = @amount_tendered.to_f - @amount_due.to_f
end
def make_change
@dollars = @change.to_i
@coin_change = (@[email protected]_f) * 100
@quarters = (@coin_change/25).to_i
@coin_change -= (@quarters*25)
@dimes = (@coin_change/10).to_i
@coin_change -= (@dimes*10)
@nickels = (@coin_change/5).to_i
@coin_change -= (@nickels*5)
@pennies = (@coin_change/1).to_i
end
def display_change
puts "You should issue!"
puts "dollars: #{@dollars}" if @dollars != 0
puts "quarters: #{@quarters}" if @quarters != 0
puts "dimes: #{@dimes}" if @dimes != 0
puts "nickels: #{@nickels}" if @nickels != 0
puts "pennies: #{@pennies}" if @pennies != 0
end
end
Change.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment