From 565e099b19852644e88517de5ded6dcd18ce12ef Mon Sep 17 00:00:00 2001 From: Elise Pham Date: Mon, 25 Feb 2019 14:46:35 -0800 Subject: [PATCH 01/10] completed wave1 --- Rakefile | 9 +++++++++ lib/main.rb | 10 ++++++++++ lib/planet.rb | 23 +++++++++++++++++++++++ specs/planet_spec.rb | 28 ++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 Rakefile create mode 100644 lib/main.rb create mode 100644 lib/planet.rb create mode 100644 specs/planet_spec.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..f29f7c93 --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList['specs/*_spec.rb'] +end + +task default: :test \ No newline at end of file diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..ba1fb43b --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,10 @@ +require_relative 'planet.rb' + +def main + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') + puts earth.summary + puts mars.summary +end + +main \ No newline at end of file diff --git a/lib/planet.rb b/lib/planet.rb new file mode 100644 index 00000000..7b7498a0 --- /dev/null +++ b/lib/planet.rb @@ -0,0 +1,23 @@ +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) + if !mass_kg.is_a?(Numeric) || mass_kg < 0 + raise ArgumentError.new("Planet mass has to be a positive number") + end + + if !distance_from_sun_km.is_a?(Numeric) || distance_from_sun_km < 0 + raise ArgumentError.new("Distance from the sun has to be a postive number.") + end + + @name = name + @color = color + @mass_kg = mass_kg + @distance_from_sun_km = distance_from_sun_km + @fun_fact = fun_fact + end + + def summary + return "The #{@name} is #{@color}, weighs #{@mass_kg} kg, and is about #{@distance_from_sun_km} km from the sun. Fun fact: #{@fun_fact}" + end +end \ No newline at end of file diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb new file mode 100644 index 00000000..374255d0 --- /dev/null +++ b/specs/planet_spec.rb @@ -0,0 +1,28 @@ +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" + +require_relative "../lib/planet.rb" + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe "Planet" do + it "outputs a string when you invoke its summary method" do + mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') + + expect(mars.summary).must_be_instance_of String + end + + it "raises argument error if you input a negative number for the planet's mass" do + expect { + Planet.new('Mars', 'red', -1, 2.29e8, 'Mars has two moons called Phobos and Deimos') + }.must_raise ArgumentError + end + + it "raises argument error if you input a negative number for the planet's distance from the Sun" do + expect { + Planet.new('Mars', 'red', 6.4171e23, -2, 'Mars has two moons called Phobos and Deimos') + }.must_raise ArgumentError + end +end + From 922da08e979b1a58ffb10d82f7592066125db148 Mon Sep 17 00:00:00 2001 From: Elise Pham Date: Mon, 25 Feb 2019 21:16:18 -0800 Subject: [PATCH 02/10] Completed Wave2 --- lib/main.rb | 23 ++++++++++++++++++++-- lib/planet.rb | 2 +- lib/solar_system.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 lib/solar_system.rb diff --git a/lib/main.rb b/lib/main.rb index ba1fb43b..df07e049 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,10 +1,29 @@ require_relative 'planet.rb' +require_relative 'solar_system.rb' def main earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') - puts earth.summary - puts mars.summary + mercury = Planet.new('Mercury', 'white', 3.3022e23, 6.98169e7, 'Mercury is the smallest planet in the solar system') + jupiter = Planet.new('Jupiter', 'orange', 1.8982e27, 7.79e8, 'Jupiter is the largest planet in the solar system') + venus = Planet.new('Venus', 'yellow', 4.8675e24, 1.082e8, 'Venus has the longest rotation period of any planet in the Solar System') + # puts earth.summary + # puts mars.summary + + sun = SolarSystem.new("Sun") + sun.add_planet(earth) + sun.add_planet(mars) + sun.add_planet(mercury) + sun.add_planet(jupiter) + sun.add_planet(venus) + + list = sun.list_planets + puts list + + found_planet = sun.find_planet_by_name('Jupiter') + puts found_planet.summary + + puts "The distance between Earth and Mars is #{sun.distance_between('Jupiter', 'Mars')} km" end main \ No newline at end of file diff --git a/lib/planet.rb b/lib/planet.rb index 7b7498a0..69e35658 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -18,6 +18,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "The #{@name} is #{@color}, weighs #{@mass_kg} kg, and is about #{@distance_from_sun_km} km from the sun. Fun fact: #{@fun_fact}" + return "The #{name} is #{color}, weighs #{mass_kg} kg, and is about #{distance_from_sun_km} km from the sun. Fun fact: #{fun_fact}" end end \ No newline at end of file diff --git a/lib/solar_system.rb b/lib/solar_system.rb new file mode 100644 index 00000000..9a70ba9e --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,48 @@ +require_relative 'planet.rb' +require "set" + +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = Array.new + @name_set = Set.new + end + + def add_planet(planet) + if @name_set.include?(planet) + raise ArgumentError.new("Planet already exists") + end + + @planets << planet + @name_set << planet.name + end + + def list_planets + result = "Planets orbitting #{star_name}:" + list = "" + @planets.each_index { |i| + list += "\n#{i + 1}. #{@planets[i].name}" + } + return result + list + end + + def find_planet_by_name(search_name) + if !@name_set.include?(search_name) + raise ArgumentError.new("There is no such planet") + end + + @planets.each do |element| + if element.name == search_name.capitalize + return element + end + end + end + + def distance_between(planet1, planet2) + distance = find_planet_by_name(planet1).distance_from_sun_km - find_planet_by_name(planet2).distance_from_sun_km + return distance.abs + end +end + From b8967b8d582c36455464af6f75cf26d3f480d31f Mon Sep 17 00:00:00 2001 From: Elise Pham Date: Mon, 25 Feb 2019 21:35:36 -0800 Subject: [PATCH 03/10] Added more tests for wave1 --- lib/main.rb | 2 ++ lib/planet.rb | 4 ++-- specs/planet_spec.rb | 12 ++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index df07e049..ff8a069b 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -7,6 +7,7 @@ def main mercury = Planet.new('Mercury', 'white', 3.3022e23, 6.98169e7, 'Mercury is the smallest planet in the solar system') jupiter = Planet.new('Jupiter', 'orange', 1.8982e27, 7.79e8, 'Jupiter is the largest planet in the solar system') venus = Planet.new('Venus', 'yellow', 4.8675e24, 1.082e8, 'Venus has the longest rotation period of any planet in the Solar System') + uranus = Planet.new('Uranus', 'baby blue', 8.6810e25, 2.75e9, 'Uranus is named after the Greek mythological figure Ouranos, the God of the sky') # puts earth.summary # puts mars.summary @@ -16,6 +17,7 @@ def main sun.add_planet(mercury) sun.add_planet(jupiter) sun.add_planet(venus) + sun.add_planet(uranus) list = sun.list_planets puts list diff --git a/lib/planet.rb b/lib/planet.rb index 69e35658..7e1e456f 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -2,11 +2,11 @@ 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) - if !mass_kg.is_a?(Numeric) || mass_kg < 0 + if !mass_kg.is_a?(Numeric) || mass_kg <= 0 raise ArgumentError.new("Planet mass has to be a positive number") end - if !distance_from_sun_km.is_a?(Numeric) || distance_from_sun_km < 0 + if !distance_from_sun_km.is_a?(Numeric) || distance_from_sun_km <= 0 raise ArgumentError.new("Distance from the sun has to be a postive number.") end diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb index 374255d0..26da6b1e 100644 --- a/specs/planet_spec.rb +++ b/specs/planet_spec.rb @@ -19,10 +19,22 @@ }.must_raise ArgumentError end + it "raises argument error if you input 0 for the planet's mass" do + expect { + Planet.new('Mars', 'red', 0, 2.29e8, 'Mars has two moons called Phobos and Deimos') + }.must_raise ArgumentError + end + it "raises argument error if you input a negative number for the planet's distance from the Sun" do expect { Planet.new('Mars', 'red', 6.4171e23, -2, 'Mars has two moons called Phobos and Deimos') }.must_raise ArgumentError end + + it "raises argument error if you input 0 for the planet's distance from the Sun" do + expect { + Planet.new('Mars', 'red', 6.4171e23, 0, 'Mars has two moons called Phobos and Deimos') + }.must_raise ArgumentError + end end From 3c18dbdbcd46e165be1bb9b0ca1734fecce43aa6 Mon Sep 17 00:00:00 2001 From: Elise Pham Date: Mon, 25 Feb 2019 22:01:38 -0800 Subject: [PATCH 04/10] get rid of set --- 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 9a70ba9e..ebe3e00d 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -1,5 +1,4 @@ require_relative 'planet.rb' -require "set" class SolarSystem attr_reader :star_name, :planets @@ -7,16 +6,14 @@ class SolarSystem def initialize(star_name) @star_name = star_name @planets = Array.new - @name_set = Set.new end def add_planet(planet) - if @name_set.include?(planet) + if @planets.include?(planet) raise ArgumentError.new("Planet already exists") end @planets << planet - @name_set << planet.name end def list_planets @@ -29,7 +26,7 @@ def list_planets end def find_planet_by_name(search_name) - if !@name_set.include?(search_name) + if !@planets.include?(search_name) raise ArgumentError.new("There is no such planet") end From a9b91d8221a2d7e6a409e7aa0413cb11b10a09e4 Mon Sep 17 00:00:00 2001 From: Elise Pham Date: Mon, 25 Feb 2019 22:13:11 -0800 Subject: [PATCH 05/10] use .any? --- lib/solar_system.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index ebe3e00d..ac34c4fc 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -26,7 +26,11 @@ def list_planets end def find_planet_by_name(search_name) - if !@planets.include?(search_name) + is_included = @planets.any? { |planet| + planet.name == search_name + } + + if !is_included raise ArgumentError.new("There is no such planet") end From f2149d5863f18a4edcebd1df0654be9757fb035d Mon Sep 17 00:00:00 2001 From: Elise Pham Date: Tue, 26 Feb 2019 21:49:38 -0800 Subject: [PATCH 06/10] revise main.rb file --- lib/main.rb | 102 ++++++++++++++++++++++++++++++++++++------- lib/solar_system.rb | 31 ++++++------- specs/planet_spec.rb | 21 ++++++++- 3 files changed, 119 insertions(+), 35 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index ff8a069b..5e217143 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,31 +1,103 @@ require_relative 'planet.rb' require_relative 'solar_system.rb' -def main +def build_solar_system + solar_system = SolarSystem.new("Sun") earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') mercury = Planet.new('Mercury', 'white', 3.3022e23, 6.98169e7, 'Mercury is the smallest planet in the solar system') jupiter = Planet.new('Jupiter', 'orange', 1.8982e27, 7.79e8, 'Jupiter is the largest planet in the solar system') venus = Planet.new('Venus', 'yellow', 4.8675e24, 1.082e8, 'Venus has the longest rotation period of any planet in the Solar System') uranus = Planet.new('Uranus', 'baby blue', 8.6810e25, 2.75e9, 'Uranus is named after the Greek mythological figure Ouranos, the God of the sky') - # puts earth.summary - # puts mars.summary + solar_system.add_planet(earth) + solar_system.add_planet(mars) + solar_system.add_planet(mercury) + solar_system.add_planet(jupiter) + solar_system.add_planet(venus) + solar_system.add_planet(uranus) + return solar_system +end + +def ask_planet_details(solar_system) + puts "Which planet do you want to learn about?" + puts "#{solar_system.list_planets}" + input_planet = gets.chomp + puts "#{solar_system.find_planet_by_name(input_planet).summary}" +end + +def add_new_planet(solar_system) + puts "What is the name of the planet you want to add?" + planet_name = gets.chomp + + puts "What is the color of your new planet?" + planet_color = gets.chomp + + puts "What is the mass of your new planet?" + planet_mass = gets.chomp.to_i + + puts "How far is your new planet from the Sun?" + planet_distance = gets.chomp.to_i + + puts "Please add some fun facts about your new planet" + planet_fact = gets.chomp + + new_planet = Planet.new(planet_name, planet_color, planet_mass, planet_distance, planet_fact) + solar_system.add_planet(new_planet) + puts "Congratulations! You completed adding a new planet" +end + +def find_distance_between(solar_system) + puts "What is the name of the first planet?" + first_planet = gets.chomp + puts "What is the name of the second planet?" + second_planet = gets.chomp + puts "The distance between #{first_planet} and #{second_planet} is #{solar_system.distance_between(first_planet, second_planet)} km" +end + +def main + sun = build_solar_system + + options = { + 1 => "List planets", + 2 => "Planet details", + 3 => "Add a planet", + 4 => "Find distance between two planets", + 5 => "Exit" + } + questions = "What do you want to do next?\n" + options.each { |key, value| + questions += "#{key}. #{value}\n" + } + + while true + puts questions + input = gets.chomp.downcase + if input == "exit" || input.to_i == 5 + break + end + + if input == "list planets" || input.to_i == 1 + puts "#{sun.list_planets}" + next + end - sun = SolarSystem.new("Sun") - sun.add_planet(earth) - sun.add_planet(mars) - sun.add_planet(mercury) - sun.add_planet(jupiter) - sun.add_planet(venus) - sun.add_planet(uranus) + if input == "planet details" || input.to_i == 2 + ask_planet_details(sun) + next + end - list = sun.list_planets - puts list + if input == "add a planet" || input.to_i == 3 + add_new_planet(sun) + next + end - found_planet = sun.find_planet_by_name('Jupiter') - puts found_planet.summary + if input == "find distance between two planets" || input.to_i == 4 + find_distance_between(sun) + next + end - puts "The distance between Earth and Mars is #{sun.distance_between('Jupiter', 'Mars')} km" + puts "You chose an invalid option. Please choose again" + end end main \ No newline at end of file diff --git a/lib/solar_system.rb b/lib/solar_system.rb index ac34c4fc..e7bba7e5 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -9,36 +9,31 @@ def initialize(star_name) end def add_planet(planet) - if @planets.include?(planet) - raise ArgumentError.new("Planet already exists") + @planets.each do |p| + if p.name.casecmp(planet.name) == 0 + raise ArgumentError.new("Planet already exists") + end end @planets << planet end def list_planets - result = "Planets orbitting #{star_name}:" - list = "" + list = "Planets orbitting #{star_name}:\n" @planets.each_index { |i| - list += "\n#{i + 1}. #{@planets[i].name}" + list += "#{i + 1}. #{@planets[i].name}\n" } - return result + list + return list end - def find_planet_by_name(search_name) - is_included = @planets.any? { |planet| - planet.name == search_name - } - - if !is_included - raise ArgumentError.new("There is no such planet") - end - - @planets.each do |element| - if element.name == search_name.capitalize - return element + def find_planet_by_name(name) + @planets.each do |planet| + if planet.name.casecmp(name) == 0 + return planet end end + + raise ArgumentError.new("There is no such planet #{name}") end def distance_between(planet1, planet2) diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb index 26da6b1e..107893de 100644 --- a/specs/planet_spec.rb +++ b/specs/planet_spec.rb @@ -3,13 +3,15 @@ require "minitest/skip_dsl" require_relative "../lib/planet.rb" +require_relative "../lib/solar_system.rb" +require_relative "../lib/main.rb" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -describe "Planet" do +describe "Planet class" do it "outputs a string when you invoke its summary method" do mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') - + expect(mars.summary).must_be_instance_of String end @@ -38,3 +40,18 @@ end end +describe "solar_system class" do + it "returns a list of type String of planets orbitting the star" do + + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') + sun = SolarSystem.new("Sun") + sun.add_planet(earth) + sun.add_planet(mars) + + expect(sun.list_planets).must_be_instance_of String + end + + +end + From 3fb8d0786a934fdbac95aa578b4b9b17b073dbda Mon Sep 17 00:00:00 2001 From: Elise Pham Date: Tue, 26 Feb 2019 21:57:13 -0800 Subject: [PATCH 07/10] update main method to use case statement --- lib/main.rb | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 5e217143..c7d0360b 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -72,31 +72,21 @@ def main while true puts questions input = gets.chomp.downcase - if input == "exit" || input.to_i == 5 - break - end - - if input == "list planets" || input.to_i == 1 + case input + when "exit", "5" + puts "Goodbye" + return + when "list planets", "1" puts "#{sun.list_planets}" - next - end - - if input == "planet details" || input.to_i == 2 + when "planet details", "2" ask_planet_details(sun) - next - end - - if input == "add a planet" || input.to_i == 3 + when "add a planet", "3" add_new_planet(sun) - next - end - - if input == "find distance between two planets" || input.to_i == 4 + when "find distance between two planets", "4" find_distance_between(sun) - next + else + puts "You chose an invalid option. Please choose again" end - - puts "You chose an invalid option. Please choose again" end end From 6ec983e5ae13082ecdf9c814c62f0f53dbf64f40 Mon Sep 17 00:00:00 2001 From: Elise Pham Date: Tue, 26 Feb 2019 22:19:10 -0800 Subject: [PATCH 08/10] handling errors --- lib/main.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index c7d0360b..fa8e05b7 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -22,7 +22,11 @@ def ask_planet_details(solar_system) puts "Which planet do you want to learn about?" puts "#{solar_system.list_planets}" input_planet = gets.chomp - puts "#{solar_system.find_planet_by_name(input_planet).summary}" + begin + puts "#{solar_system.find_planet_by_name(input_planet).summary}" + rescue ArgumentError => e + puts e + end end def add_new_planet(solar_system) @@ -41,9 +45,13 @@ def add_new_planet(solar_system) puts "Please add some fun facts about your new planet" planet_fact = gets.chomp - new_planet = Planet.new(planet_name, planet_color, planet_mass, planet_distance, planet_fact) - solar_system.add_planet(new_planet) - puts "Congratulations! You completed adding a new planet" + begin + new_planet = Planet.new(planet_name, planet_color, planet_mass, planet_distance, planet_fact) + solar_system.add_planet(new_planet) + puts "Congratulations! You added #{planet_name}" + rescue ArgumentError => e + puts e + end end def find_distance_between(solar_system) @@ -51,7 +59,11 @@ def find_distance_between(solar_system) first_planet = gets.chomp puts "What is the name of the second planet?" second_planet = gets.chomp - puts "The distance between #{first_planet} and #{second_planet} is #{solar_system.distance_between(first_planet, second_planet)} km" + begin + puts "The distance between #{first_planet} and #{second_planet} is #{solar_system.distance_between(first_planet, second_planet)} km" + rescue ArgumentError => e + puts e + end end def main @@ -64,7 +76,7 @@ def main 4 => "Find distance between two planets", 5 => "Exit" } - questions = "What do you want to do next?\n" + questions = "\nWhat do you want to do next?\n" options.each { |key, value| questions += "#{key}. #{value}\n" } From 59897b740889dbaeed03915e61fdbbd6f1ea2129 Mon Sep 17 00:00:00 2001 From: Elise Pham Date: Wed, 27 Feb 2019 10:00:42 -0800 Subject: [PATCH 09/10] Remove require_relative for main.rb in spec file --- specs/planet_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb index 107893de..ee935eda 100644 --- a/specs/planet_spec.rb +++ b/specs/planet_spec.rb @@ -4,7 +4,6 @@ require_relative "../lib/planet.rb" require_relative "../lib/solar_system.rb" -require_relative "../lib/main.rb" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 34d28c5bdcb79eae36db03f1de0e9c1407b909ff Mon Sep 17 00:00:00 2001 From: Elise Pham Date: Mon, 4 Mar 2019 13:52:00 -0800 Subject: [PATCH 10/10] Fix indentation --- lib/main.rb | 160 +++++++++++++++++++++---------------------- lib/planet.rb | 34 ++++----- lib/solar_system.rb | 68 +++++++++--------- specs/planet_spec.rb | 80 +++++++++++----------- 4 files changed, 170 insertions(+), 172 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index fa8e05b7..669cfe5a 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -2,104 +2,104 @@ require_relative 'solar_system.rb' def build_solar_system - solar_system = SolarSystem.new("Sun") - earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') - mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') - mercury = Planet.new('Mercury', 'white', 3.3022e23, 6.98169e7, 'Mercury is the smallest planet in the solar system') - jupiter = Planet.new('Jupiter', 'orange', 1.8982e27, 7.79e8, 'Jupiter is the largest planet in the solar system') - venus = Planet.new('Venus', 'yellow', 4.8675e24, 1.082e8, 'Venus has the longest rotation period of any planet in the Solar System') - uranus = Planet.new('Uranus', 'baby blue', 8.6810e25, 2.75e9, 'Uranus is named after the Greek mythological figure Ouranos, the God of the sky') - solar_system.add_planet(earth) - solar_system.add_planet(mars) - solar_system.add_planet(mercury) - solar_system.add_planet(jupiter) - solar_system.add_planet(venus) - solar_system.add_planet(uranus) - return solar_system + solar_system = SolarSystem.new("Sun") + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') + mercury = Planet.new('Mercury', 'white', 3.3022e23, 6.98169e7, 'Mercury is the smallest planet in the solar system') + jupiter = Planet.new('Jupiter', 'orange', 1.8982e27, 7.79e8, 'Jupiter is the largest planet in the solar system') + venus = Planet.new('Venus', 'yellow', 4.8675e24, 1.082e8, 'Venus has the longest rotation period of any planet in the Solar System') + uranus = Planet.new('Uranus', 'baby blue', 8.6810e25, 2.75e9, 'Uranus is named after the Greek mythological figure Ouranos, the God of the sky') + solar_system.add_planet(earth) + solar_system.add_planet(mars) + solar_system.add_planet(mercury) + solar_system.add_planet(jupiter) + solar_system.add_planet(venus) + solar_system.add_planet(uranus) + return solar_system end def ask_planet_details(solar_system) - puts "Which planet do you want to learn about?" - puts "#{solar_system.list_planets}" - input_planet = gets.chomp - begin - puts "#{solar_system.find_planet_by_name(input_planet).summary}" - rescue ArgumentError => e - puts e - end + puts "Which planet do you want to learn about?" + puts "#{solar_system.list_planets}" + input_planet = gets.chomp + begin + puts "#{solar_system.find_planet_by_name(input_planet).summary}" + rescue ArgumentError => e + puts e + end end def add_new_planet(solar_system) - puts "What is the name of the planet you want to add?" - planet_name = gets.chomp - - puts "What is the color of your new planet?" - planet_color = gets.chomp + puts "What is the name of the planet you want to add?" + planet_name = gets.chomp + + puts "What is the color of your new planet?" + planet_color = gets.chomp - puts "What is the mass of your new planet?" - planet_mass = gets.chomp.to_i + puts "What is the mass of your new planet?" + planet_mass = gets.chomp.to_i - puts "How far is your new planet from the Sun?" - planet_distance = gets.chomp.to_i + puts "How far is your new planet from the Sun?" + planet_distance = gets.chomp.to_i - puts "Please add some fun facts about your new planet" - planet_fact = gets.chomp + puts "Please add some fun facts about your new planet" + planet_fact = gets.chomp - begin - new_planet = Planet.new(planet_name, planet_color, planet_mass, planet_distance, planet_fact) - solar_system.add_planet(new_planet) - puts "Congratulations! You added #{planet_name}" - rescue ArgumentError => e - puts e - end + begin + new_planet = Planet.new(planet_name, planet_color, planet_mass, planet_distance, planet_fact) + solar_system.add_planet(new_planet) + puts "Congratulations! You added #{planet_name}" + rescue ArgumentError => e + puts e + end end def find_distance_between(solar_system) - puts "What is the name of the first planet?" - first_planet = gets.chomp - puts "What is the name of the second planet?" - second_planet = gets.chomp - begin - puts "The distance between #{first_planet} and #{second_planet} is #{solar_system.distance_between(first_planet, second_planet)} km" - rescue ArgumentError => e - puts e - end + puts "What is the name of the first planet?" + first_planet = gets.chomp + puts "What is the name of the second planet?" + second_planet = gets.chomp + begin + puts "The distance between #{first_planet} and #{second_planet} is #{solar_system.distance_between(first_planet, second_planet)} km" + rescue ArgumentError => e + puts e + end end def main - sun = build_solar_system + sun = build_solar_system - options = { - 1 => "List planets", - 2 => "Planet details", - 3 => "Add a planet", - 4 => "Find distance between two planets", - 5 => "Exit" - } - questions = "\nWhat do you want to do next?\n" - options.each { |key, value| - questions += "#{key}. #{value}\n" - } - - while true - puts questions - input = gets.chomp.downcase - case input - when "exit", "5" - puts "Goodbye" - return - when "list planets", "1" - puts "#{sun.list_planets}" - when "planet details", "2" - ask_planet_details(sun) - when "add a planet", "3" - add_new_planet(sun) - when "find distance between two planets", "4" - find_distance_between(sun) - else - puts "You chose an invalid option. Please choose again" - end + options = { + 1 => "List planets", + 2 => "Planet details", + 3 => "Add a planet", + 4 => "Find distance between two planets", + 5 => "Exit" + } + questions = "\nWhat do you want to do next?\n" + options.each { |key, value| + questions += "#{key}. #{value}\n" + } + + while true + puts questions + input = gets.chomp.downcase + case input + when "exit", "5" + puts "Goodbye" + return + when "list planets", "1" + puts "#{sun.list_planets}" + when "planet details", "2" + ask_planet_details(sun) + when "add a planet", "3" + add_new_planet(sun) + when "find distance between two planets", "4" + find_distance_between(sun) + else + puts "You chose an invalid option. Please choose again" end + end end main \ No newline at end of file diff --git a/lib/planet.rb b/lib/planet.rb index 7e1e456f..e2dd94cc 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -1,23 +1,23 @@ class Planet - attr_reader :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) - if !mass_kg.is_a?(Numeric) || mass_kg <= 0 - raise ArgumentError.new("Planet mass has to be a positive number") - end - - if !distance_from_sun_km.is_a?(Numeric) || distance_from_sun_km <= 0 - raise ArgumentError.new("Distance from the sun has to be a postive number.") - end - - @name = name - @color = color - @mass_kg = mass_kg - @distance_from_sun_km = distance_from_sun_km - @fun_fact = fun_fact + def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) + if !mass_kg.is_a?(Numeric) || mass_kg <= 0 + raise ArgumentError.new("Planet mass has to be a positive number") end - def summary - return "The #{name} is #{color}, weighs #{mass_kg} kg, and is about #{distance_from_sun_km} km from the sun. Fun fact: #{fun_fact}" + if !distance_from_sun_km.is_a?(Numeric) || distance_from_sun_km <= 0 + raise ArgumentError.new("Distance from the sun has to be a postive number.") end + + @name = name + @color = color + @mass_kg = mass_kg + @distance_from_sun_km = distance_from_sun_km + @fun_fact = fun_fact + end + + def summary + return "The #{name} is #{color}, weighs #{mass_kg} kg, and is about #{distance_from_sun_km} km from the sun. Fun fact: #{fun_fact}" + end end \ No newline at end of file diff --git a/lib/solar_system.rb b/lib/solar_system.rb index e7bba7e5..61a4d5da 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -1,44 +1,44 @@ require_relative 'planet.rb' class SolarSystem - attr_reader :star_name, :planets - - def initialize(star_name) - @star_name = star_name - @planets = Array.new - end - - def add_planet(planet) - @planets.each do |p| - if p.name.casecmp(planet.name) == 0 - raise ArgumentError.new("Planet already exists") - end - end - - @planets << planet + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = Array.new + end + + def add_planet(planet) + @planets.each do |p| + if p.name.casecmp(planet.name) == 0 + raise ArgumentError.new("Planet already exists") + end end - def list_planets - list = "Planets orbitting #{star_name}:\n" - @planets.each_index { |i| - list += "#{i + 1}. #{@planets[i].name}\n" - } - return list + @planets << planet + end + + def list_planets + list = "Planets orbitting #{star_name}:\n" + @planets.each_index { |i| + list += "#{i + 1}. #{@planets[i].name}\n" + } + return list + end + + def find_planet_by_name(name) + @planets.each do |planet| + if planet.name.casecmp(name) == 0 + return planet + end end - def find_planet_by_name(name) - @planets.each do |planet| - if planet.name.casecmp(name) == 0 - return planet - end - end + raise ArgumentError.new("There is no such planet #{name}") + end - raise ArgumentError.new("There is no such planet #{name}") - end - - def distance_between(planet1, planet2) - distance = find_planet_by_name(planet1).distance_from_sun_km - find_planet_by_name(planet2).distance_from_sun_km - return distance.abs - end + def distance_between(planet1, planet2) + distance = find_planet_by_name(planet1).distance_from_sun_km - find_planet_by_name(planet2).distance_from_sun_km + return distance.abs + end end diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb index ee935eda..d9bb1363 100644 --- a/specs/planet_spec.rb +++ b/specs/planet_spec.rb @@ -8,49 +8,47 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe "Planet class" do - it "outputs a string when you invoke its summary method" do - mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') - - expect(mars.summary).must_be_instance_of String - end - - it "raises argument error if you input a negative number for the planet's mass" do - expect { - Planet.new('Mars', 'red', -1, 2.29e8, 'Mars has two moons called Phobos and Deimos') - }.must_raise ArgumentError - end - - it "raises argument error if you input 0 for the planet's mass" do - expect { - Planet.new('Mars', 'red', 0, 2.29e8, 'Mars has two moons called Phobos and Deimos') - }.must_raise ArgumentError - end - - it "raises argument error if you input a negative number for the planet's distance from the Sun" do - expect { - Planet.new('Mars', 'red', 6.4171e23, -2, 'Mars has two moons called Phobos and Deimos') - }.must_raise ArgumentError - end - - it "raises argument error if you input 0 for the planet's distance from the Sun" do - expect { - Planet.new('Mars', 'red', 6.4171e23, 0, 'Mars has two moons called Phobos and Deimos') - }.must_raise ArgumentError - end + it "outputs a string when you invoke its summary method" do + mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') + + expect(mars.summary).must_be_instance_of String + end + + it "raises argument error if you input a negative number for the planet's mass" do + expect { + Planet.new('Mars', 'red', -1, 2.29e8, 'Mars has two moons called Phobos and Deimos') + }.must_raise ArgumentError + end + + it "raises argument error if you input 0 for the planet's mass" do + expect { + Planet.new('Mars', 'red', 0, 2.29e8, 'Mars has two moons called Phobos and Deimos') + }.must_raise ArgumentError + end + + it "raises argument error if you input a negative number for the planet's distance from the Sun" do + expect { + Planet.new('Mars', 'red', 6.4171e23, -2, 'Mars has two moons called Phobos and Deimos') + }.must_raise ArgumentError + end + + it "raises argument error if you input 0 for the planet's distance from the Sun" do + expect { + Planet.new('Mars', 'red', 6.4171e23, 0, 'Mars has two moons called Phobos and Deimos') + }.must_raise ArgumentError + end end describe "solar_system class" do - it "returns a list of type String of planets orbitting the star" do - - earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') - mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') - sun = SolarSystem.new("Sun") - sun.add_planet(earth) - sun.add_planet(mars) - - expect(sun.list_planets).must_be_instance_of String - end - - + it "returns a list of type String of planets orbitting the star" do + + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + mars = Planet.new('Mars', 'red', 6.4171e23, 2.29e8, 'Mars has two moons called Phobos and Deimos') + sun = SolarSystem.new("Sun") + sun.add_planet(earth) + sun.add_planet(mars) + + expect(sun.list_planets).must_be_instance_of String + end end