diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..5b2c06bf --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,77 @@ +require_relative 'planet' +require_relative 'solar_system' + +def main + solar_system = SolarSystem.new("Sun") + mercury = Planet.new("Mercury", "light-grey", 3.285e23, 5.800e7, "if you lived on Mercury you'd have a birthday every 3 months.") + venus = Planet.new("Venus", "white", 4.867e24, 1.080e8, "it is named after the Roman goddess of love and beauty.") + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "it is the only planet known to support life.") + mars = Planet.new("Mars", "red", 6.39e23, 2.066e8, "it has the highest mountain in our solar system (24km).") + jupiter = Planet.new("Jupiter", "yellow-orange", 1.898e27, 7.680e8, "it has 79 moons.") + saturn = Planet.new("Saturn", "yellow-brown", 5.68e26, 1.426e9, "one of Saturn's rings, discovered in 2009, could fit a billion earths.") + uranus = Planet.new("Uranus", "blue-green", 8.681e25, 2.959e9, "it was the first planet discovered by a telescope in 1781.") + neptune = Planet.new("Neptune", "blue", 1.024e26, 4.476e9, "a year on Neptune is equivalent to 165 years on Earth.") + + solar_system.add_planet(mercury) + solar_system.add_planet(venus) + solar_system.add_planet(earth) + solar_system.add_planet(mars) + solar_system.add_planet(jupiter) + solar_system.add_planet(saturn) + solar_system.add_planet(uranus) + solar_system.add_planet(neptune) + + list = solar_system.list_planets + + puts "Welcome to the Solar System Program! You may: \n 1. Check out a list of the planets \n 2. Learn about a planet \n 3. Add a planet \n 4. Calculate the distance between two planets \n 5. Exit" + + user_input = nil + + until user_input == 5 + puts + print "Please enter a number to continue: " + user_input = gets.chomp.to_i + until user_input >= 1 && user_input <= 5 + print "Sorry, that is not a valid option. Please check the list and try again: " + user_input = gets.chomp.to_i + end + + case user_input + when 1 + puts list + when 2 + puts "Which planet do you want details on?" + planet = gets.chomp + info = solar_system.find_planet_by_name(planet) + puts info.summary + when 3 + puts "Add a planet by entering the following information." + print "Name: " + name = gets.chomp.capitalize + print "Color: " + color = gets.chomp.downcase + print "Mass (Kg): " + mass = gets.chomp.to_f + print "Distance From Sun (Km): " + distance = gets.chomp.to_f + print "Fun Fact: " + fact = gets.chomp.downcase + user_planet = Planet.new(name, color, mass, distance, fact) + solar_system.add_planet(user_planet) + puts "Your planet has been added to the solar system! Here is the updated list: " + list = solar_system.list_planets + print list + when 4 + print "Enter the first planet: " + first_planet = gets.chomp.capitalize + print "Enter the second planet: " + second_planet = gets.chomp.capitalize + puts "The distance between #{first_planet} and #{second_planet} is #{solar_system.distance_between(first_planet, second_planet)} km." + when 5 + puts "Thank you for using the Solar System Program. Goodbye!" + exit + end + end +end + +main diff --git a/lib/planet.rb b/lib/planet.rb new file mode 100644 index 00000000..8f7db3ef --- /dev/null +++ b/lib/planet.rb @@ -0,0 +1,23 @@ +class Planet + attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + + def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) + @name = name + @color = color + @mass_kg = mass_kg + @distance_from_sun_km = distance_from_sun_km + @fun_fact = fun_fact + + if mass_kg <= 0 + raise ArgumentError.new("Mass must be greater than 0 kg.") + end + + if distance_from_sun_km <= 0 + raise ArgumentError.new("Distance from the sun must be greater than 0 km.") + end + end + + def summary + return "#{@name} is #{@color}, weighs #{@mass_kg} kg, and is #{@distance_from_sun_km} km from the sun. A fun fact is that #{@fun_fact}" + end +end \ No newline at end of file diff --git a/lib/solar_system.rb b/lib/solar_system.rb new file mode 100644 index 00000000..9a7449af --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,37 @@ +require_relative 'planet' + +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + def add_planet(planet) + @planets << planet + end + + def list_planets + output_string = "Planets orbiting the #{@star_name} \n" + @planets.each_with_index do |planet, index| + output_string += "#{index + 1}. #{planet.name} \n" + end + return output_string + end + + def find_planet_by_name(planet_name) + found_planet = @planets.find { |planet| planet.name == planet_name.capitalize} + if found_planet.nil? + raise ArgumentError.new("Sorry, that planet does not exist in this solar system.") + end + return found_planet + end + + def distance_between(first_planet, second_planet) + first_planet_distance = find_planet_by_name(first_planet).distance_from_sun_km + second_planet_distance = find_planet_by_name(second_planet).distance_from_sun_km + distance_between_planets = (first_planet_distance - second_planet_distance).abs + return distance_between_planets + end +end \ No newline at end of file diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..a34efe99 --- /dev/null +++ b/main.rb @@ -0,0 +1,69 @@ +require_relative 'planet' +require_relative 'solar_system' + +def main + solar_system = SolarSystem.new("Sun") + mercury = Planet.new("Mercury", "light-grey", 3.285e23, 5.800e7, "if you lived on Mercury you'd have a birthday every 3 months.") + venus = Planet.new("Venus", "white", 4.867e24, 1.080e8, "it is named after the Roman goddess of love and beauty.") + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "it is the only planet known to support life.") + mars = Planet.new("Mars", "red", 6.39e23, 2.066e8, "it has the highest mountain in our solar system (24km).") + jupiter = Planet.new("Jupiter", "yellow-orange", 1.898e27, 7.680e8, "it has 79 moons.") + saturn = Planet.new("Saturn", "yellow-brown", 5.68e26, 1.426e9, "one of Saturn's rings, discovered in 2009, could fit a billion earths.") + uranus = Planet.new("Uranus", "blue-green", 8.681e25, 2.959e9, "it was the first planet discovered by a telescope in 1781.") + neptune = Planet.new("Neptune", "blue", 1.024e26, 4.476e9, "a year on Neptune is equivalent to 165 years on Earth.") + + solar_system.add_planet(mercury) + solar_system.add_planet(venus) + solar_system.add_planet(earth) + solar_system.add_planet(mars) + solar_system.add_planet(jupiter) + solar_system.add_planet(saturn) + solar_system.add_planet(uranus) + solar_system.add_planet(neptune) + + list = solar_system.list_planets + + puts "Welcome to the Solar System Program! You may: \n 1. See a list of the planets \n 2. See details for a planet \n 3. Add a planet \n 4. Exit" + + user_input = nil + + until user_input == 4 + puts + print "Please enter a number to continue: " + user_input = gets.chomp.to_i + until user_input >= 1 && user_input <= 4 + print "Sorry, that is not a valid option. Please check the list and try again: " + user_input = gets.chomp.to_i + end + + case user_input + when 1 + puts list + when 2 + puts "Which planet do you want details on?" + planet = gets.chomp + info = solar_system.find_planet_by_name(planet) + puts info.summary + when 3 + puts "Add a planet by entering the following information." + print "Name: " + name = gets.chomp + print "Color: " + color = gets.chomp + print "Mass (Kg): " + mass = gets.chomp + print "Distance From Sun (Km): " + distance = gets.chomp + print "Fun Fact: " + fact = gets.chomp + user_planet = Planet.new(name, color, mass, distance, fact) + solar_system.add_planet(user_planet) + puts "Awesome! Your planet has been added to the solar system." + when 4 + puts "Thank you for using the Solar System Program. Goodbye!" + exit + end + end +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..16347822 --- /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 #{@color}, weighs #{@mass_kg} kg, and is #{@distance_from_sun_km} km from the sun. A fun fact is that #{@fun_fact}" + end +end \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..141a278d --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,30 @@ +require_relative 'planet' + +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + def add_planet(planet) + @planets << planet + end + + def list_planets + output_string = "Planets orbiting the #{@star_name} \n" + @planets.each_with_index do |planet, index| + output_string += " #{index + 1}. #{planet.name} \n" + end + return output_string + end + + def find_planet_by_name(planet_name) + found_planet = @planets.find { |planet| planet.name == planet_name.capitalize} + if found_planet.nil? + raise ArgumentError.new("Sorry, that planet does not exist in this solar system.") + end + return found_planet + end +end \ No newline at end of file diff --git a/test/planet_test.rb b/test/planet_test.rb new file mode 100644 index 00000000..188258d9 --- /dev/null +++ b/test/planet_test.rb @@ -0,0 +1,36 @@ +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' + +require_relative '../lib/planet.rb' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe 'Planet' do + it 'creates an instance of Planet' do + name = 'Earth' + color = 'blue-green' + mass_kg = 5.972e24 + distance_from_star_km = 1.496e8 + fun_fact = "it is the only planet known to support life." + + earth = Planet.new(name, color, mass_kg, distance_from_star_km, fun_fact) + expect(earth).must_be_instance_of Planet + end + + it 'raises an error when the mass or distance from the sun are less than or equal to 0' do + expect {Planet.new('Earth','blue-green',0,1.496e8 ,"it is the only planet known to support life.")}.must_raise ArgumentError + expect {Planet.new('Earth','blue-green',5.972e24,0,"it is the only planet known to support life.")}.must_raise ArgumentError + end + + it 'returns a summary for an instance of Planet' do + name = 'Earth' + color = 'blue-green' + mass_kg = 5.972e24 + distance_from_star_km = 1.496e8 + fun_fact = "it is the only planet known to support life." + + earth = Planet.new(name, color, mass_kg, distance_from_star_km, fun_fact) + expect(earth.summary).must_equal "Earth is blue-green, weighs 5.972e+24 kg, and is 149600000.0 km from the sun. A fun fact is that it is the only planet known to support life." + end +end diff --git a/test/solar_system_test.rb b/test/solar_system_test.rb new file mode 100644 index 00000000..f21c87c7 --- /dev/null +++ b/test/solar_system_test.rb @@ -0,0 +1,79 @@ +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' + +require_relative '../lib/solar_system.rb' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe 'Solar System' do + describe 'initialize solar system' do + it 'creates an instance of Solar System' do + star_name = 'Sun' + solar_system = SolarSystem.new(star_name) + expect(solar_system).must_be_instance_of SolarSystem + end + end +end + + describe 'add planet' do + it 'adds a planet' do + solar_system = SolarSystem.new("Sun") + + earth = Planet.new('Earth','blue-green',5.972e24,1.496e8,"it is the only planet known to support life.") + mars = Planet.new("Mars", "red", 6.39e23, 2.066e8, "it has the highest mountain in our solar system (24km).") + + solar_system.add_planet(earth) + solar_system.add_planet(mars) + + expect(solar_system.planets.length).must_equal 2 + end + end + + describe 'list planet' do + it 'lists the planets' do + solar_system = SolarSystem.new('Sun') + expect(solar_system.list_planets).must_be_instance_of String + + earth = Planet.new('Earth','blue-green',5.972e24,1.496e8,"it is the only planet known to support life.") + mars = Planet.new("Mars", "red", 6.39e23, 2.066e8, "it has the highest mountain in our solar system (24km).") + solar_system.add_planet(earth) + solar_system.add_planet(mars) + expect(solar_system.list_planets).must_equal "Planets orbiting the Sun \n1. Earth \n2. Mars \n" + end + end + + describe 'finds a planet by name' do + it 'raises an error if the planet cannot be found' do + solar_system = SolarSystem.new("Sun") + + earth = Planet.new('Earth','blue-green',5.972e24,1.496e8,"it is the only planet known to support life.") + mars = Planet.new("Mars","red",6.39e23,2.066e8,"it has the highest mountain in our solar system (24km).") + + solar_system.add_planet(earth) + solar_system.add_planet(mars) + + expect{solar_system.find_planet_by_name("Mercury")}.must_raise ArgumentError + end + + it 'if the planet can be found it must be an instance of the class Planet' do + solar_system = SolarSystem.new("Sun") + earth = Planet.new('Earth','blue-green',5.972e24,1.496e8,"it is the only planet known to support life.") + solar_system.add_planet(earth) + expect(solar_system.find_planet_by_name("Earth")).must_be_instance_of Planet + end + end + + describe 'distance between' do + it 'calculates the distance between two planets' do + solar_system = SolarSystem.new("Sun") + + earth = Planet.new("Earth",'blue-green',5.972e24,1.496e8,"it is the only planet known to support life.") + mars = Planet.new("Mars","red",6.39e23,2.066e8,"it has the highest mountain in our solar system (24km).") + + solar_system.add_planet(earth) + solar_system.add_planet(mars) + + expect(solar_system.distance_between("Earth", "Mars")).must_be_close_to 57000000 + end + end