Skip to content

Instantly share code, notes, and snippets.

@laurenkruczyk
Last active August 29, 2015 13:56
Show Gist options
  • Save laurenkruczyk/9142763 to your computer and use it in GitHub Desktop.
Save laurenkruczyk/9142763 to your computer and use it in GitHub Desktop.
Cash Register Challenge II Updated with Methods
def make_money(to_money)
sprintf("$ %.2f",to_money)
end
def get_user_input(text)
puts text
gets.chomp
end
list_of_purchases=[]
sales_price = 0
subtotal=0
#KEEP GETTING PURCHASE UNTIL USER SAYS DONE
while true
sales_price = get_user_input ("What is the sale price?")
if sales_price == "done"
break
end
sales_price=sales_price.to_f
list_of_purchases << sales_price
subtotal = subtotal + sales_price
puts "Subtotal: #{make_money(subtotal)}"
end
#PRINT OUT LIST OF PURCHASE
puts "Here are your item prices:"
list_of_purchases.each do|item|
puts make_money(item)
end
#GET THE AMOUNT TNDERED
puts "the total amount due is #{make_money(subtotal)}."
cust_cash = get_user_input("What is the amount tendered?").to_f
#DO CHANGE
change_due = cust_cash - subtotal
puts "===Thank You!==="
puts "The total change due is #{make_money(change_due)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment