diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..fe0dea81 --- /dev/null +++ b/main.rb @@ -0,0 +1,35 @@ +require_relative "planet" +require_relative "solar_system" + +solar_system = SolarSystem.new("Sol") +mercury = Planet.new("Mercury", "dark grey", 3.285e23, 5.791e7, "Named for the messenger of the Roman gods due to its orbital speed") +solar_system.add_planet(mercury) +venus = Planet.new("Venus", "red-brown", 4.867e24, 1.082e8, "Both the morning star and the evening star") +solar_system.add_planet(venus) +earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") +solar_system.add_planet(earth) +mars = Planet.new("Mars", "red", 6.4169e23, 2.279e8, "Home to the tallest mountain in the solar system") +solar_system.add_planet(mars) +jupiter = Planet.new("Jupiter", "red, white, and brown", 1.898e27, 7.785e8, "The planet with the shortest day in the solar system") +solar_system.add_planet(jupiter) + +puts "Hello, please see the the following options for this program. You can: \nA: List Planets \nB: List Planet Details \nC: Add Planet \nD: Find the Distance Between Two Planets" +puts "Please enter the corresponding letter for the action you would like to take. You can exit by entering 'exit'." +response = gets.chomp.downcase +while response != "exit" + if response == "a" + puts solar_system.list_planets + elsif response == "b" + puts "Please enter what planet you would like to learn about: " + planet_choice = solar_system.find_planet_by_name(gets.chomp) + puts planet_choice.summary + elsif response == "c" + solar_system.user_adds_planet + elsif response == "d" + puts solar_system.distance_between_by_input + else + puts "Please make sure to select from the choices above. You can exit be entering 'exit'." + end + puts "\nPlease enter what you would like to do: \nA: List Planets \nB: List Planet Details \nC: Add Planet \nD: Find the Distance Between Two Planets" + response = gets.chomp +end diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..9ad62ed2 --- /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. It is #{@fun_fact.downcase}." + end +end diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..35065c0b --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,64 @@ +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.push(planet) + end + + def list_planets + planet_list = "Planets orbiting #{star_name}: " + i = 0 + @planets.each do + planet_list += "\n#{i + 1}. #{@planets[i].name}" + i += 1 + end + return planet_list + end + + def find_planet_by_name(name) + i = 0 + @planets.each do + if @planets[i].name.downcase == name.downcase + return @planets[i] + end + i += 1 + end + end + + def user_adds_planet + puts "Please complete the following information for the planet you would like added: \nName: " + name = gets.chomp.capitalize + puts "Color: " + color = gets.chomp + puts "Mass in kg: " + mass = gets.chomp + puts "Distance from sun in km: " + distance = gets.chomp + puts "Fun fact: " + fact = gets.chomp + new_planet = Planet.new(name, color, mass, distance, fact) + self.add_planet(new_planet) + puts "Thank you for adding #{name} to the solar system!" + return new_planet + end + + def distance_between(planet1, planet2) + return (planet1.distance_from_sun_km - planet2.distance_from_sun_km).abs + end + + def distance_between_by_input + puts "Please enter the two planets you would like to find the distance between:" + print "Planet 1: " + planet1 = self.find_planet_by_name(gets.chomp) + print "Planet 2: " + planet2 = self.find_planet_by_name(gets.chomp) + return self.distance_between(planet1, planet2) + end +end