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
97 changes: 97 additions & 0 deletions Solar-System
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Brittany Jones

Choose a reason for hiding this comment

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

This file should end with .rb

# Ada Solar system
# Last edited 2/9/18

# Create a SolarSystem class with an @planets instance variable.
# Create an initialize method which should take a collection of planet names and store them in an @planets instance variable.

# Create a class with a hash of planets and their attributes.
# Give each planet a year_length attribute which is the length of time the planet takes to go around its star.
# Give each planet a distance_from_the_sun attribute

class Planet
# Create reader methods to give a user access to read the instance variables.
attr_accessor :planet_hash

Choose a reason for hiding this comment

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

Code inside a class should be indented. Either use 1 tab or 2 spaces to indent code.

Also why make this a hash or an accessor?

By being a hash a user of Planet must know the structure of the hash and they keys.

By using attr_accessor you are giving users the ability to change @planet_hash. You probably don't want to do that.

Granted in this project it's a minor issue.

def initialize(name, color, moons, radius, distance_from_the_sun, year_length, fun_fact)
@planet_hash = {
"Name" => name,
"Color" => color,
"Moons" => moons,
"Radius" => radius,
"Distance from the Sun" => distance_from_the_sun,
"Year Length" => year_length,
"Fun Fact" => fun_fact
}
end
end

#Create a SolarSystem class with an @planets instance variable.
#Make your SolarSystem class take an array of Planets instead of hashes. class will take an array of hashes, instead of hashes.
class SolarSystem
attr_accessor :planets

Choose a reason for hiding this comment

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

Similar issue with making this an acessor.

def initialize
@planets = planets
@planets = []

Choose a reason for hiding this comment

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

You are blanking out @planets so anything sent into initialize is erased.

Also your initialize method isn't getting sent a parameter.

You should have:

def initialize(planets)
  @planets = planets
end

end

def add_planet(planet)
@planets.push(planet)
end

Choose a reason for hiding this comment

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

weird indentation here with end


# Add planets to the @planets array.
def print
planets.each do |planet|
puts planet.name

Choose a reason for hiding this comment

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

You're doing what's called mixing roles here. Each class should focus on one role. SolarSystem should deal with storing planets and returning a list of planet names. Planet should store planet details and provide getter methods.

The main program's methods can deal with Input and output to the screen. That separation of roles can make things easier to reuse and modify.

end
end
end

mercury= Planet.new("This planet is named after the Roman deity Mercury, the messenger to the gods.", "Grey", "0", "1,516 miles", "35.98 million miles", "88 days", "Mercury is the closest planet to the Sun and is also the smallest of the eight planets in our solar system. For every 2 orbits of the Sun, which takes around 88 Earth days, Mercury completes three rotations of its axis.")

venus= Planet.new("Venus, the brightest planet in the night sky, was named after the Roman goddess of love and beauty.", "Orange", "0", "3,760 miles", "67.24 million miles", "225 days", "The surface of the planet is obscured by an opaque layer of clouds made up of sulphuric acid.")

earth= Planet.new("The Earth is the only planet in our solar system not to be named after a Greek or Roman deity.", "Blue and Green", "1", "3,959 miles", "92.96 million miles", "365.24 days", "The Earth was formed approximately 4.54 billion years ago and is the only known planet to support life.")

mars= Planet.new("Mars carries the name of the Roman god of war", "Red", "2", "2,106 miles" ,"141.6 million miles", "687 days", " Phobos (fear) and Deimos (panic) are the Red Planet's two small moons. They are named after the horses that pulled the chariot of the Greek war god Ares, the counterpart to the Roman war god Mars.")

jupiter= Planet.new("Jupiter was named after the king of the Roman gods.", "Red, orange, brown", "53", "43,441 miles", "483.8 million miles", "4380 days", "Jupiter is the fifth planet out from the Sun, and is two and a half times more massive than all the other planets in the solar system combined.")

hoth= Planet.new("Hoth is an ice planet in the Star Wars fictional universe.", "Blue, white", "3", "90 miles", "534 million miles", "38 days", "Hoth is home to only a few species, including the towering, predatory Wampa and the gray snow-lizards known as Tauntauns.")

# Create a method that returns the Planet's attributes in an easy to read fashion.
def print_attributes(planet)
each_planet = planet.planet_hash
each_planet.keys.each do |key|
puts "#{key}: #{each_planet[key]}"
end
end

puts "Astrology 101: Take a look at some Planets in our Solar System"
puts "\n1. Mercury\n2. Venus\n3. Earth\n4. Mars\n5. Jupiter\n6. Hoth\n\nOf the planets listed, which one would you like to learn more about?\n\n"

planet_choice = gets.chomp.capitalize
puts "\n------------------------------------------------------------\n\n"

case planet_choice

Choose a reason for hiding this comment

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

You're not using the SolarSystem instance!

when "Mercury"
print_attributes(mercury)
when "Venus"
print_attributes(venus)
when "Earth"
print_attributes(earth)
when "Mars"
print_attributes(mars)
when "Jupiter"
print_attributes(jupiter)
when "Hoth"
print_attributes(hoth)
else
print "Would you like to enter your own planet?"
create_new_planet=gets.chomp.capitalize
if create_new_planet == "Yes"
puts "What is the name of your planet?"
new_planet_name = gets.chomp.capitalize
end
new_planet_name = Planet.new("Hoth is an ice planet in the Star Wars fictional universe.", "Blue, white", "3", "90 miles", "534 million miles", "38 days", "Hoth is home to only a few species, including the towering, predatory Wampa and the gray snow-lizards known as Tauntauns.")

end