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

def main
solar_system = SolarSystem.new('Sol')

earth = Planet.new('earth', 'blue-green', '5.972e24', '1.496e8', 'only planet known to support life')
solar_system.add_planet(earth)
mars = Planet.new('mars', 'reddish-brown','‎0.64171','141.6 million mi','it has the longest rotation period, rotates in the opposite direction to most other planets')
solar_system.add_planet(mars)
jupiter = Planet.new('jupiter','orange with white bands','1,898.19','483.8 million mi','the largest in the Solar System')
solar_system.add_planet(jupiter)
saturn = Planet.new('saturn','pale-gold','5.9724','890.8 million mi','second-largest in the Solar System. It is a gas giant a radius nine times that of Earth')
solar_system.add_planet(saturn)

list = solar_system.list_planets
puts list

found_planet = solar_system.find_planet_by_name("earth")
puts found_planet.summary

options = [ "list planets", "get planet details", "add a planet", "exit"]

puts "What would you like to do, please choose from the following options:"
puts options
user_input = gets.chomp.downcase

while options.include? user_input
case user_input
when "list planets"
puts solar_system.list_planets
when "get planet details"
puts solar_system.planet_details.summary

Choose a reason for hiding this comment

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

What if it doesn't find the planet? you should probably return planet.summary from planet_details instead.

when "add a planet"
puts solar_system.new_planet
when "exit"
exit
end

puts "What would you like to do next?"
user_input = gets.chomp.downcase
end
end

main
17 changes: 17 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

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

def summary
return "#{@name.capitalize}, has a #{color} color, with a mass of #{mass_kg}, holding a distance from the sun of #{distance_from_sun_km}, a fun fact is: #{fun_fact}"
end

end
72 changes: 72 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Solar System
class SolarSystem
attr_reader :star_name, :planets

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

def add_planet(planet)
@planets << planet
end

def list_planets
i = 0
list = []
@planets.each do |planet|
list << "#{i += 1}. #{planet.name}"
end
puts "Planets orbiting #{star_name}:"
return list
end

def find_planet_by_name(name)
@planets.each do |planet|
if planet.name == name.downcase
return planet
elsif planet.name != name.downcase
puts "no related data found, please enter a listed planet"
planet_details

Choose a reason for hiding this comment

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

This is a very dangerous way to call this method! Calling a method inside itself is called recursion, and if the method has a bug, there is no escape except to shut the program down.

end
end
end

def planet_details
puts "Enter the planet you want a little more info of"
user_input = gets.chomp.downcase

planet_info = find_planet_by_name(user_input)
return planet_info
end

def new_planet
puts "We will need the following planet information:"

puts "Planet name:"
name = gets.chomp.downcase

puts "Planet color:"
color = gets.chomp.downcase

puts "Planet Mass:"
mass = gets.chomp.to_i

puts "Planet Distance from the Sun:"
distance_from_sun = gets.chomp

puts "Planet Fun Fact:"
fun_fact = gets.chomp_to_i

Choose a reason for hiding this comment

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

A couple of weird things are happening here. 1) You use gets.chomp_to_i on line 59 when you mean gets.chomp.to_i as you did on line 53. But, the fun fact isn't a number, it seems you mixed up line 56 and 59. More testing would have probably caught this.


new_planet = Planet.new(name, color, mass, distance_from_sun, fun_fact)
add_planet(new_planet)

end
end