Skip to content

Instantly share code, notes, and snippets.

@HeroicEric
Forked from sjwats/coffeejamesimproved.rb
Last active December 28, 2015 18:59
Show Gist options
  • Select an option

  • Save HeroicEric/7547308 to your computer and use it in GitHub Desktop.

Select an option

Save HeroicEric/7547308 to your computer and use it in GitHub Desktop.
#LIGHT = 5
#MEDIUM = 7.5
#BOLD = 9.75
sub_total = 0
sales_list = []
def is_valid_money?(amount)
!!amount.to_s.match(/\A\d+(\.\d{2})?\z/)
end
def display_change(amount_tendered, subtotal)
change = amount_tendered.to_f - sub_total.to_f
puts "===Thank You!==="
puts "The total change due is $#{change}."
time_stamp = Time.now.strftime('%D %H:%M %p')
puts "#{time_stamp}"
puts"================"
end
while true
puts "What is the sale price?"
price = gets.chomp
break if price == "done"
if is_valid_money?(price)
sales_list << price.to_f
puts "The sale price is $#{price.to_f}"
sub_total = sales_list.inject(0) {|sum, item_price| sum + item_price}
puts "Subtotal: $#{sub_total}"
else
puts "Invalid amount of currency. Please enter price again with two decimal places/
or type 'done' to complete transaction."
end
end
puts "Here are your item prices:"
sales_list.each{|item| puts "$#{item}"}
puts "The total amount due is $#{sub_total}"
puts "What is the amount tendered?"
while false
amount_tendered = gets.chomp
if amount_tendered.to_f >= sub_total
if is_valid_money?(amount_tendered)
dispsense_change(amount_tendered, subtotal)
break
else
puts "Invalid currency. Please re-enter the amount tendered with two/
decimal places"
end
else
abort("WARNING: Customer still owes $#{(amount_tendered.to_f-sub_total).abs}. Exiting...")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment