forked from Ada-C7/Solar-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolar_system5.rb
More file actions
175 lines (158 loc) · 4.03 KB
/
solar_system5.rb
File metadata and controls
175 lines (158 loc) · 4.03 KB
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
#Kelly Souza Ada C7 - Solar System Project
class SolarSystem
attr_reader :name, :planets
#initialize class Solar_systen
def initialize(name, planets = [])
@name = name
@planets = planets
end
#def method to add multiple planets in an array
def add_planets(new_planet_array)
@planets.concat(new_planet_array)
end
#def method to display planet info in solar system
def display_planets
@planets.each do |name|
name.all_data
end
end
end
#create class Planet
class Planet
attr_reader :name, :mass, :distance_from_the_sun, :diameter, :moons, :atmosphere
#initialize class planet
def initialize(planet_hash)
@name = planet_hash[:name]
@mass = planet_hash[:mass]
@distance_from_the_sun = planet_hash[:distance_from_the_sun]
@diameter = planet_hash[:diameter]
@moons = planet_hash[:moons]
@atmosphere = planet_hash[:atmosphere]
end
#define method to output data in neat format
def all_data
puts "The planet #{@name} has a mass of #{@mass} kg and diameter of #{@diameter} km."
puts "It is #{@distance_from_the_sun} km from the sun and has #{@moons} moons."
puts "Its main atmospheric gas is #{@atmosphere}."
end
def puts_name
puts "#{name.capitalize}"
end
end
planets = [
mercury_hash = {
name: "Mercury",
mass: 5839,
distance_from_the_sun: 234,
diameter: 23234,
moons: 2,
atmosphere: "none"
},
venus_hash = {
name: "Venus",
mass: 8923,
distance_from_the_sun: 234,
diameter: 23234,
moons: 5,
atmosphere: "oxygen"
},
earth_hash = {
name: "Earth",
mass: 23959,
distance_from_the_sun: 234,
diameter: 23234,
moons: 2,
atmosphere: "carbon dioxide"
},
mars_hash = {
name: "Mars",
mass: 21123,
distance_from_the_sun: 234,
diameter: 23234,
moons: 16,
atmosphere: "methane"
},
jupiter_hash = {
name: "Jupiter",
mass: 90875,
distance_from_the_sun: 234,
diameter: 23234,
moons: 7,
atmosphere: "hydrogen"
},
saturn_hash = {
name: "Saturn",
mass: 5839,
distance_from_the_sun: 234,
diameter: 23234,
moons: 4,
atmosphere: "oxygeb"
},
uranus_hash = {
name: "Uranus",
mass: 58522,
distance_from_the_sun: 234,
diameter: 23234,
moons: 2,
atmosphere: "carbon dioxide"
},
neptune_hash = {
name: "Neptune",
mass: 13518,
distance_from_the_sun: 234,
diameter: 23234,
moons: 6,
atmosphere: "ammonia"
}]
mercury = Planet.new(mercury_hash)
venus = Planet.new(venus_hash)
earth = Planet.new(earth_hash)
mars = Planet.new(mars_hash)
jupiter = Planet.new(jupiter_hash)
saturn = Planet.new(saturn_hash)
uranus = Planet.new(uranus_hash)
neptune = Planet.new(neptune_hash)
our_planets = Array.new
our_planets = [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune]
#adding multiple planets into the solar system
milky_way = SolarSystem.new("Milky-way")
milky_way.add_planets(our_planets)
#adding a single planet
pluto_hash = {
name: "Pluto",
mass: 345,
distance_from_the_sun: 114,
diameter: 777,
moons: 9,
atmosphere: "hydrogen"
}
#create planet object pluto
pluto = Planet.new(pluto_hash)
# create array of new planet(s)
new_planet_array = [pluto]
#add new planet(s) to SolarSystem
milky_way.add_planets(new_planet_array)
#print a list of planets
i = 1
milky_way.planets.each do |planet|
print "#{i}."
planet.puts_name
i += 1
end
puts "or Exit"
#get user input to choose planet information
continue = true
while continue
puts "Please select a planet you would like to learn more about!"
planet_choice = gets.chomp.capitalize
milky_way.planets.each do |planet|
if planet_choice == planet.name
print planet.name + ": "
planet.all_data
elsif planet_choice == "Exit"
continue = false
end
end
end
puts "We hope you learned lots!"
puts "Come again soon!"