Skip to content
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
24 changes: 24 additions & 0 deletions planets2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Planet

attr_accessor :name, :diameter, :mass, :number_of_moons, :color, :distance_from_the_sun

def initialize(planet_hash)
@name = planet_hash[:name]
@diameter = planet_hash[:diameter]
@mass = planet_hash[:mass]
@color = planet_hash[:color]
@number_of_moons = planet_hash[:number_of_moons]
@distance_from_the_sun = planet_hash[:distance_from_the_sun]
end

def number_of_moons(number_of_moons = 0)
@number_of_moons = number_of_moons
end

def about
puts "I am a planet named #{@name}! I have a diameter of #{@diameter} meters and a mass of #{@mass} kilograms."
puts "I have #{@number_of_moons} moons and I am #{@color}."
puts "I am #{@distance_from_the_sun} meters from the sun"
end

end
48 changes: 48 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class SolarSystem

attr_accessor :name, :planets

Choose a reason for hiding this comment

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

Watch your indentation here - you want to tab this in once

Copy link
Author

Choose a reason for hiding this comment

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

I just added in my Planet class and solar system program. Sorry they were missing the first time around!


def initialize (planet_hash)

Choose a reason for hiding this comment

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

It seems like this is good - but without providing an example for how you're instantiating your SolarSystem, I'm not sure if it is "correct" or not

@name = planet_hash[:name]
@planets = []
end

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

def add_multiple_planets(planet_array)

Choose a reason for hiding this comment

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

This is very good! You can also use a method called concat on an array, which will add a whole array of items:
@planets.concat(planet_array)

planet_array.each do |plan|
@planets.push(plan)
end
end

def about

Choose a reason for hiding this comment

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

Cool! I think the way you make this interactive to learn about each of the planets. I like the way that you use the count to dynamically display all of the different planets, and a while loop to continue throughout.

puts "Here are the planets in the #{@name} solar system:"
count = 1

@planets.each do |planet|
puts "#{count}. #{planet.name}"
count += 1
end
puts "#{count}. Exit"
puts "Which planet do you want to learn about?"
learn = gets.chomp.to_i
continue = true

while continue == true

Choose a reason for hiding this comment

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

Since continue is a boolean value, you can change this like to: while continue

if learn < count
planets[learn-1].about
puts "What planet do you want to learn about next?"
learn = gets.chomp.to_i
elsif learn == count
puts "Goodbye!"
continue = false
else
puts "I am sorry. I don't know what you mean. Please enter in a number from 1 to #{count-1} to learn about a planet, or #{count} to exit"
learn = gets.chomp.to_i
end
end
end

end
63 changes: 63 additions & 0 deletions solar_system_program.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "./planets2"
require "./solar_system"

apple_hash = {
name:"Apple",
diameter: 60,
mass:4_500,
number_of_moons:0,
color:"red",
distance_from_the_sun:4000,
}

apple = Planet.new(apple_hash)

pear_hash = {
name: "Pear",
diameter: 20,
mass: 3000,
number_of_moons: 4,
color: "green",
distance_from_the_sun: 5000,
}

pear = Planet.new(pear_hash)

orange_hash = {
name: "Orange",
diameter: 10,
mass: 500,
color: "orange",
distance_from_the_sun:10000,
}

orange = Planet.new(orange_hash)

orange.number_of_moons

planets_array = []
planets_array.push(apple)
planets_array.push(pear)

tree_hash = {
name: "Tree",
}

tree = SolarSystem.new(tree_hash)


tree.add_planet(orange)

tree.add_multiple_planets(planets_array)

tree.about

# Empty Hash template to create new planets
#empty_planet = {
# name:,
# diameter:,
# mass:,
# number_of_moons:,
# color:,
# distance_from_the_sun:,
# }