From 1319449db3bfd7ebcf955d981e672cc3b21f3458 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 13:39:14 -0800 Subject: [PATCH 01/18] Added planet file --- planet.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 planet.rb diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..e69de29b From 4012ff1ea5f32889a9a15c0375841463662e53e5 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 13:52:55 -0800 Subject: [PATCH 02/18] Created Planet class with construction with 5 parameters --- planet.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/planet.rb b/planet.rb index e69de29b..68ba9d27 100644 --- a/planet.rb +++ b/planet.rb @@ -0,0 +1,10 @@ +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 + @mass_kg = mass_kg + @distance_from_sun_km = distance_from_sun_km + @fun_fact = fun_fact + end +end From eb5ac4282766b64903eb1a360387b2630d249254 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 14:08:01 -0800 Subject: [PATCH 03/18] Added main file with main method to instantiate two planets and added color to initialized variables in planet.rb --- main.rb | 12 ++++++++++++ planet.rb | 1 + 2 files changed, 13 insertions(+) create mode 100644 main.rb diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..feafa647 --- /dev/null +++ b/main.rb @@ -0,0 +1,12 @@ +require_relative "planet" + +def main + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") + venus = Planet.new("Venus", "red-brown", 4.867e24, 1.082e8, "Both the morning star and the evening star") + + puts earth.name + puts venus.color + puts venus.fun_fact +end + +main diff --git a/planet.rb b/planet.rb index 68ba9d27..a29edb2f 100644 --- a/planet.rb +++ b/planet.rb @@ -3,6 +3,7 @@ class Planet 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 From d2af4eb5fc53b004038786d98ec3cb2fd3bb5498 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 14:20:56 -0800 Subject: [PATCH 04/18] Added summary method to Planet class --- planet.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/planet.rb b/planet.rb index a29edb2f..9ad62ed2 100644 --- a/planet.rb +++ b/planet.rb @@ -8,4 +8,8 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @distance_from_sun_km = distance_from_sun_km @fun_fact = fun_fact end + + def summary + return "#{@name} is #{@color}, weighs #{@mass_kg} kg, and is #{@distance_from_sun_km} km from the sun. It is #{@fun_fact.downcase}." + end end From 259424e7f7c78817b111534f4fcf4dfe077ba3a9 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 14:22:14 -0800 Subject: [PATCH 05/18] Updated main method to use summary method from planet file --- main.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main.rb b/main.rb index feafa647..a77a61ca 100644 --- a/main.rb +++ b/main.rb @@ -4,9 +4,8 @@ def main earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") venus = Planet.new("Venus", "red-brown", 4.867e24, 1.082e8, "Both the morning star and the evening star") - puts earth.name - puts venus.color - puts venus.fun_fact + puts earth.summary + puts venus.summary end main From 4e282bfb26385abf4fd0654d6d548567f399c9ac Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 14:44:34 -0800 Subject: [PATCH 06/18] Added solar system file that contains solarsystem method --- solar_system.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 solar_system.rb diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..e69de29b From 39f3a35355dfe9a7067fe0ece6fb0f455f34f11e Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 15:18:57 -0800 Subject: [PATCH 07/18] Added add_planet method to Solar System planet --- solar_system.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/solar_system.rb b/solar_system.rb index e69de29b..4bc939bf 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -0,0 +1,25 @@ +require_relative "planet" + +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name, planets) + @star_name = star_name + @planets = [] + end + + def add_planet(planet) + @planets.push(planet) + end +end + +earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") +venus = Planet.new("Venus", "red-brown", 4.867e24, 1.082e8, "Both the morning star and the evening star") + +sun = SolarSystem.new("sun", nil) + +sun.add_planet(earth) +sun.add_planet(venus) + +puts sun.star_name +puts sun.planets From 8fc2c16e69f367b34b6e88cdba71acf99c8765d3 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 15:41:42 -0800 Subject: [PATCH 08/18] Added list_planets method to solar_system.rb and deleted test code --- solar_system.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/solar_system.rb b/solar_system.rb index 4bc939bf..32656efe 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -11,6 +11,16 @@ def initialize(star_name, planets) def add_planet(planet) @planets.push(planet) end + + def list_planets + planet_list = "Planets orbiting #{star_name}: " + i = 0 + @planets.each do + planet_list += "\n#{i + 1}. #{@planets[i].name}" + i += 1 + end + return planet_list + end end earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") @@ -21,5 +31,4 @@ def add_planet(planet) sun.add_planet(earth) sun.add_planet(venus) -puts sun.star_name -puts sun.planets +puts sun.list_planets From 2a0a01653ad4bcf33c8d250e6a722a8e1f113d24 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 16:34:35 -0800 Subject: [PATCH 09/18] Updated SolarSystem to take one parameter and updated main method to create solar system instance and additional planets --- main.rb | 21 ++++++++++++++++++--- solar_system.rb | 12 +----------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/main.rb b/main.rb index a77a61ca..2e75bf7d 100644 --- a/main.rb +++ b/main.rb @@ -1,11 +1,26 @@ require_relative "planet" +require_relative "solar_system" def main - earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") + solar_system = SolarSystem.new("Sol") + + mercury = Planet.new("Mercury", "dark grey", 3.285e23, 5.791e7, "Named for the messenger of the Roman gods due to its orbital speed") + solar_system.add_planet(mercury) + venus = Planet.new("Venus", "red-brown", 4.867e24, 1.082e8, "Both the morning star and the evening star") + solar_system.add_planet(venus) + + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") + solar_system.add_planet(earth) + + mars = Planet.new("Mars", "red", 6.4169e23, 2.279e8, "Home to the tallest mountain in the solar system") + solar_system.add_planet(mars) + + jupiter = Planet.new("Jupiter", "red, white, and brown", 1.898e27, 7.785e8, "The planet with the shortest day in the solar system") + solar_system.add_planet(jupiter) - puts earth.summary - puts venus.summary + list = solar_system.list_planets + puts list end main diff --git a/solar_system.rb b/solar_system.rb index 32656efe..a64e1ed9 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -3,7 +3,7 @@ class SolarSystem attr_reader :star_name, :planets - def initialize(star_name, planets) + def initialize(star_name) @star_name = star_name @planets = [] end @@ -22,13 +22,3 @@ def list_planets return planet_list end end - -earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") -venus = Planet.new("Venus", "red-brown", 4.867e24, 1.082e8, "Both the morning star and the evening star") - -sun = SolarSystem.new("sun", nil) - -sun.add_planet(earth) -sun.add_planet(venus) - -puts sun.list_planets From cd0d3e7c9ecbf63c354efdc52f928c42d11215bf Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 20:07:03 -0800 Subject: [PATCH 10/18] Added find_planet_by_name method and updated driver method --- main.rb | 3 +++ solar_system.rb | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/main.rb b/main.rb index 2e75bf7d..257d2fa7 100644 --- a/main.rb +++ b/main.rb @@ -21,6 +21,9 @@ def main list = solar_system.list_planets puts list + + found_planet = solar_system.find_planet_by_name("earth") + puts found_planet.summary end main diff --git a/solar_system.rb b/solar_system.rb index a64e1ed9..550c304d 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -21,4 +21,14 @@ def list_planets end return planet_list end + + def find_planet_by_name(name) + i = 0 + @planets.each do + if @planets[i].name.downcase == name.downcase + return @planets[i] + end + i += 1 + end + end end From ab4c51a85a06828b9ede94b0c2ee86b945d8d8f2 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 25 Feb 2019 22:38:04 -0800 Subject: [PATCH 11/18] Added control loop to main method --- main.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/main.rb b/main.rb index 257d2fa7..d4ac4b28 100644 --- a/main.rb +++ b/main.rb @@ -19,11 +19,16 @@ def main jupiter = Planet.new("Jupiter", "red, white, and brown", 1.898e27, 7.785e8, "The planet with the shortest day in the solar system") solar_system.add_planet(jupiter) - list = solar_system.list_planets - puts list - - found_planet = solar_system.find_planet_by_name("earth") - puts found_planet.summary + response = nil + while response != "exit" + puts "Please enter what you would like to do: " + response = gets.chomp + if response == "list planets" + puts solar_system.list_planets + end + puts "Please enter what you would like to do: " + response = gets.chomp + end end main From de1794f365d0555742548e898182099107c1a2bb Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Tue, 26 Feb 2019 11:48:36 -0800 Subject: [PATCH 12/18] Added planet details option to control loop in main method --- main.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main.rb b/main.rb index d4ac4b28..14bfe0fb 100644 --- a/main.rb +++ b/main.rb @@ -25,6 +25,11 @@ def main response = gets.chomp if response == "list planets" puts solar_system.list_planets + elsif response == "planet details" + puts "Please enter what planet you would like to learn about: " + planet_choice = gets.chomp + selected_planet = solar_system.find_planet_by_name(planet_choice) + puts selected_planet.summary end puts "Please enter what you would like to do: " response = gets.chomp From 3f9af58adfb73cd7820f69e20fe3a2006fb7efe5 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Tue, 26 Feb 2019 12:05:24 -0800 Subject: [PATCH 13/18] Added add planet option to control loop in main method --- main.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/main.rb b/main.rb index 14bfe0fb..923c2d31 100644 --- a/main.rb +++ b/main.rb @@ -30,6 +30,20 @@ def main planet_choice = gets.chomp selected_planet = solar_system.find_planet_by_name(planet_choice) puts selected_planet.summary + elsif response == "add planet" + puts "Please complete the following information for the planet you would like added: \nName: " + name = gets.chomp + puts "Color: " + color = gets.chomp + puts "Mass in kg: " + mass = gets.chomp + puts "Distance from sun in km: " + distance = gets.chomp + puts "Fun fact: " + fact = gets.chomp + new_planet = Planet.new(name, color, mass, distance, fact) + solar_system.add_planet(new_planet) + puts "Thank you for adding #{name} to the solar system!" end puts "Please enter what you would like to do: " response = gets.chomp From 83d3b94e3de5f1a407385377f088d7b663b5be1e Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Tue, 26 Feb 2019 12:32:14 -0800 Subject: [PATCH 14/18] Cleaned up main by creating user add planet method in solar system and fixed bugs in control loop in main --- main.rb | 19 +++---------------- solar_system.rb | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/main.rb b/main.rb index 923c2d31..98346401 100644 --- a/main.rb +++ b/main.rb @@ -19,10 +19,9 @@ def main jupiter = Planet.new("Jupiter", "red, white, and brown", 1.898e27, 7.785e8, "The planet with the shortest day in the solar system") solar_system.add_planet(jupiter) - response = nil + puts "Please enter what you would like to do: " + response = gets.chomp while response != "exit" - puts "Please enter what you would like to do: " - response = gets.chomp if response == "list planets" puts solar_system.list_planets elsif response == "planet details" @@ -31,19 +30,7 @@ def main selected_planet = solar_system.find_planet_by_name(planet_choice) puts selected_planet.summary elsif response == "add planet" - puts "Please complete the following information for the planet you would like added: \nName: " - name = gets.chomp - puts "Color: " - color = gets.chomp - puts "Mass in kg: " - mass = gets.chomp - puts "Distance from sun in km: " - distance = gets.chomp - puts "Fun fact: " - fact = gets.chomp - new_planet = Planet.new(name, color, mass, distance, fact) - solar_system.add_planet(new_planet) - puts "Thank you for adding #{name} to the solar system!" + solar_system.user_adds_planet end puts "Please enter what you would like to do: " response = gets.chomp diff --git a/solar_system.rb b/solar_system.rb index 550c304d..cde970cb 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -31,4 +31,21 @@ def find_planet_by_name(name) i += 1 end end + + def user_adds_planet + puts "Please complete the following information for the planet you would like added: \nName: " + name = gets.chomp + puts "Color: " + color = gets.chomp + puts "Mass in kg: " + mass = gets.chomp + puts "Distance from sun in km: " + distance = gets.chomp + puts "Fun fact: " + fact = gets.chomp + new_planet = Planet.new(name, color, mass, distance, fact) + self.add_planet(new_planet) + puts "Thank you for adding #{name} to the solar system!" + return new_planet + end end From 0f2ffc9d1e83bfecc6d1a734a8e640359e494a09 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Tue, 26 Feb 2019 13:49:29 -0800 Subject: [PATCH 15/18] Created distance between method and added it to the control loop in main --- main.rb | 7 +++++++ solar_system.rb | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/main.rb b/main.rb index 98346401..f73a5d3a 100644 --- a/main.rb +++ b/main.rb @@ -31,6 +31,13 @@ def main puts selected_planet.summary elsif response == "add planet" solar_system.user_adds_planet + elsif response == "find distance" + puts "Please enter the two planets you would like to find the distance between:" + planet1 = gets.chomp + planet2 = gets.chomp + planet1 = solar_system.find_planet_by_name(planet1) + planet2 = solar_system.find_planet_by_name(planet2) + puts solar_system.distance_between(planet1, planet2) end puts "Please enter what you would like to do: " response = gets.chomp diff --git a/solar_system.rb b/solar_system.rb index cde970cb..678deec4 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -48,4 +48,8 @@ def user_adds_planet puts "Thank you for adding #{name} to the solar system!" return new_planet end + + def distance_between(planet1, planet2) + return (planet1.distance_from_sun_km - planet2.distance_from_sun_km).abs + end end From fd98cd76d74b9042fe2fcfc55dbe2099715984ed Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Tue, 26 Feb 2019 21:28:46 -0800 Subject: [PATCH 16/18] Added find distance by input to solar system class --- solar_system.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/solar_system.rb b/solar_system.rb index 678deec4..51159947 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -52,4 +52,13 @@ def user_adds_planet def distance_between(planet1, planet2) return (planet1.distance_from_sun_km - planet2.distance_from_sun_km).abs end + + def distance_between_by_input + puts "Please enter the two planets you would like to find the distance between:" + print "Planet 1: " + planet1 = self.find_planet_by_name(gets.chomp) + print "Planet 2: " + planet2 = self.find_planet_by_name(gets.chomp) + return self.distance_between(planet1, planet2) + end end From 326322ea6a9c57f5577fe1ee6319abd4097f7110 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Tue, 26 Feb 2019 21:29:33 -0800 Subject: [PATCH 17/18] Cleaned up main.rb and removed main method --- main.rb | 72 ++++++++++++++++++++++++--------------------------------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/main.rb b/main.rb index f73a5d3a..a8dfda26 100644 --- a/main.rb +++ b/main.rb @@ -1,47 +1,35 @@ require_relative "planet" require_relative "solar_system" -def main - solar_system = SolarSystem.new("Sol") - - mercury = Planet.new("Mercury", "dark grey", 3.285e23, 5.791e7, "Named for the messenger of the Roman gods due to its orbital speed") - solar_system.add_planet(mercury) - - venus = Planet.new("Venus", "red-brown", 4.867e24, 1.082e8, "Both the morning star and the evening star") - solar_system.add_planet(venus) - - earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") - solar_system.add_planet(earth) - - mars = Planet.new("Mars", "red", 6.4169e23, 2.279e8, "Home to the tallest mountain in the solar system") - solar_system.add_planet(mars) - - jupiter = Planet.new("Jupiter", "red, white, and brown", 1.898e27, 7.785e8, "The planet with the shortest day in the solar system") - solar_system.add_planet(jupiter) - - puts "Please enter what you would like to do: " - response = gets.chomp - while response != "exit" - if response == "list planets" - puts solar_system.list_planets - elsif response == "planet details" - puts "Please enter what planet you would like to learn about: " - planet_choice = gets.chomp - selected_planet = solar_system.find_planet_by_name(planet_choice) - puts selected_planet.summary - elsif response == "add planet" - solar_system.user_adds_planet - elsif response == "find distance" - puts "Please enter the two planets you would like to find the distance between:" - planet1 = gets.chomp - planet2 = gets.chomp - planet1 = solar_system.find_planet_by_name(planet1) - planet2 = solar_system.find_planet_by_name(planet2) - puts solar_system.distance_between(planet1, planet2) - end - puts "Please enter what you would like to do: " - response = gets.chomp +solar_system = SolarSystem.new("Sol") +mercury = Planet.new("Mercury", "dark grey", 3.285e23, 5.791e7, "Named for the messenger of the Roman gods due to its orbital speed") +solar_system.add_planet(mercury) +venus = Planet.new("Venus", "red-brown", 4.867e24, 1.082e8, "Both the morning star and the evening star") +solar_system.add_planet(venus) +earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") +solar_system.add_planet(earth) +mars = Planet.new("Mars", "red", 6.4169e23, 2.279e8, "Home to the tallest mountain in the solar system") +solar_system.add_planet(mars) +jupiter = Planet.new("Jupiter", "red, white, and brown", 1.898e27, 7.785e8, "The planet with the shortest day in the solar system") +solar_system.add_planet(jupiter) + +puts "Hello, please see the the following options for this program. You can: \nA: List Planets \nB: List Planet Details \nC: Add Planet \nD: Find the Distance Between Two Planets" +puts "Please enter the corresponding for the action you would like to take. You can exit by entering 'exit'." +response = gets.chomp.downcase +while response != "exit" + if response == "a" + puts solar_system.list_planets + elsif response == "b" + puts "Please enter what planet you would like to learn about: " + planet_choice = solar_system.find_planet_by_name(gets.chomp) + puts planet_choice.summary + elsif response == "c" + solar_system.user_adds_planet + elsif response == "d" + puts solar_system.distance_between_by_input + else + puts "Please make sure to select from the choices above. You can exit be entering 'exit'." end + puts "\nPlease enter what you would like to do: \nA: List Planets \nB: List Planet Details \nC: Add Planet \nD: Find the Distance Between Two Planets" + response = gets.chomp end - -main From 91629ed6ee6210e53045a0040f6785474755bb39 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Tue, 26 Feb 2019 21:51:33 -0800 Subject: [PATCH 18/18] Small stylistic changes --- main.rb | 2 +- solar_system.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/main.rb b/main.rb index a8dfda26..fe0dea81 100644 --- a/main.rb +++ b/main.rb @@ -14,7 +14,7 @@ solar_system.add_planet(jupiter) puts "Hello, please see the the following options for this program. You can: \nA: List Planets \nB: List Planet Details \nC: Add Planet \nD: Find the Distance Between Two Planets" -puts "Please enter the corresponding for the action you would like to take. You can exit by entering 'exit'." +puts "Please enter the corresponding letter for the action you would like to take. You can exit by entering 'exit'." response = gets.chomp.downcase while response != "exit" if response == "a" diff --git a/solar_system.rb b/solar_system.rb index 51159947..35065c0b 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -34,7 +34,7 @@ def find_planet_by_name(name) def user_adds_planet puts "Please complete the following information for the planet you would like added: \nName: " - name = gets.chomp + name = gets.chomp.capitalize puts "Color: " color = gets.chomp puts "Mass in kg: "