diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..85dabf01 --- /dev/null +++ b/main.rb @@ -0,0 +1,52 @@ +require "terminal-table" +require_relative "planet" +require_relative "solar_system" + +def main + solar_system = SolarSystem.new("A Wrinkle in Time") + + camazotz = Planet.new(“Camazotz”, “red”, 68.9, 22.777, “Ruled by a disembodied brain”) + ixchel = Planet.new(“Ixchel”, “pink”, 49, 10.8E9, “Inhabited by sightless creatures”) + uriel = Planet.new(“Uriel”, “blue”, 10, 411.5E1, “Lots of extremely tall mountains”) + + solar_system.add_planet(camazotz) + solar_system.add_planet(ixchel) + solar_system.add_planet(uriel) + + directions = "" + + until directions == "exit" + puts "Please enter list planets to see all the planets, planet details to learn more about each planet, or add planet to enter a new planet. + directions = gets.chomp.downcase + if directions == "list planets"EXIT + list = solar_system.list_planets + puts list + elsif directions == "planet details" + puts "Please enter the planet name you'd like to see details for?" + show_details = gets.chomp + found_planet = solar_system.find_planet_by_name(show_details) + puts found_planet.summary + elsif directions == "add planet" + planet_new = add_new_planet + solar_system.add_planet(planet_new) + else + puts "\nThanks!" + end + end +end + +def add_new_planet + puts "What is the name of the planet?" + name = gets.chomp.capitalize + puts "What color is the planet?" + color = gets.chomp.downcase + puts "What is the planet's mass in kilograms?" + mass_kg = gets.chomp.to_f + puts "What is the planet's distance from A Wrinkle in Time?" + distance_from_sun_km = gets.chomp.to_i + puts "What is a fun fact about the planet?" + fun_fact = gets.chomp + new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..cd32f251 --- /dev/null +++ b/planet.rb @@ -0,0 +1,20 @@ +require "terminal-table" + +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 + table = Terminal::Table.new :headings => ["Name", "Color", "Mass (kg)", "Distance From Sun (km)", "Fun Fact"] do |t| + t.add_row [@name, @color, @mass_kg, @distance_from_sun_km, @fun_fact] + end + return table + end +end diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..3700fcc2 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,26 @@ +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 + list = "Planets orbiting #{@star_name}:\n" + @planets.each_with_index do |planets, number| + item = "#{number + 1}. #{planets.name}\n" + list += item + end + return list + end + + def find_planet_by_name(find_planet) + repeat_name = @planets.find { |planet_name| planet_name.name == find_planet.capitalize } + return repeat_name + end +end