From 83d5bbbe0c8bcc4c62daa3181d46b2ac7f1a2fa1 Mon Sep 17 00:00:00 2001 From: laneia Date: Tue, 26 Feb 2019 11:58:17 -0800 Subject: [PATCH 01/11] Initial work waves 1-3 --- main.rb | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ planet.rb | 15 +++++++ solar_system.rb | 33 +++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 main.rb create mode 100644 planet.rb create mode 100644 solar_system.rb diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..5dc5ff30 --- /dev/null +++ b/main.rb @@ -0,0 +1,110 @@ +require_relative "planet" +require_relative "solar_system" + +def add_new_planet + puts "Okay, what's the name of the planet we're missing?" + name = gets.chomp + puts "Cool, what color is it?" + color = gets.chomp + puts "What about its mass in kilograms?" + mass_kg = gets.chomp + puts "Now I need its distance from the sun in kilometers." + distance_from_sun_km = gets.chomp + puts "And finally, what's a fun fact about this planet?" + fun_fact = gets.chomp + + new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) + #return new_planet +end + +def main + solar_system = SolarSystem.new("Sol") + + mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.") + solar_system.add_planet(mercury) + + venus = Planet.new("Venus", "yellow", 4.87e24, 108.2e6, "it probably had a moon, but collided with it after another impact reversed the planet's spin.") + solar_system.add_planet(venus) + + earth = Planet.new("Earth", "blue", 5.97e24, 149.6e6, "it has more life than the rest of the known universe, as far as we know.") + solar_system.add_planet(earth) + + mars = Planet.new("Mars", "red", 0.64e24, 227.9e6, "it has liquid water.") + solar_system.add_planet(mars) + + jupiter = Planet.new("Jupiter", "orange", 1898e24, 778.3e6, "its gravity breaks apart many comets.") + solar_system.add_planet(jupiter) + + saturn = Planet.new("Saturn", "gold", 569e24, 1427e6, "it's not known how or when its rings formed.") + solar_system.add_planet(saturn) + + uranus = Planet.new("Uranus", "blue", 86.8e24, 2871e6, "it also has large storms in its atmosphere, like Jupiter.") + solar_system.add_planet(uranus) + + neptune = Planet.new("Neptune", "blue", 102e24, 5913e6, "the wind can go as fast as 1,700 km/hr - faster than the speed of sound.") + solar_system.add_planet(neptune) + + continue = true + + while continue == true + puts "What would you like to do next?" + puts "list planets | planet details | add planet | exit" + get_input = gets.chomp + if get_input == "exit" + puts "See ya!" + return continue == false + elsif get_input == "list planets" + puts solar_system.list_planets + elsif get_input == "planet details" + puts solar_system.find_planet_by_name + elsif get_input == "add planet" + solar_system.add_planet(add_new_planet) + puts "Planet added to the solar system!" + else + puts "Invalid prompt." + end + end +end + +# unless the_input == "exit" +# if the_input == "list planets" +# puts solar_system.list_planets +# get_input +# elsif the_input == "planet details" +# puts solar_system.find_planet_by_name +# get_input +# elsif the_input == "add planet" +# add_new_planet +# puts solar_system.add_planet(add_new_planet) +# get_input +# else +# puts "Invalid prompt." +# end +# else puts "See ya!" end + +# puts "The farthest planet from Sol is #{neptune.name} at #{neptune.distance_from_sun_km} kilometers!" + +# puts "The biggest planet by far is #{jupiter.name} at #{jupiter.mass_kg} kilograms!" + +# puts saturn.summary + +# list = solar_system.list_planets +# puts list + +# found_planet = solar_system.find_planet_by_name("earth") +# puts found_planet +# puts found_planet.summary + +puts "Welcome to the solar system!" +main + +# def get_input +# puts "What would you like to do next?" +# puts "list planets | planet details | add planet | exit" +# return gets.chomp +# # return user_input +# end + +# puts "What would you like to do next?" +# puts "list planets | planet details | add planet | exit" +# the_input = gets.chomp diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..ee3d8899 --- /dev/null +++ b/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 that weighs #{mass_kg} kilograms and orbits #{@distance_from_sun_km} kilometers from the sun. A cool fact about #{@name} is that #{fun_fact}" + end +end diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..01c21f0b --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,33 @@ +require_relative "planet" + +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + @planet_print = ["Planets orbiting #{star_name}"] + end + + def add_planet(planet) + @planets << planet + end + + def list_planets + @planets.each_index do |index| + @planet_print << "#{index + 1}. #{@planets[index].name}" + end + return @planet_print + end + + def find_planet_by_name + puts "Which planet would you like to learn about?" + input_name = gets.chomp + @planets.each do |each_planet| + if each_planet.name.casecmp(input_name) == 0 + return each_planet.summary + end + end + return "Invalid planet." + end +end From 1966f47070150a0cdfbab495fe428c65213094a8 Mon Sep 17 00:00:00 2001 From: laneia Date: Tue, 26 Feb 2019 13:15:14 -0800 Subject: [PATCH 02/11] Added optional enhancements. --- main.rb | 56 +++++++++++++------------------------------------ solar_system.rb | 18 ++++++++++++++++ 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/main.rb b/main.rb index 5dc5ff30..68cb20d3 100644 --- a/main.rb +++ b/main.rb @@ -8,8 +8,16 @@ def add_new_planet color = gets.chomp puts "What about its mass in kilograms?" mass_kg = gets.chomp + until mass_kg.to_f > 0 + puts "Must be a number greater than zero." + mass_kg = gets.chomp + end puts "Now I need its distance from the sun in kilometers." distance_from_sun_km = gets.chomp + until distance_from_sun_km.to_f > 0 + puts "Must be a number greater than zero." + distance_from_sun_km = gets.chomp + end puts "And finally, what's a fun fact about this planet?" fun_fact = gets.chomp @@ -48,7 +56,7 @@ def main while continue == true puts "What would you like to do next?" - puts "list planets | planet details | add planet | exit" + puts "list planets | planet details | add planet | distance between planets | exit" get_input = gets.chomp if get_input == "exit" puts "See ya!" @@ -60,51 +68,17 @@ def main elsif get_input == "add planet" solar_system.add_planet(add_new_planet) puts "Planet added to the solar system!" + elsif get_input == "distance between planets" + puts "What's the first planet you're interested in?" + planet1_name = gets.chomp + puts "Sweet, what's the second planet?" + planet2_name = gets.chomp + puts solar_system.distance_between(planet1_name, planet2_name) else puts "Invalid prompt." end end end -# unless the_input == "exit" -# if the_input == "list planets" -# puts solar_system.list_planets -# get_input -# elsif the_input == "planet details" -# puts solar_system.find_planet_by_name -# get_input -# elsif the_input == "add planet" -# add_new_planet -# puts solar_system.add_planet(add_new_planet) -# get_input -# else -# puts "Invalid prompt." -# end -# else puts "See ya!" end - -# puts "The farthest planet from Sol is #{neptune.name} at #{neptune.distance_from_sun_km} kilometers!" - -# puts "The biggest planet by far is #{jupiter.name} at #{jupiter.mass_kg} kilograms!" - -# puts saturn.summary - -# list = solar_system.list_planets -# puts list - -# found_planet = solar_system.find_planet_by_name("earth") -# puts found_planet -# puts found_planet.summary - puts "Welcome to the solar system!" main - -# def get_input -# puts "What would you like to do next?" -# puts "list planets | planet details | add planet | exit" -# return gets.chomp -# # return user_input -# end - -# puts "What would you like to do next?" -# puts "list planets | planet details | add planet | exit" -# the_input = gets.chomp diff --git a/solar_system.rb b/solar_system.rb index 01c21f0b..c2bd7a2d 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -30,4 +30,22 @@ def find_planet_by_name end return "Invalid planet." end + + def find_planet_distance_by_name(a_planet) + @planets.each do |each_planet| + if each_planet.name.casecmp(a_planet) == 0 + return each_planet.distance_from_sun_km + end + end + return "Invalid planet." + end + + def distance_between(planet1_name, planet2_name) + planet1_position = find_planet_distance_by_name(planet1_name) + planet2_position = find_planet_distance_by_name(planet2_name) + if planet1_position.is_a?(Float) == false || planet2_position.is_a?(Float) == false + return "Invalid planet entered." + end + return "#{(planet2_position - planet1_position).abs} kilometers" + end end From 11b781eba65fd5e7d25a883ab7cadb80dc5cae02 Mon Sep 17 00:00:00 2001 From: laneia Date: Tue, 26 Feb 2019 13:44:25 -0800 Subject: [PATCH 03/11] Moved 3 files to lib, added spec for planet --- lib/main.rb | 83 +++++++++++++++++++++++++++++++++++++++++++++ lib/planet.rb | 15 ++++++++ lib/solar_system.rb | 51 ++++++++++++++++++++++++++++ planet_spec.rb | 15 ++++++++ 4 files changed, 164 insertions(+) create mode 100644 lib/main.rb create mode 100644 lib/planet.rb create mode 100644 lib/solar_system.rb create mode 100644 planet_spec.rb diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..87b3d6da --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,83 @@ +require_relative "planet" +require_relative "solar_system" + +def add_new_planet + puts "Okay, what's the name of the planet we're missing?" + name = gets.chomp + puts "Cool, what color is it?" + color = gets.chomp + puts "What about its mass in kilograms?" + mass_kg = gets.chomp + until mass_kg.to_f > 0 + puts "Must be a number greater than zero." + mass_kg = gets.chomp + end + puts "Now I need its distance from the sun in kilometers." + distance_from_sun_km = gets.chomp + until distance_from_sun_km.to_f > 0 + puts "Must be a number greater than zero." + distance_from_sun_km = gets.chomp + end + puts "And finally, what's a fun fact about this planet?" + fun_fact = gets.chomp + + new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) +end + +def main + solar_system = SolarSystem.new("Sol") + + mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.") + solar_system.add_planet(mercury) + + venus = Planet.new("Venus", "yellow", 4.87e24, 108.2e6, "it probably had a moon, but collided with it after another impact reversed the planet's spin.") + solar_system.add_planet(venus) + + earth = Planet.new("Earth", "blue", 5.97e24, 149.6e6, "it has more life than the rest of the known universe, as far as we know.") + solar_system.add_planet(earth) + + mars = Planet.new("Mars", "red", 0.64e24, 227.9e6, "it has liquid water.") + solar_system.add_planet(mars) + + jupiter = Planet.new("Jupiter", "orange", 1898e24, 778.3e6, "its gravity breaks apart many comets.") + solar_system.add_planet(jupiter) + + saturn = Planet.new("Saturn", "gold", 569e24, 1427e6, "it's not known how or when its rings formed.") + solar_system.add_planet(saturn) + + uranus = Planet.new("Uranus", "blue", 86.8e24, 2871e6, "it also has large storms in its atmosphere, like Jupiter.") + solar_system.add_planet(uranus) + + neptune = Planet.new("Neptune", "blue", 102e24, 5913e6, "the wind can go as fast as 1,700 km/hr - faster than the speed of sound.") + solar_system.add_planet(neptune) + + continue = true + + while continue == true + puts "What would you like to do next?" + puts "list planets | planet details | add planet | distance between planets | exit" + get_input = gets.chomp + if get_input == "exit" + puts "See ya!" + return continue == false + elsif get_input == "list planets" + puts solar_system.list_planets + elsif get_input == "planet details" + puts solar_system.find_planet_by_name + elsif get_input == "add planet" + solar_system.add_planet(add_new_planet) + puts "Planet added to the solar system!" + elsif get_input == "distance between planets" + puts "What's the first planet you're interested in?" + planet1_name = gets.chomp + puts "Sweet, what's the second planet?" + planet2_name = gets.chomp + puts solar_system.distance_between(planet1_name, planet2_name) + else + puts "Invalid prompt." + end + end +end + +puts "Welcome to the solar system!" +main diff --git a/lib/planet.rb b/lib/planet.rb new file mode 100644 index 00000000..ee3d8899 --- /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 that weighs #{mass_kg} kilograms and orbits #{@distance_from_sun_km} kilometers from the sun. A cool fact about #{@name} is that #{fun_fact}" + end +end diff --git a/lib/solar_system.rb b/lib/solar_system.rb new file mode 100644 index 00000000..c2bd7a2d --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,51 @@ +require_relative "planet" + +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + @planet_print = ["Planets orbiting #{star_name}"] + end + + def add_planet(planet) + @planets << planet + end + + def list_planets + @planets.each_index do |index| + @planet_print << "#{index + 1}. #{@planets[index].name}" + end + return @planet_print + end + + def find_planet_by_name + puts "Which planet would you like to learn about?" + input_name = gets.chomp + @planets.each do |each_planet| + if each_planet.name.casecmp(input_name) == 0 + return each_planet.summary + end + end + return "Invalid planet." + end + + def find_planet_distance_by_name(a_planet) + @planets.each do |each_planet| + if each_planet.name.casecmp(a_planet) == 0 + return each_planet.distance_from_sun_km + end + end + return "Invalid planet." + end + + def distance_between(planet1_name, planet2_name) + planet1_position = find_planet_distance_by_name(planet1_name) + planet2_position = find_planet_distance_by_name(planet2_name) + if planet1_position.is_a?(Float) == false || planet2_position.is_a?(Float) == false + return "Invalid planet entered." + end + return "#{(planet2_position - planet1_position).abs} kilometers" + end +end diff --git a/planet_spec.rb b/planet_spec.rb new file mode 100644 index 00000000..9aaa9571 --- /dev/null +++ b/planet_spec.rb @@ -0,0 +1,15 @@ +gem "minitest", ">= 5.0.0" +require "minitest" +require "minitest/spec" +require "minitest/autorun" +require "minitest/reporters" +require "minitest/pride" + +require_relative "lib/planet" + +describe "Planets tests" do + it "Test planet variable assignment and summary" do + mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.") + expect(mercury.summary).must_equal "Mercury is a grey planet that weighs 3.3e+23 kilograms and orbits 57900000.0 kilometers from the sun. A cool fact about Mercury is that it has ice in craters that never receive sunlight." + end +end From 1141af53957769c424313b5c416c9c7f793d38f9 Mon Sep 17 00:00:00 2001 From: laneia Date: Tue, 26 Feb 2019 22:14:52 -0800 Subject: [PATCH 04/11] Continued spec work. Tried to get distance method to work with new planet. --- lib/main.rb | 52 ++++++++++++++++++++------------------ lib/solar_system.rb | 35 ++++++++++++++++++++++--- specs/planet_spec.rb | 25 ++++++++++++++++++ specs/solar_system_spec.rb | 17 +++++++++++++ 4 files changed, 101 insertions(+), 28 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 87b3d6da..f48f4eb4 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,29 +1,6 @@ require_relative "planet" require_relative "solar_system" -def add_new_planet - puts "Okay, what's the name of the planet we're missing?" - name = gets.chomp - puts "Cool, what color is it?" - color = gets.chomp - puts "What about its mass in kilograms?" - mass_kg = gets.chomp - until mass_kg.to_f > 0 - puts "Must be a number greater than zero." - mass_kg = gets.chomp - end - puts "Now I need its distance from the sun in kilometers." - distance_from_sun_km = gets.chomp - until distance_from_sun_km.to_f > 0 - puts "Must be a number greater than zero." - distance_from_sun_km = gets.chomp - end - puts "And finally, what's a fun fact about this planet?" - fun_fact = gets.chomp - - new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) -end - def main solar_system = SolarSystem.new("Sol") @@ -63,7 +40,9 @@ def main elsif get_input == "list planets" puts solar_system.list_planets elsif get_input == "planet details" - puts solar_system.find_planet_by_name + puts "Which planet would you like to learn about?" + input_name = gets.chomp + puts solar_system.find_planet_by_name(input_name) elsif get_input == "add planet" solar_system.add_planet(add_new_planet) puts "Planet added to the solar system!" @@ -81,3 +60,28 @@ def main puts "Welcome to the solar system!" main + +# Moved to solar_system.rb for now + +# def add_new_planet +# puts "Okay, what's the name of the planet we're missing?" +# name = gets.chomp +# puts "Cool, what color is it?" +# color = gets.chomp +# puts "What about its mass in kilograms?" +# mass_kg = gets.chomp +# until mass_kg.to_f > 0 +# puts "Must be a number greater than zero." +# mass_kg = gets.chomp +# end +# puts "Now I need its distance from the sun in kilometers." +# distance_from_sun_km = gets.chomp +# until distance_from_sun_km.to_f > 0 +# puts "Must be a number greater than zero." +# distance_from_sun_km = gets.chomp +# end +# puts "And finally, what's a fun fact about this planet?" +# fun_fact = gets.chomp + +# return new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) +# end diff --git a/lib/solar_system.rb b/lib/solar_system.rb index c2bd7a2d..b4f8366e 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -1,18 +1,46 @@ require_relative "planet" class SolarSystem - attr_reader :star_name, :planets + attr_reader :star_name + attr_accessor :planets def initialize(star_name) @star_name = star_name @planets = [] @planet_print = ["Planets orbiting #{star_name}"] + #@new_planet end def add_planet(planet) @planets << planet end + def add_new_planet + puts "Okay, what's the name of the planet we're missing?" + @name = gets.chomp + puts "Cool, what color is it?" + @color = gets.chomp + puts "What about its mass in kilograms?" + @mass_kg = gets.chomp + until @mass_kg.to_f > 0 + puts "Must be a number greater than zero." + @mass_kg = gets.chomp + end + puts "Now I need its distance from the sun in kilometers." + @distance_from_sun_km = gets.chomp + until @distance_from_sun_km.to_f > 0 + puts "Must be a number greater than zero." + @distance_from_sun_km = gets.chomp + end + puts "And finally, what's a fun fact about this planet?" + @fun_fact = gets.chomp + + @new_planet = @name.capitalize + @new_planet = Planet.new(@name, @color, @mass_kg, @distance_from_sun_km, @fun_fact) + @planets << @new_planet + return @planets + end + def list_planets @planets.each_index do |index| @planet_print << "#{index + 1}. #{@planets[index].name}" @@ -20,9 +48,7 @@ def list_planets return @planet_print end - def find_planet_by_name - puts "Which planet would you like to learn about?" - input_name = gets.chomp + def find_planet_by_name(input_name) @planets.each do |each_planet| if each_planet.name.casecmp(input_name) == 0 return each_planet.summary @@ -31,6 +57,7 @@ def find_planet_by_name return "Invalid planet." end + # Why won't this method work on planets added via add_new_planet? def find_planet_distance_by_name(a_planet) @planets.each do |each_planet| if each_planet.name.casecmp(a_planet) == 0 diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb new file mode 100644 index 00000000..9626f1f0 --- /dev/null +++ b/specs/planet_spec.rb @@ -0,0 +1,25 @@ +gem "minitest", ">= 5.0.0" +require "minitest" +require "minitest/spec" +require "minitest/autorun" +require "minitest/reporters" +require "minitest/pride" + +require_relative "../lib/planet" + +describe "Planets tests" do + it "Tests mass is greater than zero" do + mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.") + expect(mercury.mass_kg).must_be :>, 0 + end + + it "Tests distance is greater than zero" do + mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.") + expect(mercury.distance_from_sun_km).must_be :>, 0 + end + + it "Test planet variable assignment and summary" do + mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.") + expect(mercury.summary).must_equal "Mercury is a grey planet that weighs 3.3e+23 kilograms and orbits 57900000.0 kilometers from the sun. A cool fact about Mercury is that it has ice in craters that never receive sunlight." + end +end diff --git a/specs/solar_system_spec.rb b/specs/solar_system_spec.rb new file mode 100644 index 00000000..3653be4f --- /dev/null +++ b/specs/solar_system_spec.rb @@ -0,0 +1,17 @@ +gem "minitest", ">= 5.0.0" +require "minitest" +require "minitest/spec" +require "minitest/autorun" +require "minitest/reporters" +require "minitest/pride" + +# require_relative "../lib/planet" +require_relative "../lib/solar_system" + +describe "Solar system tests" do + it "Tests planet finding" do + solar_system = SolarSystem.new("Sol") + @planets = ["earth", "mars"] + expect(solar_system.find_planet_by_name("mars2")).must_match "Invalid planet." + end +end From 7710c9a9ff5eece8efa4364ba6c1effb701132fe Mon Sep 17 00:00:00 2001 From: laneia Date: Tue, 26 Feb 2019 22:17:23 -0800 Subject: [PATCH 05/11] Fixed call method --- lib/main.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index f48f4eb4..bb5796d2 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -44,7 +44,7 @@ def main input_name = gets.chomp puts solar_system.find_planet_by_name(input_name) elsif get_input == "add planet" - solar_system.add_planet(add_new_planet) + solar_system.add_new_planet puts "Planet added to the solar system!" elsif get_input == "distance between planets" puts "What's the first planet you're interested in?" From ff9557d77f23c3caea729e8bba72e9a1e7f70c21 Mon Sep 17 00:00:00 2001 From: laneia Date: Wed, 27 Feb 2019 08:38:45 -0800 Subject: [PATCH 06/11] minor adjustments --- lib/solar_system.rb | 47 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index b4f8366e..2270317c 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -1,8 +1,7 @@ require_relative "planet" class SolarSystem - attr_reader :star_name - attr_accessor :planets + attr_reader :star_name, :planets def initialize(star_name) @star_name = star_name @@ -16,29 +15,29 @@ def add_planet(planet) end def add_new_planet - puts "Okay, what's the name of the planet we're missing?" - @name = gets.chomp - puts "Cool, what color is it?" - @color = gets.chomp - puts "What about its mass in kilograms?" - @mass_kg = gets.chomp - until @mass_kg.to_f > 0 - puts "Must be a number greater than zero." - @mass_kg = gets.chomp + print "Okay, what's the name of the planet we're missing?\n" + new_name = gets.chomp + print "Cool, what color is it?\n" + new_color = gets.chomp + print "What about its mass in kilograms?\n" + new_mass_kg = gets.chomp + until new_mass_kg.to_f > 0 + print "Must be a number greater than zero.\n" + new_mass_kg = gets.chomp end - puts "Now I need its distance from the sun in kilometers." - @distance_from_sun_km = gets.chomp - until @distance_from_sun_km.to_f > 0 - puts "Must be a number greater than zero." - @distance_from_sun_km = gets.chomp + print "Now I need its distance from the sun in kilometers.\n" + new_distance = gets.chomp + until new_distance.to_f > 0 + print "Must be a number greater than zero.\n" + new_distance = gets.chomp end - puts "And finally, what's a fun fact about this planet?" - @fun_fact = gets.chomp + print "And finally, what's a fun fact about this planet?\n" + new_fun_fact = gets.chomp - @new_planet = @name.capitalize - @new_planet = Planet.new(@name, @color, @mass_kg, @distance_from_sun_km, @fun_fact) - @planets << @new_planet - return @planets + new_planet = Planet.new(new_name, new_color, new_mass_kg, new_distance, new_fun_fact) + add_planet(new_planet) + + print @planets end def list_planets @@ -58,9 +57,9 @@ def find_planet_by_name(input_name) end # Why won't this method work on planets added via add_new_planet? - def find_planet_distance_by_name(a_planet) + def find_planet_distance_by_name(planet) @planets.each do |each_planet| - if each_planet.name.casecmp(a_planet) == 0 + if each_planet.name.casecmp(planet) == 0 return each_planet.distance_from_sun_km end end From 4e1a32fef15eace4618c7c8e77e8e37c9c19c694 Mon Sep 17 00:00:00 2001 From: laneia Date: Wed, 27 Feb 2019 10:11:06 -0800 Subject: [PATCH 07/11] got distance between added planet and other planets working. GET IT. --- lib/solar_system.rb | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 2270317c..fa7a9e80 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -20,24 +20,22 @@ def add_new_planet print "Cool, what color is it?\n" new_color = gets.chomp print "What about its mass in kilograms?\n" - new_mass_kg = gets.chomp - until new_mass_kg.to_f > 0 + new_mass_kg = gets.chomp.to_f + until new_mass_kg > 0 print "Must be a number greater than zero.\n" - new_mass_kg = gets.chomp + new_mass_kg = gets.chomp.to_f end print "Now I need its distance from the sun in kilometers.\n" - new_distance = gets.chomp - until new_distance.to_f > 0 + new_distance = gets.chomp.to_f + until new_distance > 0 print "Must be a number greater than zero.\n" - new_distance = gets.chomp + new_distance = gets.chomp.to_f end print "And finally, what's a fun fact about this planet?\n" new_fun_fact = gets.chomp new_planet = Planet.new(new_name, new_color, new_mass_kg, new_distance, new_fun_fact) add_planet(new_planet) - - print @planets end def list_planets @@ -56,7 +54,6 @@ def find_planet_by_name(input_name) return "Invalid planet." end - # Why won't this method work on planets added via add_new_planet? def find_planet_distance_by_name(planet) @planets.each do |each_planet| if each_planet.name.casecmp(planet) == 0 From c02ce3a7428cb2991a192537627a48341d42743f Mon Sep 17 00:00:00 2001 From: laneia <39808568+laneia@users.noreply.github.com> Date: Wed, 27 Feb 2019 10:20:15 -0800 Subject: [PATCH 08/11] Delete planet.rb --- planet.rb | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 planet.rb diff --git a/planet.rb b/planet.rb deleted file mode 100644 index ee3d8899..00000000 --- a/planet.rb +++ /dev/null @@ -1,15 +0,0 @@ -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 that weighs #{mass_kg} kilograms and orbits #{@distance_from_sun_km} kilometers from the sun. A cool fact about #{@name} is that #{fun_fact}" - end -end From db820c8b7bb0639b8d1c13318b91c0fe29d599dd Mon Sep 17 00:00:00 2001 From: laneia <39808568+laneia@users.noreply.github.com> Date: Wed, 27 Feb 2019 10:20:39 -0800 Subject: [PATCH 09/11] Delete solar_system.rb --- solar_system.rb | 51 ------------------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 solar_system.rb diff --git a/solar_system.rb b/solar_system.rb deleted file mode 100644 index c2bd7a2d..00000000 --- a/solar_system.rb +++ /dev/null @@ -1,51 +0,0 @@ -require_relative "planet" - -class SolarSystem - attr_reader :star_name, :planets - - def initialize(star_name) - @star_name = star_name - @planets = [] - @planet_print = ["Planets orbiting #{star_name}"] - end - - def add_planet(planet) - @planets << planet - end - - def list_planets - @planets.each_index do |index| - @planet_print << "#{index + 1}. #{@planets[index].name}" - end - return @planet_print - end - - def find_planet_by_name - puts "Which planet would you like to learn about?" - input_name = gets.chomp - @planets.each do |each_planet| - if each_planet.name.casecmp(input_name) == 0 - return each_planet.summary - end - end - return "Invalid planet." - end - - def find_planet_distance_by_name(a_planet) - @planets.each do |each_planet| - if each_planet.name.casecmp(a_planet) == 0 - return each_planet.distance_from_sun_km - end - end - return "Invalid planet." - end - - def distance_between(planet1_name, planet2_name) - planet1_position = find_planet_distance_by_name(planet1_name) - planet2_position = find_planet_distance_by_name(planet2_name) - if planet1_position.is_a?(Float) == false || planet2_position.is_a?(Float) == false - return "Invalid planet entered." - end - return "#{(planet2_position - planet1_position).abs} kilometers" - end -end From aff4cabb223df33724584438e0a83ea4c91085ea Mon Sep 17 00:00:00 2001 From: laneia <39808568+laneia@users.noreply.github.com> Date: Wed, 27 Feb 2019 10:20:48 -0800 Subject: [PATCH 10/11] Delete planet_spec.rb --- planet_spec.rb | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 planet_spec.rb diff --git a/planet_spec.rb b/planet_spec.rb deleted file mode 100644 index 9aaa9571..00000000 --- a/planet_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -gem "minitest", ">= 5.0.0" -require "minitest" -require "minitest/spec" -require "minitest/autorun" -require "minitest/reporters" -require "minitest/pride" - -require_relative "lib/planet" - -describe "Planets tests" do - it "Test planet variable assignment and summary" do - mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.") - expect(mercury.summary).must_equal "Mercury is a grey planet that weighs 3.3e+23 kilograms and orbits 57900000.0 kilometers from the sun. A cool fact about Mercury is that it has ice in craters that never receive sunlight." - end -end From 0a924c624e3a1dd7693dd3f4fffb24e67e4b294c Mon Sep 17 00:00:00 2001 From: laneia <39808568+laneia@users.noreply.github.com> Date: Wed, 27 Feb 2019 10:20:57 -0800 Subject: [PATCH 11/11] Delete main.rb --- main.rb | 84 --------------------------------------------------------- 1 file changed, 84 deletions(-) delete mode 100644 main.rb diff --git a/main.rb b/main.rb deleted file mode 100644 index 68cb20d3..00000000 --- a/main.rb +++ /dev/null @@ -1,84 +0,0 @@ -require_relative "planet" -require_relative "solar_system" - -def add_new_planet - puts "Okay, what's the name of the planet we're missing?" - name = gets.chomp - puts "Cool, what color is it?" - color = gets.chomp - puts "What about its mass in kilograms?" - mass_kg = gets.chomp - until mass_kg.to_f > 0 - puts "Must be a number greater than zero." - mass_kg = gets.chomp - end - puts "Now I need its distance from the sun in kilometers." - distance_from_sun_km = gets.chomp - until distance_from_sun_km.to_f > 0 - puts "Must be a number greater than zero." - distance_from_sun_km = gets.chomp - end - puts "And finally, what's a fun fact about this planet?" - fun_fact = gets.chomp - - new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) - #return new_planet -end - -def main - solar_system = SolarSystem.new("Sol") - - mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.") - solar_system.add_planet(mercury) - - venus = Planet.new("Venus", "yellow", 4.87e24, 108.2e6, "it probably had a moon, but collided with it after another impact reversed the planet's spin.") - solar_system.add_planet(venus) - - earth = Planet.new("Earth", "blue", 5.97e24, 149.6e6, "it has more life than the rest of the known universe, as far as we know.") - solar_system.add_planet(earth) - - mars = Planet.new("Mars", "red", 0.64e24, 227.9e6, "it has liquid water.") - solar_system.add_planet(mars) - - jupiter = Planet.new("Jupiter", "orange", 1898e24, 778.3e6, "its gravity breaks apart many comets.") - solar_system.add_planet(jupiter) - - saturn = Planet.new("Saturn", "gold", 569e24, 1427e6, "it's not known how or when its rings formed.") - solar_system.add_planet(saturn) - - uranus = Planet.new("Uranus", "blue", 86.8e24, 2871e6, "it also has large storms in its atmosphere, like Jupiter.") - solar_system.add_planet(uranus) - - neptune = Planet.new("Neptune", "blue", 102e24, 5913e6, "the wind can go as fast as 1,700 km/hr - faster than the speed of sound.") - solar_system.add_planet(neptune) - - continue = true - - while continue == true - puts "What would you like to do next?" - puts "list planets | planet details | add planet | distance between planets | exit" - get_input = gets.chomp - if get_input == "exit" - puts "See ya!" - return continue == false - elsif get_input == "list planets" - puts solar_system.list_planets - elsif get_input == "planet details" - puts solar_system.find_planet_by_name - elsif get_input == "add planet" - solar_system.add_planet(add_new_planet) - puts "Planet added to the solar system!" - elsif get_input == "distance between planets" - puts "What's the first planet you're interested in?" - planet1_name = gets.chomp - puts "Sweet, what's the second planet?" - planet2_name = gets.chomp - puts solar_system.distance_between(planet1_name, planet2_name) - else - puts "Invalid prompt." - end - end -end - -puts "Welcome to the solar system!" -main