diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..7bcfa7f5 --- /dev/null +++ b/main.rb @@ -0,0 +1,59 @@ +# main.rb + +require_relative "planet.rb" +require_relative "solar_system.rb" + +# option menu +def options + return " + Options available to you... + 1. See a LIST of all the planets on file + 2. See a specific planet's DETAILS + 3. ADD a planet + 4. EXIT\n + Please enter your option:" +end + +def main + solar_system = SolarSystem.new("Omicron") + + vulcan = Planet.new("Vulcan", "brown & reddish", 546700, 8426000, "Has no moon") + solar_system.add_planet(vulcan) + shoreleave = Planet.new("Shore Leave Planet", "green", 492600, 8634000, "Created as an amusement park") + solar_system.add_planet(shoreleave) + nibiru = Planet.new("Nibiru", "reddish & green", 734500, 3264000, "Home to chalk-skinned aliens") + solar_system.add_planet(nibiru) + vendikar = Planet.new("Vendikar", "red & grey", 236300, 284200, "Very rocky!") + solar_system.add_planet(vendikar) + + # Welcome message + puts "Welcome to the PlanetDex!" + + # user input time + # fencepost loop + puts options + option = gets.chomp + + loop do + if option.include?("list") || option == "1" + puts solar_system.list_planets + elsif option.include?("detail") || option == "2" + puts "Which planet would you like to learn about?" + planet = gets.chomp.downcase + puts solar_system.find_planet_by_name(planet).summary + elsif option.include?("add") || option == "3" + puts solar_system.new_planet + elsif option.include?("exit") || option == "4" + puts "Thank you for using the PlanetDex!" + exit + else + puts "That is not a valid option." + puts "Please re-enter your option:" + end + + puts options + option = gets.chomp + end +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..fb45c984 --- /dev/null +++ b/planet.rb @@ -0,0 +1,41 @@ +# planet.rb + +require "terminal-table" + +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 mass_kg + pretty_mass = @mass_kg.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse + return pretty_mass + end + + def distance_from_sun + pretty_distance = @distance_from_sun_km.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse + return pretty_distance + end + + def summary + table = Terminal::Table.new do |t| + t << ["Name:", @name] + t << :separator + t << ["Color:", @color] + t << :separator + t << ["Mass:\n(kilograms)", mass_kg] + t << :separator + t << ["Distance from the sun:\n(kilometers)", distance_from_sun] + t << :separator + t << ["Fun Fact:", @fun_fact] + end + + return table + end +end diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..bfdc1bfc --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,52 @@ +# solar_system.rb + +require "terminal-table" + +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name, planets = []) + @star_name = star_name + @planets = [] + end + + def add_planet(planet) + @planets << planet + end + + def list_planets + table = Terminal::Table.new do |t| + @planets.each_with_index do |planet, index| + t << ["#{index + 1}. ", planet.name.capitalize] + end + end + + return table + end + + def new_planet + puts "What is the planet's name?" + name = gets.chomp + puts "What is the planet's color?" + color = gets.chomp + puts "What is the planet's mass (kg)?" + mass = gets.chomp + puts "What is the planet's distance from the sun (km)?" + sun_distance = gets.chomp + puts "Please enter a fun fact about the planet!" + fun_fact = gets.chomp + + name = Planet.new(name, color, mass, sun_distance, fun_fact) + add_planet(name) + + return "Successfully added new planet!" + end + + def find_planet_by_name(planet_string) + find_planet = @planets.find(ifnone = nil) do |planet| + planet.name.capitalize == planet_string.capitalize + end + + return find_planet + end +end