From 3d6b471bd1e209b31dc501dda1430a5ed9d3f0a6 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 13:55:37 -0800 Subject: [PATCH 01/35] created new file w/ Planet class. --- planet.rb | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 planet.rb diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..2df37239 --- /dev/null +++ b/planet.rb @@ -0,0 +1,2 @@ +class Planet +end From 6f7f64af2fbed26fddd10cabbf81b6f740b90ca3 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:01:09 -0800 Subject: [PATCH 02/35] added planet constructor --- planet.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/planet.rb b/planet.rb index 2df37239..e6314eed 100644 --- a/planet.rb +++ b/planet.rb @@ -1,2 +1,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:) + @name = name + @color = color + @mass_kg = mass_kg + @distance_from_sun_km = distance_from_sun_km + @fun_fact = fun_fact + end end From d2bbfbe19529f9b65d09e370796f8fea99e420eb Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:08:02 -0800 Subject: [PATCH 03/35] changed planet param to positional, added main file for main method. --- main.rb | 6 ++++++ planet.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 main.rb diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..7c99c60a --- /dev/null +++ b/main.rb @@ -0,0 +1,6 @@ +require_relative "planet.rb" + +def main.rb +end + +main diff --git a/planet.rb b/planet.rb index e6314eed..a29edb2f 100644 --- a/planet.rb +++ b/planet.rb @@ -1,7 +1,7 @@ class Planet attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact - def initialize(name:, color:, mass_kg:, distance_from_sun_km:, fun_fact:) + def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @name = name @color = color @mass_kg = mass_kg From 7ef773f61da78fa95a0c6219df703275e2e13e72 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:31:43 -0800 Subject: [PATCH 04/35] added minitest setup, beg writing tests. --- Rakefile | 9 +++++++++ main.rb => lib/main.rb | 0 planet.rb => lib/planet.rb | 10 +++++----- specs/planet_spec.rb | 26 ++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 Rakefile rename main.rb => lib/main.rb (100%) rename planet.rb => lib/planet.rb (53%) create mode 100644 specs/planet_spec.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..6200bfcb --- /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 diff --git a/main.rb b/lib/main.rb similarity index 100% rename from main.rb rename to lib/main.rb diff --git a/planet.rb b/lib/planet.rb similarity index 53% rename from planet.rb rename to lib/planet.rb index a29edb2f..1d2e4c6f 100644 --- a/planet.rb +++ b/lib/planet.rb @@ -2,10 +2,10 @@ class Planet attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) - @name = name - @color = color - @mass_kg = mass_kg - @distance_from_sun_km = distance_from_sun_km - @fun_fact = fun_fact + # @name = name + # @color = color + # @mass_kg = mass_kg + # @distance_from_sun_km = distance_from_sun_km + # @fun_fact = fun_fact end end diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb new file mode 100644 index 00000000..bb62cc4f --- /dev/null +++ b/specs/planet_spec.rb @@ -0,0 +1,26 @@ +gem "minitest", ">= 5.0.0" +require "minitest/pride" +require "minitest/autorun" +require_relative "../lib/planet" + +describe "Planet" do + let (:earth) { + Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") + } + + it "is an instance of Planet" do + expect(earth).must_be_instance_of Planet + end + + it "has a name" do + expect(earth.name).must_equal "Earth" + end + + it "has a color" do + expect(earth.color).must_equal "blue-green" + end + + it "has a mass" do + expect(earth.mass_kg).must_be_close_to 5.972e24, 0.01 + end +end From e1a5fd9188498d554195579157f9fcc58346d321 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:34:30 -0800 Subject: [PATCH 05/35] finished writind first wave of tests. --- specs/planet_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb index bb62cc4f..adbdc2d0 100644 --- a/specs/planet_spec.rb +++ b/specs/planet_spec.rb @@ -23,4 +23,12 @@ it "has a mass" do expect(earth.mass_kg).must_be_close_to 5.972e24, 0.01 end + + it "has a distance from sun" do + expect(earth.distance_from_sun_km).must_be_close_to 1.496e8, 0.01 + end + + it "has a fun fact" do + expect(earth.fun_fact).must_equal "Only planet known to support life" + end end From 7cfe5700511149a2e1f2f6385e6bc8a9064fd3ce Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:35:15 -0800 Subject: [PATCH 06/35] uncommented constructor, all tests pass. --- lib/planet.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/planet.rb b/lib/planet.rb index 1d2e4c6f..a29edb2f 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -2,10 +2,10 @@ class Planet attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) - # @name = name - # @color = color - # @mass_kg = mass_kg - # @distance_from_sun_km = distance_from_sun_km - # @fun_fact = fun_fact + @name = name + @color = color + @mass_kg = mass_kg + @distance_from_sun_km = distance_from_sun_km + @fun_fact = fun_fact end end From 77f41e7d497bd11d86a51fa33c9159b13ccfead0 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:41:29 -0800 Subject: [PATCH 07/35] added summary test of string --- specs/planet_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb index adbdc2d0..33667d34 100644 --- a/specs/planet_spec.rb +++ b/specs/planet_spec.rb @@ -31,4 +31,10 @@ it "has a fun fact" do expect(earth.fun_fact).must_equal "Only planet known to support life" end + + describe "summary" do + it "returns a string" do + expect(earth.summry).must_be_a_kind_of String + end + end end From 8eb900c6c96a119e2ac06329d49597a22dccf245 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:42:57 -0800 Subject: [PATCH 08/35] fixed typos --- specs/planet_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb index 33667d34..9c4725f9 100644 --- a/specs/planet_spec.rb +++ b/specs/planet_spec.rb @@ -34,7 +34,7 @@ describe "summary" do it "returns a string" do - expect(earth.summry).must_be_a_kind_of String + expect(earth.summary).must_be_kind_of String end end end From a59c2a2f45462f8e505b393a6ef07e30d292f376 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:43:26 -0800 Subject: [PATCH 09/35] added summary method. --- lib/planet.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/planet.rb b/lib/planet.rb index a29edb2f..687d9b83 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -8,4 +8,8 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @distance_from_sun_km = distance_from_sun_km @fun_fact = fun_fact end + + def summary + # return "" + end end From 703f0f26c22ececd0a47f692e42185afdb664447 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:55:27 -0800 Subject: [PATCH 10/35] adjusted case of planet params. --- lib/planet.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/planet.rb b/lib/planet.rb index 687d9b83..0bf0e6c3 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -2,14 +2,14 @@ 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 + @name = name.capitalize + @color = color.downcase @mass_kg = mass_kg @distance_from_sun_km = distance_from_sun_km - @fun_fact = fun_fact + @fun_fact = fun_fact.downcase end def summary - # return "" + return "" end end From 2fe30463afaae23e7abbe4c67952e5d8edb57c10 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:58:28 -0800 Subject: [PATCH 11/35] added method test for summary of planet attributes. --- specs/planet_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb index 9c4725f9..8b09ea6b 100644 --- a/specs/planet_spec.rb +++ b/specs/planet_spec.rb @@ -29,12 +29,16 @@ end it "has a fun fact" do - expect(earth.fun_fact).must_equal "Only planet known to support life" + expect(earth.fun_fact).must_equal "only planet known to support life" end describe "summary" do it "returns a string" do expect(earth.summary).must_be_kind_of String end + + it "returns a summary of planet's attributes" do + expect(earth.summary).must_equal "#{earth.name} is a #{earth.color} planet that is #{earth.distance_from_sun_km} km from it's sun. It has a mass of #{earth.mass_kg} kg and is known for #{earth.fun_fact}." + end end end From 9d4f91074c1759525ff402fa2804bf47626a9bab Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 14:59:18 -0800 Subject: [PATCH 12/35] added return summary statements. all tests are up to date and passing. --- lib/planet.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/planet.rb b/lib/planet.rb index 0bf0e6c3..cc71c875 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -10,6 +10,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "" + return "#{name} is a #{color} planet that is #{distance_from_sun_km} km from it's sun. It has a mass of #{mass_kg} kg and is known for #{fun_fact}." end end From 3d7521cf7da15fdfe162455ae34f7598b87ffaa9 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 15:09:00 -0800 Subject: [PATCH 13/35] added tests for edge case for mass and distance == 0. --- specs/planet_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb index 8b09ea6b..707381ca 100644 --- a/specs/planet_spec.rb +++ b/specs/planet_spec.rb @@ -41,4 +41,18 @@ expect(earth.summary).must_equal "#{earth.name} is a #{earth.color} planet that is #{earth.distance_from_sun_km} km from it's sun. It has a mass of #{earth.mass_kg} kg and is known for #{earth.fun_fact}." end end + + describe "test edge case for distance and mass" do + it "0 mass will raise argument error" do + expect { + Planet.new("Invisible", "clear", 0, 234230, " located everywhere, but nowhere") + }.must_raise ArgumentError + end + + it "0 distance will raise argument error" do + expect { + Planet.new("Rock", "jelly", 34535555, 0, " located everywhere, but nowhere") + }.must_raise ArgumentError + end + end end From ba9f58ef6eae5e174d1933f60aa5b6894f356c88 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 15:14:18 -0800 Subject: [PATCH 14/35] added error checking for params of distance and mass --- lib/planet.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/planet.rb b/lib/planet.rb index cc71c875..0b2a7b59 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -4,8 +4,8 @@ class Planet def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @name = name.capitalize @color = color.downcase - @mass_kg = mass_kg - @distance_from_sun_km = distance_from_sun_km + @mass_kg = mass_kg > 0 ? mass_kg : (raise ArgumentError.new("Mass must be greater than 0.")) + @distance_from_sun_km = distance_from_sun_km > 0 ? distance_from_sun_km : (raise ArgumentError.new("Distance must be greater than 0.")) @fun_fact = fun_fact.downcase end From 8f694fa54b7da219d7256a466048ce9d018b3d53 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 15:35:39 -0800 Subject: [PATCH 15/35] added beg tests --- specs/solar_system_spec.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 specs/solar_system_spec.rb diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb new file mode 100644 index 00000000..a6b392f8 --- /dev/null +++ b/specs/solar_system_spec.rb @@ -0,0 +1,21 @@ +gem "minitest", ">= 5.0.0" +require "minitest/pride" +require "minitest/autorun" +require_relative "../lib/solar_system" + +describe "SolarSystem" do + let (:test_system) { + SolarSystem.new("sol") + } + it "is an instance of SolarSystem" do + expect(:test_system).must_be_instance_of SolarSystem + end + + it "has a name" do + expect(:test_system.name).must_equal "sol" + end + + it "has planets" do + # expect(:test_system.planets) + end +end From 52b002fef9b939b0b1c426dfccf9a25d3fc23606 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 15:36:12 -0800 Subject: [PATCH 16/35] added SolarSystem class --- lib/solar_system.rb | 2 ++ 1 file changed, 2 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..47d47c4a --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,2 @@ +class SolarSystem +end From a641b901b4d3176c3b2e3144feafa1b2d50586b7 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 15:42:17 -0800 Subject: [PATCH 17/35] added constructor, intial tests pass. --- lib/solar_system.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 47d47c4a..4512389a 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -1,2 +1,7 @@ class SolarSystem + attr_reader :name, :planets + + def initialize(name) + @name = name + end end From 063a5277dffc22fc78e4f0881486d218910839c7 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 16:08:26 -0800 Subject: [PATCH 18/35] added add_planet tests and methods. test pass. --- lib/solar_system.rb | 6 ++++++ specs/solar_system_spec.rb | 15 ++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 4512389a..6be43642 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -3,5 +3,11 @@ class SolarSystem def initialize(name) @name = name + @planets = Array.new + end + + def add_planet(instance_of_planet) + @planets.push(instance_of_planet) + return true end end diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb index a6b392f8..5641b8ac 100644 --- a/specs/solar_system_spec.rb +++ b/specs/solar_system_spec.rb @@ -2,20 +2,25 @@ require "minitest/pride" require "minitest/autorun" require_relative "../lib/solar_system" +require_relative "../lib/planet" describe "SolarSystem" do - let (:test_system) { + let (:sol_system) { SolarSystem.new("sol") } it "is an instance of SolarSystem" do - expect(:test_system).must_be_instance_of SolarSystem + expect(sol_system).must_be_instance_of SolarSystem end it "has a name" do - expect(:test_system.name).must_equal "sol" + expect(sol_system.name).must_equal "sol" end - it "has planets" do - # expect(:test_system.planets) + describe "SolarSystem#add_planet" do + it "has a plant instance added to planets" do + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") + sol_system.add_planet(earth) + expect(sol_system.planets).must_include earth + end end end From bb81e722baa5112e0528878cf1b9518bd625c727 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 16:10:05 -0800 Subject: [PATCH 19/35] added require_relative path for solar_system.rb --- lib/main.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/main.rb b/lib/main.rb index 7c99c60a..35e167b2 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,4 +1,5 @@ require_relative "planet.rb" +require_relative "solar_system.rb" def main.rb end From d523f390d8f1110e25db3a6a3965f286bb9f7326 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 16:47:09 -0800 Subject: [PATCH 20/35] added tests and methods for list_planets, add_planets in solar_system. --- lib/solar_system.rb | 10 +++++++++- specs/solar_system_spec.rb | 29 +++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 6be43642..11320dec 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -2,7 +2,7 @@ class SolarSystem attr_reader :name, :planets def initialize(name) - @name = name + @name = name.capitalize @planets = Array.new end @@ -10,4 +10,12 @@ def add_planet(instance_of_planet) @planets.push(instance_of_planet) return true end + + def list_planets + string_planets = "Planets orbiting #{self.name}" + @planets.each_with_index do |planet, i| + string_planets += "\n#{i + 1}. #{planet.name}" + end + return string_planets + end end diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb index 5641b8ac..8de53994 100644 --- a/specs/solar_system_spec.rb +++ b/specs/solar_system_spec.rb @@ -13,14 +13,35 @@ end it "has a name" do - expect(sol_system.name).must_equal "sol" + expect(sol_system.name).must_equal "Sol" end describe "SolarSystem#add_planet" do - it "has a plant instance added to planets" do - earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") - sol_system.add_planet(earth) + let(:earth) { + Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") + } + let(:mars) { + Planet.new("mars", "red", 5.9234, 1.2348, "Only planet thats red and dusty") + } + before do + planets_test = [earth, mars] + planets_test.each do |planet| + sol_system.add_planet(planet) + end + end + + it "has a planet instance added to planets" do expect(sol_system.planets).must_include earth end + + describe "SolarSystem#list_planets" do + it "returns a string" do + expect(sol_system.list_planets).must_be_instance_of String + end + + it "returns formatted list of sol_system" do + expect(sol_system.list_planets).must_equal "Planets orbiting Sol\n1. Earth\n2. Mars" + end + end end end From 4dc77beae0d09f030d20fa0963d193bcce857ec5 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 16:56:07 -0800 Subject: [PATCH 21/35] added solar_system model to main --- lib/main.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index 35e167b2..c795a36c 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,7 +1,15 @@ require_relative "planet.rb" require_relative "solar_system.rb" -def main.rb +def main + solar_system = SolarSystem.new("Sol") + + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") + solar_system.add_planet(Planet.new("Willer", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life")) + solar_system.add_planet(Planet.new("Saver", "amber", 9.34e54, 2.345e23, "first planet in the universe")) + solar_system.add_planet(Planet.new("Kater", "green", 2.342e33, 4.234e43, "only planet to be made of dolphins")) + solar_system.add_planet(Planet.new("Singher", "onyx", 4.23e23, 46.2e23, "only planet to emmit song-like sounds")) + puts solar_system.list_planets end main From 54b279cb9d779a2a49947346411aabb14b692646 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 19:32:40 -0800 Subject: [PATCH 22/35] raise exception if not of type Planet when adding planet, added test to confirm. --- lib/solar_system.rb | 11 +++++++++-- specs/solar_system_spec.rb | 34 +++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 11320dec..025c38e8 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -6,8 +6,8 @@ def initialize(name) @planets = Array.new end - def add_planet(instance_of_planet) - @planets.push(instance_of_planet) + def add_planet(planet) + planet.instance_of?(Planet) ? @planets << planet : (raise ArgumentError.new("Not a Planet")) return true end @@ -18,4 +18,11 @@ def list_planets end return string_planets end + + def find_planet_by_name(name) + @planets.each do |planet| + return planet if planet.name == name.capitalize + end + return "Match not found." + end end diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb index 8de53994..7e25355d 100644 --- a/specs/solar_system_spec.rb +++ b/specs/solar_system_spec.rb @@ -16,24 +16,29 @@ expect(sol_system.name).must_equal "Sol" end - describe "SolarSystem#add_planet" do - let(:earth) { - Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") - } - let(:mars) { - Planet.new("mars", "red", 5.9234, 1.2348, "Only planet thats red and dusty") - } - before do - planets_test = [earth, mars] - planets_test.each do |planet| - sol_system.add_planet(planet) - end + let(:earth) { + Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") + } + let(:mars) { + Planet.new("mars", "red", 5.9234, 1.2348, "Only planet thats red and dusty") + } + before do + planets_test = [earth, mars] + planets_test.each do |planet| + sol_system.add_planet(planet) end - + end + describe "SolarSystem#add_planet" do it "has a planet instance added to planets" do expect(sol_system.planets).must_include earth end + it "raises ArgumentError if arg of type Planet" do + expect { + sol_system.add_planet("Earther") + }.must_raise ArgumentError + end + describe "SolarSystem#list_planets" do it "returns a string" do expect(sol_system.list_planets).must_be_instance_of String @@ -43,5 +48,8 @@ expect(sol_system.list_planets).must_equal "Planets orbiting Sol\n1. Earth\n2. Mars" end end + + describe "SolarSystem#find_planet_by_name" do + end end end From a974a7c897bc8be85e421af05fd43c35a2b00b00 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 19:43:20 -0800 Subject: [PATCH 23/35] added find_planet_by_name methods with tests to confirm. tests pass. --- lib/solar_system.rb | 2 +- specs/solar_system_spec.rb | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 025c38e8..2c145cac 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -23,6 +23,6 @@ def find_planet_by_name(name) @planets.each do |planet| return planet if planet.name == name.capitalize end - return "Match not found." + raise ArgumentError.new("No planet by #{name} found.") end end diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb index 7e25355d..b8e1d5b1 100644 --- a/specs/solar_system_spec.rb +++ b/specs/solar_system_spec.rb @@ -38,18 +38,31 @@ sol_system.add_planet("Earther") }.must_raise ArgumentError end + end + + describe "SolarSystem#list_planets" do + it "returns a string" do + expect(sol_system.list_planets).must_be_instance_of String + end - describe "SolarSystem#list_planets" do - it "returns a string" do - expect(sol_system.list_planets).must_be_instance_of String - end + it "returns formatted list of sol_system" do + expect(sol_system.list_planets).must_equal "Planets orbiting Sol\n1. Earth\n2. Mars" + end + end - it "returns formatted list of sol_system" do - expect(sol_system.list_planets).must_equal "Planets orbiting Sol\n1. Earth\n2. Mars" - end + describe "SolarSystem#find_planet_by_name" do + it "returns instance of planet" do + expect(sol_system.find_planet_by_name("mArS")).must_be_instance_of Planet end - describe "SolarSystem#find_planet_by_name" do + it "returns planet with same name" do + expect(sol_system.find_planet_by_name("mArS").name).must_equal "Mars" + end + + it "raises argument error if no planet by that name" do + expect { + sol_system.find_planet_by_name("whamo") + }.must_raise ArgumentError end end end From 543f8e758c5d29d443f7a153b3216c7f130e73aa Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 19:52:22 -0800 Subject: [PATCH 24/35] changed variables in main, added find_planet_by_name method in main. --- lib/main.rb | 15 +++++++-------- specs/solar_system_spec.rb | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index c795a36c..f84e5f9c 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -2,14 +2,13 @@ require_relative "solar_system.rb" def main - solar_system = SolarSystem.new("Sol") - - earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") - solar_system.add_planet(Planet.new("Willer", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life")) - solar_system.add_planet(Planet.new("Saver", "amber", 9.34e54, 2.345e23, "first planet in the universe")) - solar_system.add_planet(Planet.new("Kater", "green", 2.342e33, 4.234e43, "only planet to be made of dolphins")) - solar_system.add_planet(Planet.new("Singher", "onyx", 4.23e23, 46.2e23, "only planet to emmit song-like sounds")) - puts solar_system.list_planets + err_system = SolarSystem.new("Err") + err_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e8, "raining rainbows")) + err_system.add_planet(Planet.new("Saverr", "amber", 9.34e54, 2.345e23, "saving time by reversing spin")) + err_system.add_planet(Planet.new("Katerr", "green", 2.342e33, 4.234e43, "being made of dolphins")) + err_system.add_planet(Planet.new("Singherr", "onyx", 4.23e23, 46.2e23, "emmitting song-like sounds")) + puts err_system.list_planets + puts err_system.find_planet_by_name("Willerr").summary end main diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb index b8e1d5b1..a0fe37de 100644 --- a/specs/solar_system_spec.rb +++ b/specs/solar_system_spec.rb @@ -55,7 +55,7 @@ expect(sol_system.find_planet_by_name("mArS")).must_be_instance_of Planet end - it "returns planet with same name" do + it "returns planet with same name, ignores mixed up/downcase" do expect(sol_system.find_planet_by_name("mArS").name).must_equal "Mars" end From 9e6efff1c215389a780c19a5483961fb6f08a39e Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 21:18:33 -0800 Subject: [PATCH 25/35] added distance between method and tests, test pass. --- lib/main.rb | 2 +- lib/solar_system.rb | 10 +++++++++- specs/solar_system_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index f84e5f9c..80bc62d0 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -3,7 +3,7 @@ def main err_system = SolarSystem.new("Err") - err_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e8, "raining rainbows")) + err_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e28, "raining rainbows")) err_system.add_planet(Planet.new("Saverr", "amber", 9.34e54, 2.345e23, "saving time by reversing spin")) err_system.add_planet(Planet.new("Katerr", "green", 2.342e33, 4.234e43, "being made of dolphins")) err_system.add_planet(Planet.new("Singherr", "onyx", 4.23e23, 46.2e23, "emmitting song-like sounds")) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 2c145cac..c5640680 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -7,7 +7,8 @@ def initialize(name) end def add_planet(planet) - planet.instance_of?(Planet) ? @planets << planet : (raise ArgumentError.new("Not a Planet")) + raise ArgumentError.new("#{planet.capitalize} is not a Planet") if !planet.instance_of?(Planet) + @planets << planet return true end @@ -25,4 +26,11 @@ def find_planet_by_name(name) end raise ArgumentError.new("No planet by #{name} found.") end + + def distance_between(planet1, planet2) + if planet1.instance_of?(Planet) && planet2.instance_of?(Planet) + return (planet1.distance_from_sun_km - planet2.distance_from_sun_km).abs.to_i + end + raise ArgumentError.new("#{planet1} and/or #{planet2} not of type Planet") + end end diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb index a0fe37de..4ae905f7 100644 --- a/specs/solar_system_spec.rb +++ b/specs/solar_system_spec.rb @@ -65,4 +65,25 @@ }.must_raise ArgumentError end end + + describe "SolarSystem#distance_between" do + it "returns an integer" do + expect(sol_system.distance_between(earth, mars)).must_be_instance_of Integer + end + + it "returns positive value" do + expect(sol_system.distance_between(mars, earth) >= 0).must_equal true + expect(sol_system.distance_between(earth, mars) >= 0).must_equal true + end + + it "raises ArgumentError if args not type Planet" do + expect { + sol_system.distance_between(mars, "earth") + }.must_raise ArgumentError + + expect { + sol_system.distance_between("earth", mars) + }.must_raise ArgumentError + end + end end From b6fcf04691ef5e19a75e8fb2c20361a705a665eb Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 21:26:01 -0800 Subject: [PATCH 26/35] added is_a_Planet? method. --- lib/solar_system.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index c5640680..0f3fc079 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -7,7 +7,7 @@ def initialize(name) end def add_planet(planet) - raise ArgumentError.new("#{planet.capitalize} is not a Planet") if !planet.instance_of?(Planet) + raise ArgumentError.new("#{planet.capitalize} is not a Planet") if !is_a_Planet?(planet) @planets << planet return true end @@ -28,9 +28,13 @@ def find_planet_by_name(name) end def distance_between(planet1, planet2) - if planet1.instance_of?(Planet) && planet2.instance_of?(Planet) + if is_a_Planet?(planet1) && is_a_Planet?(planet2) return (planet1.distance_from_sun_km - planet2.distance_from_sun_km).abs.to_i end raise ArgumentError.new("#{planet1} and/or #{planet2} not of type Planet") end + + def is_a_Planet?(planet) + return planet.instance_of?(Planet) + end end From b077a0f58ff0087ff64d2d22d0b2fd39699ccca9 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 25 Feb 2019 22:29:40 -0800 Subject: [PATCH 27/35] created layout of main. --- lib/main.rb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 80bc62d0..27e623b4 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -2,13 +2,32 @@ require_relative "solar_system.rb" def main + create_system + continue = true + while continue + display_options + continue = do_option(get_option) + end +end + +def create_system err_system = SolarSystem.new("Err") err_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e28, "raining rainbows")) err_system.add_planet(Planet.new("Saverr", "amber", 9.34e54, 2.345e23, "saving time by reversing spin")) err_system.add_planet(Planet.new("Katerr", "green", 2.342e33, 4.234e43, "being made of dolphins")) err_system.add_planet(Planet.new("Singherr", "onyx", 4.23e23, 46.2e23, "emmitting song-like sounds")) - puts err_system.list_planets - puts err_system.find_planet_by_name("Willerr").summary +end + +def display_options + puts "options" +end + +def get_option + return "option" +end + +def do_option(option) + return false if option == "option" end main From b9c2ce72fd04390cb92a0edb8c192d9bb6c242f6 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Tue, 26 Feb 2019 13:14:27 -0800 Subject: [PATCH 28/35] streamed lined main outline --- lib/main.rb | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 27e623b4..34840dd5 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -2,32 +2,54 @@ require_relative "solar_system.rb" def main - create_system + display(load_solar_sytem_program) continue = true while continue - display_options + display(options) continue = do_option(get_option) end + display(good_bye) end -def create_system +def display(string) + puts string + return true +end + +def load_solar_sytem_program err_system = SolarSystem.new("Err") - err_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e28, "raining rainbows")) - err_system.add_planet(Planet.new("Saverr", "amber", 9.34e54, 2.345e23, "saving time by reversing spin")) - err_system.add_planet(Planet.new("Katerr", "green", 2.342e33, 4.234e43, "being made of dolphins")) - err_system.add_planet(Planet.new("Singherr", "onyx", 4.23e23, 46.2e23, "emmitting song-like sounds")) + create_system(err_system) + return "Welcome to the Solar System Program. \n ou are in the Solar System of #{err_system.name}." end -def display_options - puts "options" +def create_system(solar_system) + solar_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e28, "raining rainbows")) + solar_system.add_planet(Planet.new("Saverr", "amber", 9.34e54, 2.345e23, "saving time by reversing spin")) + solar_system.add_planet(Planet.new("Katerr", "green", 2.342e33, 4.234e43, "being made of dolphins")) + solar_system.add_planet(Planet.new("Singherr", "onyx", 4.23e23, 46.2e23, "emmitting song-like sounds")) end -def get_option - return "option" +def options + return "\nEnter LIST to display all the planets in system." + +"\nEnter ADD to add planets" + +"\mEnter CALC to calculate the distance between two planets" + +"\nEnter INFO to display information about a planet" + +"\nEnter Q to quit program" end -def do_option(option) - return false if option == "option" +def get_option + print "Please Enter Selection: " + user_input = gets.chomp.upcase + return user_input if ["LIST", "ADD", "CALC", "INFO", "Q"].include?(user_input) + puts "Hmm... I don't understand." + get_option end -main +def do_option(option_key) + case option_key + when "LIST" + when "ADD" + when "CALC" + when "Q" + end +end From 5d1389aeb47092cb37c05358d633c723bed3ca30 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Tue, 26 Feb 2019 13:27:33 -0800 Subject: [PATCH 29/35] added missing methods, put in module, still working out main structure --- lib/main.rb | 102 +++++++++++++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 44 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 34840dd5..dd8fd040 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,55 +1,69 @@ require_relative "planet.rb" require_relative "solar_system.rb" -def main - display(load_solar_sytem_program) - continue = true - while continue - display(options) - continue = do_option(get_option) - end - display(good_bye) -end +module CLI_SOLAR_SYSTEM + attr_reader :solar_system -def display(string) - puts string - return true -end + def main + display(load_solar_sytem_program) + continue = true + while continue + display(options) + continue = do_option(get_option) + end + display(good_bye) + end -def load_solar_sytem_program - err_system = SolarSystem.new("Err") - create_system(err_system) - return "Welcome to the Solar System Program. \n ou are in the Solar System of #{err_system.name}." -end + def display(string) + puts string + return true + end -def create_system(solar_system) - solar_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e28, "raining rainbows")) - solar_system.add_planet(Planet.new("Saverr", "amber", 9.34e54, 2.345e23, "saving time by reversing spin")) - solar_system.add_planet(Planet.new("Katerr", "green", 2.342e33, 4.234e43, "being made of dolphins")) - solar_system.add_planet(Planet.new("Singherr", "onyx", 4.23e23, 46.2e23, "emmitting song-like sounds")) -end + def load_solar_sytem_program + solar_system = SolarSystem.new("Err") + create_system(solar_system) + return "Welcome to the Solar System Program. \n ou are in the Solar System of #{err_system.name}." + end -def options - return "\nEnter LIST to display all the planets in system." - +"\nEnter ADD to add planets" - +"\mEnter CALC to calculate the distance between two planets" - +"\nEnter INFO to display information about a planet" - +"\nEnter Q to quit program" -end + def create_system(solar_system) + solar_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e28, "raining rainbows")) + solar_system.add_planet(Planet.new("Saverr", "amber", 9.34e54, 2.345e23, "saving time by reversing spin")) + solar_system.add_planet(Planet.new("Katerr", "green", 2.342e33, 4.234e43, "being made of dolphins")) + solar_system.add_planet(Planet.new("Singherr", "onyx", 4.23e23, 46.2e23, "emmitting song-like sounds")) + end -def get_option - print "Please Enter Selection: " - user_input = gets.chomp.upcase - return user_input if ["LIST", "ADD", "CALC", "INFO", "Q"].include?(user_input) - puts "Hmm... I don't understand." - get_option -end + def options + return "\nEnter LIST to display all the planets in system." + +"\nEnter ADD to add planets" + +"\mEnter CALC to calculate the distance between two planets" + +"\nEnter INFO to display information about a planet" + +"\nEnter Q to quit program" + end + + def get_option + print "Please Enter Selection: " + user_input = gets.chomp.upcase + return user_input if ["LIST", "ADD", "CALC", "INFO", "Q"].include?(user_input) + puts "Hmm... I don't understand." + get_option + end + + def do_option(option_key) + case option_key + when "LIST" + display(solar_system.list_planets) + when "ADD" + user_add_planet + when "CALC" + user_calc_distance + when "Q" + return false + end + end + + def user_add_planet + end -def do_option(option_key) - case option_key - when "LIST" - when "ADD" - when "CALC" - when "Q" + def user_calc_distance end end From d34dcab5cee0714c4eea587c328035a962bacb9f Mon Sep 17 00:00:00 2001 From: qqdipps Date: Tue, 26 Feb 2019 19:32:03 -0800 Subject: [PATCH 30/35] added main helper methods, and exception handling for CLI --- lib/main.rb | 164 ++++++++++++++++++++++++++++++-------------- lib/planet.rb | 2 +- lib/solar_system.rb | 2 +- 3 files changed, 113 insertions(+), 55 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index dd8fd040..ef423bd8 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,69 +1,127 @@ require_relative "planet.rb" require_relative "solar_system.rb" -module CLI_SOLAR_SYSTEM - attr_reader :solar_system - - def main - display(load_solar_sytem_program) - continue = true - while continue - display(options) - continue = do_option(get_option) - end - display(good_bye) +def main + display(load_solar_system_program) + continue = true + while continue + display(options) + continue = do_option(get_option) end + display(good_bye) +end - def display(string) - puts string - return true - end +def display(string) + puts string + return true +end - def load_solar_sytem_program - solar_system = SolarSystem.new("Err") - create_system(solar_system) - return "Welcome to the Solar System Program. \n ou are in the Solar System of #{err_system.name}." - end +def load_solar_system_program + @solar_system = SolarSystem.new("Err") + create_system(solar_system) + return "Welcome to the Solar System Program." \ + + "\n\nYou are in the Solar System of #{solar_system.name}.\n" +end - def create_system(solar_system) - solar_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e28, "raining rainbows")) - solar_system.add_planet(Planet.new("Saverr", "amber", 9.34e54, 2.345e23, "saving time by reversing spin")) - solar_system.add_planet(Planet.new("Katerr", "green", 2.342e33, 4.234e43, "being made of dolphins")) - solar_system.add_planet(Planet.new("Singherr", "onyx", 4.23e23, 46.2e23, "emmitting song-like sounds")) - end +def solar_system + return @solar_system +end - def options - return "\nEnter LIST to display all the planets in system." - +"\nEnter ADD to add planets" - +"\mEnter CALC to calculate the distance between two planets" - +"\nEnter INFO to display information about a planet" - +"\nEnter Q to quit program" - end +def create_system(solar_system) + solar_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e28, + "raining rainbows")) + solar_system.add_planet(Planet.new("Saverr", "amber", 9.34e54, 2.345e23, + "saving time by reversing spin")) + solar_system.add_planet(Planet.new("Katerr", "green", 2.342e33, 4.234e43, + "being made of dolphins")) + solar_system.add_planet(Planet.new("Singherr", "onyx", 4.23e23, 46.2e23, + "emmitting song-like sounds")) +end - def get_option - print "Please Enter Selection: " - user_input = gets.chomp.upcase - return user_input if ["LIST", "ADD", "CALC", "INFO", "Q"].include?(user_input) - puts "Hmm... I don't understand." - get_option - end +def options + return ("\nEnter LIST to display all the planets in system." \ + + "\nEnter ADD to add planets" \ + + "\nEnter CALC to calculate the distance between two planets" \ + + "\nEnter INFO to display information about a planet" \ + + "\nEnter Q to quit program") +end - def do_option(option_key) - case option_key - when "LIST" - display(solar_system.list_planets) - when "ADD" - user_add_planet - when "CALC" - user_calc_distance - when "Q" - return false - end +def get_option + print "\nPlease Enter Selection: " + user_input = gets.chomp.upcase + return user_input if ["LIST", "ADD", "CALC", "INFO", "Q"].include?(user_input) + puts "Hmm... I don't understand \"#{user_input}\"." + get_option +end + +def do_option(option_key) + case option_key + when "LIST" + display(solar_system.list_planets) + when "ADD" + user_add_planet + when "CALC" + user_calc_distance + when "INFO" + display(user_info_planet) + when "Q" + return false end +end - def user_add_planet +# handle exceptions? +def user_add_planet + puts "To add a planet follow the prompts: " + print "Name? " + name = gets.chomp.capitalize + print "Color? " + color = gets.chomp.downcase + print "Mass in kg? " + mass = gets.chomp.to_i + print "Distance from sun in km? " + distance = gets.chomp.to_i + print "Fun fact? " + fun_fact = gets.chomp + begin + solar_system.add_planet(Planet.new(name, color, mass, distance, fun_fact)) + rescue ArgumentError => e + puts "Error: #{e} \nReselect option from main menu to try again." end + return true +end - def user_calc_distance +# handle exceptions? +def user_calc_distance + print "Enter first planet: " + planet1 = get_planet + return true if !planet1 + print "Enter second planet: " + planet2 = get_planet + return true if !planet2 + print "The distance between #{planet1.name} and #{planet2.name} is " + puts "#{solar_system.distance_between(planet1, planet2)} km " + return true +end + +def user_info_planet + print "\nWhich planet would you like more information on? " + planet = get_planet + return planet.summary if planet + return "Try using the numeric tags associated with planets from LIST command." +end + +def get_planet + planet_name = gets.chomp.capitalize + begin + planet = solar_system.find_planet_by_name(planet_name) + rescue ArgumentError + planet = planet_name.to_i.to_s == planet_name ? solar_system.planets[planet_name.to_i - 1] : nil + if !solar_system.is_a_Planet?(planet) + puts "Hmm.. #{planet_name} is not in the system of #{solar_system.name}" + puts "\nReselect option from main menu to try again." + end end + return planet end + +main diff --git a/lib/planet.rb b/lib/planet.rb index 0b2a7b59..42f1b300 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -10,6 +10,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "#{name} is a #{color} planet that is #{distance_from_sun_km} km from it's sun. It has a mass of #{mass_kg} kg and is known for #{fun_fact}." + return "\n#{name} is a #{color} planet that is #{distance_from_sun_km} km from it's sun. \nIt has a mass of #{mass_kg} kg and \nis known for #{fun_fact}." end end diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 0f3fc079..4aed9821 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -13,7 +13,7 @@ def add_planet(planet) end def list_planets - string_planets = "Planets orbiting #{self.name}" + string_planets = "\nPlanets orbiting #{self.name}" @planets.each_with_index do |planet, i| string_planets += "\n#{i + 1}. #{planet.name}" end From 710e3449e0991f9bbbde88801d5ebdc9bd4ced4e Mon Sep 17 00:00:00 2001 From: qqdipps Date: Tue, 26 Feb 2019 22:25:52 -0800 Subject: [PATCH 31/35] adding style --- lib/main.rb | 55 ++++++++++++++++++++++++++++++++------------- lib/solar_system.rb | 5 +++-- solar.txt | 11 +++++++++ 3 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 solar.txt diff --git a/lib/main.rb b/lib/main.rb index ef423bd8..66d08ba7 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,3 +1,5 @@ +require "lolcat" +require "colorize" require_relative "planet.rb" require_relative "solar_system.rb" @@ -11,16 +13,26 @@ def main display(good_bye) end -def display(string) - puts string +def display(string, char = true, lag = 0.03) + if char + count = 0 + string.each_char do |ch| + print ch + sleep lag if count < 36 + count += 1 + end + else + puts string + end return true end def load_solar_system_program @solar_system = SolarSystem.new("Err") + create_system(solar_system) - return "Welcome to the Solar System Program." \ - + "\n\nYou are in the Solar System of #{solar_system.name}.\n" + return "Welcome to the".cyan + " Solar System Program.".light_magenta.bold \ + + read_file("./solar.txt").yellow + "\n\nYou are in the Solar System of #{solar_system.name}.\n".cyan end def solar_system @@ -39,31 +51,36 @@ def create_system(solar_system) end def options - return ("\nEnter LIST to display all the planets in system." \ - + "\nEnter ADD to add planets" \ - + "\nEnter CALC to calculate the distance between two planets" \ - + "\nEnter INFO to display information about a planet" \ - + "\nEnter Q to quit program") + string_of_options = "\nEnter ".green + "LIST".cyan.on_blue.italic + " to display all the planets in system.".green \ + + "\nEnter ".green + "ADD ".cyan.on_blue.italic + " to add planets".green \ + + "\nEnter ".green + "CALC".cyan.on_blue.italic + " to calculate the distance between two planets".green \ + + "\nEnter ".green + "INFO".cyan.on_blue.italic + " to display information about a planet".green \ + + "\nEnter ".green + "Q ".cyan.on_blue.italic + " to quit program\n".green end def get_option - print "\nPlease Enter Selection: " + print "\nPlease Enter Selection: ".cyan user_input = gets.chomp.upcase return user_input if ["LIST", "ADD", "CALC", "INFO", "Q"].include?(user_input) - puts "Hmm... I don't understand \"#{user_input}\"." + "Hmm... ".each_char do |ch| + print ch + sleep 0.09 + end + puts "I don't understand \"#{user_input}\".".red.bold.underline + sleep 1 get_option end def do_option(option_key) case option_key when "LIST" - display(solar_system.list_planets) + display(solar_system.list_planets.yellow) when "ADD" user_add_planet when "CALC" user_calc_distance when "INFO" - display(user_info_planet) + display(user_info_planet.cyan) when "Q" return false end @@ -84,8 +101,8 @@ def user_add_planet fun_fact = gets.chomp begin solar_system.add_planet(Planet.new(name, color, mass, distance, fun_fact)) - rescue ArgumentError => e - puts "Error: #{e} \nReselect option from main menu to try again." + rescue ArgumentError => error_message + puts "Error: #{error_message} \nReselect option from main menu to try again." end return true end @@ -124,4 +141,12 @@ def get_planet return planet end +def read_file(file) + read = "" + File.open(file, "r") do |f| + read += f.read + end + return read +end + main diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 4aed9821..e4fd535f 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -8,14 +8,15 @@ def initialize(name) def add_planet(planet) raise ArgumentError.new("#{planet.capitalize} is not a Planet") if !is_a_Planet?(planet) + @planets << planet return true end def list_planets - string_planets = "\nPlanets orbiting #{self.name}" + string_planets = "\nPlanets orbiting #{self.name}\n" @planets.each_with_index do |planet, i| - string_planets += "\n#{i + 1}. #{planet.name}" + string_planets += "#{i + 1}. #{planet.name}\n" end return string_planets end diff --git a/solar.txt b/solar.txt new file mode 100644 index 00000000..48895842 --- /dev/null +++ b/solar.txt @@ -0,0 +1,11 @@ + + + * + + ' | + () .-.,="``"=. - o - + '=/_ \ | + * | '=._ | + \ `=./`, ' + . '=.__.=' `=' * + + + + O * ' . \ No newline at end of file From e5abe55beaac18f95dafad882f1c0df33c04087c Mon Sep 17 00:00:00 2001 From: qqdipps Date: Wed, 27 Feb 2019 00:17:33 -0800 Subject: [PATCH 32/35] fixed tests, updated style, added param checking for adding planet if planet is already logged --- distance.txt | 21 +++++++++++++ goodbye.txt | 9 ++++++ lib/main.rb | 61 ++++++++++++++++++++++---------------- lib/planet.rb | 2 +- lib/solar_system.rb | 9 ++++-- planet.txt | 12 ++++++++ specs/planet_spec.rb | 3 +- specs/solar_system_spec.rb | 11 ++++++- 8 files changed, 97 insertions(+), 31 deletions(-) create mode 100644 distance.txt create mode 100644 goodbye.txt create mode 100644 planet.txt diff --git a/distance.txt b/distance.txt new file mode 100644 index 00000000..e17e3b5f --- /dev/null +++ b/distance.txt @@ -0,0 +1,21 @@ + + + ________ + `---. `. + \ `. + )_______`. + .' //`---...___ + / || // || `-._ + )`-| =//= || || || / ).`. + _............_ ).-| || || `........'`-._ (o) + .-' `.----`. _...'.....__ || || _____ ||-\__.' + .' | Inter- | ).---.) /_______..--' || || ----- _ ||_/ +.'_ |Plantery| || || `-------' || || =\\= \_.' +| | | Travel | |'---'| )`-| || || _..-' +'--'_____________|_____| ).-| =\\= || \\ _.-' || +|[]--------------/ / __==\ \\ _.-' /o'\ + \ .--. / _...--'' '-.__......_.-' \__/ + `-._//'o\\___.'---'' \ .' + LGB \__/ -' / / + ___.' / + `-------' \ No newline at end of file diff --git a/goodbye.txt b/goodbye.txt new file mode 100644 index 00000000..b225f78c --- /dev/null +++ b/goodbye.txt @@ -0,0 +1,9 @@ + + + __ + \ \_____ +###[==_____> + /_/ __ + \ \_____ + ###[==_____> + /_/ diff --git a/lib/main.rb b/lib/main.rb index 66d08ba7..815c63b4 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,4 +1,3 @@ -require "lolcat" require "colorize" require_relative "planet.rb" require_relative "solar_system.rb" @@ -7,7 +6,7 @@ def main display(load_solar_system_program) continue = true while continue - display(options) + puts options continue = do_option(get_option) end display(good_bye) @@ -18,7 +17,7 @@ def display(string, char = true, lag = 0.03) count = 0 string.each_char do |ch| print ch - sleep lag if count < 36 + sleep lag if count < 100 count += 1 end else @@ -51,21 +50,19 @@ def create_system(solar_system) end def options - string_of_options = "\nEnter ".green + "LIST".cyan.on_blue.italic + " to display all the planets in system.".green \ + string_of_options = "\n\nEnter ".green + "LIST".cyan.on_blue.italic + " to display all the planets in system.".green \ + "\nEnter ".green + "ADD ".cyan.on_blue.italic + " to add planets".green \ + "\nEnter ".green + "CALC".cyan.on_blue.italic + " to calculate the distance between two planets".green \ + "\nEnter ".green + "INFO".cyan.on_blue.italic + " to display information about a planet".green \ + "\nEnter ".green + "Q ".cyan.on_blue.italic + " to quit program\n".green + return string_of_options end def get_option print "\nPlease Enter Selection: ".cyan user_input = gets.chomp.upcase return user_input if ["LIST", "ADD", "CALC", "INFO", "Q"].include?(user_input) - "Hmm... ".each_char do |ch| - print ch - sleep 0.09 - end + display("Hmm... ", true, 0.5) puts "I don't understand \"#{user_input}\".".red.bold.underline sleep 1 get_option @@ -81,50 +78,60 @@ def do_option(option_key) user_calc_distance when "INFO" display(user_info_planet.cyan) + sleep 1 when "Q" return false end end -# handle exceptions? def user_add_planet - puts "To add a planet follow the prompts: " - print "Name? " + display("\nTo add a planet follow the prompts: \n".yellow) + print "Name? ".yellow name = gets.chomp.capitalize - print "Color? " + print "Color? ".yellow color = gets.chomp.downcase - print "Mass in kg? " + print "Mass in kg? ".yellow mass = gets.chomp.to_i - print "Distance from sun in km? " + print "Distance from sun in km? ".yellow distance = gets.chomp.to_i - print "Fun fact? " + print "Fun fact? ".yellow fun_fact = gets.chomp begin solar_system.add_planet(Planet.new(name, color, mass, distance, fun_fact)) + display("\nCongratulations #{solar_system.planets[-1].name} Succesfully Added!!".cyan.on_blue) + puts read_file("./planet.txt").light_magenta rescue ArgumentError => error_message - puts "Error: #{error_message} \nReselect option from main menu to try again." + display("\nError: #{error_message} \nReselect option from main menu to try again.".red.underline.bold) end return true end -# handle exceptions? def user_calc_distance - print "Enter first planet: " + print "\nEnter first planet: ".yellow planet1 = get_planet return true if !planet1 - print "Enter second planet: " + print "Enter second planet: ".yellow planet2 = get_planet return true if !planet2 - print "The distance between #{planet1.name} and #{planet2.name} is " - puts "#{solar_system.distance_between(planet1, planet2)} km " + begin + distance = solar_system.distance_between(planet1, planet2) + puts read_file("./distance.txt").blue.bold + display("\nDirections from #{planet1.name.upcase} to #{planet2.name.upcase}:".light_magenta) + display("ONWARD -> ".cyan.on_blue) + display("%0.3e km ".cyan.on_blue % [distance]) + display("\nIt will take %0.2e light years, expected day of arrival: May 4th".magenta % [distance / 1.057e13]) + sleep 1 + rescue ArgumentError => error_message + display("\nError: #{error_message} \nReselect option from main menu to try again.".red.underline.bold) + end return true end def user_info_planet - print "\nWhich planet would you like more information on? " + print "\nWhich planet would you like more information on? ".yellow planet = get_planet return planet.summary if planet - return "Try using the numeric tags associated with planets from LIST command." + return "Try using the numeric tags associated with planets from LIST command.".red end def get_planet @@ -134,8 +141,8 @@ def get_planet rescue ArgumentError planet = planet_name.to_i.to_s == planet_name ? solar_system.planets[planet_name.to_i - 1] : nil if !solar_system.is_a_Planet?(planet) - puts "Hmm.. #{planet_name} is not in the system of #{solar_system.name}" - puts "\nReselect option from main menu to try again." + display("Hmm... ", true, 0.5) + puts "#{planet_name} is not in the system of #{solar_system.name}".cyan end end return planet @@ -149,4 +156,8 @@ def read_file(file) return read end +def good_bye + return "Goodbye! Visit the #{solar_system.name} system again soon! Safe Travels!\n".cyan.on_blue +end + main diff --git a/lib/planet.rb b/lib/planet.rb index 42f1b300..2103668c 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -10,6 +10,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "\n#{name} is a #{color} planet that is #{distance_from_sun_km} km from it's sun. \nIt has a mass of #{mass_kg} kg and \nis known for #{fun_fact}." + return "\n#{name} is a #{color} planet that is %0.2e km from it's sun. \nIt has a mass of %0.2e kg and \nis known for #{fun_fact}." % [distance_from_sun_km, mass_kg] end end diff --git a/lib/solar_system.rb b/lib/solar_system.rb index e4fd535f..82a09204 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -8,8 +8,13 @@ def initialize(name) def add_planet(planet) raise ArgumentError.new("#{planet.capitalize} is not a Planet") if !is_a_Planet?(planet) - - @planets << planet + if planets.any? do |logged_planet| + planet.name == logged_planet.name + end + raise ArgumentError.new("#{planet.name}\'s name is in use.") + else + @planets << planet + end return true end diff --git a/planet.txt b/planet.txt new file mode 100644 index 00000000..5ca9fbc0 --- /dev/null +++ b/planet.txt @@ -0,0 +1,12 @@ + + .::. + .:' .: + ,MMM8&&&.:' .:' + MMMMM88&&&& .:' + MMMMM88&&&&&&:' + MMMMM88&&&&&& + .:MMMMM88&&&&&& + .:' MMMMM88&&&& +.:' .:'MMM8&&&' +:' .:' +'::' \ No newline at end of file diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb index 707381ca..0e60f214 100644 --- a/specs/planet_spec.rb +++ b/specs/planet_spec.rb @@ -36,9 +36,8 @@ it "returns a string" do expect(earth.summary).must_be_kind_of String end - it "returns a summary of planet's attributes" do - expect(earth.summary).must_equal "#{earth.name} is a #{earth.color} planet that is #{earth.distance_from_sun_km} km from it's sun. It has a mass of #{earth.mass_kg} kg and is known for #{earth.fun_fact}." + expect(earth.summary).must_equal "\nEarth is a blue-green planet that is 1.50e+08 km from it's sun. \nIt has a mass of 5.97e+24 kg and \nis known for only planet known to support life." end end diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb index 4ae905f7..9e9c386b 100644 --- a/specs/solar_system_spec.rb +++ b/specs/solar_system_spec.rb @@ -22,6 +22,9 @@ let(:mars) { Planet.new("mars", "red", 5.9234, 1.2348, "Only planet thats red and dusty") } + let(:mars2) { + Planet.new("mars", "red2", 25.9234, 12.2348, "Onlasdfy planet thats red and dusty") + } before do planets_test = [earth, mars] planets_test.each do |planet| @@ -38,6 +41,12 @@ sol_system.add_planet("Earther") }.must_raise ArgumentError end + + it "raises ArgumentError if name is in use by another planet" do + expect { + sol_system.add_planet(mars2) + }.must_raise ArgumentError + end end describe "SolarSystem#list_planets" do @@ -46,7 +55,7 @@ end it "returns formatted list of sol_system" do - expect(sol_system.list_planets).must_equal "Planets orbiting Sol\n1. Earth\n2. Mars" + expect(sol_system.list_planets).must_equal "\nPlanets orbiting Sol\n1. Earth\n2. Mars\n" end end From a111dc4c4b7dbf032b72d3aecec41d8734cc065e Mon Sep 17 00:00:00 2001 From: qqdipps Date: Wed, 27 Feb 2019 00:32:32 -0800 Subject: [PATCH 33/35] final txt ascii art added --- lib/main.rb | 2 +- planet.txt | 2 +- solar.txt | 2 +- unicorn.txt | 17 +++++++++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 unicorn.txt diff --git a/lib/main.rb b/lib/main.rb index 815c63b4..d07945ef 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -157,7 +157,7 @@ def read_file(file) end def good_bye - return "Goodbye! Visit the #{solar_system.name} system again soon! Safe Travels!\n".cyan.on_blue + return "\n\nGoodbye! Visit the #{solar_system.name} system again soon! Safe Travels!\n".magenta.bold.italic.underline + read_file("./unicorn.txt").light_magenta.bold + "\n" end main diff --git a/planet.txt b/planet.txt index 5ca9fbc0..a4bd805c 100644 --- a/planet.txt +++ b/planet.txt @@ -9,4 +9,4 @@ .:' MMMMM88&&&& .:' .:'MMM8&&&' :' .:' -'::' \ No newline at end of file +'::' jgs \ No newline at end of file diff --git a/solar.txt b/solar.txt index 48895842..51948f93 100644 --- a/solar.txt +++ b/solar.txt @@ -8,4 +8,4 @@ \ `=./`, ' . '=.__.=' `=' * + + - O * ' . \ No newline at end of file + O * ' . jgs \ No newline at end of file diff --git a/unicorn.txt b/unicorn.txt new file mode 100644 index 00000000..8de66371 --- /dev/null +++ b/unicorn.txt @@ -0,0 +1,17 @@ + . + . . . . . . + . . . * + . * . . . . . . + . + "You Are Here" . . + . . . +. | . . . . . . + | . . . +. + . + \|/ . . . . + . . V . * . . . . + . + + . . . + + . . + .+. . + . . . + . . . . . + . . . . . . . . ! / + * . . . + . . - O - + . . . + . . * . . / | + . + . . . .. + . +. . . . * . * . +.. . * + . . . . . . . . + . . + \ No newline at end of file From 2b4f52acb35f6a18e7ce996e47b1c2bff0e429b8 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Wed, 27 Feb 2019 00:36:18 -0800 Subject: [PATCH 34/35] added file for art --- distance.txt => art/distance.txt | 0 goodbye.txt => art/goodbye.txt | 0 planet.txt => art/planet.txt | 0 solar.txt => art/solar.txt | 0 unicorn.txt => art/unicorn.txt | 0 lib/main.rb | 8 ++++---- 6 files changed, 4 insertions(+), 4 deletions(-) rename distance.txt => art/distance.txt (100%) rename goodbye.txt => art/goodbye.txt (100%) rename planet.txt => art/planet.txt (100%) rename solar.txt => art/solar.txt (100%) rename unicorn.txt => art/unicorn.txt (100%) diff --git a/distance.txt b/art/distance.txt similarity index 100% rename from distance.txt rename to art/distance.txt diff --git a/goodbye.txt b/art/goodbye.txt similarity index 100% rename from goodbye.txt rename to art/goodbye.txt diff --git a/planet.txt b/art/planet.txt similarity index 100% rename from planet.txt rename to art/planet.txt diff --git a/solar.txt b/art/solar.txt similarity index 100% rename from solar.txt rename to art/solar.txt diff --git a/unicorn.txt b/art/unicorn.txt similarity index 100% rename from unicorn.txt rename to art/unicorn.txt diff --git a/lib/main.rb b/lib/main.rb index d07945ef..5ab10360 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -31,7 +31,7 @@ def load_solar_system_program create_system(solar_system) return "Welcome to the".cyan + " Solar System Program.".light_magenta.bold \ - + read_file("./solar.txt").yellow + "\n\nYou are in the Solar System of #{solar_system.name}.\n".cyan + + read_file("./art/solar.txt").yellow + "\n\nYou are in the Solar System of #{solar_system.name}.\n".cyan end def solar_system @@ -99,7 +99,7 @@ def user_add_planet begin solar_system.add_planet(Planet.new(name, color, mass, distance, fun_fact)) display("\nCongratulations #{solar_system.planets[-1].name} Succesfully Added!!".cyan.on_blue) - puts read_file("./planet.txt").light_magenta + puts read_file("./art./planet.txt").light_magenta rescue ArgumentError => error_message display("\nError: #{error_message} \nReselect option from main menu to try again.".red.underline.bold) end @@ -115,7 +115,7 @@ def user_calc_distance return true if !planet2 begin distance = solar_system.distance_between(planet1, planet2) - puts read_file("./distance.txt").blue.bold + puts read_file("./art./distance.txt").blue.bold display("\nDirections from #{planet1.name.upcase} to #{planet2.name.upcase}:".light_magenta) display("ONWARD -> ".cyan.on_blue) display("%0.3e km ".cyan.on_blue % [distance]) @@ -157,7 +157,7 @@ def read_file(file) end def good_bye - return "\n\nGoodbye! Visit the #{solar_system.name} system again soon! Safe Travels!\n".magenta.bold.italic.underline + read_file("./unicorn.txt").light_magenta.bold + "\n" + return "\n\nGoodbye! Visit the #{solar_system.name} system again soon! Safe Travels!\n".magenta.bold.italic.underline + read_file("./art./unicorn.txt").light_magenta.bold + "\n" end main From d9fad6991b8e534c40322a388b7293b346368f96 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Wed, 27 Feb 2019 01:36:53 -0800 Subject: [PATCH 35/35] fixed broken file paths --- lib/main.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 5ab10360..fc7e546b 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -99,7 +99,7 @@ def user_add_planet begin solar_system.add_planet(Planet.new(name, color, mass, distance, fun_fact)) display("\nCongratulations #{solar_system.planets[-1].name} Succesfully Added!!".cyan.on_blue) - puts read_file("./art./planet.txt").light_magenta + puts read_file("./art/planet.txt").light_magenta rescue ArgumentError => error_message display("\nError: #{error_message} \nReselect option from main menu to try again.".red.underline.bold) end @@ -115,7 +115,7 @@ def user_calc_distance return true if !planet2 begin distance = solar_system.distance_between(planet1, planet2) - puts read_file("./art./distance.txt").blue.bold + puts read_file("./art/distance.txt").blue.bold display("\nDirections from #{planet1.name.upcase} to #{planet2.name.upcase}:".light_magenta) display("ONWARD -> ".cyan.on_blue) display("%0.3e km ".cyan.on_blue % [distance]) @@ -157,7 +157,7 @@ def read_file(file) end def good_bye - return "\n\nGoodbye! Visit the #{solar_system.name} system again soon! Safe Travels!\n".magenta.bold.italic.underline + read_file("./art./unicorn.txt").light_magenta.bold + "\n" + return "\n\nGoodbye! Visit the #{solar_system.name} system again soon! Safe Travels!\n".magenta.bold.italic.underline + read_file("./art/unicorn.txt").light_magenta.bold + "\n" end main