Skip to content

Instantly share code, notes, and snippets.

@vanmichael
Created November 15, 2013 15:21
Show Gist options
  • Save vanmichael/7486008 to your computer and use it in GitHub Desktop.
Save vanmichael/7486008 to your computer and use it in GitHub Desktop.
#Van's Cash Register
require 'date'
require 'time'
price = 0
items = []
sub_total = 0
def numeric?(object)
object =~ /\A\$?\d+(\.\d{1,2})?\z/
end
while price != "done" do
puts "Add a sale price"
price = gets.chomp
sub_total += price.to_f
puts "Subtotal: $" + sprintf('%0.2f', sub_total.to_f)
items<<price unless price == "done"
end
puts "Here are your items:"
items.each do |i|
puts "$" + i.to_s
end
puts "Total Amount Due is: " + sprintf('$%0.2f', sub_total.to_f)
puts "What is the amount tendered?"
amount_tendered = gets.chomp
if numeric?(amount_tendered)
if amount_tendered.to_f >= sub_total.to_f
puts "===Thank You!==="
puts "The total change due is #{sprintf('$%0.2f', (amount_tendered.to_f-sub_total.to_f))}"
puts ""
puts DateTime.now
else
puts "WARNING: Customer still owes #{sprintf('$%0.2f', (sub_total.to_f-amount_tendered.to_f))}! Exiting..."
end
else
puts "WARNING: Invalid currency detected! Exiting..."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment