diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..7f7b91bd --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,59 @@ +require_relative "planet" +require_relative "solar_system" + +# method that creates an instance of a solar system and adds instances of planets to it +# also, creates a control panel for the user +def main + # creating an instance of a solar system + big_ol_pupper = SolarSystem.new("Big Ol Pupper") + + # creating instances of planets + waffles = Planet.new("Waffles", "\'maple syrup\' brown", 3.555e23, 1.273e12, "It is the only known " \ + "planet to be smothered in fried chicken") + cuddles = Planet.new("Cuddles", "soft velvet grey", 9.973e30, 0.000002, "This planet loves the Big Ol " \ + "Pupper so much that it can\'t get close enough to it") + whiskey = Planet.new("Whiskey", "rich amber", 4.342e27, 3.473e13, "This planet has been aged for" \ + "billions of years in a giant cosmic cedar barrel") + bear = Planet.new("Bear", "jet black", 5.342e20, 5.342e14, "Contains more hugs per capita than any other" \ + "known planet") + pancake = Planet.new("Pancake", "light brown", 7.888e24, 3.342e5, "There is no planet fluffier or " \ + "flatter than Pancake") + + # adding instances of planets to an array + big_ol_pupper.add_planet(waffles) + big_ol_pupper.add_planet(cuddles) + big_ol_pupper.add_planet(whiskey) + big_ol_pupper.add_planet(bear) + big_ol_pupper.add_planet(pancake) + + # control panel that allows users to list planets, look at planet detials, add planets, or exit program + selection = "list planets" + while ["list planets", "planet details", "add planet", "exit"].include?(selection) + print "\nWhat would you like to do next? (list planets, planet details, add planet, or exit): " + selection = gets.chomp.downcase + + until ["list planets", "planet details", "add planet", "exit"].include?(selection) + print "Whoopsie Daisy! Please select an option from the list (list planets, planet details, add planet, or exit): " + selection = gets.chomp.downcase + end + + case selection + when "list planets" + puts "\n#{big_ol_pupper.list_planets}" + when "planet details" + print "\nWhat planet would you like to see details for?: " + planet_details = gets.chomp.downcase + found_planet = big_ol_pupper.find_planet_by_name(planet_details) + puts "\n====== #{found_planet.name} ======" + puts "#{found_planet.summary}" + when "add planet" + big_ol_pupper.add_new_planet + puts "\n========= New planet has been added to the Big Ol Pupper solar system! <3 ==========" + when "exit" + break + end + end +end + +# runs main method +main diff --git a/lib/planet.rb b/lib/planet.rb new file mode 100644 index 00000000..5ea1a178 --- /dev/null +++ b/lib/planet.rb @@ -0,0 +1,20 @@ +# class that creates instances of planets +class Planet + attr_accessor :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + + # constructor that initializes a name, color, mass (kg), distance from the sun (km), and fun fact + # about each instance of a 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 + + # method that prints out a summary of a planet instance + def summary + return "#{@name} is a #{@color} planet with a mass of #{@mass_kg} kg and is #{@distance_from_sun_km} " \ + "km from the sun! DID YOU KNOW: #{@fun_fact}?!?!" + end +end diff --git a/lib/solar_system.rb b/lib/solar_system.rb new file mode 100644 index 00000000..7df46a63 --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,49 @@ +# class that is responsible for keeping track of a collection of instances of Planet +class SolarSystem + # creating methods to read/return star_name & planets + attr_reader :star_name, :planets + + # constructor that accepts and stores star_name in an instance variable + # and assigns an empty array to the instance variable planets + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + # method that adds a planet to the @planets array + def add_planet(planet) + @planets << planet + end + + # method that returns a list of all planets in a given solar system + def list_planets + planet_list = "Doggos orbiting #{@star_name}: " + @planets.each_with_index { |planet, i| planet_list += "\n#{i + 1}. #{planet.name}" } + return planet_list + end + + # method that returns the instance of a planet when given a string of its name + def find_planet_by_name(planet_string) + @planets.each do |planet| + if planet.name == planet_string.capitalize + return planet + end + end + end + + # method that adds a new planet to the existing solar system + def add_new_planet + print "\nWhat is the name of the planet you would like to add?: " + new_planet = gets.chomp.capitalize + print "What is the color of the planet your are adding?: " + new_planet_color = gets.chomp.downcase + print "What is the mass of the planet you are adding (in kg)?: " + new_planet_mass = gets.chomp + print "What is the distance of this planet from Big Ol Pupper (in km)?: " + new_planet_distance = gets.chomp + print "What is a fun fact about this planet?: " + new_planet_fun_fact = gets.chomp.capitalize + new_planet_final = Planet.new(new_planet, new_planet_color, new_planet_mass, new_planet_distance, new_planet_fun_fact) + add_planet(new_planet_final) + end +end