From e291531f1a24f7a32514614923878e851e2d6ea0 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Tue, 6 Feb 2018 11:15:26 -0800 Subject: [PATCH 1/2] My random menu --- random_menu.rb | 161 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 random_menu.rb diff --git a/random_menu.rb b/random_menu.rb new file mode 100644 index 0000000..6f7e5e4 --- /dev/null +++ b/random_menu.rb @@ -0,0 +1,161 @@ +# Create a program that runs from the Terminal that will create a random menu +# based on several items that you determine. + +# Come up with the three arrays of ten items each: +# (Each list should be a different type of food or descriptor for that food). + +adjectives = %w[delicious smoky green overdue awsome expensive cheap special grandma foreign] +cooking_styles = %w[fried seared boiled baked grilled aged cold raw special smoked] +foods = %w[salmon bread aspargus beef chicken risotto pasta eggs soup salad] +display_adjec = [] +display_style = [] +display_food = [] +num_of_itens = 10 + + +# Welcome message and prompt the user for a choice of using pre-fixed arrays or +# create their own: +puts "Hello! This is a random menu program!" +puts "\n" +puts "You can: +1 - Use our itens +2 - Create you own itens." +puts "\n" +print "Please select your choice (1 or 2): " +choice = gets.chomp +# Validade choice input: +while choice != "1" && choice != "2" + puts "Invalid choice!" + print "Please select your choice (1 or 2): " + choice = gets.chomp +end + +# Run code depending on user input +if choice == "1" + puts "\n" + print "How many itens would you like to see on your menu?" + num_of_itens = gets.chomp.to_i + while num_of_itens < 1 || num_of_itens > 10 + puts "Number invalid. Please choose from 1 to 10 itens" + print "So how many itens would you like to see on your menu?" + num_of_itens = gets.chomp.to_i + end + + # Fill 3 different arrays with random unique numbers: + puts "\n" + puts "This is your new menu:" + puts "\n" + + num_of_itens.times do |i| + random_num = rand(0..num_of_itens - 1 ) + while display_adjec.include?(random_num) + random_num = rand(0..num_of_itens - 1 ) + end + display_adjec << random_num + end + + num_of_itens.times do |i| + random_num = rand(0..num_of_itens - 1 ) + while display_style.include?(random_num) + random_num = rand(0..num_of_itens - 1 ) + end + display_style << random_num + end + + num_of_itens.times do |i| + random_num = rand(0..num_of_itens - 1 ) + while display_food.include?(random_num) + random_num = rand(0..num_of_itens - 1 ) + end + display_food << random_num + end + + + # Display itens: + num_of_itens.times do |i| + puts "#{i + 1}. #{adjectives[display_adjec[i]]} #{cooking_styles[display_style[i]]} #{foods[display_food[i]]}" + end + + +else + + # Clear inital arrays: + adjectives.clear + cooking_styles.clear + foods.clear + + #Prompt user for amount of itens to generate: + puts "\n" + print "How many itens would you like to see on your menu?" + num_of_itens = gets.chomp.to_i + # Validade numbers between 1 and 20: + while num_of_itens < 1 || num_of_itens > 20 + puts "Number invalid. You can have 1 to 20 itens on your new menu!" + print "So how many itens would you like to have?" + num_of_itens = gets.chomp.to_i + end + + # Colect adjectives: + puts "\n" + puts "Please enter #{num_of_itens} ADJECTIVES:" + num_of_itens.times do |i| + print "#{i + 1}. " + item = gets.chomp + adjectives << item + end + + # Colect cooking styles: + puts "\n" + puts "Please enter #{num_of_itens} COOKING STYLES:" + num_of_itens.times do |i| + print "#{i + 1}. " + item = gets.chomp + cooking_styles << item + end + + # Colect types of food: + puts "\n" + puts "Please enter #{num_of_itens} TYPES OF FOOD:" + num_of_itens.times do |i| + print "#{i + 1}. " + item = gets.chomp + foods << item + end + + # Fill 3 different arrays with random unique numbers: + puts "\n" + puts "This is your new menu:" + puts "\n" + + num_of_itens.times do |i| + random_num = rand(0..num_of_itens - 1 ) + while display_adjec.include?(random_num) + random_num = rand(0..num_of_itens - 1 ) + end + display_adjec << random_num + end + + num_of_itens.times do |i| + random_num = rand(0..num_of_itens - 1 ) + while display_style.include?(random_num) + random_num = rand(0..num_of_itens - 1 ) + end + display_style << random_num + end + + num_of_itens.times do |i| + random_num = rand(0..num_of_itens - 1 ) + while display_food.include?(random_num) + random_num = rand(0..num_of_itens - 1 ) + end + display_food << random_num + end + + # Display itens: + num_of_itens.times do |i| + puts "#{i + 1}. #{adjectives[display_adjec[i]]} #{cooking_styles[display_style[i]]} #{foods[display_food[i]]}" + end + +end + +puts "\n" From 569b710990541e8d6098421554da620581b1dab4 Mon Sep 17 00:00:00 2001 From: Leticia Tran Date: Wed, 7 Feb 2018 22:57:03 -0800 Subject: [PATCH 2/2] Updated code to hold methods instead of repeating lines and changed structure for a cleanner look. --- random_menu.rb | 209 +++++++++++++++++++++---------------------------- 1 file changed, 90 insertions(+), 119 deletions(-) diff --git a/random_menu.rb b/random_menu.rb index 6f7e5e4..4e03819 100644 --- a/random_menu.rb +++ b/random_menu.rb @@ -1,9 +1,12 @@ # Create a program that runs from the Terminal that will create a random menu # based on several items that you determine. + +############################################################## +# METHODS AND INITIAL VARIABLES: + # Come up with the three arrays of ten items each: # (Each list should be a different type of food or descriptor for that food). - adjectives = %w[delicious smoky green overdue awsome expensive cheap special grandma foreign] cooking_styles = %w[fried seared boiled baked grilled aged cold raw special smoked] foods = %w[salmon bread aspargus beef chicken risotto pasta eggs soup salad] @@ -12,150 +15,118 @@ display_food = [] num_of_itens = 10 - -# Welcome message and prompt the user for a choice of using pre-fixed arrays or -# create their own: -puts "Hello! This is a random menu program!" -puts "\n" -puts "You can: -1 - Use our itens -2 - Create you own itens." -puts "\n" -print "Please select your choice (1 or 2): " -choice = gets.chomp -# Validade choice input: -while choice != "1" && choice != "2" - puts "Invalid choice!" - print "Please select your choice (1 or 2): " - choice = gets.chomp -end - -# Run code depending on user input -if choice == "1" - puts "\n" - print "How many itens would you like to see on your menu?" +# Method to retrieve what the size of the menu should be: +def define_size_of_menu (max_valid) + print "How many itens would you like to see on your menu? " num_of_itens = gets.chomp.to_i - while num_of_itens < 1 || num_of_itens > 10 - puts "Number invalid. Please choose from 1 to 10 itens" - print "So how many itens would you like to see on your menu?" + while num_of_itens < 1 || num_of_itens > max_valid + puts "Number invalid. Please choose from 1 to #{max_valid} itens " + print "So how many itens would you like to see on your menu? " num_of_itens = gets.chomp.to_i end + return num_of_itens +end - # Fill 3 different arrays with random unique numbers: - puts "\n" - puts "This is your new menu:" - puts "\n" - - num_of_itens.times do |i| - random_num = rand(0..num_of_itens - 1 ) - while display_adjec.include?(random_num) - random_num = rand(0..num_of_itens - 1 ) - end - display_adjec << random_num - end - - num_of_itens.times do |i| - random_num = rand(0..num_of_itens - 1 ) - while display_style.include?(random_num) - random_num = rand(0..num_of_itens - 1 ) - end - display_style << random_num - end - +# Method for generating and populating a array with random numbers: +def create_rand_num (random_num_array, num_of_itens) num_of_itens.times do |i| random_num = rand(0..num_of_itens - 1 ) - while display_food.include?(random_num) + while random_num_array.include?(random_num) random_num = rand(0..num_of_itens - 1 ) end - display_food << random_num + random_num_array << random_num end +end - - # Display itens: +#Method to collect information for new menu: +def collect_new_itens (num_of_itens, type_of_item) num_of_itens.times do |i| - puts "#{i + 1}. #{adjectives[display_adjec[i]]} #{cooking_styles[display_style[i]]} #{foods[display_food[i]]}" + print "#{i + 1}. " + item = gets.chomp + type_of_item << item end +end + ############################################################## -else - - # Clear inital arrays: - adjectives.clear - cooking_styles.clear - foods.clear - #Prompt user for amount of itens to generate: + # Display welcome message and prompt the user for a choice of using pre-fixed arrays or + # create their own: puts "\n" - print "How many itens would you like to see on your menu?" - num_of_itens = gets.chomp.to_i - # Validade numbers between 1 and 20: - while num_of_itens < 1 || num_of_itens > 20 - puts "Number invalid. You can have 1 to 20 itens on your new menu!" - print "So how many itens would you like to have?" - num_of_itens = gets.chomp.to_i - end - - # Colect adjectives: + puts "Hello! This is a random menu program!" puts "\n" - puts "Please enter #{num_of_itens} ADJECTIVES:" - num_of_itens.times do |i| - print "#{i + 1}. " - item = gets.chomp - adjectives << item - end - - # Colect cooking styles: + puts "You can: + 1 - Use our itens + 2 - Create you own itens." puts "\n" - puts "Please enter #{num_of_itens} COOKING STYLES:" - num_of_itens.times do |i| - print "#{i + 1}. " - item = gets.chomp - cooking_styles << item + print "Please select your choice (1 or 2): " + choice = gets.chomp + # Validade choice input: + while choice != "1" && choice != "2" + puts "Invalid choice!" + print "Please select your choice (1 or 2): " + choice = gets.chomp end - # Colect types of food: - puts "\n" - puts "Please enter #{num_of_itens} TYPES OF FOOD:" - num_of_itens.times do |i| - print "#{i + 1}. " - item = gets.chomp - foods << item - end + # Run code depending on user input/choice: + if choice == "1" - # Fill 3 different arrays with random unique numbers: - puts "\n" - puts "This is your new menu:" - puts "\n" + # Define menu size by using the define_size_of_menu method: + max_valid = adjectives.length + num_of_itens = define_size_of_menu(max_valid) - num_of_itens.times do |i| - random_num = rand(0..num_of_itens - 1 ) - while display_adjec.include?(random_num) - random_num = rand(0..num_of_itens - 1 ) - end - display_adjec << random_num - end + # Fill 3 different arrays with random unique numbers using the create_rand_num method: + create_rand_num(display_adjec, num_of_itens) + create_rand_num(display_style, num_of_itens) + create_rand_num(display_food, num_of_itens) - num_of_itens.times do |i| - random_num = rand(0..num_of_itens - 1 ) - while display_style.include?(random_num) - random_num = rand(0..num_of_itens - 1 ) + # Display itens: + puts "\n" + puts "****** ****** ******" + puts "This is your new menu: " + puts "\n" + + num_of_itens.times do |i| + puts "#{i + 1}. #{adjectives[display_adjec[i]]} #{cooking_styles[display_style[i]]} #{foods[display_food[i]]}" end - display_style << random_num - end - num_of_itens.times do |i| - random_num = rand(0..num_of_itens - 1 ) - while display_food.include?(random_num) - random_num = rand(0..num_of_itens - 1 ) + + else + puts "\n" + # Clear inital arrays: + adjectives.clear + cooking_styles.clear + foods.clear + + # Define menu size by using the define_size_of_menu method: + max_valid = 20 + num_of_itens = define_size_of_menu(max_valid) + + #Colect itens for the new menu: + puts "Please enter #{num_of_itens} ADJECTIVES:" + collect_new_itens(num_of_itens, adjectives) + puts "\n" + puts "Please enter #{num_of_itens} COOKING STYLES:" + collect_new_itens(num_of_itens, cooking_styles) + puts "\n" + puts "Please enter #{num_of_itens} TYPES OF FOODS:" + collect_new_itens(num_of_itens, foods) + + # Fill 3 different arrays with random unique numbers using the create_rand_num method: + create_rand_num(display_adjec, num_of_itens) + create_rand_num(display_style, num_of_itens) + create_rand_num(display_food, num_of_itens) + + # Display itens: + puts "\n" + puts "****** ****** ******" + puts "This is your new menu: " + puts "\n" + + num_of_itens.times do |i| + puts "#{i + 1}. #{adjectives[display_adjec[i]]} #{cooking_styles[display_style[i]]} #{foods[display_food[i]]}" end - display_food << random_num - end - # Display itens: - num_of_itens.times do |i| - puts "#{i + 1}. #{adjectives[display_adjec[i]]} #{cooking_styles[display_style[i]]} #{foods[display_food[i]]}" end -end - -puts "\n" + puts "\n"