diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..b9171a71 --- /dev/null +++ b/main.rb @@ -0,0 +1,46 @@ +# main.rb +require_relative 'planet' +require_relative 'solar_system' + +def main + solar_system = SolarSystem.new('Sol') + + earth = Planet.new('earth', 'blue-green', '5.972e24', '1.496e8', 'only planet known to support life') + solar_system.add_planet(earth) + mars = Planet.new('mars', 'reddish-brown','‎0.64171','141.6 million mi','it has the longest rotation period, rotates in the opposite direction to most other planets') + solar_system.add_planet(mars) + jupiter = Planet.new('jupiter','orange with white bands','1,898.19','483.8 million mi','the largest in the Solar System') + solar_system.add_planet(jupiter) + saturn = Planet.new('saturn','pale-gold','5.9724','890.8 million mi','second-largest in the Solar System. It is a gas giant a radius nine times that of Earth') + solar_system.add_planet(saturn) + + list = solar_system.list_planets + puts list + + found_planet = solar_system.find_planet_by_name("earth") + puts found_planet.summary + + options = [ "list planets", "get planet details", "add a planet", "exit"] + + puts "What would you like to do, please choose from the following options:" + puts options + user_input = gets.chomp.downcase + + while options.include? user_input + case user_input + when "list planets" + puts solar_system.list_planets + when "get planet details" + puts solar_system.planet_details.summary + when "add a planet" + puts solar_system.new_planet + when "exit" + exit +end + + puts "What would you like to do next?" + user_input = gets.chomp.downcase +end +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..21dfb7b2 --- /dev/null +++ b/planet.rb @@ -0,0 +1,17 @@ + +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.capitalize}, has a #{color} color, with a mass of #{mass_kg}, holding a distance from the sun of #{distance_from_sun_km}, a fun fact is: #{fun_fact}" + end + +end diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..37db1a37 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,72 @@ +# Solar System +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 + i = 0 + list = [] + @planets.each do |planet| + list << "#{i += 1}. #{planet.name}" + end + puts "Planets orbiting #{star_name}:" + return list + end + + def find_planet_by_name(name) + @planets.each do |planet| + if planet.name == name.downcase + return planet + elsif planet.name != name.downcase + puts "no related data found, please enter a listed planet" + planet_details + end + end + end + + def planet_details + puts "Enter the planet you want a little more info of" + user_input = gets.chomp.downcase + + planet_info = find_planet_by_name(user_input) + return planet_info + end + + def new_planet + puts "We will need the following planet information:" + + puts "Planet name:" + name = gets.chomp.downcase + + puts "Planet color:" + color = gets.chomp.downcase + + puts "Planet Mass:" + mass = gets.chomp.to_i + + puts "Planet Distance from the Sun:" + distance_from_sun = gets.chomp + + puts "Planet Fun Fact:" + fun_fact = gets.chomp_to_i + + new_planet = Planet.new(name, color, mass, distance_from_sun, fun_fact) + add_planet(new_planet) + + end +end + + + + + + +