Last active
August 29, 2015 14:01
-
-
Save Murphydbuffalo/f9a9364bb3653bc13591 to your computer and use it in GitHub Desktop.
The solution to the second cashier problem
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
puts "Please enter item prices: " #prompts for item prices | |
input = gets.chomp | |
charges = [] | |
def total(array) | |
array.inject(0) {|total, each| total + each} | |
end | |
until input.downcase == "done" | |
charges << input.to_f | |
subtotal = "Current subtotal of $#{total(charges)}." | |
puts subtotal #returns a running subtotal for each | |
input = gets.chomp | |
end | |
charges.each {|amount| puts "$#{amount}"} #at "done" returns a list of all prices entered | |
puts "$#{total(charges)} due." #also returns total due | |
puts "Cash tendered: " #prompts for cash tendered | |
payment = gets.chomp | |
change = (payment.to_f - total(charges)).round(2) | |
time = Time.new | |
if change > 0 | |
puts "Your change is $#{change.abs}, thank you!" #calculates change due and date/time | |
puts time.strftime("%m/%d/%y at %H:%M") | |
elsif change == 0 | |
puts "Exact change, thanks!" | |
else | |
puts "You still owe $#{change.abs}." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment