From 26c1b171982d82e6a85ebfd26656397b0d16970e Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Mon, 25 Feb 2019 13:51:43 -0800 Subject: [PATCH 1/7] Wave 1 - defined Planet class --- planet.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 planet.rb diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..767d3121 --- /dev/null +++ b/planet.rb @@ -0,0 +1,21 @@ +class Planet + attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + + def initialize(name, color, mass_kg, distance_from_sun_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 summary + return "#{@name}: #{@color} in color, mass: #{@mass_kg}kg, distance from sun: #{distance_from_sun_km}km, fun fact: #{fun_fact}" + end +end + +ariel = Planet.new("Ariel", "blue-ish", 6455783, 308546069, "Ariel has an extensive network of medical-related industries.") + +londinium = Planet.new("Londinium", "green-ish", 1236620, 1346380, "Londinium is where the Parliament is housed.") + +puts ariel.fun_fact From a113bd1a07fc47b63bb16e34a04fd87377e6e72a Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Mon, 25 Feb 2019 14:22:05 -0800 Subject: [PATCH 2/7] added main.rb --- main.rb | 16 ++++++++++++++++ planet.rb | 8 +++----- 2 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 main.rb diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..6ee98578 --- /dev/null +++ b/main.rb @@ -0,0 +1,16 @@ + +# 1. **OPTIONAL:** Add error checking to your constructor. +# - Both `mass_kg` and `distance_from_sun_km` must be numbers that are greater than zero. +# - What should your program do if they aren't? +# - How will you make sure this behavior works? + +# 1. **OPTIONAL:** Add minitest tests for `Planet`. + +require_relative "planet" + +def main + puts ARIEL.summary + puts LONDINIUM.summary +end + +main diff --git a/planet.rb b/planet.rb index 767d3121..406c81f2 100644 --- a/planet.rb +++ b/planet.rb @@ -10,12 +10,10 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "#{@name}: #{@color} in color, mass: #{@mass_kg}kg, distance from sun: #{distance_from_sun_km}km, fun fact: #{fun_fact}" + return "#{@name}: #{@color} in color, mass: #{@mass_kg}kg, distance from sun: #{@distance_from_sun_km}km, fun fact: #{@fun_fact}" end end -ariel = Planet.new("Ariel", "blue-ish", 6455783, 308546069, "Ariel has an extensive network of medical-related industries.") +ARIEL = Planet.new("Ariel", "blue-ish", 6455783, 308546069, "Ariel has an extensive network of medical-related industries.") -londinium = Planet.new("Londinium", "green-ish", 1236620, 1346380, "Londinium is where the Parliament is housed.") - -puts ariel.fun_fact +LONDINIUM = Planet.new("Londinium", "green-ish", 1236620, 1346380, "Londinium is where the Parliament is housed.") From f5040116d2ccbf09fe4beb795f8e6731f16c3908 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Tue, 26 Feb 2019 10:01:58 -0800 Subject: [PATCH 3/7] refactored main method to match requirements --- main.rb | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/main.rb b/main.rb index 6ee98578..10e9161f 100644 --- a/main.rb +++ b/main.rb @@ -1,16 +1,45 @@ - -# 1. **OPTIONAL:** Add error checking to your constructor. -# - Both `mass_kg` and `distance_from_sun_km` must be numbers that are greater than zero. -# - What should your program do if they aren't? -# - How will you make sure this behavior works? - -# 1. **OPTIONAL:** Add minitest tests for `Planet`. - require_relative "planet" +require_relative "solar_system" + +# Restructure `main` to do the following: +# 1. Create a `SolarSystem` and add some `Planet`s +# 1. Enter a control loop that repeatedly asks the user what to do next. The two options at this point are `list planets` and `exit`. def main - puts ARIEL.summary - puts LONDINIUM.summary + new_sol_sys = SolarSystem.new("Phunghoule") + bebop = Planet.new("Bebop", "gray", 5, 2.2, "Bebop's nickname is Mrs. Tubbins!") + ninou = Planet.new("Ninou", "black", 3, 2.2, "Ninou is the best planet ever!") + andrew = Planet.new("Andrew", "white", 91, 3, "Andrew is full of gas") + new_sol_sys.add_planet(bebop) + new_sol_sys.add_planet(ninou) + new_sol_sys.add_planet(andrew) + + puts "What do you want to do next? Type either LIST PLANETS, or EXIT." + input = gets.chomp.downcase + if input.downcase == "list planets" + puts new_sol_sys.list_planets + elsif input.downcase == "exit" + break + else + puts "What do you want to do next? Type either LIST PLANETS, or EXIT." + end end main + +# solar_system = SolarSystem.new("Sol") + +# earth = Planet.new("Earth", "red", 12345, 54321, "It's a Pepper!") +# solar_system.add_planet(earth) + +# # list = solar_system.list_planets +# # puts list + +# found_planet = solar_system.find_planet_by_name("Earth") + +# # found_planet is an instance of class Planet +# puts "#{found_planet}" +# # # => # + +# # puts found_planet[0].summary +# puts found_planet.summary From 3fd1a978e804cabb13df9f926c11b7d4c57e5343 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Tue, 26 Feb 2019 16:08:02 -0800 Subject: [PATCH 4/7] finished wave 3 --- main.rb | 74 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/main.rb b/main.rb index 10e9161f..d57b9fbf 100644 --- a/main.rb +++ b/main.rb @@ -6,40 +6,48 @@ # 1. Enter a control loop that repeatedly asks the user what to do next. The two options at this point are `list planets` and `exit`. def main - new_sol_sys = SolarSystem.new("Phunghoule") - bebop = Planet.new("Bebop", "gray", 5, 2.2, "Bebop's nickname is Mrs. Tubbins!") - ninou = Planet.new("Ninou", "black", 3, 2.2, "Ninou is the best planet ever!") - andrew = Planet.new("Andrew", "white", 91, 3, "Andrew is full of gas") - new_sol_sys.add_planet(bebop) - new_sol_sys.add_planet(ninou) - new_sol_sys.add_planet(andrew) - - puts "What do you want to do next? Type either LIST PLANETS, or EXIT." - input = gets.chomp.downcase - if input.downcase == "list planets" - puts new_sol_sys.list_planets - elsif input.downcase == "exit" - break - else - puts "What do you want to do next? Type either LIST PLANETS, or EXIT." + solar_system = SolarSystem.new("Phunghoule") + + bebop = Planet.new(name: "Bebop", color: "gray", mass_kg: 5, distance_from_sun_km: 2.2, fun_fact: "Bebop's nickname is Mrs. Tubbins!") + ninou = Planet.new(name: "Ninou", color: "black", mass_kg: 3, distance_from_sun_km: 2.2, fun_fact: "Ninou is the best planet ever!") + andrew = Planet.new(name: "Andrew", color: "white", mass_kg: 91, distance_from_sun_km: 3, fun_fact: "Andrew is full of gas") + + solar_system.add_planet(bebop) + solar_system.add_planet(ninou) + solar_system.add_planet(andrew) + + loop do + puts "Choose your next adventure: " + puts "\ntype LIST to see a list of all planets, " + puts "type LEARN to learn about the planets," + puts "type ADD to create your own planet, or" + puts "type EXIT to quit" + + selection = gets.chomp.downcase + + case selection + when "exit" + puts "Goodbye." + exit + when "list" + puts "#{solar_system.list_planets}" + when "learn" + case learn_choice + when "bebop" + puts "#{solar_system.planets[0].summary}" + when "ninou" + puts "#{solar_system.planets[1].summary}" + when "andrew" + puts "#{solar_system.planets[2].summary}" + else puts "Error: not a valid selection. Try again: " +learn_choice = gets.chomp.downcase + end + when "add" + solar_system.add_planet_details + puts "Thanks! Your planet has been added to the solar system: " + puts "#{solar_system.list_planets}" + end end end main - -# solar_system = SolarSystem.new("Sol") - -# earth = Planet.new("Earth", "red", 12345, 54321, "It's a Pepper!") -# solar_system.add_planet(earth) - -# # list = solar_system.list_planets -# # puts list - -# found_planet = solar_system.find_planet_by_name("Earth") - -# # found_planet is an instance of class Planet -# puts "#{found_planet}" -# # # => # - -# # puts found_planet[0].summary -# puts found_planet.summary From ea9b2841517f4d4e5077253b14e9099e9dc911de Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Tue, 26 Feb 2019 16:08:27 -0800 Subject: [PATCH 5/7] completed wave 3 --- planet.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/planet.rb b/planet.rb index 406c81f2..377f31d9 100644 --- a/planet.rb +++ b/planet.rb @@ -1,7 +1,7 @@ class Planet attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact - def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) + def initialize(name: "", color: "", mass_kg: 0, distance_from_sun_km: 0, fun_fact: "") @name = name @color = color @mass_kg = mass_kg @@ -10,10 +10,10 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "#{@name}: #{@color} in color, mass: #{@mass_kg}kg, distance from sun: #{@distance_from_sun_km}km, fun fact: #{@fun_fact}" + return "The planet, #{@name} is #{@color} in color, its mass is #{@mass_kg}kg, has a distance of #{@distance_from_sun_km}km from sun, fun fact: #{@fun_fact}" end end -ARIEL = Planet.new("Ariel", "blue-ish", 6455783, 308546069, "Ariel has an extensive network of medical-related industries.") +ARIEL = Planet.new(name: "Ariel", color: "blue-ish", mass_kg: 6455783, distance_from_sun_km: 308546069, fun_fact: "Ariel has an extensive network of medical-related industries.") -LONDINIUM = Planet.new("Londinium", "green-ish", 1236620, 1346380, "Londinium is where the Parliament is housed.") +LONDINIUM = Planet.new(name: "Londinium", color: "green-ish", mass_kg: 1236620, distance_from_sun_km: 1346380, fun_fact: "Londinium is where the Parliament is housed.") From f2f46ab3ad9fc5e15c4b59ca9ed7efd425fd6138 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Tue, 26 Feb 2019 16:08:44 -0800 Subject: [PATCH 6/7] finished wave 3 --- solar_system.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 solar_system.rb diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..f6e27723 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,47 @@ +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + def add_planet_details + puts "Enter the new planet's name: " + name = gets.chomp.capitalize + puts "Color of the planet: " + color = gets.chomp.downcase + puts "Mass in kilograms: " + mass = gets.chomp.to_i + puts "Distance from the sun in kg" + distance = gets.chomp.to_i + puts "A fun fact about the planet: " + fun_fact = gets.chomp.capitalize + + new_planet = Planet.new(name: "#{name}", color: "#{color}", mass_kg: mass, distance_from_sun_km: distance, fun_fact: "#{fun_fact}") + + add_planet(new_planet) + end + + def add_planet(planet) + @planets.push(planet) + return planet + end + + def list_planets + list_of_planets = "Planets orbiting #{@star_name}" + @planets.each_with_index do |planet, index| + list_of_planets += "\n#{index + 1}: #{planet.name}" + end + return list_of_planets + end + + def find_planet_by_name(planet_name) + @planets.each do |planet| + if planet.name.capitalize == planet_name.capitalize + return planet + end + end + return nil + end +end From c95379292aee350a8870fc6abd7cbd74072b5bdd Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Tue, 26 Feb 2019 17:01:48 -0800 Subject: [PATCH 7/7] refactored 'learn about planet code' to be dynamic --- main.rb | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/main.rb b/main.rb index d57b9fbf..57bce4f7 100644 --- a/main.rb +++ b/main.rb @@ -1,9 +1,6 @@ require_relative "planet" require_relative "solar_system" - -# Restructure `main` to do the following: -# 1. Create a `SolarSystem` and add some `Planet`s -# 1. Enter a control loop that repeatedly asks the user what to do next. The two options at this point are `list planets` and `exit`. +require "pry" def main solar_system = SolarSystem.new("Phunghoule") @@ -32,15 +29,15 @@ def main when "list" puts "#{solar_system.list_planets}" when "learn" - case learn_choice - when "bebop" - puts "#{solar_system.planets[0].summary}" - when "ninou" - puts "#{solar_system.planets[1].summary}" - when "andrew" - puts "#{solar_system.planets[2].summary}" - else puts "Error: not a valid selection. Try again: " -learn_choice = gets.chomp.downcase + puts "#{solar_system.list_planets}" + puts "Type the name of the planet you want to learn about: " + learn_choice = gets.chomp.capitalize! + + solar_system.planets.each_with_index do |planet, index| + puts "#{planet.name}" + if planet.name == learn_choice + puts "#{planet.summary}" + end end when "add" solar_system.add_planet_details