Skip to content

Instantly share code, notes, and snippets.

@allcentury
Forked from gsheppard/cashregister2.rb
Last active August 29, 2015 13:56
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 allcentury/9121696 to your computer and use it in GitHub Desktop.
Save allcentury/9121696 to your computer and use it in GitHub Desktop.
Cash Register II w/ Methods
=begin
As a cashier
I want to enter the sale price of each item
So that the program will calculate the amount due
Developed by: Anthony Ross, Dan Clarke, and Greg Sheppard
=end
def sub_total(array)
subtotal_amt = 0.0
array.each do |x|
subtotal_amt += x
end
subtotal_amt
end
def due(amount_tendered, subtotal)
amount_tendered - subtotal
end
def currency_conv(float)
"$%0.2f" % float
end
def list(array)
array.each do |x|
puts currency_conv(x)
end
end
trans = []
item = ''
while true
puts "What is the sale price?"
item = gets.chomp
if item == 'done'
break
end
trans << item.to_f
puts "\nSubtotal: #{currency_conv(sub_total(trans))} \n "
end
puts "\nHere are your item prices: \n "
list(trans)
puts "The total amount due is: #{currency_conv(sub_total(trans))}"
puts "\nWhat is the amount tendered?"
amount = gets.chomp.to_f
amount_due = due(amount, sub_total(trans))
if amount_due > 0
puts "\n===Thank You!==="
puts "The total change due is #{currency_conv(amount_due) } \n "
puts Time.now.strftime("%m/%d/%Y %I:%M%p") # Format should be 02/12/2013 5:50PM
puts "================"
else
puts " \nWARNING: Customer still owes #{currency_conv(amount_due)}! Exiting..."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment