diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..bb5796d2 --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,87 @@ +require_relative "planet" +require_relative "solar_system" + +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 "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_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 + +# 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/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..fa7a9e80 --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,74 @@ +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}"] + #@new_planet + end + + def add_planet(planet) + @planets << planet + end + + def add_new_planet + 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.to_f + until new_mass_kg > 0 + print "Must be a number greater than zero.\n" + new_mass_kg = gets.chomp.to_f + end + print "Now I need its distance from the sun in kilometers.\n" + new_distance = gets.chomp.to_f + until new_distance > 0 + print "Must be a number greater than zero.\n" + 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) + 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(input_name) + @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(planet) + @planets.each do |each_planet| + if each_planet.name.casecmp(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/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