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

def main
solar_system = SolarSystem.new("Sun")

jupiter = Planet.new("Jupiter", "multicolor", "1.89e27", "7.78e8", "Has more than 75 moons!")
mars = Planet.new("Mars", "red", "6.41e23", "2.27e8", "Had water in the Ancient Past!")
earth = Planet.new("Earth", "blue-green", "5.972e24", "1.496e8", "Only planet known to support life")

solar_system.add_planets(earth)
solar_system.add_planets(jupiter)
solar_system.add_planets(mars)

x = 1
while x != 2
puts "What would you like to do?"
puts "1. List planet"

Choose a reason for hiding this comment

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

I don't know that I like the variable name x here - could you call this something more descriptive? Maybe user_choice?

puts "2. Exit"
puts "3. Planet details"
puts "4. Add planet"
x = gets.chomp.to_i
if x == 1
list = solar_system.list_planets
puts list
elsif x == 3

Choose a reason for hiding this comment

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

The user should be able to type a command like "list planets" or "quit" for these, not a number.

Also, why are your numbered options out of order? That makes this code just a bit more confusing.

puts "Which Planet would you like to know about?"
planet = gets.chomp.downcase
found_planet = solar_system.find_planet_by_name(planet)
puts found_planet.summary
elsif x == 4
puts "What is the name of the name of the planet you want to add?"
name = gets.chomp
puts "What is the color of your planet?"

Choose a reason for hiding this comment

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

The code to view planet details and to add a planet ends up being pretty big. It might be wise to isolate each of these in its own method.

Choose a reason for hiding this comment

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

Also, this is a big long block of code with no breaks. You should split it up using empty lines.

color = gets.chomp
puts "What is the weight in Kg?"
mass = gets.chomp.to_f
puts "What is the distance from the Sun?"
distance = gets.chomp.to_f
puts "Tell me a fun fact about this planet:"
fun_fact = gets.chomp
name = Planet.new("#{name}", "#{color}", "#{mass}", "#{distance}", "#{fun_fact}")
solar_system.add_planets(name)
end
end

#list = solar_system.list_planets

# found_planet = solar_system.find_planet_by_name("Earth")
# puts found_planet
# puts found_planet.summary
#puts list
end

main
22 changes: 22 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Planet
attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact

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

earth = Planet.new("Earth", "blue-green", "5.972e24", "1.496e8", "Only planet known to support life")
puts earth.name
puts earth.fun_fact

Choose a reason for hiding this comment

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

Here you are creating an instance of Planet inside that class. I'm not sure if this will work, or what effect it will have.

In general, please remove all debugging work before submitting your assignment.


def summary
return "Planet #{name} is #{color}, it weigh #{mass_kg}kg and it's #{distance_from_sun_km} km away from Sun, something fun about this planet is #{fun_fact}"
end
end

#puts earth.fun_fact
#earth.color = "pink"
25 changes: 25 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class SolarSystem
attr_reader :star_name, :planet

def initialize(star_name)
@planets = []
@star_name = star_name
end

def add_planets(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.

Small nitpick: the spec asked for a method named add_planet, not add_planet. This may seem like a small thing, but this sort of error makes everything a little more complex, especially when working with a big team of engineers. Software engineering is all about attention to detail.

Since the method only adds one planet at a time, add_planet is definitely the right choice.


def find_planet_by_name(name)
#raise ArgumentError unless @planet.include?(name.capitalize)
@planets.find { |planet| planet.name.downcase == name.downcase }
end

Choose a reason for hiding this comment

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

Good use of an enumerable here.


def list_planets
rotating_planets = @planets.map.with_index { |item, index|
"#{index + 1}" + "." + item.name
}
puts "Planets orbiting Sun are: "
return rotating_planets

Choose a reason for hiding this comment

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

Instead of putsing here, you should add the output on line 22 to the string you return.

end
end