diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..36298d28 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.byebug_history diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..0c2d13fe --- /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['test/*_test.rb'] +end + +task default: :test diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..0d9bda2d --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,71 @@ +require 'byebug' +require_relative 'planet' +require_relative 'solar_system' + +def main + earth = Planet.new( + 'Earth', + 'blue-green', + 5.972e+24, + 149.6e+6, + "Only planet known to support life" + ) + + moon = Planet.new( + 'Moon', + 'grey', + 7.348e+22, + 150e+6, + "Earth's only natural satellite" + ) + + ############################################## + + solar_system = SolarSystem.new('Sun') + + solar_system.add_planet(earth) + solar_system.add_planet(moon) + + ############################################## + + continue = true + while continue + puts "This program has created a solar system with several planets. From here, you can:\n" + puts "1. List the planets in the solar system (enter 1)\n" + puts "2. Display details about a single planet in the system (enter 2)\n" + puts "3. Add a planet to the system (enter 3)\n" + puts "4. Calculate the distance between two planets (in km) (enter 4)\n" + puts "5. Exit (enter 5)\n" + + response = gets.chomp.to_i + + case response + when 1 + puts solar_system.list_planets + when 2 + puts "Please enter the name of the planet you'd like to know more about:" + planet_inspect = gets.chomp + planet_inspect = solar_system.find_planet_by_name(planet_inspect) + puts planet_inspect.summary + when 3 + solar_system.user_add_planet + when 4 + puts "Please enter the name of the first planet:" + planet_1 = gets.chomp + puts "Please enter the name of the second planet:" + planet_2 = gets.chomp + + solar_system.find_planet_by_name(planet_1) + solar_system.find_planet_by_name(planet_2) + distance = solar_system.distance_between(planet_1, planet_2) + puts "The distance between #{planet_1} and #{planet_2} is approximately: #{distance} km." + when 5 + puts "OK! Terminating program." + continue = false + else + puts "That was not a valid response." + end + end +end + +main \ No newline at end of file diff --git a/lib/planet.rb b/lib/planet.rb new file mode 100644 index 00000000..ebba3c47 --- /dev/null +++ b/lib/planet.rb @@ -0,0 +1,32 @@ +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 + + if (mass_kg.class == Float || mass_kg.class == Integer) && mass_kg > 0 + @mass_kg = mass_kg + else + raise ArgumentError, "For #{self.name}, this isn't the right type of input for mass in kg - please enter an number greater than 0." + end + + if (distance_from_sun_km.class == Float || distance_from_sun_km.class == Integer) && distance_from_sun_km > 0 + @distance_from_sun_km = distance_from_sun_km + else + raise ArgumentError, "For #{self.name}, this isn't the right type of input for distance from sun in km - please enter an number greater than 0." + end + + @fun_fact = fun_fact + end + + def summary + stats = "<< Summary of planet: #{name.upcase} >>\n" + + "Color: #{color}\n" + + "Mass (kg): #{mass_kg}\n" + + "Distance from sun (km): #{distance_from_sun_km}\n" + + "Fun fact: #{fun_fact}" + return stats + 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..0c27da3e --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,64 @@ +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 + summary_list = "Planets orbiting <#{@star_name}>\n" + @planets.each.with_index(1) { |planet, idx| summary_list << "#{idx}. #{planet.name}\n"} + return summary_list + end + + def check_valid_planet(planet) + @planets.each do |one_planet| + if one_planet.name.upcase == planet.upcase + return one_planet + end + end + raise ArgumentError, "#{planet} is not part of the given solar system." + end + + def find_planet_by_name(planet) + found_planet = check_valid_planet(planet) + return found_planet + end + + def distance_between(planet_1, planet_2) + planet_1 = check_valid_planet(planet_1) + planet_2 = check_valid_planet(planet_2) + distance_bw_planets = (planet_1.distance_from_sun_km - planet_2.distance_from_sun_km).abs + return distance_bw_planets + end + + def user_add_planet + puts "We need the following details about the planet:" + puts "What is the name of the planet?" + planet_name = gets.chomp + puts "What color is the planet?" + planet_color = gets.chomp + puts "What is the mass (in kg) of the planet?" + planet_mass_kg = gets.chomp.to_i + puts "What is the planet's distance from the sun (in km)?" + planet_dist_km = gets.chomp.to_i + puts "What is a fun fact about the planet?" + planet_fun_fact = gets.chomp + + new_planet = Planet.new( + planet_name, + planet_color, + planet_mass_kg, + planet_dist_km, + planet_fun_fact + ) + + add_planet(new_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..6743196f --- /dev/null +++ b/test/planet_test.rb @@ -0,0 +1,75 @@ +require 'minitest/autorun' +require 'minitest/spec' +require 'minitest/reporters' +require 'minitest/pride' + +require_relative '../lib/planet' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe 'Correct initialize' do + + it 'raises an ArgumentError for invalid measurement inputs' do + expect { + Planet.new( + 'Earth', + 'blue-green', + 'very big', + 149.6e+6, + "Only planet known to support life" + ) + }.must_raise ArgumentError + + expect { + Planet.new( + 'Earth', + 'blue-green', + 5.972e+24, + 'very far', + "Only planet known to support life" + ) + }.must_raise ArgumentError + + expect { + Planet.new( + 'Earth', + 'blue-green', + '', + 'very far', + "Only planet known to support life" + ) + }.must_raise ArgumentError + end + + it 'raises an ArgumentError for measurement inputs <= 0' do + expect { + Planet.new( + 'Earth', + 'blue-green', + 5.972e+24, + -149.6e+6, + "Only planet known to support life" + ) + }.must_raise ArgumentError + + expect { + Planet.new( + 'Earth', + 'blue-green', + -5.972e+24, + 'very far', + "Only planet known to support life" + ) + }.must_raise ArgumentError + + expect { + Planet.new( + 'Earth', + 'blue-green', + 0, + 900, + "Only planet known to support life" + ) + }.must_raise ArgumentError + end +end \ No newline at end of file diff --git a/test/solar_system_test.rb b/test/solar_system_test.rb new file mode 100644 index 00000000..df65cb4f --- /dev/null +++ b/test/solar_system_test.rb @@ -0,0 +1,44 @@ +require 'minitest/autorun' +require 'minitest/spec' +require 'minitest/reporters' +require 'minitest/pride' + +require_relative '../lib/planet' +require_relative '../lib/solar_system' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe 'checking solar system planets' do + # Arrange + earth = Planet.new( + 'Earth', + 'blue-green', + 5.972e+24, + 149.6e+6, + "Only planet known to support life" + ) + + moon = Planet.new( + 'Moon', + 'grey', + 7.348e+22, + 150e+6, + "Earth's only natural satellite" + ) + solar_system = SolarSystem.new('Sun') + + solar_system.add_planet(earth) + solar_system.add_planet(moon) + + it 'raises an ArgumentError for invalid planets in solar system' do + expect { + solar_system.check_valid_planet('not a real planet') + }.must_raise ArgumentError + end + + it 'raises an ArgumentError for invalid planets in solar system' do + expect { + solar_system.check_valid_planet('') + }.must_raise ArgumentError + end +end \ No newline at end of file