diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..9ebe58ef --- /dev/null +++ b/main.rb @@ -0,0 +1,52 @@ +require_relative "planet.rb" +require_relative "solar_system.rb" + +def main + jupiter = Planet.new("Jupiter", "reddish", 578944636, 75436.32, "This planet has been a planet since the big bang.") + + saturn = Planet.new("Saturn", "multi-colored striped", 39435845, 222343, "This planet has rings of gas, dust and rock.") + + largo = Planet.new("Largo", "fushia", 4332, 435, "This planet is bright and sparsely populated. Life is not carbon-based.") + + office_space = Planet.new("Office Space", "blue-grey", 4534534, 66685995, "Residents of this planet must submit one TPS Report every Friday. Visitors may be required to work on the weekend.") + + playground = Planet.new("Playground", "orange", 686869939393, 8899494, "This planet is located at the edge of the universe.") + + solar_system_1 = SolarSystem.new("Sol") + solar_system_1.add_planet(jupiter) + solar_system_1.add_planet(saturn) + solar_system_1.add_planet(largo) + solar_system_1.add_planet(office_space) + solar_system_1.add_planet(playground) + + list = solar_system_1.list_planets + user_choice = "" + until user_choice == "EXIT" + puts "Hello! What would you like to do? You can LIST PLANETS, ADD A PLANET or EXIT." + user_choice = gets.chomp.upcase + if user_choice.upcase == "EXIT" + exit + end + if user_choice.upcase == "LIST PLANETS" + puts list + puts "\nWhich planet would you like to learn about? (enter its name)" + user_planet_choice = gets.chomp + user_choice_planet = solar_system_1.find_planet_by_name(user_planet_choice) + puts user_choice_planet.summary + puts "-------" + elsif user_choice.upcase == "ADD A PLANET" + new_planet = solar_system_1.add_planet_from_user_input + solar_system_1.planets << new_planet + list = solar_system_1.list_planets + else + puts "Oops, that's an invalid input! Please try again." + end + end + + # found_planet = solar_system_1.find_planet_by_name("Jupiter") + # puts found_planet.name + # puts found_planet.summary + # return "#{jupiter.summary} #{saturn.summary} #{playground.summary}" +end + +puts main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..c84149c1 --- /dev/null +++ b/planet.rb @@ -0,0 +1,23 @@ +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 + + unless mass_kg > 0 + raise ArgumentError, "Mass must be greater than zero." + end + + unless distance_from_sun_km > 0 + raise ArgumentError, "Distance from Sun must be greater than zero." + end + end + + def summary + return "#{name} is #{color}. #{fun_fact} Its mass is #{mass_kg} kg and it is #{distance_from_sun_km} km from the sun." + end +end diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..5beec0d4 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,49 @@ +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 + planet_list = "Planets orbiting #{@star_name}\n" + @planets.each_with_index do |item, index| + planet_list += "#{index + 1}. #{item.name}\n" + end + + return planet_list + end + + def find_planet_by_name(planet_name) + @planets.each do |this_planet| + if this_planet.name.downcase == planet_name.downcase + return this_planet + end + end + end + + def add_planet_from_user_input + puts "Please enter some details about this planet." + print "Name: " + @name = gets.chomp.capitalize + print "Color: " + @color = gets.chomp + print "Mass in kilograms: " + @mass_kg = gets.to_i + print "Distance from its sun: " + @distance_from_sun_km = gets.to_i + puts "Please enter one fun fact about this planet." + @fun_fact = gets.chomp + + return user_added_planet = Planet.new(@name, @color, @mass_kg, @distance_from_sun_km, @fun_fact) + # add_planet(user_added_planet) + # @planets << planet + # return user_added_planet + # puts "You added this planet: #{user_added_planet.name}" + end +end