diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..fa5cbe45 --- /dev/null +++ b/main.rb @@ -0,0 +1,66 @@ +require_relative "planet.rb" +require_relative "solar_system.rb" + +def input_handle(variable) + puts "What is the #{variable} of the planet" + variable = gets.chomp.downcase + while variable =='' + puts "Please type in a valid input." + variable = gets.chomp.downcase + end + return variable +end + +def validate_integer(variable) + puts "What is the #{variable} of the planet" + variable = gets.chomp.downcase.to_i + while variable <= 0 + puts "Input must a number greater than zero. Please try again." + variable = gets.chomp.downcase.to_i + end + return variable +end + +def main + solar_system = SolarSystem.new('Sol') + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + mars = Planet.new('Mars', "red", 6.39e23, 2.279e8,'Mars is named after a mythological figure - the Roman god of war') + venus = Planet.new('Venus', "yellow-white", 4.867e24, 1.082e8,'Venus is the second brightest natural object in the sky') + mercury = Planet.new('Mercury', "dark gray", 3.30e23, 5.8e7,'Only two spacecraft have ever visited Mercuryß') + jupiter = Planet.new('Jupiter', "white, red, orange, brown, and yellow", 1.898e27, 7.785e8,'It is made primarily of gases and is therefore known as a "gas giant"') + solar_system.add_planet(earth) + solar_system.add_planet(mars) + solar_system.add_planet(venus) + solar_system.add_planet(mercury) + solar_system.add_planet(jupiter) + + while true + puts "What would you like to do? Choose from these options: [planet details], [list planets], [add planet] or [exit]." + user_input = gets.chomp.downcase + if user_input == "exit" + return + + elsif user_input == "list planets" + list = solar_system.list_planets + puts list + + elsif user_input == "planet details" + puts "What is the name of the planet you wish to learn about?" + name = gets.chomp + found_planet = solar_system.find_planet_by_name(name) + puts found_planet if !found_planet.is_a? Planet + puts found_planet.summary if found_planet.is_a? Planet + + elsif user_input == "add planet" + name = input_handle('name') + color = input_handle('color') + mass_kg = validate_integer('mass_kg') + distance_from_sun_km = validate_integer('distance_from_sun_km') + fun_fact = input_handle('fun_fact') + name = Planet.new(name.capitalize, color, mass_kg, distance_from_sun_km, fun_fact) + solar_system.add_planet(name) + end + end +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..a3b91b50 --- /dev/null +++ b/planet.rb @@ -0,0 +1,17 @@ +require 'terminal-table' +require 'colorize' +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) + raise ArgumentError.new("Both mass_kg and distance_from_sun_km must be a number that is greater than 0") if mass_kg.zero? || distance_from_sun_km.zero? + @name = name + @color = color + @mass_kg = mass_kg + @distance_from_sun_km = distance_from_sun_km + @fun_fact = fun_fact + end + + def summary + return "The planet name is #{@name}. Color of the planet is #{@color}. Planet mass is #{@mass_kg} kg. Distance from the sun is #{@distance_from_sun_km} km. #{@fun_fact}.".colorize(:magenta) + end +end diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..73263a83 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,43 @@ +require 'terminal-table' +require 'colorize' + +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 + counter = 0 + rows = [] + @planets.each do |planet| + rows << ["#{counter += 1}".colorize(:light_blue), "#{planet.name} ".colorize(:light_green)] + end + table = Terminal::Table.new do |t| + t.title = "Planets orbiting #{@star_name}".colorize(:color => :red) + t.rows = rows + t.style = {:width => 40, :padding_left => 3, :border_x => "=", :border_i => "x"} + end + return table + end + + def find_planet_by_name(name) + found = false + @planets.each do |planet| + if planet.name.downcase == name.downcase + found = true + return planet + else + found = false + end + end + if found == false + return "#{name.capitalize} is not a planet. However, you can create your own planet!" + end + end +end