From b9edb3383695fae87cd780cbc94ab51e5b34c91c Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Tue, 26 Feb 2019 16:19:33 -0800 Subject: [PATCH 01/10] finished wave 1 --- lib/main.rb | 23 +++++++++++++++++++++++ lib/planet.rb | 15 +++++++++++++++ 2 files changed, 38 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..65107afc --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,23 @@ +require_relative "planet" +require_relative "solar_system" + +def main + solar_system = SolarSystem.new("Sol") + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "it's the only planet known to support life") + + venus = Planet.new("Venus", "yellow", 4.867e24, 67.3e6, "40 spacecraft have explored Venus") + + mercury = Planet.new("Mercury", "blue", 3.285e23, 28.6e6, "kid-friendly") + + mars = Planet.new("Mars", "orange", 6.39e23, 141.7e6, "it has 2 moons") + + jupiter = Planet.new("Jupiter", "red", 1.89e27, 495.7e6, "it has more than 75 moons") + + saturn = Planet.new("Saturn", "brown", 5.68e26, 934.9e6, "it has 62 moons") + + uranus = Planet.new("Uranus", "blue", 8.68e25, 1.9e9, "it has 62 moons") + + neptune = Planet.new("Neptune", "purple", 1.024e26, 2.78e9, "it has 62 moons") +end + +main diff --git a/lib/planet.rb b/lib/planet.rb new file mode 100644 index 00000000..6c525d48 --- /dev/null +++ b/lib/planet.rb @@ -0,0 +1,15 @@ +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} is a #{color} planet. Its mass is #{mass_kg}kg, and it is #{distance_from_sun_km}km from the Sun. Fun fact: #{fun_fact}." + end +end From f1165ebe8452a1d3a7b09330cb7c3b1c98d5f036 Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Tue, 26 Feb 2019 16:31:09 -0800 Subject: [PATCH 02/10] finished wave 2 --- lib/main.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++ lib/solar_system.rb | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 lib/solar_system.rb diff --git a/lib/main.rb b/lib/main.rb index 65107afc..a297f4c5 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -3,21 +3,69 @@ def main solar_system = SolarSystem.new("Sol") + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "it's the only planet known to support life") + solar_system.add_planet(earth) venus = Planet.new("Venus", "yellow", 4.867e24, 67.3e6, "40 spacecraft have explored Venus") + solar_system.add_planet(venus) mercury = Planet.new("Mercury", "blue", 3.285e23, 28.6e6, "kid-friendly") + solar_system.add_planet(mercury) mars = Planet.new("Mars", "orange", 6.39e23, 141.7e6, "it has 2 moons") + solar_system.add_planet(mars) jupiter = Planet.new("Jupiter", "red", 1.89e27, 495.7e6, "it has more than 75 moons") + solar_system.add_planet(jupiter) saturn = Planet.new("Saturn", "brown", 5.68e26, 934.9e6, "it has 62 moons") + solar_system.add_planet(saturn) uranus = Planet.new("Uranus", "blue", 8.68e25, 1.9e9, "it has 62 moons") + solar_system.add_planet(uranus) neptune = Planet.new("Neptune", "purple", 1.024e26, 2.78e9, "it has 62 moons") + solar_system.add_planet(neptune) + + puts "What would you like to do?" + print "Please enter \"list planets\", \"planet details\", \"add planet\", or \"exit\": " + input = gets.chomp.downcase + + if input == "list planets" + list = solar_system.list_planets + puts list + end + + if input == "planet details" + puts "\nWhich planet do you want to learn about? Choose one: Earth, Venus, Mercury, Mars, Jupiter, Saturn, Uranus, Neptune " + planet_chosen = gets.chomp.downcase + puts "\n" + puts solar_system.find_planet_by_name(planet_chosen) + end + + if input == "add planet" + print "\nWhat is the planet's name?" + name = gets.chomp.capitalize + print "What is its color?" + color = gets.chomp + print "What is its mass(kg)?" + mass_kg = gets.chomp + print "What is its distance from the Sun in km?" + distance_from_sun_km = gets.chomp + print "What is its fun fact?" + fun_fact = gets.chomp + + planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) + solar_system.add_planet(planet) + puts "\n#{name} has been added with the details below:" + puts solar_system.find_planet_by_name(name) + end + + if input == "exit" + puts "Good bye!" + exit + end end main diff --git a/lib/solar_system.rb b/lib/solar_system.rb new file mode 100644 index 00000000..09d211fc --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,41 @@ +require_relative "planet" + +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + def add_planet(planet) + @planets.push(planet) + end + + def list_planets + list = "\nPlanets orbitting #{@star_name}:\n" + @planets.each_with_index do |planet, index| + list = list + "#{index + 1}. #{planet.name}\n" + end + return list + end + + def find_planet_by_name(name) + found_planets = @planets.select do |planet| + planet.name.downcase == name.downcase + end + + # if the planet is not found, raise an exception + raise ArgumentError, "#{name} is not found." if found_planets.empty? + return "#{found_planets[0].summary}" if found_planets.length == 1 + + # if there are multiple planets with the given name, list them all + found_list = "There are multiple planets named #{name}:\n" + found_index = 0 + found_planets.each_with_index do |found_planet, index| + found_list += "#{found_index + 1}. #{found_planet.name}: #{found_planet.summary}.\n" + found_index += 1 + end + return found_list + end +end From d0b917bd2c11e63aee71b7decbd181807329751a Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Tue, 26 Feb 2019 16:39:32 -0800 Subject: [PATCH 03/10] finished wave 3 --- lib/planet.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/planet.rb b/lib/planet.rb index 6c525d48..e1816ad3 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -9,6 +9,7 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @fun_fact = fun_fact end + # gets the details of the planet def summary return "#{name} is a #{color} planet. Its mass is #{mass_kg}kg, and it is #{distance_from_sun_km}km from the Sun. Fun fact: #{fun_fact}." end From 84f975065f7f4a4010e0e2b3fe9818363b553a7b Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Tue, 26 Feb 2019 16:42:16 -0800 Subject: [PATCH 04/10] finished wave 3 --- lib/main.rb | 1 + lib/solar_system.rb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/main.rb b/lib/main.rb index a297f4c5..fb0b7759 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -2,6 +2,7 @@ require_relative "solar_system" def main + # initializes the solar system's name solar_system = SolarSystem.new("Sol") earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "it's the only planet known to support life") diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 09d211fc..df7e9392 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -27,6 +27,8 @@ def find_planet_by_name(name) # if the planet is not found, raise an exception raise ArgumentError, "#{name} is not found." if found_planets.empty? + + # if there's only one planet return "#{found_planets[0].summary}" if found_planets.length == 1 # if there are multiple planets with the given name, list them all From c8a0949d8a6b8d8c237a65cdaeb2d404846e14e4 Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Wed, 27 Feb 2019 02:02:29 -0800 Subject: [PATCH 05/10] did optional enhancements --- lib/main.rb | 101 ++++++++++++++++++++++++------------- lib/solar_system.rb | 27 ++++------ specs/planet_spec.rb | 34 +++++++++++++ specs/solar_system_spec.rb | 60 ++++++++++++++++++++++ 4 files changed, 172 insertions(+), 50 deletions(-) create mode 100644 specs/planet_spec.rb create mode 100644 specs/solar_system_spec.rb diff --git a/lib/main.rb b/lib/main.rb index fb0b7759..04edb32e 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,6 +1,14 @@ require_relative "planet" require_relative "solar_system" +def handling_input(input) + # Optional: handling user entering bad command + until input == "list planets" || input == "planet details" || input == "add planet" || input == "find distance" || input == "exit" + print "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " + input = gets.chomp.downcase + end +end + def main # initializes the solar system's name solar_system = SolarSystem.new("Sol") @@ -30,42 +38,67 @@ def main solar_system.add_planet(neptune) puts "What would you like to do?" - print "Please enter \"list planets\", \"planet details\", \"add planet\", or \"exit\": " + print "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " input = gets.chomp.downcase - if input == "list planets" - list = solar_system.list_planets - puts list - end - - if input == "planet details" - puts "\nWhich planet do you want to learn about? Choose one: Earth, Venus, Mercury, Mars, Jupiter, Saturn, Uranus, Neptune " - planet_chosen = gets.chomp.downcase - puts "\n" - puts solar_system.find_planet_by_name(planet_chosen) - end - - if input == "add planet" - print "\nWhat is the planet's name?" - name = gets.chomp.capitalize - print "What is its color?" - color = gets.chomp - print "What is its mass(kg)?" - mass_kg = gets.chomp - print "What is its distance from the Sun in km?" - distance_from_sun_km = gets.chomp - print "What is its fun fact?" - fun_fact = gets.chomp - - planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) - solar_system.add_planet(planet) - puts "\n#{name} has been added with the details below:" - puts solar_system.find_planet_by_name(name) - end - - if input == "exit" - puts "Good bye!" - exit + handling_input(input) + + until input == "exit" + if input == "list planets" + list = solar_system.list_planets + puts list + end + + if input == "planet details" + puts "\nWhich planet do you want to learn about? Choose one: Earth, Venus, Mercury, Mars, Jupiter, Saturn, Uranus, Neptune " + planet_chosen = gets.chomp.downcase + # Optional: when user enters a planet that doesn't exist, raise an exception. + if !solar_system.planets.map { |planet| planet.name }.include?(planet_chosen.capitalize) + raise ArgumentError, "Planet entered is not in the list." + end + puts "\n" + puts solar_system.find_planet_by_name(planet_chosen).summary + end + + if input == "add planet" + print "\nWhat is the planet's name?" + name = gets.chomp.capitalize + print "What is its color?" + color = gets.chomp + print "What is its mass(kg)?" + mass_kg = gets.chomp.to_f + # Optional: if user enters a bad value + until mass_kg > 0 + print "Mass must be a number greater than 0. Please enter again: " + mass_kg = gets.chomp.to_f + end + print "What is its distance from the Sun in km?" + distance_from_sun_km = gets.chomp.to_f + # Optional: if the user enters a bad value + until distance_from_sun_km > 0 + print "Distance from the Sun must be a number greater than 0. Please enter again: " + distance_from_sun_km = gets.chomp.to_f + end + print "What is its fun fact?" + fun_fact = gets.chomp + + planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) + solar_system.add_planet(planet) + puts "\n#{name} has been added with the details below:" + puts solar_system.find_planet_by_name(name).summary + end + + # OPTIONAL: Implement the optional distance_between method + if input == "find distance" + print "Enter first planet: " + planet_name1 = gets.chomp.downcase + print "Enter second planet: " + planet_name2 = gets.chomp.downcase + puts "The distance is #{solar_system.distance_between(planet_name1, planet_name2)}" + end + puts "What would you like to do next?" + print "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " + input = gets.chomp end end diff --git a/lib/solar_system.rb b/lib/solar_system.rb index df7e9392..a377dd4f 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -1,7 +1,7 @@ require_relative "planet" class SolarSystem - attr_reader :star_name, :planets + attr_reader :star_name, :planets #should keep? if so, need to add to spec def initialize(star_name) @star_name = star_name @@ -9,6 +9,7 @@ def initialize(star_name) end def add_planet(planet) + raise ArgumentError, "Planet has already been added." if @planets.any? { |i| i.name.downcase == planet.name.downcase } @planets.push(planet) end @@ -21,23 +22,17 @@ def list_planets end def find_planet_by_name(name) - found_planets = @planets.select do |planet| - planet.name.downcase == name.downcase + @planets.each do |planet| + return planet if (planet.name).downcase == name.downcase end - # if the planet is not found, raise an exception - raise ArgumentError, "#{name} is not found." if found_planets.empty? - - # if there's only one planet - return "#{found_planets[0].summary}" if found_planets.length == 1 + raise ArgumentError, "#{name} is not found." + end - # if there are multiple planets with the given name, list them all - found_list = "There are multiple planets named #{name}:\n" - found_index = 0 - found_planets.each_with_index do |found_planet, index| - found_list += "#{found_index + 1}. #{found_planet.name}: #{found_planet.summary}.\n" - found_index += 1 - end - return found_list + # Optional: takes two planet names as parameters and returns the distance between them + def distance_between(planet_name1, planet_name2) + distance_planet1 = find_planet_by_name(planet_name1).distance_from_sun_km + distance_planet2 = find_planet_by_name(planet_name2).distance_from_sun_km + return (distance_planet1 - distance_planet2).abs end end diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb new file mode 100644 index 00000000..880d73c5 --- /dev/null +++ b/specs/planet_spec.rb @@ -0,0 +1,34 @@ + +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" + +require_relative "../lib/planet" + +# Get that nice colorized output +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe "planet" do + describe "initialize" do + it "will create an instance of the class Planet with 5 parameters" do + new_planet = Planet.new("earth", "blue", 45, 67, "supports life") + expect(new_planet).must_be_instance_of Planet + end + + it "will correctly read the parameters of the planet" do + new_planet = Planet.new("earth", "blue", 45, 67, "supports life") + expect(new_planet.name).must_equal "earth" + expect(new_planet.color).must_equal "blue" + expect(new_planet.mass_kg).must_equal 45 + expect(new_planet.distance_from_sun_km).must_equal 67 + expect(new_planet.fun_fact).must_equal "supports life" + end + + describe "summary" do + it "will return a summary of the planet" do + new_planet = Planet.new("earth", "blue", 45, 67, "supports life") + expect(new_planet.summary).must_be_instance_of String + end + end + end +end diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb new file mode 100644 index 00000000..2d7035df --- /dev/null +++ b/specs/solar_system_spec.rb @@ -0,0 +1,60 @@ +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" + +require_relative "../lib/solar_system" + +# Get that nice colorized output +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe "solar_system" do + describe "initialize method" do + new_sol = SolarSystem.new("New Sol") + it "will create an instance of the class SolarSystem with an empty array planets " do + expect(new_sol).must_be_instance_of SolarSystem + expect(new_sol.planets).must_equal [] + end + + it "will read the star name when initialized" do + expect(new_sol.star_name).must_equal "New Sol" + end + end + + describe "add_planet method" do + new_sol = SolarSystem.new("New Sol") + it "will add a planet and return an array of planets" do + pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") + expect(new_sol.add_planet(pluto)).must_be_instance_of Array + end + end + + describe "list_planets method" do + new_sol = SolarSystem.new("New Sol") + pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") + new_sol.add_planet(pluto) + it "will return true if the list is an instance of a string" do + expect(new_sol.list_planets).must_be_instance_of String + end + end + + describe "find_planet_by_name method" do + new_sol = SolarSystem.new("New Sol") + pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") + new_sol.add_planet(pluto) + it "will return an instance of Planet" do + expect(new_sol.find_planet_by_name("Pluto")).must_be_instance_of Planet + end + end + + # describe "distance_between method" do + # it "will return the distance between two planets" do + # new_sol = SolarSystem.new("New_Sol") + # pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") + # puffer = Planet.new("Puffer", "yellow", 78, 89, "not a planet") + # new_sol.add_planet(pluto) + # new_sol.add_planet(puffer) + # puts new_sol.distance_between(pluto, puffer) + # expect(new_sol.distance_between(pluto, puffer)).must_equal 2 + # end + # end +end From 51d52a68c6ce733f6a6cb66366fd66895a218d93 Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Wed, 27 Feb 2019 13:24:13 -0800 Subject: [PATCH 06/10] colorized the outputs --- lib/main.rb | 63 ++++++++++++++++++++++++--------------------------- lib/planet.rb | 2 +- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 04edb32e..0889d177 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,18 +1,19 @@ require_relative "planet" require_relative "solar_system" +require "awesome_print" +require "colorize" + def handling_input(input) # Optional: handling user entering bad command until input == "list planets" || input == "planet details" || input == "add planet" || input == "find distance" || input == "exit" - print "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " + print "Invalid input.\nPlease enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " input = gets.chomp.downcase end + return input end -def main - # initializes the solar system's name - solar_system = SolarSystem.new("Sol") - +def existing_planets(solar_system) earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "it's the only planet known to support life") solar_system.add_planet(earth) @@ -24,81 +25,75 @@ def main mars = Planet.new("Mars", "orange", 6.39e23, 141.7e6, "it has 2 moons") solar_system.add_planet(mars) +end - jupiter = Planet.new("Jupiter", "red", 1.89e27, 495.7e6, "it has more than 75 moons") - solar_system.add_planet(jupiter) - - saturn = Planet.new("Saturn", "brown", 5.68e26, 934.9e6, "it has 62 moons") - solar_system.add_planet(saturn) - - uranus = Planet.new("Uranus", "blue", 8.68e25, 1.9e9, "it has 62 moons") - solar_system.add_planet(uranus) - - neptune = Planet.new("Neptune", "purple", 1.024e26, 2.78e9, "it has 62 moons") - solar_system.add_planet(neptune) - +def main + # initializes the solar system's name + solar_system = SolarSystem.new("Sol") + existing_planets(solar_system) puts "What would you like to do?" print "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " input = gets.chomp.downcase - handling_input(input) + input = handling_input(input) until input == "exit" if input == "list planets" list = solar_system.list_planets - puts list + puts list.colorize(:green) end if input == "planet details" - puts "\nWhich planet do you want to learn about? Choose one: Earth, Venus, Mercury, Mars, Jupiter, Saturn, Uranus, Neptune " + puts "Which planet do you want to learn about? Choose one: Earth, Venus, Mercury, Mars" planet_chosen = gets.chomp.downcase # Optional: when user enters a planet that doesn't exist, raise an exception. if !solar_system.planets.map { |planet| planet.name }.include?(planet_chosen.capitalize) raise ArgumentError, "Planet entered is not in the list." end puts "\n" - puts solar_system.find_planet_by_name(planet_chosen).summary + puts solar_system.find_planet_by_name(planet_chosen).summary.colorize(:green) end if input == "add planet" - print "\nWhat is the planet's name?" + puts "What is the planet's name?" name = gets.chomp.capitalize - print "What is its color?" + puts "What is its color?" color = gets.chomp - print "What is its mass(kg)?" + puts "What is its mass(kg)?" mass_kg = gets.chomp.to_f # Optional: if user enters a bad value until mass_kg > 0 - print "Mass must be a number greater than 0. Please enter again: " + puts "Mass must be a number greater than 0. Please enter again: " mass_kg = gets.chomp.to_f end - print "What is its distance from the Sun in km?" + puts "What is its distance from the Sun in km?" distance_from_sun_km = gets.chomp.to_f # Optional: if the user enters a bad value until distance_from_sun_km > 0 - print "Distance from the Sun must be a number greater than 0. Please enter again: " + puts "Distance from the Sun must be a number greater than 0. Please enter again: " distance_from_sun_km = gets.chomp.to_f end - print "What is its fun fact?" + puts "What is its fun fact?" fun_fact = gets.chomp planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) solar_system.add_planet(planet) - puts "\n#{name} has been added with the details below:" - puts solar_system.find_planet_by_name(name).summary + puts "#{name} has been added with the details below:".colorize(:green) + puts solar_system.find_planet_by_name(name).summary.colorize(:green) end # OPTIONAL: Implement the optional distance_between method if input == "find distance" - print "Enter first planet: " + puts "Enter first planet: " planet_name1 = gets.chomp.downcase - print "Enter second planet: " + puts "Enter second planet: " planet_name2 = gets.chomp.downcase - puts "The distance is #{solar_system.distance_between(planet_name1, planet_name2)}" + puts "The distance is #{solar_system.distance_between(planet_name1, planet_name2)}".colorize(:green) end puts "What would you like to do next?" - print "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " + puts "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " input = gets.chomp + input = handling_input(input) end end diff --git a/lib/planet.rb b/lib/planet.rb index e1816ad3..47e426bc 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -11,6 +11,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) # gets the details of the planet def summary - return "#{name} is a #{color} planet. Its mass is #{mass_kg}kg, and it is #{distance_from_sun_km}km from the Sun. Fun fact: #{fun_fact}." + return "#{name} is a/an #{color} planet. Its mass is #{mass_kg}kg, and it is #{distance_from_sun_km}km from the Sun. Fun fact: #{fun_fact}." end end From 718e98f7387b5010d7e7426a9dc5b977fcf025ff Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Wed, 27 Feb 2019 15:07:14 -0800 Subject: [PATCH 07/10] refactored --- lib/solar_system.rb | 1 + specs/solar_system_spec.rb | 31 +++++++++++++++++++------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index a377dd4f..413bc0eb 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -9,6 +9,7 @@ def initialize(star_name) end def add_planet(planet) + raise ArgumentError, "Planet has to be an instance of Planet class" if !planet.is_a?(Planet) raise ArgumentError, "Planet has already been added." if @planets.any? { |i| i.name.downcase == planet.name.downcase } @planets.push(planet) end diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb index 2d7035df..3b9dd067 100644 --- a/specs/solar_system_spec.rb +++ b/specs/solar_system_spec.rb @@ -22,10 +22,18 @@ describe "add_planet method" do new_sol = SolarSystem.new("New Sol") - it "will add a planet and return an array of planets" do + it "will take a planet and return an array of planets" do pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") expect(new_sol.add_planet(pluto)).must_be_instance_of Array end + + it "will take the parameter as a planet" do + pluto = "Pluto" + # For some reason this is not working: expect(new_sol.add_planet(pluto)).must_raise ArgumentError + assert_raises ArgumentError do + new_sol.add_planet(pluto) + end + end end describe "list_planets method" do @@ -46,15 +54,14 @@ end end - # describe "distance_between method" do - # it "will return the distance between two planets" do - # new_sol = SolarSystem.new("New_Sol") - # pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") - # puffer = Planet.new("Puffer", "yellow", 78, 89, "not a planet") - # new_sol.add_planet(pluto) - # new_sol.add_planet(puffer) - # puts new_sol.distance_between(pluto, puffer) - # expect(new_sol.distance_between(pluto, puffer)).must_equal 2 - # end - # end + describe "distance_between method" do + it "will return the distance between two planets" do + new_sol = SolarSystem.new("New_Sol") + pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") + puffer = Planet.new("Puffer", "yellow", 78, 89, "not a planet") + new_sol.add_planet(pluto) + new_sol.add_planet(puffer) + expect(new_sol.distance_between(pluto.name, puffer.name)).must_equal 2 + end + end end From 5d7423e08e63a64d32f032c9f213977908e39cb3 Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Wed, 27 Feb 2019 15:12:22 -0800 Subject: [PATCH 08/10] refactored --- specs/solar_system_spec.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb index 3b9dd067..36d7f0fb 100644 --- a/specs/solar_system_spec.rb +++ b/specs/solar_system_spec.rb @@ -29,10 +29,9 @@ it "will take the parameter as a planet" do pluto = "Pluto" - # For some reason this is not working: expect(new_sol.add_planet(pluto)).must_raise ArgumentError - assert_raises ArgumentError do - new_sol.add_planet(pluto) - end + # note to me: when using curly braces, the code inside runs before expect + # when using with .must_raise + expect { new_sol.add_planet(pluto) }.must_raise ArgumentError end end From 52befe1857023e5a5a3045da3ac15d19e880266f Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Wed, 27 Feb 2019 15:21:01 -0800 Subject: [PATCH 09/10] added Rakefile --- Rakefile | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Rakefile 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 From 4563bb5f88690f6b70d5003df3ba42cb41821476 Mon Sep 17 00:00:00 2001 From: Sopheary Chiv Date: Sun, 3 Mar 2019 09:56:04 -0800 Subject: [PATCH 10/10] refactored: added helper methods in main file --- lib/main.rb | 138 ++++++++++++++++++++----------------- specs/solar_system_spec.rb | 1 - 2 files changed, 75 insertions(+), 64 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 0889d177..98c5d9b4 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -4,92 +4,104 @@ require "awesome_print" require "colorize" +SOLAR_SYSTEM = SolarSystem.new("Sol") + +# Optional: handling user entering bad command def handling_input(input) - # Optional: handling user entering bad command - until input == "list planets" || input == "planet details" || input == "add planet" || input == "find distance" || input == "exit" + possible_inputs = ["list planets", "planet details", "add planet", "find distance", "exit"] + until possible_inputs.any? { |word| word == input } print "Invalid input.\nPlease enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " input = gets.chomp.downcase end return input end -def existing_planets(solar_system) +def existing_planets earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "it's the only planet known to support life") - solar_system.add_planet(earth) + SOLAR_SYSTEM.add_planet(earth) venus = Planet.new("Venus", "yellow", 4.867e24, 67.3e6, "40 spacecraft have explored Venus") - solar_system.add_planet(venus) + SOLAR_SYSTEM.add_planet(venus) mercury = Planet.new("Mercury", "blue", 3.285e23, 28.6e6, "kid-friendly") - solar_system.add_planet(mercury) + SOLAR_SYSTEM.add_planet(mercury) mars = Planet.new("Mars", "orange", 6.39e23, 141.7e6, "it has 2 moons") - solar_system.add_planet(mars) + SOLAR_SYSTEM.add_planet(mars) end -def main - # initializes the solar system's name - solar_system = SolarSystem.new("Sol") - existing_planets(solar_system) - puts "What would you like to do?" - print "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " - input = gets.chomp.downcase - - input = handling_input(input) - - until input == "exit" - if input == "list planets" - list = solar_system.list_planets - puts list.colorize(:green) - end +def list_planets(input) + if input == "list planets" + list = SOLAR_SYSTEM.list_planets + puts list.colorize(:green) + end +end - if input == "planet details" - puts "Which planet do you want to learn about? Choose one: Earth, Venus, Mercury, Mars" - planet_chosen = gets.chomp.downcase - # Optional: when user enters a planet that doesn't exist, raise an exception. - if !solar_system.planets.map { |planet| planet.name }.include?(planet_chosen.capitalize) - raise ArgumentError, "Planet entered is not in the list." - end - puts "\n" - puts solar_system.find_planet_by_name(planet_chosen).summary.colorize(:green) +def planet_details(input) + if input == "planet details" + puts "Which planet do you want to learn about? Choose one: Earth, Venus, Mercury, Mars" + planet_chosen = gets.chomp.downcase + # Optional: when user enters a planet that doesn't exist, raise an exception. + if !SOLAR_SYSTEM.planets.map { |planet| planet.name }.include?(planet_chosen.capitalize) + raise ArgumentError, "Planet entered is not in the list." end + puts "\n" + puts SOLAR_SYSTEM.find_planet_by_name(planet_chosen).summary.colorize(:green) + end +end - if input == "add planet" - puts "What is the planet's name?" - name = gets.chomp.capitalize - puts "What is its color?" - color = gets.chomp - puts "What is its mass(kg)?" +def add_planet(input) + if input == "add planet" + puts "What is the planet's name?" + name = gets.chomp.capitalize + puts "What is its color?" + color = gets.chomp + puts "What is its mass(kg)?" + mass_kg = gets.chomp.to_f + # Optional: if user enters a bad value + until mass_kg > 0 + puts "Mass must be a number greater than 0. Please enter again: " mass_kg = gets.chomp.to_f - # Optional: if user enters a bad value - until mass_kg > 0 - puts "Mass must be a number greater than 0. Please enter again: " - mass_kg = gets.chomp.to_f - end - puts "What is its distance from the Sun in km?" + end + puts "What is its distance from the Sun in km?" + distance_from_sun_km = gets.chomp.to_f + # Optional: if the user enters a bad value + until distance_from_sun_km > 0 + puts "Distance from the Sun must be a number greater than 0. Please enter again: " distance_from_sun_km = gets.chomp.to_f - # Optional: if the user enters a bad value - until distance_from_sun_km > 0 - puts "Distance from the Sun must be a number greater than 0. Please enter again: " - distance_from_sun_km = gets.chomp.to_f - end - puts "What is its fun fact?" - fun_fact = gets.chomp - - planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) - solar_system.add_planet(planet) - puts "#{name} has been added with the details below:".colorize(:green) - puts solar_system.find_planet_by_name(name).summary.colorize(:green) end + puts "What is its fun fact?" + fun_fact = gets.chomp - # OPTIONAL: Implement the optional distance_between method - if input == "find distance" - puts "Enter first planet: " - planet_name1 = gets.chomp.downcase - puts "Enter second planet: " - planet_name2 = gets.chomp.downcase - puts "The distance is #{solar_system.distance_between(planet_name1, planet_name2)}".colorize(:green) - end + planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) + SOLAR_SYSTEM.add_planet(planet) + puts "#{name} has been added with the details below:".colorize(:green) + puts SOLAR_SYSTEM.find_planet_by_name(name).summary.colorize(:green) + end +end + +def find_distance(input) + if input == "find distance" + puts "Enter first planet: " + planet_name1 = gets.chomp.downcase + puts "Enter second planet: " + planet_name2 = gets.chomp.downcase + puts "The distance is #{SOLAR_SYSTEM.distance_between(planet_name1, planet_name2)}".colorize(:green) + end +end + +def main + existing_planets + puts "What would you like to do?" + print "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " + input = gets.chomp.downcase + input = handling_input(input) + + until input == "exit" + list_planets(input) + planet_details(input) + add_planet(input) + find_distance(input) # OPTIONAL: Implement the optional distance_between method puts "What would you like to do next?" puts "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " input = gets.chomp diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb index 36d7f0fb..e9de1d86 100644 --- a/specs/solar_system_spec.rb +++ b/specs/solar_system_spec.rb @@ -1,6 +1,5 @@ require "minitest/autorun" require "minitest/reporters" -require "minitest/skip_dsl" require_relative "../lib/solar_system"