-
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 - Erica #24
base: master
Are you sure you want to change the base?
Sockets - Erica #24
Changes from all commits
1319449
4012ff1
eb5ac42
d2af4eb
259424e
4e282bf
39f3a35
8fc2c16
2a0a016
cd0d3e7
ab4c51a
de1794f
3f9af58
83d3b94
0f2ffc9
fd98cd7
326322e
91629ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require_relative "planet" | ||
require_relative "solar_system" | ||
|
||
solar_system = SolarSystem.new("Sol") | ||
mercury = Planet.new("Mercury", "dark grey", 3.285e23, 5.791e7, "Named for the messenger of the Roman gods due to its orbital speed") | ||
solar_system.add_planet(mercury) | ||
venus = Planet.new("Venus", "red-brown", 4.867e24, 1.082e8, "Both the morning star and the evening star") | ||
solar_system.add_planet(venus) | ||
earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "The densest planet in the solar system") | ||
solar_system.add_planet(earth) | ||
mars = Planet.new("Mars", "red", 6.4169e23, 2.279e8, "Home to the tallest mountain in the solar system") | ||
solar_system.add_planet(mars) | ||
jupiter = Planet.new("Jupiter", "red, white, and brown", 1.898e27, 7.785e8, "The planet with the shortest day in the solar system") | ||
solar_system.add_planet(jupiter) | ||
|
||
puts "Hello, please see the the following options for this program. You can: \nA: List Planets \nB: List Planet Details \nC: Add Planet \nD: Find the Distance Between Two Planets" | ||
puts "Please enter the corresponding letter for the action you would like to take. You can exit by entering 'exit'." | ||
response = gets.chomp.downcase | ||
while response != "exit" | ||
if response == "a" | ||
puts solar_system.list_planets | ||
elsif response == "b" | ||
puts "Please enter what planet you would like to learn about: " | ||
planet_choice = solar_system.find_planet_by_name(gets.chomp) | ||
puts planet_choice.summary | ||
elsif response == "c" | ||
solar_system.user_adds_planet | ||
elsif response == "d" | ||
puts solar_system.distance_between_by_input | ||
else | ||
puts "Please make sure to select from the choices above. You can exit be entering 'exit'." | ||
end | ||
puts "\nPlease enter what you would like to do: \nA: List Planets \nB: List Planet Details \nC: Add Planet \nD: Find the Distance Between Two Planets" | ||
response = gets.chomp | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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 summary | ||
return "#{@name} is #{@color}, weighs #{@mass_kg} kg, and is #{@distance_from_sun_km} km from the sun. It is #{@fun_fact.downcase}." | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require_relative "planet" | ||
|
||
class SolarSystem | ||
attr_reader :star_name, :planets | ||
|
||
def initialize(star_name) | ||
@star_name = star_name | ||
@planets = [] | ||
end | ||
|
||
def add_planet(planet) | ||
@planets.push(planet) | ||
end | ||
|
||
def list_planets | ||
planet_list = "Planets orbiting #{star_name}: " | ||
i = 0 | ||
@planets.each do | ||
planet_list += "\n#{i + 1}. #{@planets[i].name}" | ||
i += 1 | ||
end | ||
return planet_list | ||
end | ||
|
||
def find_planet_by_name(name) | ||
i = 0 | ||
@planets.each do | ||
if @planets[i].name.downcase == name.downcase | ||
return @planets[i] | ||
end | ||
i += 1 | ||
end | ||
end | ||
|
||
def user_adds_planet | ||
puts "Please complete the following information for the planet you would like added: \nName: " | ||
name = gets.chomp.capitalize | ||
puts "Color: " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that you've broken this out as a separate method. Since this method interacts with the user, it might fit better in |
||
color = gets.chomp | ||
puts "Mass in kg: " | ||
mass = gets.chomp | ||
puts "Distance from sun in km: " | ||
distance = gets.chomp | ||
puts "Fun fact: " | ||
fact = gets.chomp | ||
new_planet = Planet.new(name, color, mass, distance, fact) | ||
self.add_planet(new_planet) | ||
puts "Thank you for adding #{name} to the solar system!" | ||
return new_planet | ||
end | ||
|
||
def distance_between(planet1, planet2) | ||
return (planet1.distance_from_sun_km - planet2.distance_from_sun_km).abs | ||
end | ||
|
||
def distance_between_by_input | ||
puts "Please enter the two planets you would like to find the distance between:" | ||
print "Planet 1: " | ||
planet1 = self.find_planet_by_name(gets.chomp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, it might make sense to split the user interaction pieces of this out into a separate method in |
||
print "Planet 2: " | ||
planet2 = self.find_planet_by_name(gets.chomp) | ||
return self.distance_between(planet1, planet2) | ||
end | ||
end |
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.
You should wrap all this work in a
main
method.