forked from AdaGold/solar-system
-
Notifications
You must be signed in to change notification settings - Fork 48
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
hanalways
wants to merge
9
commits into
Ada-C11:master
Choose a base branch
from
hanalways:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sockets - Hana #38
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7fbfe99
finished Wave 2.2
hanalways aa8121e
finished wave 3
hanalways cac8399
refactored main with a case statement
hanalways 60322ab
changed case statement to if/elsif
hanalways 2bc190f
created solar system method new_planet
hanalways 272fa9a
created while loop, loop bug
hanalways f561163
fixed main loop bug
hanalways 9d37702
fixed find_planet_by_name method
hanalways 61e1991
clean up of pry/comments
hanalways File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?" | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 inSolarSystem
.