Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
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}
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
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 =
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!"
puts "You still owe $#{change.abs}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment