Skip to content

Instantly share code, notes, and snippets.

@alacritythief
Created August 13, 2014 18:34
Show Gist options
  • Save alacritythief/588a6c928805fdd84a20 to your computer and use it in GitHub Desktop.
Save alacritythief/588a6c928805fdd84a20 to your computer and use it in GitHub Desktop.
Cash Register II Challenge
# CASH 2
subtotal_arr = []
puts "What is the sale price?"
amt_due = gets.chomp.downcase
puts
while amt_due != "done"
subtotal_arr << amt_due.to_f
subtotal_sum = subtotal_arr.inject(:+)
puts "Subtotal: $#{sprintf('%.2f', subtotal_sum)}"
puts
amt_due = gets.chomp.downcase
end
puts
puts "Here are your item prices:"
puts
subtotal_arr.each do |arr|
puts "$#{sprintf('%.2f', arr)}"
end
puts
puts "The total amount due is $#{sprintf('%.2f', subtotal_sum)}"
puts
puts "What is the amount tendered?"
amt_tnd = gets.to_f
if amt_tnd == subtotal_sum
puts
puts "========== Thank You! ============"
puts Time.now.strftime("%m/%d/%Y %l:%M%p")
puts "============ PAID ================"
else
change = amt_tnd - subtotal_sum
tender = sprintf('%.2f', change.abs)
if change > 0
puts
puts "========== Thank You! ============"
puts "The total change due is $#{tender}"
puts
puts Time.now.strftime("%m/%d/%Y %l:%M%p")
puts "============= PAID ==============="
else
puts
puts "==================================================="
puts " WARNING: Customer still owes $#{tender}! Exiting..."
puts "==================================================="
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment