From 7fbfe99aea518b9ca1e8affa62342bcb66607271 Mon Sep 17 00:00:00 2001 From: Hana Date: Mon, 25 Feb 2019 14:50:30 -0800 Subject: [PATCH 1/9] finished Wave 2.2 --- main.rb | 12 +++++++++++ planet.rb | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ solar_system.rb | 22 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 main.rb create mode 100644 planet.rb create mode 100644 solar_system.rb diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..655708df --- /dev/null +++ b/main.rb @@ -0,0 +1,12 @@ +require_relative "planet.rb" + +def main + vulcan = Planet.new("Vulcan", "brown & reddish", 546700, 8426000, "Has no moon") + + shoreleave = Planet.new("Shore Leave Planet", "green", 492600, 8634000, "Created as an amusement park") + + puts vulcan.summary + puts shoreleave.summary +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..32e95864 --- /dev/null +++ b/planet.rb @@ -0,0 +1,56 @@ +# Solar System +# REMEMBER: table gem is useful here! + +# Load Planet into pry: +require "pry" +require "terminal-table" + +class Planet + attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + + def initialize(name, color, mass_kg, distance_from_sum_km, fun_fact) + @name = name + @color = color + @mass_kg = mass_kg + @distance_from_sun_km = distance_from_sun_km + @fun_fact = fun_fact + end + + def name + return @name + end + + def color + return @color + end + + def mass_kg + pretty_mass = @mass_kg.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse + return pretty_mass + end + + def distance_from_sun + pretty_distance = @distance_from_sum_km.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse + return pretty_distance + end + + def fun_fact + return @fun_fact + end + + def summary + table = Terminal::Table.new do |t| + t << ["Name:", @name] + t << :separator + t << ["Color:", @color] + t << :separator + t << ["Mass:\n(kilograms)", mass_kg] + t << :separator + t << ["Distance from the sun:\n(kilometers)", distance_from_sun] # not working + t << :separator + t << ["Fun Fact:", @fun_fact] + end + + return table + end +end diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..f0f31eae --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,22 @@ +# solar_system.rb +require "terminal-table" + +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + def add_planet(planet) + @planets << planet + end + + def list_planets + table = Terminal::Table.new do |t| + :title => "Planets orbiting #{@star_name}", :rows => rows + + return table + +end \ No newline at end of file From aa8121e6e66bad285f6a79a5773d4ba625dc5533 Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 26 Feb 2019 10:21:17 -0800 Subject: [PATCH 2/9] finished wave 3 --- main.rb | 63 ++++++++++++++++++++++++++++++++++++++++++++++--- planet.rb | 18 +++----------- solar_system.rb | 25 ++++++++++++++++---- 3 files changed, 84 insertions(+), 22 deletions(-) diff --git a/main.rb b/main.rb index 655708df..bb95c951 100644 --- a/main.rb +++ b/main.rb @@ -1,12 +1,69 @@ +require "pry" + require_relative "planet.rb" +require_relative "solar_system.rb" def main - vulcan = Planet.new("Vulcan", "brown & reddish", 546700, 8426000, "Has no moon") + solar_system = SolarSystem.new("Omicron") + # planets + vulcan = Planet.new("Vulcan", "brown & reddish", 546700, 8426000, "Has no moon") shoreleave = Planet.new("Shore Leave Planet", "green", 492600, 8634000, "Created as an amusement park") + nibiru = Planet.new("Nibiru", "reddish & green", 734500, 3264000, "Home to chalk-skinned aliens") + vendikar = Planet.new("Vendikar", "red & grey", 236300, 284200, "Very rocky!") + + solar_system.add_planet(vulcan) + solar_system.add_planet(shoreleave) + solar_system.add_planet(nibiru) + solar_system.add_planet(vendikar) + + found_planet = solar_system.find_planet_by_name("vuLcAN") + + # user input time + option = "" + # option should be 1 or 0 + # 0 to exit + + until option.include?("list") || option == "exit" || option.include?("detail") || option.include?("add") + puts "You can LIST planets, see planet DETAILS, ADD a planet, or EXIT" + puts "What would you like to do?" + option = gets.chomp.downcase + end + + choice = "" + if option.include?("list") + puts solar_system.list_planets + elsif option.include?("detail") + puts "Which planet would you like to learn about?" + choice = gets.chomp.downcase + end + + case choice + when "1", "vulcan" + puts solar_system.planets[0].summary + when "2", "shore leave planet" + puts solar_system.planets[1].summary + when "3", "nibiru" + puts solar_system.planets[2].summary + when "4", "vendikar" + puts solar_system.planets[3].summary + end + + if option.include?("add") + puts "What is the planet's name?" + name = gets.chomp + puts "What is the planet's color?" + color = gets.chomp + puts "What is the planet's mass (kg)?" + mass = gets.chomp + puts "What is the planet's distance from the sun (km)?" + sun_distance = gets.chomp + puts "Please enter a fun fact about the planet!" + fun_fact = gets.chomp - puts vulcan.summary - puts shoreleave.summary + name = Planet.new(name, color, mass, sun_distance, fun_fact) + add_planet(name) + end end main diff --git a/planet.rb b/planet.rb index 32e95864..1cbd864f 100644 --- a/planet.rb +++ b/planet.rb @@ -8,7 +8,7 @@ class Planet attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact - def initialize(name, color, mass_kg, distance_from_sum_km, fun_fact) + def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @name = name @color = color @mass_kg = mass_kg @@ -16,28 +16,16 @@ def initialize(name, color, mass_kg, distance_from_sum_km, fun_fact) @fun_fact = fun_fact end - def name - return @name - end - - def color - return @color - end - def mass_kg pretty_mass = @mass_kg.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse return pretty_mass end def distance_from_sun - pretty_distance = @distance_from_sum_km.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse + pretty_distance = @distance_from_sun_km.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse return pretty_distance end - def fun_fact - return @fun_fact - end - def summary table = Terminal::Table.new do |t| t << ["Name:", @name] @@ -46,7 +34,7 @@ def summary t << :separator t << ["Mass:\n(kilograms)", mass_kg] t << :separator - t << ["Distance from the sun:\n(kilometers)", distance_from_sun] # not working + t << ["Distance from the sun:\n(kilometers)", distance_from_sun] t << :separator t << ["Fun Fact:", @fun_fact] end diff --git a/solar_system.rb b/solar_system.rb index f0f31eae..45c51cf0 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -4,7 +4,7 @@ class SolarSystem attr_reader :star_name, :planets - def initialize(star_name) + def initialize(star_name, planets = []) @star_name = star_name @planets = [] end @@ -13,10 +13,27 @@ def add_planet(planet) @planets << planet end + def planet_array + return @planets + end + def list_planets table = Terminal::Table.new do |t| - :title => "Planets orbiting #{@star_name}", :rows => rows - + @planets.each_with_index do |planet, index| + t << ["#{index + 1}. ", planet.name.capitalize] + end + end + return table + end + + def find_planet_by_name(planet_string) + @planets.each do |planet| + if planet.name.include?(planet_string.capitalize) + return planet + end + end -end \ No newline at end of file + return "No matches found" + end +end From cac8399c0a1fbcdd76dcc670590f55d1df9d6463 Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 26 Feb 2019 13:29:54 -0800 Subject: [PATCH 3/9] refactored main with a case statement --- main.rb | 67 ++++++++++++++++++++++++++----------------------- solar_system.rb | 4 --- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/main.rb b/main.rb index bb95c951..80e326de 100644 --- a/main.rb +++ b/main.rb @@ -3,53 +3,53 @@ require_relative "planet.rb" require_relative "solar_system.rb" +# option menu +def options + return "Options available to you... + 1. See a LIST of all the planets on file + 2. See a specific planet's DETAILS + 3. ADD a planet + 4. Quit\n + Please enter your option:" +end + def main solar_system = SolarSystem.new("Omicron") - # planets vulcan = Planet.new("Vulcan", "brown & reddish", 546700, 8426000, "Has no moon") - shoreleave = Planet.new("Shore Leave Planet", "green", 492600, 8634000, "Created as an amusement park") - nibiru = Planet.new("Nibiru", "reddish & green", 734500, 3264000, "Home to chalk-skinned aliens") - vendikar = Planet.new("Vendikar", "red & grey", 236300, 284200, "Very rocky!") - solar_system.add_planet(vulcan) + shoreleave = Planet.new("Shore Leave Planet", "green", 492600, 8634000, "Created as an amusement park") solar_system.add_planet(shoreleave) + nibiru = Planet.new("Nibiru", "reddish & green", 734500, 3264000, "Home to chalk-skinned aliens") solar_system.add_planet(nibiru) + vendikar = Planet.new("Vendikar", "red & grey", 236300, 284200, "Very rocky!") solar_system.add_planet(vendikar) - found_planet = solar_system.find_planet_by_name("vuLcAN") + # Welcome message + puts "Welcome to the PlanetDex!" # user input time - option = "" - # option should be 1 or 0 - # 0 to exit - - until option.include?("list") || option == "exit" || option.include?("detail") || option.include?("add") - puts "You can LIST planets, see planet DETAILS, ADD a planet, or EXIT" - puts "What would you like to do?" - option = gets.chomp.downcase - end + puts options + option = gets.chomp - choice = "" - if option.include?("list") + case + when option.include?("list"), "1" puts solar_system.list_planets - elsif option.include?("detail") + when option.include?("detail"), "2" puts "Which planet would you like to learn about?" choice = gets.chomp.downcase - end - - case choice - when "1", "vulcan" - puts solar_system.planets[0].summary - when "2", "shore leave planet" - puts solar_system.planets[1].summary - when "3", "nibiru" - puts solar_system.planets[2].summary - when "4", "vendikar" - puts solar_system.planets[3].summary - end - if option.include?("add") + case choice + when "1", "vulcan" + puts solar_system.planets[0].summary + when "2", "shore leave planet" + puts solar_system.planets[1].summary + when "3", "nibiru" + puts solar_system.planets[2].summary + when "4", "vendikar" + puts solar_system.planets[3].summary + end + when option.include?("add") puts "What is the planet's name?" name = gets.chomp puts "What is the planet's color?" @@ -63,6 +63,11 @@ def main name = Planet.new(name, color, mass, sun_distance, fun_fact) add_planet(name) + when option.include?("exit"), "4" + puts "Thank you for using the PlanetDex!" + else + puts "That is not a valid option." + puts "Please re-enter your option:" end end diff --git a/solar_system.rb b/solar_system.rb index 45c51cf0..72f163d5 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -13,10 +13,6 @@ def add_planet(planet) @planets << planet end - def planet_array - return @planets - end - def list_planets table = Terminal::Table.new do |t| @planets.each_with_index do |planet, index| From 60322ab9ade6c2f316c14aeb6f13ff1b97233b96 Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 26 Feb 2019 13:40:53 -0800 Subject: [PATCH 4/9] changed case statement to if/elsif --- main.rb | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/main.rb b/main.rb index 80e326de..d1871cb8 100644 --- a/main.rb +++ b/main.rb @@ -32,24 +32,13 @@ def main puts options option = gets.chomp - case - when option.include?("list"), "1" + if option.include?("list") || option == "1" puts solar_system.list_planets - when option.include?("detail"), "2" + elsif option.include?("detail") || option == "2" puts "Which planet would you like to learn about?" - choice = gets.chomp.downcase - - case choice - when "1", "vulcan" - puts solar_system.planets[0].summary - when "2", "shore leave planet" - puts solar_system.planets[1].summary - when "3", "nibiru" - puts solar_system.planets[2].summary - when "4", "vendikar" - puts solar_system.planets[3].summary - end - when option.include?("add") + planet = gets.chomp.downcase + puts solar_system.find_planet_by_name(planet).summary + elsif option.include?("add") || option == "3" puts "What is the planet's name?" name = gets.chomp puts "What is the planet's color?" @@ -63,7 +52,7 @@ def main name = Planet.new(name, color, mass, sun_distance, fun_fact) add_planet(name) - when option.include?("exit"), "4" + elsif option.include?("exit") || option == "4" puts "Thank you for using the PlanetDex!" else puts "That is not a valid option." From 2bc190f39bc296d910c0183c0f74cbbd320de4d7 Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 26 Feb 2019 13:53:55 -0800 Subject: [PATCH 5/9] created solar system method new_planet --- main.rb | 15 ++------------- solar_system.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/main.rb b/main.rb index d1871cb8..76df76bc 100644 --- a/main.rb +++ b/main.rb @@ -39,21 +39,10 @@ def main planet = gets.chomp.downcase puts solar_system.find_planet_by_name(planet).summary elsif option.include?("add") || option == "3" - puts "What is the planet's name?" - name = gets.chomp - puts "What is the planet's color?" - color = gets.chomp - puts "What is the planet's mass (kg)?" - mass = gets.chomp - puts "What is the planet's distance from the sun (km)?" - sun_distance = gets.chomp - puts "Please enter a fun fact about the planet!" - fun_fact = gets.chomp - - name = Planet.new(name, color, mass, sun_distance, fun_fact) - add_planet(name) + puts solar_system.new_planet elsif option.include?("exit") || option == "4" puts "Thank you for using the PlanetDex!" + exit else puts "That is not a valid option." puts "Please re-enter your option:" diff --git a/solar_system.rb b/solar_system.rb index 72f163d5..23f39869 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -23,6 +23,24 @@ def list_planets return table end + def new_planet + puts "What is the planet's name?" + name = gets.chomp + puts "What is the planet's color?" + color = gets.chomp + puts "What is the planet's mass (kg)?" + mass = gets.chomp + puts "What is the planet's distance from the sun (km)?" + sun_distance = gets.chomp + puts "Please enter a fun fact about the planet!" + fun_fact = gets.chomp + + name = Planet.new(name, color, mass, sun_distance, fun_fact) + add_planet(name) + + return "Successfully added new planet!" + end + def find_planet_by_name(planet_string) @planets.each do |planet| if planet.name.include?(planet_string.capitalize) From 272fa9a5b45774ee2985d5e5f823df0baf35376b Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 26 Feb 2019 13:59:26 -0800 Subject: [PATCH 6/9] created while loop, loop bug --- main.rb | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/main.rb b/main.rb index 76df76bc..ec2c219c 100644 --- a/main.rb +++ b/main.rb @@ -29,23 +29,27 @@ def main puts "Welcome to the PlanetDex!" # user input time + exit = "N" + puts options option = gets.chomp - if option.include?("list") || option == "1" - puts solar_system.list_planets - elsif option.include?("detail") || option == "2" - puts "Which planet would you like to learn about?" - planet = gets.chomp.downcase - puts solar_system.find_planet_by_name(planet).summary - elsif option.include?("add") || option == "3" - puts solar_system.new_planet - elsif option.include?("exit") || option == "4" - puts "Thank you for using the PlanetDex!" - exit - else - puts "That is not a valid option." - puts "Please re-enter your option:" + while exit == "N" + if option.include?("list") || option == "1" + puts solar_system.list_planets + elsif option.include?("detail") || option == "2" + puts "Which planet would you like to learn about?" + planet = gets.chomp.downcase + puts solar_system.find_planet_by_name(planet).summary + elsif option.include?("add") || option == "3" # looping bug! + puts solar_system.new_planet + elsif option.include?("exit") || option == "4" + puts "Are you sure you want to quit? (Y/N) " + exit = gets.chomp.upcase + else + puts "That is not a valid option." + puts "Please re-enter your option:" + end end end From f561163412b39a7143291fd4b0b0d6a48d806c21 Mon Sep 17 00:00:00 2001 From: Hana Date: Wed, 27 Feb 2019 05:18:31 -0800 Subject: [PATCH 7/9] fixed main loop bug --- main.rb | 17 ++++++++++------- solar_system.rb | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/main.rb b/main.rb index ec2c219c..4a8b5951 100644 --- a/main.rb +++ b/main.rb @@ -5,11 +5,12 @@ # option menu def options - return "Options available to you... + return " + Options available to you... 1. See a LIST of all the planets on file 2. See a specific planet's DETAILS 3. ADD a planet - 4. Quit\n + 4. EXIT\n Please enter your option:" end @@ -29,27 +30,29 @@ def main puts "Welcome to the PlanetDex!" # user input time - exit = "N" + exit = "n" puts options option = gets.chomp - while exit == "N" + loop do if option.include?("list") || option == "1" puts solar_system.list_planets elsif option.include?("detail") || option == "2" puts "Which planet would you like to learn about?" planet = gets.chomp.downcase puts solar_system.find_planet_by_name(planet).summary - elsif option.include?("add") || option == "3" # looping bug! + elsif option.include?("add") || option == "3" puts solar_system.new_planet elsif option.include?("exit") || option == "4" - puts "Are you sure you want to quit? (Y/N) " - exit = gets.chomp.upcase + break else puts "That is not a valid option." puts "Please re-enter your option:" end + + puts options + option = gets.chomp end end diff --git a/solar_system.rb b/solar_system.rb index 23f39869..6924e435 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -48,6 +48,6 @@ def find_planet_by_name(planet_string) end end - return "No matches found" + return nil end end From 9d37702f67eb73bf7b8e38a741a5b24b64b6530e Mon Sep 17 00:00:00 2001 From: Hana Date: Wed, 27 Feb 2019 05:42:08 -0800 Subject: [PATCH 8/9] fixed find_planet_by_name method --- main.rb | 6 +++--- solar_system.rb | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/main.rb b/main.rb index 4a8b5951..5b783362 100644 --- a/main.rb +++ b/main.rb @@ -30,8 +30,7 @@ def main puts "Welcome to the PlanetDex!" # user input time - exit = "n" - + # fencepost loop puts options option = gets.chomp @@ -45,7 +44,8 @@ def main elsif option.include?("add") || option == "3" puts solar_system.new_planet elsif option.include?("exit") || option == "4" - break + puts "Thank you for using the PlanetDex!" + exit else puts "That is not a valid option." puts "Please re-enter your option:" diff --git a/solar_system.rb b/solar_system.rb index 6924e435..f835fe78 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -42,12 +42,10 @@ def new_planet end def find_planet_by_name(planet_string) - @planets.each do |planet| - if planet.name.include?(planet_string.capitalize) - return planet - end + find_planet = @planets.find(ifnone = nil) do |planet| + planet.name.capitalize == planet_string.capitalize end - return nil + return find_planet end end From 61e1991193cf6a7da329b377d77ed44ab5718528 Mon Sep 17 00:00:00 2001 From: Hana Date: Wed, 27 Feb 2019 05:43:59 -0800 Subject: [PATCH 9/9] clean up of pry/comments --- main.rb | 2 +- planet.rb | 5 +---- solar_system.rb | 1 + 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/main.rb b/main.rb index 5b783362..7bcfa7f5 100644 --- a/main.rb +++ b/main.rb @@ -1,4 +1,4 @@ -require "pry" +# main.rb require_relative "planet.rb" require_relative "solar_system.rb" diff --git a/planet.rb b/planet.rb index 1cbd864f..fb45c984 100644 --- a/planet.rb +++ b/planet.rb @@ -1,8 +1,5 @@ -# Solar System -# REMEMBER: table gem is useful here! +# planet.rb -# Load Planet into pry: -require "pry" require "terminal-table" class Planet diff --git a/solar_system.rb b/solar_system.rb index f835fe78..bfdc1bfc 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -1,4 +1,5 @@ # solar_system.rb + require "terminal-table" class SolarSystem