I want to create a program where a user basically "creates" a grocery list where the user inputs item and price until they want to quit. If the user enters ’q’ or ’Q’, then the program should stop prompting the user and instead should calculate the subtotal, add in a nominal 6% sales tax, and display the total result.
I got the first part down where user inputs their item and price but I don't know how to make it tell me the subtotal and give them a receipt. I have been trying for 7 hours!! When I run it, it's supposed to say:
Enter an item and its price, or ’Q/q’ to quit: eggs 2.13
Enter an item and its price, or ’Q/q’ to quit: milk 1.26
Enter an item and its price, or ’Q/q’ to quit: batteries 3.14
Enter an item and its price, or ’Q/q’ to quit: q
Receipt:
--------
eggs => $2.13
milk => $1.26
batteries => $3.14
---------
subtotal: $6.53
tax: $0.39
total: $6.92
Here is the code I made: (Can anyone please help me???)
def create_list
puts 'Please enter item and its price or type "quit" to exit'
items = gets.chomp.split(' ')
grocery_list = {}
index = 0
until index == items.length
grocery_list[items[index]] = 1
index += 1
end
grocery_list
end
def add_item (list)
items = ''
until items == 'quit'
puts "Enter a new item & amount, or type 'quit'."
items = gets.chomp
if items != 'quit'
new_item = items.split(' ')
if new_item.length > 2
#store int, delete, combine array, set to list w/ stored int
qty = new_item[-1]
new_item.delete_at(-1)
new_item.join(' ')
p new_item
end
list[new_item[0]] = new_item[-1]
else
break
end
end
list
end
add_item(create_list)
puts "Receipt: "
puts "------------"