From f9c53472a05158960217d0e965ed332437643254 Mon Sep 17 00:00:00 2001 From: torshimizu Date: Sat, 10 Feb 2018 22:42:52 -0800 Subject: [PATCH 1/4] Created solar_system.rb to backup file --- solar_system.rb | 135 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 solar_system.rb diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..6002a8bb --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,135 @@ +require_relative 'classes_methods.rb' + +# ______ milky_system ______ +planetA = Planet.new("Mercury") +planetA.distance_from_sun = 36_000_000 +planetA.year_length = "0.2 Earth years" + +planetB = Planet.new("Venus") +planetB.distance_from_sun = 67_000_000 +planetB.year_length = "0.6 Earth years" + +planetC = Planet.new("Earth") +planetC.distance_from_sun = 93_000_000 +planetC.year_length = "365.26 Earth days" + +planetD = Planet.new("Mars") +planetD.distance_from_sun = 142_000_000 +planetD.year_length = "1.9 Earth years" + +planetE = Planet.new("Jupiter") +planetE.distance_from_sun = 484_000_000 +planetE.year_length = "11.9 Earth years" + +planetF = Planet.new("Saturn") +planetF.distance_from_sun = 887_000_000 +planetF.year_length = "29.5 Earth years" + +planetG = Planet.new("Uranus") +planetG.distance_from_sun = 1_784_000_000 +planetG.year_length = "84 Earth years" + +planetH = Planet.new("Neptune") +planetH.distance_from_sun = 2_794_000_000 +planetH.year_length = "164.8 Earth years" + +#____alpha_centauri_____ +planet1 = Planet.new("Alpha Centauri Bb") +planet1.distance_from_sun = 111_111_111 +planet1.year_length = "123 Earth years" + +#____instantiate Solar Systems +human_system = [planetA, planetB, planetC, planetD, planetE, planetF, planetG, planetH] +milky_system = SolarSystem.new(human_system, "Milky Way") # making a new human_system +alpha_planets= [planet1] +alpha_centauri = SolarSystem.new(alpha_planets, "Alpha Centauri") + +menu = [milky_system, alpha_centauri] + +#_____for the user interface_____ +puts "Welcome to our Solar System viewer. Let's takeoff . . ." +puts +puts " /\\" +puts " | |" +puts " | |" +puts " /|/\\|\\" +puts " /_||||_\\" +puts " T T" +puts "\n" + +puts "MENU:" +puts "0: Add a planet" +print_menu(menu) +print "Please select the number of the menu item to access: " +menu_selection = gets.chomp.to_i +puts + +case menu_selection +when 0 + print "Would you like to create a new planet? > " + create_new = gets.chomp.downcase + puts + case create_new + when "yes", "y" + users_solar_system = get_solar_system(menu) # getting solar system to add to + puts + + new_planet_name = get_planet_name # getting users planet name + user_planet = Planet.new(new_planet_name) # instantiating a new planet + users_solar_system.add_new_planet(user_planet) # adding the new planet to the human_system hash + puts + + new_planet_dist = get_distance # getting the distance of the newly created planet + user_planet.distance_from_sun = new_planet_dist # adding this new distance to the planet's instance + puts + + new_planet_year = get_year + user_planet.year_length = new_planet_year + puts + else # THIS CURRENTLY DOES NOT LOOP BACK INTO A MENU THAT CAN BE INTERACTED WITH + puts "I wouldn't want to play god either! \n \n" + puts "MENU:" + puts "0: Add a planet" + print_menu(menu) + print "Please select the number of the system you would like to browse: " + menu_selection = gets.chomp.to_i + end +when 1, 2 + working_system = choose_system(menu, menu_selection) + + puts "\n" + + view_systems = true + until view_systems == false + + + print_planet_menu = true + until print_planet_menu == false + puts "Here is the list of planets in the #{working_system.sys_name}:" + puts + choose_planet(working_system) + planet_choice = gets.chomp.to_i + puts + + until working_system.planet_hash.keys.include?(planet_choice) + puts "Invalid selection. Please choose a planet from the following list: " + choose_planet(working_system) + planet_choice = gets.chomp.to_i + end + puts working_system.planet_hash[planet_choice].planet_summary + puts + + print "Would you like to see information on another planet? > " + loop_again = gets.chomp.downcase + print_planet_menu = keep_looping(loop_again) + puts + end + + print "Would you like to view another system? > " + loop_all_again = gets.chomp.downcase + view_systems = keep_looping(loop_all_again) + end + + puts + puts "Reentering atmosphere. See ya, Earthling~" +end From cda7c42bca919cc12eaf6336431996212fe5d324 Mon Sep 17 00:00:00 2001 From: torshimizu Date: Sat, 10 Feb 2018 22:43:54 -0800 Subject: [PATCH 2/4] Create solar_classes_methods.rb Uploading to backup file --- solar_classes_methods.rb | 162 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 solar_classes_methods.rb diff --git a/solar_classes_methods.rb b/solar_classes_methods.rb new file mode 100644 index 00000000..c1079a79 --- /dev/null +++ b/solar_classes_methods.rb @@ -0,0 +1,162 @@ +class Planet + attr_reader :name + attr_accessor :year_length, :distance_from_sun + + def initialize(planet_name) + @name = planet_name + end + + def planet_summary + return "#{name}: \n Takes #{year_length} to orbit the sun. \n It's distance from the sun is #{distance_from_sun} miles." + end +end + +class SolarSystem + attr_reader :planets, :sys_name # evaluate if this was a good choice for planets + # attr_accessor :planets + + def initialize(planets, sys_name) + @planets = planets + @sys_name = sys_name + @all_summary = Array.new + @planet_names = Array.new + @planet_hash = Hash.new + create_planet_hash + planet_names + + end + + def add_new_planet(new_planet) + @planets << new_planet + create_planet_hash + create_all_summary + end + + def create_planet_hash # this method ties the index +1 of the planet to the instance of the planet, for selection in the menu + # @planet_hash = Hash[@planet_names.zip(@planets)] + @planets.each.with_index(1) do |planet, index| + @planet_hash[index] = planet + end + end + + def planet_hash + @planet_hash + end + + def planet_names # creates an array of planet_names maybe this should just be built in to create_planet_hash - same info can be accessed from there. + @planets.each do |planet| + @planet_names << planet.name + end + end + + # def list_planet_names + # @planet_names + # end + + def listplanets + numbered_planets = [] + @planets.each.with_index(1) do |planet, index| + numbered_planet = "#{index}: #{planet.name}" + numbered_planets << numbered_planet + end + numbered_planets = numbered_planets.join("\n") + return numbered_planets + end + + # ______ these methods are not used in UI ______ + def create_all_summary + @planets.each { |planet| @all_summary << planet.planet_summary } + end + + def print_all_summary + @all_summary + end + +end + + +# ________ methods when adding a new planet _________ + +def get_solar_system(menu) + puts "Which solar system is the planet in? " + print_menu(menu) + print "> " + users_system = gets.chomp.to_i + if users_system == 1 + users_system = menu[0] + elsif users_system == 2 + users_system = menu[1] + else + puts "WARNING: I don't know that system." + end + return users_system +end + +def get_planet_name + print "What is the name your new planet? > " + new_planet_name = gets.chomp.capitalize + return new_planet_name +end + +def get_distance # might need to convert ',' to '_' for bignums + print "How far is this planet from the Sun in miles? > " + miles_from_sun = gets.chomp.to_i +end + +def get_year + print "How many Earth years does it take to orbit the Sun? > " + planet_year = gets.chomp.to_f +end + +# _________ methods for user interface _________ + +def print_menu(menu) + menu.each.with_index(1) do |sol_sys, listno| + puts "#{listno}: #{sol_sys.sys_name}" + end + puts +end + +def choose_system(menu, chosen_system) # I'm not sure why I made this a method + if chosen_system == 1 # this works for now, but is not futureproof (a system cannot be added) + working_system = menu[0] + return working_system + elsif chosen_system == 2 + working_system = menu[1] + return working_system + else + puts "I don't know that system!" + end + loop_menu = true + unless loop_menu == false + print "Would you like to see a different system? (yes/no) > " + loop_menu = gets.chomp.downcase + case loop_menu + when "yes", "y" + print_menu(menu) + working_system = choose_system(menu) # this is called recursion(?) + when "no", "n" + loop_menu = false + puts + puts "Reentering atmosphere. See ya, Earthling~" + exit + end + end +end + +def choose_planet(working_system) + puts working_system.listplanets + puts + print "Please select the number of the planet you want to see: " +end + +def keep_looping(input_variable) + case input_variable + when "yes", "y" + return true + when "no", "n" + return false + else + puts "I'm not sure what you want" + end +end From 65a42bd152c3013b3769c7ee2c092221ee02fc2a Mon Sep 17 00:00:00 2001 From: torshimizu Date: Sun, 11 Feb 2018 23:03:56 -0800 Subject: [PATCH 3/4] Update solar_system.rb --- solar_system.rb | 253 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 172 insertions(+), 81 deletions(-) diff --git a/solar_system.rb b/solar_system.rb index 6002a8bb..4615d924 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -2,49 +2,63 @@ # ______ milky_system ______ planetA = Planet.new("Mercury") -planetA.distance_from_sun = 36_000_000 -planetA.year_length = "0.2 Earth years" +planetA.distance_from_sun = 57_900_000 +planetA.year_length = 0.2 +planetA.diameter = 4878 planetB = Planet.new("Venus") -planetB.distance_from_sun = 67_000_000 -planetB.year_length = "0.6 Earth years" +planetB.distance_from_sun = 108_160_000 +planetB.year_length = 0.6 +planetB.diameter = 12_104 planetC = Planet.new("Earth") planetC.distance_from_sun = 93_000_000 -planetC.year_length = "365.26 Earth days" +planetC.year_length = 1 +planetC.diameter = 12_756 planetD = Planet.new("Mars") -planetD.distance_from_sun = 142_000_000 -planetD.year_length = "1.9 Earth years" +planetD.distance_from_sun = 227_936_640 +planetD.year_length = 1.9 +planetD.diameter = 6794 planetE = Planet.new("Jupiter") -planetE.distance_from_sun = 484_000_000 -planetE.year_length = "11.9 Earth years" +planetE.distance_from_sun = 778_369_000 +planetE.year_length = 11.9 +planetE.diameter = 142_984 planetF = Planet.new("Saturn") -planetF.distance_from_sun = 887_000_000 -planetF.year_length = "29.5 Earth years" +planetF.distance_from_sun = 1_427_034_000 +planetF.year_length = 29.5 +planetF.diameter = 120_536 planetG = Planet.new("Uranus") -planetG.distance_from_sun = 1_784_000_000 -planetG.year_length = "84 Earth years" +planetG.distance_from_sun = 2_870_658_186 +planetG.year_length = "84" +planetG.diameter = 51_118 planetH = Planet.new("Neptune") -planetH.distance_from_sun = 2_794_000_000 -planetH.year_length = "164.8 Earth years" +planetH.distance_from_sun = 4_496_976_000 +planetH.year_length = "164.8" +planetH.diameter = 49_532 #____alpha_centauri_____ -planet1 = Planet.new("Alpha Centauri Bb") -planet1.distance_from_sun = 111_111_111 -planet1.year_length = "123 Earth years" +planet1 = Planet.new("Alpha Centauri Bc") +planet1.distance_from_sun = 21_093_000 +planet1.year_length = 12 +planet1.diameter = 12_000 + +planet2 = Planet.new("Proxima Centauri b") +planet2.distance_from_sun = 7_500_000 +planet2.year_length = 11.2 +planet2.diameter = 14_030 #____instantiate Solar Systems human_system = [planetA, planetB, planetC, planetD, planetE, planetF, planetG, planetH] milky_system = SolarSystem.new(human_system, "Milky Way") # making a new human_system -alpha_planets= [planet1] +alpha_planets= [planet1, planet2] alpha_centauri = SolarSystem.new(alpha_planets, "Alpha Centauri") -menu = [milky_system, alpha_centauri] +menu = [milky_system, alpha_centauri] # this has now become a poor variable name #_____for the user interface_____ puts "Welcome to our Solar System viewer. Let's takeoff . . ." @@ -57,79 +71,156 @@ puts " T T" puts "\n" -puts "MENU:" -puts "0: Add a planet" -print_menu(menu) -print "Please select the number of the menu item to access: " -menu_selection = gets.chomp.to_i -puts +menu_viewer = true +until menu_viewer == false -case menu_selection -when 0 - print "Would you like to create a new planet? > " - create_new = gets.chomp.downcase + + puts "\nMENU:" + puts "0: Other options" + print_menu(menu) + print "Please select the number of the menu item to access: " + menu_selection = gets.chomp.to_i puts - case create_new - when "yes", "y" - users_solar_system = get_solar_system(menu) # getting solar system to add to - puts - - new_planet_name = get_planet_name # getting users planet name - user_planet = Planet.new(new_planet_name) # instantiating a new planet - users_solar_system.add_new_planet(user_planet) # adding the new planet to the human_system hash - puts - - new_planet_dist = get_distance # getting the distance of the newly created planet - user_planet.distance_from_sun = new_planet_dist # adding this new distance to the planet's instance - puts - - new_planet_year = get_year - user_planet.year_length = new_planet_year - puts - else # THIS CURRENTLY DOES NOT LOOP BACK INTO A MENU THAT CAN BE INTERACTED WITH - puts "I wouldn't want to play god either! \n \n" - puts "MENU:" - puts "0: Add a planet" - print_menu(menu) - print "Please select the number of the system you would like to browse: " - menu_selection = gets.chomp.to_i - end -when 1, 2 - working_system = choose_system(menu, menu_selection) - puts "\n" + #_______ this needs case/when statement for selecting between adding a planet and calculating the distance between two planets in one system - view_systems = true - until view_systems == false + case menu_selection + when 0 + other_options = {1 => "Add a planet", 2 => "Calculate distance between two planets"} + print_other_options(other_options) + print "\nPlease select the number of the menu item to access: " + select_other_option = gets.chomp.to_i + until other_options.keys.include?(select_other_option) + puts "Invalid selection. Please select a number from the following menu: " + print_other_options(other_options) + select_other_option = gets.chomp.to_i + end - print_planet_menu = true - until print_planet_menu == false - puts "Here is the list of planets in the #{working_system.sys_name}:" - puts - choose_planet(working_system) - planet_choice = gets.chomp.to_i + case select_other_option + when 1 + print "Would you like to create a new planet? > " + create_new = gets.chomp.downcase puts - until working_system.planet_hash.keys.include?(planet_choice) - puts "Invalid selection. Please choose a planet from the following list: " - choose_planet(working_system) - planet_choice = gets.chomp.to_i + unless create_new == ("no" || "n") + puts "Which solar system is the planet in? " + users_solar_system = get_solar_system(menu) # getting solar system to add to + puts + + new_planet_name = get_planet_name # getting users planet name + user_planet = Planet.new(new_planet_name) # instantiating a new planet + users_solar_system.add_new_planet(user_planet) # adding the new planet to the human_system hash + puts + + new_planet_dist = get_distance # getting the distance of the newly created planet + user_planet.distance_from_sun = new_planet_dist # adding this new distance to the planet's instance + puts + + new_planet_year = get_year + user_planet.year_length = new_planet_year + puts + + puts user_planet.planet_summary + puts + else + puts "I wouldn't want to play god either! \n \n" end - puts working_system.planet_hash[planet_choice].planet_summary - puts - print "Would you like to see information on another planet? > " - loop_again = gets.chomp.downcase - print_planet_menu = keep_looping(loop_again) - puts + when 2 + print "\nWould you like to calculate the distance between two planets in the same system? > " + calculate_distance = gets.chomp.downcase + + + unless calculate_distance == ("no" || "n") + puts "\nWhich solar system are the planets in?" + users_solar_system = get_solar_system(menu) + puts + # working_system = choose_system(menu, users_solar_system) + + # print_planet_menu = true + # until print_planet_menu == false + puts "Here is the list of planets in the #{users_solar_system.sys_name}:" + puts + puts users_solar_system.listplanets + print "Please select the number of the first planet: " + planet_choice1 = gets.chomp.to_i + puts + + until users_solar_system.planet_hash.keys.include?(planet_choice1) + puts "Invalid selection. Please choose a planet from the following list: " + puts users_solar_system.listplanets + puts "> " + planet_choice1 = gets.chomp.to_i + end + + print "Please select the number of the second planet: " + planet_choice2 = gets.chomp.to_i + + until users_solar_system.planet_hash.keys.include?(planet_choice2) # this isn't DRY + puts "Invalid selection. Please choose a planet from the following list: " + puts users_solar_system.listplanets + planet_choice2 = gets.chomp.to_i + puts "> " + end + end + + #____ need to correlate numbered choice to planet + planet_choice1 = users_solar_system.planet_hash[planet_choice1] + planet_choice2 = users_solar_system.planet_hash[planet_choice2] + distance_between_planets = planet_choice1.distance_from_sun - planet_choice2.distance_from_sun + puts "#{planet_choice1.name} is #{distance_between_planets} km away from #{planet_choice2.name}." end - print "Would you like to view another system? > " - loop_all_again = gets.chomp.downcase - view_systems = keep_looping(loop_all_again) + when 1, 2 + working_system = choose_system(menu, menu_selection) + + puts "\n" + + # view_systems = true + # until view_systems == false + + + print_planet_menu = true + until print_planet_menu == false + puts "Here is the list of planets in the #{working_system.sys_name}:" + puts + choose_planet(working_system) + planet_choice = gets.chomp.to_i + puts + + until working_system.planet_hash.keys.include?(planet_choice) || planet_choice == 0 + puts "Invalid selection. Please choose a planet from the following list: " + choose_planet(working_system) + planet_choice = gets.chomp.to_i + end + + case planet_choice + when 0 + puts working_system.print_all_summary + else + puts working_system.planet_hash[planet_choice].planet_summary + end + puts + + print "Would you like to see information on another planet in this system? > " + loop_again = gets.chomp.downcase + print_planet_menu = keep_looping(loop_again) + # puts + end + print "\nWould you like to view the menu? > " + viewing_menu = gets.chomp.downcase + menu_viewer = keep_looping(viewing_menu) + + # print "Would you like to view another system? > " + # loop_all_again = gets.chomp.downcase + # view_systems = keep_looping(loop_all_again) + # end + else + puts "Invalid selection. \n Please select a number from the following menu: " + next end - puts - puts "Reentering atmosphere. See ya, Earthling~" end +puts +puts "Reentering atmosphere. See ya, Earthling~" From fc9c2c0e70e260fa9ac46a148d512b9a0179ee32 Mon Sep 17 00:00:00 2001 From: torshimizu Date: Sun, 11 Feb 2018 23:04:30 -0800 Subject: [PATCH 4/4] Update solar_classes_methods.rb --- solar_classes_methods.rb | 71 +++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/solar_classes_methods.rb b/solar_classes_methods.rb index c1079a79..79fcc187 100644 --- a/solar_classes_methods.rb +++ b/solar_classes_methods.rb @@ -1,13 +1,13 @@ class Planet attr_reader :name - attr_accessor :year_length, :distance_from_sun + attr_accessor :year_length, :distance_from_sun, :diameter def initialize(planet_name) @name = planet_name end def planet_summary - return "#{name}: \n Takes #{year_length} to orbit the sun. \n It's distance from the sun is #{distance_from_sun} miles." + return "#{name}: \n Takes #{year_length} Earth years to orbit the sun. \n It's distance from the sun is #{distance_from_sun} km. \n #{name}\'s diameter is #{diameter} km." end end @@ -21,6 +21,7 @@ def initialize(planets, sys_name) @all_summary = Array.new @planet_names = Array.new @planet_hash = Hash.new + create_all_summary create_planet_hash planet_names @@ -49,12 +50,8 @@ def planet_names # creates an array of planet_names maybe this should just be bu end end - # def list_planet_names - # @planet_names - # end - def listplanets - numbered_planets = [] + numbered_planets = ["0: List all summaries"] @planets.each.with_index(1) do |planet, index| numbered_planet = "#{index}: #{planet.name}" numbered_planets << numbered_planet @@ -63,7 +60,7 @@ def listplanets return numbered_planets end - # ______ these methods are not used in UI ______ +# ________ the following methods can be produced by looping over @planets and printing each planet's summary def create_all_summary @planets.each { |planet| @all_summary << planet.planet_summary } end @@ -75,22 +72,8 @@ def print_all_summary end -# ________ methods when adding a new planet _________ +# ________ methods for other options _________ -def get_solar_system(menu) - puts "Which solar system is the planet in? " - print_menu(menu) - print "> " - users_system = gets.chomp.to_i - if users_system == 1 - users_system = menu[0] - elsif users_system == 2 - users_system = menu[1] - else - puts "WARNING: I don't know that system." - end - return users_system -end def get_planet_name print "What is the name your new planet? > " @@ -100,12 +83,28 @@ def get_planet_name def get_distance # might need to convert ',' to '_' for bignums print "How far is this planet from the Sun in miles? > " - miles_from_sun = gets.chomp.to_i + miles_from_sun = gets.chomp + miles_from_sun = match_number(miles_from_sun) end def get_year print "How many Earth years does it take to orbit the Sun? > " - planet_year = gets.chomp.to_f + planet_year = gets.chomp + miles_from_sun = match_number(planet_year) +end + +def match_number(num) + until num.match(/^\-?\d*\.?\d*$/) && !num.empty? + if !num.match(/^\-?\d*\.?\d*$/) + print "Invalid input. Please enter a number without commas: " + num = gets.chomp + end + if num.empty? + print "No number inputted. Please enter a number: " + num = gets.chomp + end + end + return num end # _________ methods for user interface _________ @@ -117,6 +116,20 @@ def print_menu(menu) puts end +def get_solar_system(menu) # not sure why I have both this method and choose_system + print_menu(menu) + print "> " + users_system = gets.chomp.to_i + if users_system == 1 + users_system = menu[0] + elsif users_system == 2 + users_system = menu[1] + else + puts "WARNING: I don't know that system." + end + return users_system +end + def choose_system(menu, chosen_system) # I'm not sure why I made this a method if chosen_system == 1 # this works for now, but is not futureproof (a system cannot be added) working_system = menu[0] @@ -134,7 +147,7 @@ def choose_system(menu, chosen_system) # I'm not sure why I made this a method case loop_menu when "yes", "y" print_menu(menu) - working_system = choose_system(menu) # this is called recursion(?) + working_system = choose_system(menu, chosen_system) # this is called recursion(?) when "no", "n" loop_menu = false puts @@ -147,7 +160,11 @@ def choose_system(menu, chosen_system) # I'm not sure why I made this a method def choose_planet(working_system) puts working_system.listplanets puts - print "Please select the number of the planet you want to see: " + print "Please select the number of the item you would like to see: " +end + +def print_other_options(other_options) + other_options.each { |num, option| puts "#{num}: #{option}" } end def keep_looping(input_variable)