diff --git a/Planet.rb b/Planet.rb new file mode 100644 index 00000000..75abd2dd --- /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 "Planet name: #{@name}. #{@name.capitalize}'s color is #{@color}. This planet's mass is #{@mass_kg} and #{@distance_from_sun_km} million km away from the sun. An interesting fact is that #{@fun_fact}." + end +end diff --git a/README.md b/README.md index a69e0d94..ec34d880 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ The learning goal for this wave is to practice working with individual instances ```ruby # Load Planet into pry: - # $ pry -r ./planet.rb + # $ pry -r ./Planet.rb earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') puts earth.name diff --git a/Solar_System.rb b/Solar_System.rb new file mode 100644 index 00000000..74a67118 --- /dev/null +++ b/Solar_System.rb @@ -0,0 +1,50 @@ +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + @list_of_planets = [] + + end + + def add_planet(planet) + @planets << planet + end + + #Taking string, planet, and returning instance planet by + # pushing into an instance variable array + def list_planets + puts "Planets orbiting #{star_name}:" + + @planets.each_with_index do |planet, i| + @list_of_planets << "#{i+1}. #{planet.name}" + end + return @list_of_planets + end + + def find_planet_by_name(planet_name) + @planets.each do |planet| + if planet == planet_name + return planet.summary + end + end + return "Invalid planet." + end + + def add_new_planet + print "Which planet would you like to add? > " + new_planet_name = gets.chomp + print "What's the color? > " + new_planet_color = gets.chomp + print "What's the mass? > " + new_planet_mass = gets.chomp + print "Distance from the sun? > " + new_planet_distance = gets.chomp + puts "What about a fun fact?:" + new_planet_fact = gets.chomp.capitalize + + new_planet = Planet.new(new_planet_name, new_planet_color, new_planet_mass, new_planet_distance, new_planet_fact) + add_planet(new_planet) + end +end \ No newline at end of file diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..7612c2c6 --- /dev/null +++ b/main.rb @@ -0,0 +1,46 @@ +require_relative "Planet" +require_relative "Solar_System" + +def main + solar_system = SolarSystem.new('Sol') + + mercury = Planet.new( 'Mercury', 'dark gray', 3.285e23, 57.9, 'Mercury is shrinking!') + solar_system.add_planet(mercury) + + venus = Planet.new('Venus', 'white', 4.867e24, 108.2, 'A day on Venus is longer than a year') + solar_system.add_planet(venus) + + earth = Planet.new('Earth', 'blue-green', 5.972e24, 149.6, 'Only planet known to support life') + solar_system.add_planet(earth) + + mars = Planet.new('Mars', 'red', 6.39e24, 227.9, 'Pieces of Mars have fallen to Earth') + solar_system.add_planet(mars) + + jupiter = Planet.new('Jupiter', 'white-orange-red', 1.898e27, 778.3, 'Jupiter has the shortest day of all the planets') + solar_system.add_planet(jupiter) + + run_solar_system = true + puts "Enter: list planets | planet details | add planet | exit" + + while run_solar_system + user_input = gets.chomp + if user_input == "exit" + puts "Have a great day!" + return run_solar_system == false + elsif user_input == "list planets" + puts solar_system.list_planets + puts "Enter planet details to learn more or enter exit" + elsif user_input == "planet details" + puts "You picked planet details!" + puts "Which planet do you want to learn about?" + planet_name = gets.chomp + puts solar_system.find_planet_by_name(planet_name) #not working + elsif user_input == "add planet" + solar_system.add_new_planet + else + puts "Invalid prompt" + end + end +end + +main \ No newline at end of file