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
52 changes: 52 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require_relative "Planet"
require_relative "Solar_System"

def main
solar_system = SolarSystem.new("Solus")
neptune = Planet.new("Neptune", "blue", 1.02E26, 4.495E9, "greek god of the sea Poseidons planet")
mars = Planet.new("Mars", "red", 6.3E23, 227.9E9, "greek god of war Aries planet")
venus = Planet.new("Venus", "pink", 4.86E24, 108E9, "greek goddess of love Aphrodites planet")
solar_system.add_planet(neptune)
solar_system.add_planet(mars)
solar_system.add_planet(venus)
list = solar_system.list_planets

found_planet = solar_system.find_planet_by_name("Venus")

input = ""

until input == "exit"
puts "what would you like to do? list planets, planet details, add planet, exit"
input = gets.chomp
if input == "list planets"
list = solar_system.list_planets
puts list
elsif input == "planet details"
puts "what planet do you want to learn about?"
user_add_planet = gets.chomp
chosen_planet = solar_system.find_planet_by_name(user_add_planet)
puts chosen_planet.summary
elsif input == "add planet"
planet_new = add_new_planet
solar_system.add_planet(add_new_planet)
else

Choose a reason for hiding this comment

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

On line 30 you call add_new_planet and assign it to the planet_new variable. Then on line 31, instead of using planet_new, you call add_new_planet again! That means the user has to enter all this info twice.

Copy link
Author

Choose a reason for hiding this comment

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

THANK YOU for pointing that out. that was driving me bananas i couldnt figure out why it asked twice.

puts "toodles!"
end
end
end

def add_new_planet
puts "What is the name of the planet?"
name = gets.chomp.capitalize
puts "What color is the planet?"

Choose a reason for hiding this comment

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

I like that you broke this out into a separate method - good organization!

color = gets.chomp.downcase
puts "What is the planet's mass in kilograms?"
mass_kg = gets.chomp.to_f
puts "What is the planet's distance from the sun"
distance_from_sun_km = gets.chomp.to_i
puts "What is a fun fact about the planet?"
fun_fact = gets.chomp
new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact)
end

main
59 changes: 59 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Planet
def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact)
@name = name
@color = color
@mass_kg = mass_kg
@distance_from_sun_km = distance_from_sun_km
@fun_fact = fun_fact
end

def name
return @name
end

Choose a reason for hiding this comment

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

You should use the helper methods (attr_reader, attr_writer, attr_accessor) to generate these getter and setter methods.


def name=(name)
@name = name
end

def color
return @color
end

def color=(color)
@color = color
end

def mass_kg
return @mass_kg
end

def mass_kg=(mass_kg)
@mass_kg = mass_kg
end

def distance_from_sun_km
return @distance_from_sun_km
end

def distance_from_sun_km=(distance_from_sun_km)
@distance_from_sun_km = distance_from_sun_km
end

def fun_fact
return @fun_fact
end

def fun_fact=(fun_fact)
@fun_fact = fun_fact
end

def summary
return "The planet #{@name} is the color #{@color}. A neat fact about the planet: #{@fun_fact}."
end
end

# Load Planet into pry:
# $ pry -r ./planet.rb
mars = Planet.new("Mars", "red", 6.3E23, 227.9E9, "greek god of war Aries planet")
venus = Planet.new("Venus", "pink", 4.86E24, 108E9, "greek goddess of love Aphrodites planet")
jupiter = Planet.new("Neptune", "blue", 1.02E26, 4.495E9, "greek god of the sea Poseidons planet")
35 changes: 35 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class SolarSystem
def initialize(star_name)
@star_name = star_name
@planets = []
end

def star_name
return @star_name
end

def planets
return @planets
end

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

def list_planets
str = "Planets orbiting #{@star_name}\n"
@planets.each_with_index do |planet, index|
str += "#{index + 1}. #{planet.name}\n"
end
return str
end

def find_planet_by_name(planet_name)
@planets.each do |planet|
if planet_name.upcase == planet.name.upcase
return planet

Choose a reason for hiding this comment

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

Is there an enumerable method you could use for this?

end
end
return nil
end
end