Skip to content

Instantly share code, notes, and snippets.

@Murphydbuffalo
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Murphydbuffalo/f9a9364bb3653bc13591 to your computer and use it in GitHub Desktop.
Save Murphydbuffalo/f9a9364bb3653bc13591 to your computer and use it in GitHub Desktop.
The solution to the second cashier problem
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