diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..3a63f196 --- /dev/null +++ b/main.rb @@ -0,0 +1,53 @@ +require_relative 'planet' +require_relative 'solar_system' + + +def main + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + mars = Planet.new('Mars', 'red', 6.42e23, 2.49e8, 'the second-smallest planet in the Solar System after Mercury') + mercury = Planet.new('Mercury', 'red', 0.330e24, 57e6, 'Mercury is the smallest planet in our Solar System.') + venus = Planet.new('Venus', 'yellow', 4.87e24, 108e6, 'Venus is the hottest planet in the Solar System.') + jupiter = Planet.new('Jupiter', 'blue-red', 1.898e27, 817e6, 'Jupiter is the largest planet in our Solar System.') + saturn = Planet.new('Saturn', 'pink', 5.69e26, 1.5e9, "Saturn’s rings are made primarily of “water ice” mixed with dust and other chemicals.") + uranus = Planet.new('Uranus', 'blue', 8.68e25, 3e9, 'Uranus is the coldest of the planets.') + + solar_system = SolarSystem.new('Sol') + solar_system.add_planet(earth) + solar_system.add_planet(mars) + solar_system.add_planet(mercury) + solar_system.add_planet(venus) + solar_system.add_planet(jupiter) + solar_system.add_planet(saturn) + solar_system.add_planet(uranus) + + print "What would you like to do? Type 'list planets' to list planets or 'exit' to exit the program: " + user_input = gets.chomp + + while user_input.downcase != "exit" + puts "----------------------------------------" + + if user_input.downcase == "list planets" + puts solar_system.list_planets + elsif user_input.downcase == "planet details" + puts solar_system.get_planet_details + elsif user_input.downcase == "add planet" + solar_system.create_and_add_planet_to_solar_system + elsif user_input.downcase == "distance between" + print "For planet #1: " + planet_1 = solar_system.get_verified_planet_info + print "For planet #2: " + planet_2 = solar_system.get_verified_planet_info + puts "----------------------------------------" + puts "The distance between #{planet_1.name} and #{planet_2.name} is #{solar_system.distance_between(planet_1, planet_2)} km." + else + puts "sorry I don't know what you want me to do" + end + + puts "----------------------------------------" + print "What would you like to do next? You can type...\n- 'list planets' to list planets\n- 'add planet' to add a planet\n- 'planet details' to learn more about a specific planet\n- 'distance between' to get the distance between two planets\n- 'exit' to exit the program\nYour choice: " + user_input = gets.chomp + end + +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..fa45ab77 --- /dev/null +++ b/planet.rb @@ -0,0 +1,25 @@ +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 + + [mass_kg,distance_from_sun_km].each do |var| + raise(ArgumentError, "Sorry, #{var} needs to be a number") if var.is_a?(Float) == false && var.is_a?(Integer) == false + end + + [mass_kg,distance_from_sun_km].each do |var| + raise(ArgumentError, "Sorry, #{var} needs to be a number greater than 0!") if var < 0 + end + + end + + def summary + return "#{self.name} is a #{self.color} planet that weighs #{self.mass_kg} kg and is #{self.distance_from_sun_km} km from the sun.\nFun fact about #{self.name}: #{self.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..ad5fe9c5 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,65 @@ +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + def add_planet(planet) + @planets.push(planet) + end + + def list_planets + str_planets = "Planets orbiting #{self.star_name}" + @planets.each_with_index do |planet, i| + str_planets += "\n#{i+1}. #{planet.name}" + end + return str_planets + end + + def find_planet_by_name(name_of_planet) + planets.each do |planet| + return planet if planet.name.downcase == name_of_planet.downcase + end + end + + def distance_between(planet_1, planet_2) + return ( planet_1.distance_from_sun_km - planet_2.distance_from_sun_km ).abs + end + + def get_planet_details + print "For the planet you want more info on: " + planet_for_more_details = self.get_verified_planet_info + return planet_for_more_details.summary + + end + + def create_and_add_planet_to_solar_system + print "Please enter the name of the planet: " + name = gets.chomp + print "Please enter the color of the planet: " + color = gets.chomp + print "Please enter the mass of the planet in kg: " + mass_kg = gets.chomp.to_f + print "Please enter the distrance from the sun in km: " + distance_from_sun_km = gets.chomp.to_f + print "Please enter a fun fact about the planet: " + fun_fact = gets.chomp + new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) + self.add_planet(new_planet) + end + + def get_verified_planet_info + print "What is the planet's name? " + planet_name = gets.chomp.downcase + + while self.planets.map { |planet| planet.name.downcase }.include?(planet_name) == false + print "Sorry I don't recognize that as a planet. Please try again: " + planet_name = gets.chomp.downcase + end + + return self.find_planet_by_name(planet_name) + end + +end \ No newline at end of file