Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active February 13, 2019 03:02
Show Gist options
  • Select an option

  • Save harrisonmalone/e745e8af6bfe927b1f0a6c825c300db9 to your computer and use it in GitHub Desktop.

Select an option

Save harrisonmalone/e745e8af6bfe927b1f0a6c825c300db9 to your computer and use it in GitHub Desktop.
hash
# In your Ruby file, run a while loop that presents these options, gets input from a user (as a number), and then acts on this input.
# For example, if the user selects 1, you will add a dish, and so you will take further input from the user and add that into an array.
# Do a similar job for the rest of the options with the appropriate inputs from the user, or the appropriate output.
# write a case statement with the different options
menu = [{name: "beef", price: 400}, {name: "stew", price: 300}, {name: "hamburger", price: 500}]
input = nil
orders = []
def add_dish(menu, orders)
puts "what dish would you like to add? (number)"
menu.each_with_index do |item, index|
puts "#{index + 1}) #{item[:name]}"
end
order = gets.chomp.to_i
if order == 1
orders << menu[0]
elsif order == 2
orders << menu[1]
elsif order == 3
orders << menu[2]
end
end
def view_dishes(orders)
orders.each_with_index do |item, index|
price = item[:price] / 100.00
puts "#{index + 1}) #{item[:name]} | $#{price}"
end
end
def delete_dish(orders)
puts "which one would you like to delete (index)"
orders.each_with_index do |item, index|
puts "#{index + 1}) #{item[:name]}"
end
input = gets.chomp.to_i - 1
orders.delete_at(input)
orders.each_with_index do |item, index|
puts "#{index + 1}) #{item[:name]}"
end
end
def pay_for_dishes(orders)
total = orders.map do |order|
order[:price]
end
price = total.sum / 100.00
p "You owe $#{price}"
end
while input != 5
puts """
1. add
2. delete
3. view
4. pay
5. exit
"""
puts "what would you like to do?"
input = gets.chomp.to_i
case input
when 1 then add_dish(menu, orders)
when 2 then delete_dish(orders)
when 3 then view_dishes(orders)
when 4 then pay_for_dishes(orders)
end
end
# create a array of hashes with name and price attributes for our restuarant
# create an add function that we can use in our case statement
# create a delete function that we can use in our case statement
# whatever is in the add array of hashes sum that cost
# while the case option doesn't != exit run the loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment