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 - Nara #27
Open
bonara
wants to merge
8
commits into
Ada-C11:master
Choose a base branch
from
bonara: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 - Nara #27
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cbed3b7
Finished Wave 1
bonara 68620dc
Completed Wave 2
bonara 51d144b
Updated Wave 2 methods
bonara 2a420b6
Completed Wave 3
bonara 6a372fb
Added colorize and terminal-table gems
bonara e79aa8d
Added user input handler
bonara fd3e490
Added a method to handle input validations.
bonara b895a76
Added capitalize method to show a user input in a sentence
bonara 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,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 |
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,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 |
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,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 |
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.
The contents of this elsif would be a good candidate for a helper method!