diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..2f467dec --- /dev/null +++ b/main.rb @@ -0,0 +1,49 @@ +require_relative 'planet' +require_relative 'solar_system' +require 'pry' + +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) + + puts "What would you like to do? (A/B/C/D)" + puts "option A: list plannets" + puts "option B: exit" + puts "option C: planet details" + puts "option D: add planet" + decision = gets.chomp + while decision.upcase != "B" + if decision.upcase == "C" + puts "which planet would you like to learn more about?" + planet = gets.chomp + found_planet = solar_system.find_planet_by_name(planet) + puts found_planet.summary + puts "What would you like to do? (A/B/C/D)\noption A: list plannets\noption B: exit\noption C: planet details\noption D: add planet" + decision = gets.chomp + elsif decision.upcase == "D" + puts "So, you want to add a planet, I need to know more about it." + puts "Name?" + name = gets.chomp + puts "color?" + color = gets.chomp + puts "mass?" + mass = gets.chomp + puts "distance from sun in km?" + distance = gets.chomp + puts "fun_fact?" + fun_fact = gets.chomp + planet = Planet.new(name, color, mass, distance, fun_fact) + solar_system.add_planet(planet) + puts "What would you like to do? (A/B/C/D)\noption A: list plannets\noption B: exit\noption C: planet details\noption D: add planet" + decision = gets.chomp + elsif decision.upcase == "A" + list = solar_system.list_planets + puts list + puts "What would you like to do? (A/B/C/D)\noption A: list plannets\noption B: exit\noption C: planet details\noption D: add planet" + decision = gets.chomp + end + end +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..28e759b0 --- /dev/null +++ b/planet.rb @@ -0,0 +1,16 @@ +require 'pry' +class Planet + 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 + attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + + def summary + return "planet #{name} has a #{color} color, a mass of #{mass_kg}. It's #{distance_from_sun_km} away from the sun, and fun fact: #{fun_fact}" + end + +end diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..e1492505 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,25 @@ +require 'pry' +class SolarSystem + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + attr_reader :star_name, :planets + + def add_planet(planet) + @planets << planet + end + + def list_planets + string = "Planets orbiting #{@star_name}: \n" + list = planets.map.with_index do |planet, index| + "#{index+1}. #{planet.name}\n" + end + return string + list.join + end + # map adds a nill if it does not exist + def find_planet_by_name(target_planet) + planets.select { |planet| return planet if planet.name.upcase == target_planet.upcase } + end +end