Skip to content

Instantly share code, notes, and snippets.

@akcrono
Created August 13, 2014 15:16
Show Gist options
  • Save akcrono/d8513c81987cd3c619b7 to your computer and use it in GitHub Desktop.
Save akcrono/d8513c81987cd3c619b7 to your computer and use it in GitHub Desktop.
chash register program for Launch Academy
require 'bigdecimal'
require 'bigdecimal/util'
values = []
puts 'What is the sale price?'
print '>'
input = gets.chomp
while input != 'done'
values << input.to_f
puts 'What is the sale price?'
print '>'
input = gets.chomp
end
puts "Here are your item prices:\n\n"
values.each do |value|
puts "$#{sprintf("%.2f", value)}"
end
puts "\nWhat is the amount tendered?"
print '>'
cash = gets.chomp.to_f
total = values.inject(0) { |result, element| result + element }
if total > cash
puts "WARNING: Customer still owes $#{sprintf("%.2f", (total - cash))}! Exiting..."
exit
end
puts '===Thank You!==='
puts sprintf("%.2f", cash)
puts "The total change due is #{sprintf("%.2f", (cash - total))}"
puts
puts "#{Time.new.strftime('%m/%d/%Y %l:%M%p')}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment