forked from AdaGold/solar-system
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathsolar_system.rb
37 lines (31 loc) · 1.04 KB
/
solar_system.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 << planet
end
def list_planets
output_string = "Planets orbiting the #{@star_name} \n"
@planets.each_with_index do |planet, index|
output_string += "#{index + 1}. #{planet.name} \n"
end
return output_string
end
def find_planet_by_name(planet_name)
found_planet = @planets.find { |planet| planet.name == planet_name.capitalize}
if found_planet.nil?
raise ArgumentError.new("Sorry, that planet does not exist in this solar system.")
end
return found_planet
end
def distance_between(first_planet, second_planet)
first_planet_distance = find_planet_by_name(first_planet).distance_from_sun_km
second_planet_distance = find_planet_by_name(second_planet).distance_from_sun_km
distance_between_planets = (first_planet_distance - second_planet_distance).abs
return distance_between_planets
end
end