Skip to content

Instantly share code, notes, and snippets.

@laurenkruczyk
Last active August 29, 2015 13:56
Show Gist options
  • Save laurenkruczyk/9124269 to your computer and use it in GitHub Desktop.
Save laurenkruczyk/9124269 to your computer and use it in GitHub Desktop.
Cash Register Challenge II
require 'pry'
class Float
def money
sprintf("%.2f", self)
end
end
prices = []
# def subtotal(prices)
# prices.inject(:+)
# end
while true
puts "What is the sale price?"
price = gets.chomp
if price == "done"
break
else
prices << price.to_f
subtotal = prices.inject{|subtotal, item| subtotal + item}
puts "Subtotal: #{subtotal.to_f}"
end
end
puts "Here are your item prices:"
puts
for item in prices
puts "#{item.money}"
puts "The total amount due is #{subtotal}"
puts "What is the amount tendered?"
amount_provided = gets.chomp.to_f
change = amount_provided-subtotal
if change > 0
puts "===Thank You!==="
puts "The total change due is $#{sprintf("%.2f", change)}"
puts
puts
puts Time.now.strftime("%D, %l:%M%p")
puts '================='
elsif change < 0
puts "WARNING: Customer still owe#{sprintf('%.2f',change.abs)}"
exit
elsif change == 0
puts "You don't need to give change! (#{Time.now})"
end
puts "==Thank You!=="
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment