Skip to content

Instantly share code, notes, and snippets.

@jmoon90
Last active December 28, 2015 08:29
Show Gist options
  • Select an option

  • Save jmoon90/7471907 to your computer and use it in GitHub Desktop.

Select an option

Save jmoon90/7471907 to your computer and use it in GitHub Desktop.
It should take a amount due, tender. It should give an error and quit if it contains anything besides a number, except 1 ".". It should only have 2 decimals.
class Calculator
def amount_due(number)
if invalid?(number) == true
puts "Error message amount_due"
exit
else
list = [number]
while number != "done"
@due = number
if invalid?(number) == true
puts "Error message amount_due"
exit
else
puts "What amount is due?"
end
number = gets.chomp
if number == "done"
total_due = list.inject(0) { |sum, num| sum.to_f + num.to_f }
else
list << number
end
end
puts
list.inject(0) { |sum, num| puts sum.to_f + num.to_f }
@total = total_due
puts
puts "The amount due is $#{@total}"
end
end
def amount_tender(number)
if invalid?(number) == true
puts "Error message amount Tendered"
exit
else
@tendered = number
total = @total.to_f - @tendered.to_f
if @total.to_f < @tendered.to_f || @total.to_f == @tendered.to_f
puts "===Thank You!==="
puts "The total change due is $#{'%.2f' % total.abs}"
today_time = Time.now.strftime("%m/%d/%y %R")
puts today_time
puts "================"
else
while total > 0
total = total - @still_owe.to_f
if total.to_f < 0
puts "===Thank You!==="
puts "The total change due is $#{'%.2f' % total.abs}"
today_time = Time.now.strftime("%m/%d/%y %R")
puts today_time
puts "================"
elsif total.to_f > 0
puts "You owe total: $#{'%.2f' % total.abs}"
@still_owe = gets.chomp
if invalid?(@still_owe) == true
puts "Error message amount Tendered"
exit
end
else
puts "You have no change due"
end
end
end
end
end
def invalid?(which_method)
!which_method.match(/\A\d+(\.\d+)?\z/)
end
end
calculate = Calculator.new
puts "What amount is due?"
number = gets.chomp
total_due = calculate.amount_due(number)
puts "What amount is tendered?"
tender_amount = gets.chomp
total_tendered = calculate.amount_tender(tender_amount)
@jmoon90
Copy link
Copy Markdown
Author

jmoon90 commented Nov 15, 2013

Added a time stamp every time customers checkout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment