Skip to content
Open
Changes from all commits
Commits
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
146 changes: 146 additions & 0 deletions Solar-System.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Solar System project -create an object which contains a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby files should be named using snake_case. So this one would be solar_system.rb.

The choice is somewhat arbitrary, but when we start working on group projects it will be important for all of us to be doing the same thing.

# collection of other objects

require 'colorize'

class SolarSystem
# create a constructor to add planet name
def initialize(planets)
@planets = planets
end

attr_accessor :planets

def list
num = 1
@planets.each do |arr|
print "\n#{num}. #{arr.summary} ".cyan
num += 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced by the name of the block parameter here. arr is not very descriptive - it's not clear to the reader what type of thing you expect it to be. Calling it something like planet might be a better choice.

end
end
end

# building multiple classes and composing these together
class Planet
attr_accessor :name, :color, :orbital, :diameter, :distance

# create a constructor to add planet attributes
def initialize(name, color, orbital, diameter, distance)
@name = name
@color = color
@orbital = orbital
@diameter = diameter
@distance = distance
end
# list attributes for each planet
def summary
" #{@name}\tColor: #{@color}, Orbital: #{@orbital}
\t\tDiameter(km): #{@diameter}, Distance(km): #{@distance}\n"
end

end

# Wave 2/3 "hashes" with 5 attributes now variables
mercury = Planet.new("Mercury", "silver", 0.24, 4879, 57909227)
venus = Planet.new("Venus", "beige", 0.616, 12104, 108209475)
earth = Planet.new("Earth", "green", 1, 12756, 149598262)
mars = Planet.new("Mars", "red", 1.9, 6792, 227943824)
jupiter = Planet.new("Jupiter","brown", 11.9, 142984,778340821)
neptune = Planet.new("Neptune", "blue", 164.8, 49528, 4498396441)

# Array for SolarSystem class
planets = [ mercury, venus, mars, earth, jupiter, neptune ]

solar = SolarSystem.new(planets)

# show a list of the planets
puts "\nHERE IS OUR LIST OF PLANETS:\n".cyan

num = 1

planets.each do |x|
puts "#{num}. #{x.name}".cyan
num += 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another place where it might make sense to define a method on SolarSystem rather than accessing the list of planets directly.

end

# Ask the user if they would like to see more information
print "\nEnter (yes/no) if you would like to see more information: "
answer = gets.chomp.downcase

while answer == "yes"

print "\nWhich planet would you like to know more of? "
until (info = gets.chomp.capitalize).include?("#{@name}")
puts "I'm sorry. Please select one from the list-- "
end

case info
when "Earth"
puts earth.summary.green
when "Mercury"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This strategy of using a switch statement makes it difficult to add new planets. Could you do something like adding a method to SolarSystem to look up a planet by name? That would allow you to show info for user planets as well.

puts mercury.summary
when "Mars"
puts mars.summary.red
when "Jupiter"
puts jupiter.summary.yellow
when "Neptune"
puts neptune.summary.blue
when "Venus"
puts venus.summary.magenta
end

# ask the user if they would like to see any more information
print "\nWould you like any more information? (yes/no): "
answer = gets.chomp.downcase

end

# Ask the user if they would like to create their own planet
print "\nWould you like to create your own planet? (yes/no): "
create = gets.chomp.downcase

while create == "yes"

# Ask for the planet's name
print "\nEnter your planets' name: "
new_name = gets.chomp.capitalize

puts "That's not a planet...But I'll let you have it this time." if new_name == "Pluto"
puts "Good choice!" if new_name == "Saturn"
puts "I love Star Wars." if new_name == "Hoth" || new_name == "Tatooine" || new_name == "Jakku"

# Ask the planet's color
print "\nEnter #{new_name}s' color: "
new_color = gets.chomp.downcase

# Ask how long it takes to orbit the sun
print "\nHow long does it take #{new_name} to orbit the Sun? (*earth years) "
until (new_orbit = gets.chomp) =~ /^\d*\.?\d+$/
puts "\nInvalid. Please enter a number: "
end

# Ask the diameter
print "\nWhat is the diameter? (km) "
until (new_diameter = gets.chomp) =~ /^\d*\.?\d+$/
puts "\nInvalid. Please enter a number: "
end

# Ask how far the planet is from the sun
print "\nWhat is #{new_name}s' distance from the Sun? "
until (new_distance = gets.chomp) =~ /^\d*\.?\d+$/
puts "\nInvalid. Please enter a number: "
end
# Add the planet to the SolarSystem class
planets.push(new_name = Planet.new(new_name, new_color, new_orbit, new_diameter,
new_distance))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the new planet to planets works, but it's not great form. The purpose of SolarSystem is to keep track of our list of planets, and it's a little confusing to modify the list directly from outside. Instead, you could define a method add_planet on SolarSystem that takes an instance of Planet and adds it to the list.


# Show the new edited list of planets
puts "\nYOUR NEW PLANET IS NOW ON THE LIST-- ".cyan

solar.list

# Ask if the user would like to create more planets
print "\nWould you like to create another planet? (yes/no): "
create = gets.chomp.downcase

end