forked from Ada-C8/Solar-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolarsystem3.rb
218 lines (152 loc) · 7.09 KB
/
solarsystem3.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
######### Solar System Class below #######
class SolarSystem
attr_accessor :name, :planets, :age
def initialize(name, planets, age) #planets is an array of Planet objects
@name = name
@planets = planets
@age = age
end
def add_planet(new_planet) #adds a new Planet object to the existing array of Planets
@planets << new_planet
end
def list_planets
@planet_names = []
@planets.each_with_index do |planet, idx|
@planet_names << " #{idx + 1 }. #{planet.name} "
end
return @planet_names
end
#### Calculate Planetary distance - revisit to see if this method works as it should and whether it belongs in the solarsystem class
# def planetary_distance(input_planet)
# @planetary_distances = []
#
# @planets.each do |planet|
# if input_planet.name != planet.name
# @planetary_distances << " #{input_planet.name} is #{(input_planet.distance_from_sun - planet.distance_from_sun).abs.round(2)} million kms away from #{planet.name}"
# end
# end
#
# return @planetary_distances
# end
end #end of class Solarsystem definition
#######PLANET CLASS DEFINITION###########
class Planet
attr_accessor :discoverer, :discovered_year, :travel_advice
attr_reader :name, :distance_from_sun, :year_length, :diameter, :zodiac, :number_of_moons
def initialize(name, dist_sun, yr_length, diam, zodiac_sign, num_moons)
@name = name
@distance_from_sun = dist_sun
@year_length = yr_length
@diameter = diam
@zodiac = zodiac_sign
@number_of_moons = num_moons
@discoverer = "UNKNOWN"
@discovered_year = "UNKNOWN"
@travel_advice = "None submitted"
end #end initialize
### alternative way to initialize below, explore further
# def initialize args
# args.each do |k,v|
# instance_variable_set("@#{k}", v) unless v.nil?
# end
def attributes
return " Planet Name: #{@name}\n Distance from Sun: #{@distance_from_sun} million km \n Year Length:#{@year_length} days \n Diameter: #{@diameter} km \n Zodiac Planetary Ruler for: #{@zodiac} \n Number of moons: #{@number_of_moons}" + " \n Discovered by #{@discoverer} in the year #{@discovered_year} " + "\n \n Travel Advice and Notes: #{@travel_advice}"
end
end # end planets
####### Create planets using planet objects below #####
def create_planet
puts "\nPlease input the following information about your new planet:"
puts "Name of Planet:"
new_planet_name = gets.chomp.capitalize
puts "Distance from sun (in million km):"
new_planet_distance_from_sun = gets.chomp.to_i
puts "Year length (in days):"
new_planet_year_length = gets.chomp.to_i
puts "Diameter (in km):"
new_planet_diameter = gets.chomp.to_i
puts "Affiliated Zodiac Sign"
new_planet_zodiac = gets.chomp.capitalize
puts "Number of moons:"
new_planet_num_moons = gets.chomp.to_i
return Planet.new(new_planet_name, new_planet_distance_from_sun, new_planet_year_length, new_planet_diameter, new_planet_zodiac, new_planet_num_moons)
end
#########end##########
#### Hard coding in a preset array of Planets to initialize our Hitchhiker's Guide. This Guide is specific to the new galaxy, Bamboozle
mercury = Planet.new("Mercury", 57.9, 88, 4878, "Gemini and Virgo",0)
venus = Planet.new("Venus", 108.2, 224, 12104, "Taurus and Libra", 0)
earth = Planet.new("Earth", 149.6, 365.25, 12756, "Nothing. Earth is not a zodiac ruler :(", 1)
earth.travel_advice = "Mostly harmless."
mars= Planet.new("Mars", 227.9, 687, 6794, "Aries", 2)
jupiter = Planet.new("Jupiter", 778.3, 4328.9, 142984, "Sagittarius", 66)
saturn = Planet.new("Saturn", 1427, 10585, 120536, "Capricorn", 62)
uranus = Planet.new("Uranus", 2871, 30660, 51118, "Aquarius", 27)
neptune = Planet.new("Neptune", 44971, 60152, 49532, "Pisces", 14)
planets_array = [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune]
amy_galaxy = SolarSystem.new("Bamboozle",planets_array, 1000)
def find_idx(planet_name, planet_array)
idx = nil
planet_array.each_with_index do |planet, index|
if planet.name == planet_name.capitalize
idx = index
end
end
idx
end
########## UI #######
# puts amy_galaxy.planetary_distance(earth)
puts "\nWelcome to the Hitchhiker's Guide to the #{amy_galaxy.name} Galaxy!"
puts "\nYou can: \n 1. Look up a planet \n 2. Submit a new planet entry \nPlease enter 1 or 2 to select your option."
action = gets.chomp.to_i
until (action == 1 || action == 2)
puts "Error: Please select option 1 or 2"
action = gets.chomp.to_i
end
if action == 1
continue = nil
until continue == "no"
puts "\nThe known planets in the #{amy_galaxy.name} Galaxy are:"
puts amy_galaxy.list_planets
puts "\nWhich planet would you like to learn about? (Enter the name of the planet)"
destination_planet = gets.chomp.capitalize
idx_of_planet = find_idx(destination_planet, planets_array)
until idx_of_planet # ensures user only selects planet that exists in our galaxy
puts "\nI'm sorry, that planet is not in the #{amy_galaxy.name} Galaxy. Please select a different planet. Your options are: "
puts amy_galaxy.list_planets
destination_planet = gets.chomp.capitalize
idx_of_planet = find_idx(destination_planet, planets_array)
end
puts " \n Ahhh... #{destination_planet}."
puts " \n#{amy_galaxy.planets[idx_of_planet].attributes}"
puts "\nWould you like to learn about another planet? (yes/no)"
continue = gets.chomp.downcase
until continue == "yes" || continue == "no"
puts "Invalid input. Please type 'yes' or 'no'."
continue = gets.chomp.downcase
end
end # end of until loop
##Ask user if they've discovered a new planet and prompt them to submit a new entry if yes
puts "Have you, by any chance, recently discovered a new planet in this galaxy? (yes/no)"
discovered = gets.chomp.downcase
end #end of option 1
if action == 2 || discovered =="yes"
puts "\nAwesome, congrats on discovering a new planet!"
new_planet = create_planet
puts "What is your name? (so we can give you credit)"
new_planet.discoverer = gets.chomp.capitalize
puts "What year did you discover this?"
new_planet.discovered_year = gets.chomp.to_i
puts "Any travel notes or advice?"
new_planet.travel_advice = gets.chomp
amy_galaxy.add_planet(new_planet)
idx_of_new_planet = find_idx(new_planet.name, planets_array)
puts "\nThanks for your contribution the Hitchhiker's Guide to the #{amy_galaxy.name} Galaxy! Here is your new entry:"
puts "\n" + amy_galaxy.planets[idx_of_new_planet].attributes
end
puts "\nThanks for reading the Hitchhiker's Guide to the #{amy_galaxy.name} Galaxy. Make sure you've packed your towel. So long, and thanks for all the fish.\n \nDON'T PANIC"
### Future refactoring and enhancements
# explore to see if it is possible to make newly added planet entries available each time the program runs
# prevent user error at all points of user input
# give user achance to select between galaxies
# give user the ability to enter in new travel advice (maybe in an option 3 at the beginning)
# modify the continue/ until loops so that user has a choice after each action to do option 1 or 2
# method for age/year of planet and planetary distance