Created
November 14, 2013 23:11
-
-
Save vanmichael/7476085 to your computer and use it in GitHub Desktop.
Calculate Change from a Transaction.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Cash Register App by Van Nguyen and Dani Dewitt | |
require 'date' | |
puts "What is the amount due?" | |
amount_due = gets.chomp | |
puts "What is the amount tendered?" | |
amount_tendered = gets.chomp | |
def numeric?(object) | |
object =~ /\A\$?\d+(\.\d{1,2})?\z/ | |
end | |
def has_less_than_three_decimals?(object) | |
object.to_s.split(".")[1].length < 3 | |
end | |
if numeric?(amount_tendered) && has_less_than_three_decimals?(amount_tendered) | |
if amount_tendered.to_f >= amount_due.to_f | |
puts "===Thank You!===" | |
puts "The total change due is #{sprintf('$%0.2f', (amount_tendered.to_f-amount_due.to_f))}" | |
puts "" | |
puts DateTime.now | |
else | |
puts "WARNING: Customer still owes #{sprintf('$%0.2f', (amount_due.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