From f067120984a99258319c6ebef91601c94aadd883 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 18 Feb 2020 15:43:36 -0800 Subject: [PATCH 01/22] Wave 1: Create a class called Planet, create two instances of Planet, return summary --- lib/main.rb | 12 ++++++++++++ lib/planet.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 lib/main.rb create mode 100644 lib/planet.rb diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..57252a63 --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,12 @@ +require_relative "planet" + +def main + + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'the only planet known to support life') + mars = Planet.new('Mars', 'red', 6.39e23, 230.26e6, 'home to the largest volcano in the solar system, named Olympus Mons') + + return earth.summary, mars.summary +end + +puts main + diff --git a/lib/planet.rb b/lib/planet.rb new file mode 100644 index 00000000..9a386fe6 --- /dev/null +++ b/lib/planet.rb @@ -0,0 +1,17 @@ +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 + + raise ArgumentError.new("Mass and distance from sun must be greater than zero") if @mass_kg < 0.0000001 || @distance_from_sun_km < 0.0000001 + end + + def summary + return "Here's what we know about #{self.name}: It's #{self.color} with a mass of #{self.mass_kg} kg, and it is about #{self.distance_from_sun_km} km from the sun. #{self.name} is #{self.fun_fact}." + end +end \ No newline at end of file From 5a12584d7818061cb054c17eb83acfa76d99e81d Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 18 Feb 2020 15:47:08 -0800 Subject: [PATCH 02/22] Wave 2: Create solar_system.rb, create a new class SolarSystem --- lib/solar_system.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 lib/solar_system.rb diff --git a/lib/solar_system.rb b/lib/solar_system.rb new file mode 100644 index 00000000..b1a0272c --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,9 @@ +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = Array.new + @planet_num = 1 + end +end From 1d557767950e8da2ba756bc0849e7e4993bcc68d Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 18 Feb 2020 15:50:32 -0800 Subject: [PATCH 03/22] Wave 2: Create SolarSystem#add_planet method --- lib/solar_system.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index b1a0272c..98ce4d02 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -6,4 +6,9 @@ def initialize(star_name) @planets = Array.new @planet_num = 1 end + + # take an instance of Planet as a parameter and add it to the list of planets + def add_planet(planet) + @planets << planet.name + end end From d5ae82341224376d7e229c108e4927a88c69d576 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 18 Feb 2020 16:01:09 -0800 Subject: [PATCH 04/22] Wave 2: Create a method SolarSystem#list_planets --- lib/solar_system.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 98ce4d02..859d2f24 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -9,6 +9,12 @@ def initialize(star_name) # take an instance of Planet as a parameter and add it to the list of planets def add_planet(planet) - @planets << planet.name + @planets << "#{@planet_num}. #{planet.name}" + @planet_num += 1 + end + + # return (not puts) a string containing a list of all the planets in the system + def list_planets + return "Planets orbiting #{@star_name}:\n #{@planets}" end end From 968270d85036061b3210aeb91ebe884440805e15 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 18 Feb 2020 16:20:43 -0800 Subject: [PATCH 05/22] Wave 2: Update driver code in main.rb to create an instance of SolarSystem, add Planets to it, print the list --- lib/main.rb | 12 +++++++++--- lib/solar_system.rb | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 57252a63..195e8970 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,12 +1,18 @@ require_relative "planet" +require_relative "solar_system" def main + solar_system = SolarSystem.new("Sol") earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'the only planet known to support life') + solar_system.add_planet(earth) + mars = Planet.new('Mars', 'red', 6.39e23, 230.26e6, 'home to the largest volcano in the solar system, named Olympus Mons') + solar_system.add_planet(mars) - return earth.summary, mars.summary -end + list = solar_system.list_planets -puts main + return earth.summary, mars.summary, list +end +puts main \ No newline at end of file diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 859d2f24..2bead71d 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -15,6 +15,6 @@ def add_planet(planet) # return (not puts) a string containing a list of all the planets in the system def list_planets - return "Planets orbiting #{@star_name}:\n #{@planets}" + return "Planets orbiting #{@star_name}:\n", @planets end end From 0e95b50a06eb3f094b80de42a401a638ff59700e Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 18 Feb 2020 16:21:28 -0800 Subject: [PATCH 06/22] Add a newline between each planet summary --- lib/planet.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/planet.rb b/lib/planet.rb index 9a386fe6..d4b7c5b0 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -12,6 +12,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "Here's what we know about #{self.name}: It's #{self.color} with a mass of #{self.mass_kg} kg, and it is about #{self.distance_from_sun_km} km from the sun. #{self.name} is #{self.fun_fact}." + return "Here's what we know about #{self.name}: It's #{self.color} with a mass of #{self.mass_kg} kg, and it is about #{self.distance_from_sun_km} km from the sun. #{self.name} is #{self.fun_fact}.\n\n" end end \ No newline at end of file From 84c20c7e5bb2246acbb3b7b9aaefcecc5c34257e Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 18 Feb 2020 22:28:45 -0800 Subject: [PATCH 07/22] Added more instances of Planet, return summaries --- lib/main.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 195e8970..10a157af 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -4,15 +4,33 @@ def main solar_system = SolarSystem.new("Sol") + mercury = Planet.new('Mercury', 'red', 0.330e24, 57e6, 'the smallest planet in our Solar System') + solar_system.add_planet(mercury) + + venus = Planet.new('Venus', 'yellow', 4.87e24, 108e6, 'the hottest planet in our solar system') + solar_system.add_planet(venus) + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'the only planet known to support life') solar_system.add_planet(earth) - mars = Planet.new('Mars', 'red', 6.39e23, 230.26e6, 'home to the largest volcano in the solar system, named Olympus Mons') + mars = Planet.new('Mars', 'red', 6.39e23, 230.26e6, 'home to the largest volcano in our solar system, named Olympus Mons') solar_system.add_planet(mars) + jupiter = Planet.new('Jupiter', 'blue-red', 1.898e27, 817e6, 'the largest planet in our solar system') + solar_system.add_planet(jupiter) + + saturn = Planet.new('Saturn', 'pink', 5.69e26, 1.5e9, 'a planet with rings, which are made primarily of "water ice" mixed with dust and other chemicals') + solar_system.add_planet(saturn) + + uranus = Planet.new('Uranus', 'blue', 8.68e25, 3e9, 'the coldest planet in our solar system') + solar_system.add_planet(uranus) + + neptune = Planet.new('Neptune', 'dark blue', 1.02e26, 4.5e9, 'a planet with a distinct blue color, which is due to the absorption of red light by methane in the atmosphere') + solar_system.add_planet(neptune) + list = solar_system.list_planets - return earth.summary, mars.summary, list + return mercury.summary, venus.summary, earth.summary, mars.summary, jupiter.summary, saturn.summary, uranus.summary, neptune.summary, list end puts main \ No newline at end of file From 0b9f220ccdc12919024e70d88d4b1897549b1d2d Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Tue, 18 Feb 2020 22:32:01 -0800 Subject: [PATCH 08/22] Edited the summary method --- lib/planet.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/planet.rb b/lib/planet.rb index d4b7c5b0..41c5f6ea 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -12,6 +12,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "Here's what we know about #{self.name}: It's #{self.color} with a mass of #{self.mass_kg} kg, and it is about #{self.distance_from_sun_km} km from the sun. #{self.name} is #{self.fun_fact}.\n\n" + return "#{self.name} is a #{self.color} planet with a mass of #{self.mass_kg} kg and a distance from the sun of #{self.distance_from_sun_km} km. #{self.name} is #{self.fun_fact}.\n\n" end end \ No newline at end of file From 4a8199201d5c7356170721c8ffad9d469f86a2d3 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 11:25:44 -0800 Subject: [PATCH 09/22] Wave 2: Refactored the add_planet and list_planets methods --- lib/solar_system.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 2bead71d..bc9fa457 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -4,17 +4,25 @@ class SolarSystem def initialize(star_name) @star_name = star_name @planets = Array.new - @planet_num = 1 end # take an instance of Planet as a parameter and add it to the list of planets def add_planet(planet) - @planets << "#{@planet_num}. #{planet.name}" - @planet_num += 1 + @planets << planet end # return (not puts) a string containing a list of all the planets in the system def list_planets - return "Planets orbiting #{@star_name}:\n", @planets + return "Planets orbiting #{@star_name}:\n", + @planets.each_with_index.map do |planet, i| + "#{i+1}. #{planet.name}" + end + end + + # Create a method SolarSystem#find_planet_by_name, that takes the name of a planet as a string, and returns the corresponding instance of Planet. The lookup should be case-insensitive, so that Earth, earth and eArTh all return the same planet. + def find_planet_by_name(planet) + raise ArgumentError.new("Argument must be a string") unless planet.is_a? String + + # return planet if planet.downcase.instance_of? Planet end end From 58e9befdc4c9f958af9913caa2d543d3f01f8ad4 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 11:38:09 -0800 Subject: [PATCH 10/22] Wave 2: Fixed the find_planet_by_name method, returns planet.summary --- lib/main.rb | 4 +++- lib/solar_system.rb | 10 ++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 10a157af..8c629289 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -30,7 +30,9 @@ def main list = solar_system.list_planets - return mercury.summary, venus.summary, earth.summary, mars.summary, jupiter.summary, saturn.summary, uranus.summary, neptune.summary, list + found_planet = solar_system.find_planet_by_name('Earth') + + return mercury.summary, venus.summary, earth.summary, mars.summary, jupiter.summary, saturn.summary, uranus.summary, neptune.summary, list, found_planet end puts main \ No newline at end of file diff --git a/lib/solar_system.rb b/lib/solar_system.rb index bc9fa457..31f5b6d9 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -20,9 +20,11 @@ def list_planets end # Create a method SolarSystem#find_planet_by_name, that takes the name of a planet as a string, and returns the corresponding instance of Planet. The lookup should be case-insensitive, so that Earth, earth and eArTh all return the same planet. - def find_planet_by_name(planet) - raise ArgumentError.new("Argument must be a string") unless planet.is_a? String - - # return planet if planet.downcase.instance_of? Planet + def find_planet_by_name(planet_name) + raise ArgumentError.new("Argument must be a string") unless planet_name.is_a? String + + @planets.each do |planet| + return planet.summary if planet.name.downcase == planet_name.downcase + end end end From b7ae21f6e285338b548586739aa3d12cc624d718 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 12:26:59 -0800 Subject: [PATCH 11/22] Wave 2: Fixed the find_planet_by_name method so it returns the whole instance instead of the summary --- lib/solar_system.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 31f5b6d9..92f08fb5 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -24,7 +24,7 @@ def find_planet_by_name(planet_name) raise ArgumentError.new("Argument must be a string") unless planet_name.is_a? String @planets.each do |planet| - return planet.summary if planet.name.downcase == planet_name.downcase + return planet if planet.name.downcase == planet_name.downcase end end end From 474ffbf78eed3bdb69b97fd0049eb3a912058cfd Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 13:03:42 -0800 Subject: [PATCH 12/22] Clean up comments --- lib/solar_system.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 92f08fb5..83bf63a5 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -6,20 +6,17 @@ def initialize(star_name) @planets = Array.new end - # take an instance of Planet as a parameter and add it to the list of planets def add_planet(planet) @planets << planet end - # return (not puts) a string containing a list of all the planets in the system def list_planets - return "Planets orbiting #{@star_name}:\n", + return "\nPlanets orbiting #{@star_name}:\n", @planets.each_with_index.map do |planet, i| "#{i+1}. #{planet.name}" end end - # Create a method SolarSystem#find_planet_by_name, that takes the name of a planet as a string, and returns the corresponding instance of Planet. The lookup should be case-insensitive, so that Earth, earth and eArTh all return the same planet. def find_planet_by_name(planet_name) raise ArgumentError.new("Argument must be a string") unless planet_name.is_a? String @@ -27,4 +24,4 @@ def find_planet_by_name(planet_name) return planet if planet.name.downcase == planet_name.downcase end end -end +end \ No newline at end of file From 0cd29988bc11d0b8d40cbdd000429471e5039394 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 13:05:02 -0800 Subject: [PATCH 13/22] Wave 3: Restructure main to create a SolarSystem, add some Planets, enter a control loop --- lib/main.rb | 21 +++++++++++++++------ lib/planet.rb | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 8c629289..b7c75468 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -28,11 +28,20 @@ def main neptune = Planet.new('Neptune', 'dark blue', 1.02e26, 4.5e9, 'a planet with a distinct blue color, which is due to the absorption of red light by methane in the atmosphere') solar_system.add_planet(neptune) - list = solar_system.list_planets - - found_planet = solar_system.find_planet_by_name('Earth') - - return mercury.summary, venus.summary, earth.summary, mars.summary, jupiter.summary, saturn.summary, uranus.summary, neptune.summary, list, found_planet + puts "Welcome to the Solar System Explorer!" + + loop do + puts "\nWhat would you like to do? \n1) list planets \n2) exit" + option = gets.chomp.downcase + + case option + when "1", "list planets" + list = solar_system.list_planets + puts list + when "2", "exit" + break + end + end end -puts main \ No newline at end of file +main \ No newline at end of file diff --git a/lib/planet.rb b/lib/planet.rb index 41c5f6ea..6a34e7be 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -12,6 +12,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "#{self.name} is a #{self.color} planet with a mass of #{self.mass_kg} kg and a distance from the sun of #{self.distance_from_sun_km} km. #{self.name} is #{self.fun_fact}.\n\n" + return "\n#{self.name} is a #{self.color} planet with a mass of #{self.mass_kg} kg and a distance from the sun of #{self.distance_from_sun_km} km. #{self.name} is #{self.fun_fact}.\n" end end \ No newline at end of file From 7d943a7f137afecd6261a069a1cf8fcd96abe541 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 13:10:34 -0800 Subject: [PATCH 14/22] Wave 3: Add a planet details option to the control loop --- lib/main.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index b7c75468..3d1e4422 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -31,14 +31,18 @@ def main puts "Welcome to the Solar System Explorer!" loop do - puts "\nWhat would you like to do? \n1) list planets \n2) exit" + puts "\nWhat would you like to do? \n1) list planets \n2) planet details \n3) exit" option = gets.chomp.downcase case option when "1", "list planets" list = solar_system.list_planets puts list - when "2", "exit" + when "2", "planet details" + puts "\nWhat planet would you like to learn about?" + found_planet = solar_system.find_planet_by_name(gets.chomp) + puts found_planet.summary + when "3", "exit" break end end From ae58a2f7d1a44335e89828321a55cf83d3f54dd7 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 13:33:02 -0800 Subject: [PATCH 15/22] Wave 3: Add an add planet option to the control loop --- lib/main.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 3d1e4422..f3737ed1 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -31,7 +31,7 @@ def main puts "Welcome to the Solar System Explorer!" loop do - puts "\nWhat would you like to do? \n1) list planets \n2) planet details \n3) exit" + puts "\nWhat would you like to do? \n1) list planets \n2) planet details \n3) add planet \n4) exit" option = gets.chomp.downcase case option @@ -40,9 +40,22 @@ def main puts list when "2", "planet details" puts "\nWhat planet would you like to learn about?" - found_planet = solar_system.find_planet_by_name(gets.chomp) - puts found_planet.summary - when "3", "exit" + puts solar_system.find_planet_by_name(gets.chomp).summary + when "3", "add planet" + puts "Please enter the planet's name:" + name = gets.chomp + puts "Please enter the planet's color:" + color = gets.chomp + puts "Please enter the planet's mass (kg):" + mass_kg = gets.chomp.to_i + puts "Please enter the planet's distance from the sun (km):" + distance_from_sun_km = gets.chomp.to_i + puts "Please enter a fun fact about the planet:" + fun_fact = gets.chomp + new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) + solar_system.add_planet(new_planet) + puts "\nThank you! #{name} has been added to #{solar_system.star_name}." + when "4", "exit" break end end From 4bc5da3d1b800cc4cc2b95bfeb6671818c18118a Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 13:56:29 -0800 Subject: [PATCH 16/22] Wave 3: Change attr_reader to attr_accessor --- lib/planet.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/planet.rb b/lib/planet.rb index 6a34e7be..07030a06 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -1,5 +1,5 @@ class Planet - attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + attr_accessor :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @name = name From 46e34b6eb7217b202f1e8a01168fbad334d06593 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 13:57:09 -0800 Subject: [PATCH 17/22] Optionals: Create test file and Rakefile --- Rakefile | 7 +++++++ lib/main.rb | 3 +-- test/planet_test.rb | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 Rakefile create mode 100644 test/planet_test.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..7d9d432f --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +#!/usr/bin/ruby -wKU + +task :default => :run + +task :run do + ruby 'test/planet_test.rb' +end \ No newline at end of file diff --git a/lib/main.rb b/lib/main.rb index f3737ed1..365ff7c2 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -36,8 +36,7 @@ def main case option when "1", "list planets" - list = solar_system.list_planets - puts list + puts solar_system.list_planets when "2", "planet details" puts "\nWhat planet would you like to learn about?" puts solar_system.find_planet_by_name(gets.chomp).summary diff --git a/test/planet_test.rb b/test/planet_test.rb new file mode 100644 index 00000000..4d52e3aa --- /dev/null +++ b/test/planet_test.rb @@ -0,0 +1,22 @@ +require 'minitest' +require 'minitest/spec' +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/pride' +require 'pry' + +require_relative '../lib/main' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe "create two different instances of the Planet class" do + it "raises an ArgumentError if mass_kg is not greater than zero" do + planet = Planet.new('Earth', 'blue-green', 0, 1.496e8, 'the only planet known to support life') + expect(planet).must_raise ArgumentError + end + + it "raises an ArgumentError if distance_from_sun_km is not greater than zero" do + planet = Planet.new('Earth', 'blue-green', 5.972e24, 0, 'the only planet known to support life') + expect(planet).must_raise ArgumentError + end +end \ No newline at end of file From a9d66f4aa59f35408b7a588159c03c84d2a19a3b Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 16:35:13 -0800 Subject: [PATCH 18/22] Clean up the printing of the fun facts --- lib/main.rb | 18 +++++++++--------- lib/planet.rb | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 365ff7c2..eece0370 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -4,28 +4,28 @@ def main solar_system = SolarSystem.new("Sol") - mercury = Planet.new('Mercury', 'red', 0.330e24, 57e6, 'the smallest planet in our Solar System') + mercury = Planet.new('Mercury', 'red', 0.330e24, 57e6, 'Mercury is the smallest planet in our Solar System') solar_system.add_planet(mercury) - venus = Planet.new('Venus', 'yellow', 4.87e24, 108e6, 'the hottest planet in our solar system') + venus = Planet.new('Venus', 'yellow', 4.87e24, 108e6, 'Venus is the hottest planet in our solar system') solar_system.add_planet(venus) - earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'the only planet known to support life') + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Earth is the only planet known to support life') solar_system.add_planet(earth) - mars = Planet.new('Mars', 'red', 6.39e23, 230.26e6, 'home to the largest volcano in our solar system, named Olympus Mons') + mars = Planet.new('Mars', 'red', 6.39e23, 230.26e6, 'Mars is home to the largest volcano in our solar system, named Olympus Mons') solar_system.add_planet(mars) - jupiter = Planet.new('Jupiter', 'blue-red', 1.898e27, 817e6, 'the largest planet in our solar system') + jupiter = Planet.new('Jupiter', 'blue-red', 1.898e27, 817e6, 'Jupiter is the largest planet in our solar system') solar_system.add_planet(jupiter) - saturn = Planet.new('Saturn', 'pink', 5.69e26, 1.5e9, 'a planet with rings, which are made primarily of "water ice" mixed with dust and other chemicals') + saturn = Planet.new('Saturn', 'pink', 5.69e26, 1.5e9, 'Saturn\'s rings are made primarily of "water ice" mixed with dust and other chemicals') solar_system.add_planet(saturn) - uranus = Planet.new('Uranus', 'blue', 8.68e25, 3e9, 'the coldest planet in our solar system') + uranus = Planet.new('Uranus', 'blue', 8.68e25, 3e9, 'Uranus is the coldest planet in our solar system') solar_system.add_planet(uranus) - neptune = Planet.new('Neptune', 'dark blue', 1.02e26, 4.5e9, 'a planet with a distinct blue color, which is due to the absorption of red light by methane in the atmosphere') + neptune = Planet.new('Neptune', 'dark blue', 1.02e26, 4.5e9, 'Neptune is a planet with a distinct blue color, which is due to the absorption of red light by methane in the atmosphere') solar_system.add_planet(neptune) puts "Welcome to the Solar System Explorer!" @@ -42,7 +42,7 @@ def main puts solar_system.find_planet_by_name(gets.chomp).summary when "3", "add planet" puts "Please enter the planet's name:" - name = gets.chomp + name = gets.chomp.capitalize puts "Please enter the planet's color:" color = gets.chomp puts "Please enter the planet's mass (kg):" diff --git a/lib/planet.rb b/lib/planet.rb index 07030a06..587fe392 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -12,6 +12,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "\n#{self.name} is a #{self.color} planet with a mass of #{self.mass_kg} kg and a distance from the sun of #{self.distance_from_sun_km} km. #{self.name} is #{self.fun_fact}.\n" + return "\n#{self.name} is a #{self.color} planet with a mass of #{self.mass_kg} kg and a distance from the sun of #{self.distance_from_sun_km} km. #{self.fun_fact}.\n" end end \ No newline at end of file From 460c5afb425f787b98f7ee012a52bea69be61a10 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 22:41:51 -0800 Subject: [PATCH 19/22] Wave 3: Refactor, add separate methods for the planet details and add planet options --- lib/main.rb | 62 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index eece0370..7024d561 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,33 +1,58 @@ -require_relative "planet" -require_relative "solar_system" +require_relative 'planet' +require_relative 'solar_system' def main solar_system = SolarSystem.new("Sol") - mercury = Planet.new('Mercury', 'red', 0.330e24, 57e6, 'Mercury is the smallest planet in our Solar System') + mercury = Planet.new("Mercury", "red", 0.330e24, 57e6, "Mercury is the smallest planet in our Solar System") solar_system.add_planet(mercury) - venus = Planet.new('Venus', 'yellow', 4.87e24, 108e6, 'Venus is the hottest planet in our solar system') + venus = Planet.new("Venus", "yellow", 4.87e24, 108e6, "Venus is the hottest planet in our solar system") solar_system.add_planet(venus) - earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Earth is the only planet known to support life') + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Earth is the only planet known to support life") solar_system.add_planet(earth) - mars = Planet.new('Mars', 'red', 6.39e23, 230.26e6, 'Mars is home to the largest volcano in our solar system, named Olympus Mons') + mars = Planet.new("Mars", "red", 6.39e23, 230.26e6, "Mars is home to the largest volcano in our solar system, named Olympus Mons") solar_system.add_planet(mars) - jupiter = Planet.new('Jupiter', 'blue-red', 1.898e27, 817e6, 'Jupiter is the largest planet in our solar system') + jupiter = Planet.new("Jupiter", "blue-red", 1.898e27, 817e6, "Jupiter is the largest planet in our solar system") solar_system.add_planet(jupiter) - saturn = Planet.new('Saturn', 'pink', 5.69e26, 1.5e9, 'Saturn\'s rings are made primarily of "water ice" mixed with dust and other chemicals') + saturn = Planet.new("Saturn", "pink", 5.69e26, 1.5e9, "Saturn\'s rings are made primarily of "water ice" mixed with dust and other chemicals") solar_system.add_planet(saturn) - uranus = Planet.new('Uranus', 'blue', 8.68e25, 3e9, 'Uranus is the coldest planet in our solar system') + uranus = Planet.new("Uranus", "blue", 8.68e25, 3e9, "Uranus is the coldest planet in our solar system") solar_system.add_planet(uranus) - neptune = Planet.new('Neptune', 'dark blue', 1.02e26, 4.5e9, 'Neptune is a planet with a distinct blue color, which is due to the absorption of red light by methane in the atmosphere') + neptune = Planet.new("Neptune", "dark blue", 1.02e26, 4.5e9, "Neptune is a planet with a distinct blue color, which is due to the absorption of red light by methane in the atmosphere") solar_system.add_planet(neptune) + def planet_details_ui(solar_system) + puts "\nWhat planet would you like to learn about?" + found_planet = solar_system.find_planet_by_name(gets.chomp) + return found_planet.summary + end + + def add_planet_ui(solar_system) + puts "\nPlease enter the planet's name:" + name = gets.chomp.capitalize + puts "Please enter the planet's color:" + color = gets.chomp + puts "Please enter the planet's mass (kg):" + mass_kg = gets.chomp.to_i + puts "Please enter the planet's distance from the sun (km):" + distance_from_sun_km = gets.chomp.to_i + puts "Please enter a fun fact about the planet:" + fun_fact = gets.chomp + + # The following line assigns each new planet to the variable new_planet, which is not ideal. I tried looking into how to dynamically set variable names in Ruby, but it seems like this functionality is too complex than it's worth here. + new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) + solar_system.add_planet(new_planet) + + return "\nThank you! #{name} has been added to #{solar_system.star_name}." + end + puts "Welcome to the Solar System Explorer!" loop do @@ -38,22 +63,9 @@ def main when "1", "list planets" puts solar_system.list_planets when "2", "planet details" - puts "\nWhat planet would you like to learn about?" - puts solar_system.find_planet_by_name(gets.chomp).summary + puts planet_details_ui(solar_system) when "3", "add planet" - puts "Please enter the planet's name:" - name = gets.chomp.capitalize - puts "Please enter the planet's color:" - color = gets.chomp - puts "Please enter the planet's mass (kg):" - mass_kg = gets.chomp.to_i - puts "Please enter the planet's distance from the sun (km):" - distance_from_sun_km = gets.chomp.to_i - puts "Please enter a fun fact about the planet:" - fun_fact = gets.chomp - new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) - solar_system.add_planet(new_planet) - puts "\nThank you! #{name} has been added to #{solar_system.star_name}." + puts add_planet_ui(solar_system) when "4", "exit" break end From ce0a0429f5bf6df5610006e262636177b12b7bf2 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 22:46:27 -0800 Subject: [PATCH 20/22] Fixed error, escape quote on line 22 --- lib/main.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index 7024d561..8b78b6b8 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -19,7 +19,7 @@ def main jupiter = Planet.new("Jupiter", "blue-red", 1.898e27, 817e6, "Jupiter is the largest planet in our solar system") solar_system.add_planet(jupiter) - saturn = Planet.new("Saturn", "pink", 5.69e26, 1.5e9, "Saturn\'s rings are made primarily of "water ice" mixed with dust and other chemicals") + saturn = Planet.new("Saturn", "pink", 5.69e26, 1.5e9, "Saturn\'s rings are made primarily of \"water ice\" mixed with dust and other chemicals") solar_system.add_planet(saturn) uranus = Planet.new("Uranus", "blue", 8.68e25, 3e9, "Uranus is the coldest planet in our solar system") From 6d9a9cb39a6e03ccfa22f9b46bdd5718451bc280 Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Wed, 19 Feb 2020 22:52:53 -0800 Subject: [PATCH 21/22] Optionals: Unsuccessful attempt at adding minitests for Planet --- test/planet_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/planet_test.rb b/test/planet_test.rb index 4d52e3aa..46181a9a 100644 --- a/test/planet_test.rb +++ b/test/planet_test.rb @@ -11,12 +11,12 @@ describe "create two different instances of the Planet class" do it "raises an ArgumentError if mass_kg is not greater than zero" do - planet = Planet.new('Earth', 'blue-green', 0, 1.496e8, 'the only planet known to support life') - expect(planet).must_raise ArgumentError + earth = Planet.new("Earth", "blue-green", 0, 1.496e8, "Earth is the only planet known to support life") + expect(earth).must_raise ArgumentError end it "raises an ArgumentError if distance_from_sun_km is not greater than zero" do - planet = Planet.new('Earth', 'blue-green', 5.972e24, 0, 'the only planet known to support life') - expect(planet).must_raise ArgumentError + earth = Planet.new("Earth", "blue-green", 5.972e24, 0, "Earth is the only planet known to support life") + expect(earth).must_raise ArgumentError end end \ No newline at end of file From 139e4ba5ad97e5c925c0bd44629b24f0875041ed Mon Sep 17 00:00:00 2001 From: saraleecodes Date: Thu, 20 Feb 2020 10:16:57 -0800 Subject: [PATCH 22/22] Changed attr_accessor to attr_reader --- lib/planet.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/planet.rb b/lib/planet.rb index 587fe392..16f1a2b0 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -1,5 +1,5 @@ class Planet - attr_accessor :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + 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