Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sockets - Nara #27

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -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')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The contents of this elsif would be a good candidate for a helper method!

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
17 changes: 17 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -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