So basically I have to make a “Menu” and it askes you to enter all the item you want until you enter “0”. When you enter ‘0’ it should give you a total amount you owe…
selection = []
until selection == 0
puts "Enter your selection: "
selection = gets.chomp.to_i
if selection == 0
sum = selection.map {|sum,e| + e.to_i }
end
end
hash = {}
menu_items = ["EXIT MENU AND PRINT RECEIPT", "Hamburger", "Cheeseburger", "Larger Fry", "Small Fry", "Fountain Drink", "Chips","Hot Dog", "Pop Corn", "Candy Bar"]
hash[menu_items[1]] = 2.25
hash[menu_items[2]] = 2.75
hash[menu_items[3]] = 1.75
hash[menu_items[4]] = 1.25
hash[menu_items[5]] = 1.00
hash[menu_items[6]] = 0.50
hash[menu_items[7]] = 2.00
hash[menu_items[8]] = 1.25
hash[menu_items[9]] = 0.75
puts menu_items
total = []
selection = nil
until selection == 0
puts "Enter your selection: "
selection = gets.chomp.to_i
if selection.zero?
total = total.reduce(0, :+)
else
price = hash[menu_items[selection]].nil? ? 0.0 : hash[menu_items[selection]]
total << price
end
end
puts total
This works
Suggestions:
1- Use simple quote (’) instead of comillable (") for strings
2- In this example I only add the price of the selected elements when you enter when converting the selection with to_i is 0