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 - Hana #38

Open
wants to merge 9 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
59 changes: 59 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -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?"

Choose a reason for hiding this comment

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

This code to interact with the user might fit better in main.rb than here in SolarSystem.

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