diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..ec5afd46 --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,51 @@ +require_relative "planet.rb" +require_relative "solar_system.rb" + +def main + + #Added planets' info to each planet instance + solar_system = SolarSystem.new("Sun") + + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") + solar_system.add_planet(earth) + + mercury = Planet.new("Mercury", "dark grey", 4.867e24, 5.791e7, "Mercury is the smallest plannet in our solar system") + solar_system.add_planet(mercury) + + neptune = Planet.new("Neptune", "purple", 1.024e26, 4.495e9, "It is four times wider than Earth") + solar_system.add_planet(neptune) + + saturn = Planet.new("Saturn", "yellowish-brown", 5.683e26, 1.434e9, "Saturn is the second largest planet in our solar system") + solar_system.add_planet(saturn) + + venus = Planet.new("Venus", "opal", 4.867e24, 1.082e8, "It has intense heat and volcanic activity") + solar_system.add_planet(venus) + + ######## Prompt User's inputs######## + + puts "Welcome to Solar System program!" + puts "What would you like to do?" + puts "[list planets] [planet details] [add planet] [exit]" + user_input = gets.chomp + + until user_input.downcase == "exit" + case user_input.downcase + when "list planets" + puts solar_system.list_planets + when "planet details" + puts "Which planet would you like to learn about?" + planet_details_input = gets.chomp + found_planet = solar_system.find_planet_by_name(planet_details_input.downcase) + puts found_planet.summary + when "add planet" + solar_system.user_added_planet + puts solar_system.list_planets + end + + puts "What would you like to do next?" + puts "[list planets] [planet details] [add planet] [exit]" + user_input = gets.chomp + end +end + +main diff --git a/lib/planet.rb b/lib/planet.rb new file mode 100644 index 00000000..72415dff --- /dev/null +++ b/lib/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} has the color of #{color} with the mass of #{mass_kg} kgs. This planet is #{distance_from_sun_km} km from the sun. A fun-fact about this planet is: #{fun_fact}" + end +end diff --git a/lib/solar_system.rb b/lib/solar_system.rb new file mode 100644 index 00000000..a863f718 --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,47 @@ + +class SolarSystem + attr_reader :star_name, :planet + + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + def add_planet(planet_instance) + @planets << planet_instance + end + + def list_planets + conclusion_statement = "Planets orbiting #{star_name}:\n" + + i = 1 + planet_list = "" + @planets.each do |planet| + planet_list += "#{i}.#{planet.name} \n" + i += 1 + end + + return conclusion_statement + planet_list + end + + def find_planet_by_name(planet_name) + @planets.find { |planet| planet.name.downcase == planet_name.downcase } + end + + def user_added_planet + puts "Please enter new planet's name" + planet_name = gets.chomp + puts "Please enter new planet's color" + planet_color = gets.chomp + puts "Please enter new planet's mass in kilograms" + planet_mass = gets.chomp.to_i + puts "Please enter the distance between this new planet and the sun" + planet_distance = gets.chomp.to_i + puts "Please enter new planet's fun_fact" + fun_fact = gets.chomp + + user_planet = Planet.new(planet_name.capitalize, planet_color, planet_mass, planet_distance, fun_fact) + + add_planet(user_planet) + end +end diff --git a/specs/planet_spec.rb b/specs/planet_spec.rb new file mode 100644 index 00000000..dbc3307d --- /dev/null +++ b/specs/planet_spec.rb @@ -0,0 +1,11 @@ +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/planet.rb" +Minitest::Reporters.use! + +describe "planet" do + it "Check if a new instance is an instance of class Planet " do + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") + expect(earth).must_be_instance_of Planet + end +end