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 - Erica #24

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
1319449
Added planet file
norrise120 Feb 25, 2019
4012ff1
Created Planet class with construction with 5 parameters
norrise120 Feb 25, 2019
eb5ac42
Added main file with main method to instantiate two planets and added…
norrise120 Feb 25, 2019
d2af4eb
Added summary method to Planet class
norrise120 Feb 25, 2019
259424e
Updated main method to use summary method from planet file
norrise120 Feb 25, 2019
4e282bf
Added solar system file that contains solarsystem method
norrise120 Feb 25, 2019
39f3a35
Added add_planet method to Solar System planet
norrise120 Feb 25, 2019
8fc2c16
Added list_planets method to solar_system.rb and deleted test code
norrise120 Feb 25, 2019
2a0a016
Updated SolarSystem to take one parameter and updated main method to …
norrise120 Feb 26, 2019
cd0d3e7
Added find_planet_by_name method and updated driver method
norrise120 Feb 26, 2019
ab4c51a
Added control loop to main method
norrise120 Feb 26, 2019
de1794f
Added planet details option to control loop in main method
norrise120 Feb 26, 2019
3f9af58
Added add planet option to control loop in main method
norrise120 Feb 26, 2019
83d3b94
Cleaned up main by creating user add planet method in solar system an…
norrise120 Feb 26, 2019
0f2ffc9
Created distance between method and added it to the control loop in main
norrise120 Feb 26, 2019
fd98cd7
Added find distance by input to solar system class
norrise120 Feb 27, 2019
326322e
Cleaned up main.rb and removed main method
norrise120 Feb 27, 2019
91629ed
Small stylistic changes
norrise120 Feb 27, 2019
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
35 changes: 35 additions & 0 deletions main.rb
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")

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.

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
15 changes: 15 additions & 0 deletions planet.rb
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
64 changes: 64 additions & 0 deletions solar_system.rb
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: "

Choose a reason for hiding this comment

The 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 main.rb, possibly taking the solar system as an argument.

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)

Choose a reason for hiding this comment

The 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 main.rb

print "Planet 2: "
planet2 = self.find_planet_by_name(gets.chomp)
return self.distance_between(planet1, planet2)
end
end