forked from Ada-C9/Solar-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolar_system_wave3.rb
More file actions
109 lines (91 loc) · 2.32 KB
/
Copy pathsolar_system_wave3.rb
File metadata and controls
109 lines (91 loc) · 2.32 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
class SolarSystem
def initialize(planets)
@planets = planets
end
attr_reader :planets
def puts_self
puts self
end
def self.planets
return @planets
end
end
class Planet
attr_accessor :name, :order, :radius, :moons
def initialize(name, order, radius, moons)
@name = name
@order = order
@radius = radius
@moons = moons
end
def puts_self
puts self
end
def self.name
return @name
end
# instance method
def contents
return " #{order}. #{name}"
end
def self.contents
return @contents
end
def summary
return " #{name} - Radius: #{radius}km. Number of Moons: #{moons}."
end
def self.summary
return @summary
end
end
# input planets
planet_arr = [
Planet.new("Mercury", 1, 2440, 0),
Planet.new("Venus", 2, 6052, 0),
Planet.new("Earth", 3, 6371, 1),
Planet.new("Mars", 4, 3390, 2),
Planet.new("Jupiter", 5, 69911, 2),
Planet.new("Saturn", 6, 58232, 2),
Planet.new("Uranus", 7, 25362, 2),
Planet.new("Neptune", 8, 24622, 2),
]
#outputs planets
puts
print "Welcome to this Solar System directory! Here is a list of planets: "
puts
planet_arr.each_with_index do |planet, i|
puts "#{i+1}. #{planet.name}"
end
puts
print "Type LEARN to learn about a planet. Type NEW to add a new planet: "
selection = gets.chomp.upcase!
if selection == "LEARN"
print "Please type the number of the planet you'd like to learn about: "
learn_option = gets.chomp.to_i
counter = 0
until learn_option >= 0 && learn_option <= planet_arr.length
counter += 1
if counter == 3
puts "Too many incorrect attempts. Goodbye"
exit
else
print "You did not type a valid number. Please try again: "
learn_option = gets.chomp.to_i
end
end
puts planet_arr[learn_option-1].summary
elsif selection == "NEW"
print "What's the planet name? "
input_name = gets.chomp.upcase!
print "What's the radius of #{input_name} in kilometers? "
input_radius = gets.chomp.to_i
print "How many moons does #{input_name} have? "
input_moons = gets.chomp.to_i
planet_arr << Planet.new(input_name, 0, input_radius, input_moons)
puts "Here's an updated list of planets: "
planet_arr.each_with_index do |planet, i|
puts "#{i+1}. #{planet.summary} "
else
puts "You did not enter a valid option. Goodbye"
end
end