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

def main
sol_system = SolarSystem.new("Sol")
gem_homeland = Planet.new("Gem Homeland", "crystal", 78939, 287200, "Home of the Crystal Gems.")
sol_system.add_planet(gem_homeland)

venus = Planet.new("Venus", "purple", 78939, 2000, "The brightest object in our solar system after the sun and the moon. ")
sol_system.add_planet(venus)

earth = Planet.new("Earth", "blue", 100.25, 5000, "I live here!")
sol_system.add_planet(earth)

mars = Planet.new("Mars", "red", 78939, 2000, "John Jones is from there")
sol_system.add_planet(mars)

jupiter = Planet.new("Jupiter", "yellow", 78939, 2000, "The fastest rotating planet in the solar system.")
sol_system.add_planet(jupiter)

saturn = Planet.new("Saturn", "purple with rings", 78939, 2000, "My fav planet!")
sol_system.add_planet(saturn)

uranus = Planet.new("Uranus", "orange", 78939, 2000, "lol")
sol_system.add_planet(uranus)

neptune = Planet.new("Neptune", "green", 78939, 2000, "Not to be confused with the King of Bikini Bottom.")
sol_system.add_planet(neptune)

list = sol_system.list_planets

puts sol_system.list_planets_one
#will return a summary of the planet you put in the argument
# sol_system.find_planet_by_name_two("maRs").summary

puts "Would you like a list of planets, planet details, to add a planet, or to exit?"
puts "Enter (list), (details), (add), or (exit)."
puts "Enter List to continue or exit to end the program"

# gets user input

loop do
list = sol_system.list_planets
input = gets.chomp.downcase
break if input == "exit"

case input
when "list"
puts list
when "details"
puts "What planet do you want details for?"
details = gets.chomp.downcase
puts sol_system.find_planet_by_name_two(details).summary
when "add"
sol_system.new_planet
puts sol_system.list_planets
end
break

Choose a reason for hiding this comment

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

This break is always going to make the loop exit.

end

puts "Goodbye!"
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}: is the color #{color}, weighs #{mass_kg}kg, and is #{distance_from_sun_km}km from the sun. Fun fact: #{fun_fact} "
end
end

earth = Planet.new("Earth", "green", 100.25, 5000, "I live here!")
14 changes: 14 additions & 0 deletions planet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "minitest/autorun"
require "minitest/reporters"
require_relative "planet"

Minitest::Reporters.use!

describe "planet" do
it "will return a string" do
home_planet = earth.summary
earth = Planet.new("Earth", "green", 100.25, 5000, "I live here!")

expect(home_planet).must_be_instance_of String
end
end
58 changes: 58 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class SolarSystem
attr_reader :star_name, :planets

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

# Create a method add_planet,
#which will take an instance of Planet as a parameter and add it to the list of planets.
def add_planet(planet) #Planet.name
#iterate through each Planet.name and put it in the @planets array.
@planets << planet
end

def list_planets
result = "Planets orbitting:\n"
planet_names = @planets.map do |planet|
planet.name
end
planet_names.each_with_index do |name, index|
result += "#{index + 1}. #{name}\n"
end
return result
# @planets.name specify that you're searching the name
end

def list_planets_one

Choose a reason for hiding this comment

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

This method seems a duplication of the above

result = "Planets orbitting:\n"
@planets.each_with_index do |planet, index|
result += "#{index + 1}. #{planet.name}\n"
end
return result
end

def find_planet_by_name_two(name)
return @planets.find do |planet|
planet.name.downcase == name.downcase
end
end

def new_planet

Choose a reason for hiding this comment

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

This would be better as a method which takes a Planet as an argument and adds it to the planets array. That way you're separating the user-input task from the task of storing and organizing the planets.

puts "Add planet name:"
name = gets.chomp.downcase
puts "Add color:"
color = gets.chomp
puts "Add mass in kg:"
mass_kg = gets.chomp.to_i
puts "Distance from the sun?"
distance_from_sun_km = gets.chomp.to_i
puts "What is a fun fact about your planet?"
fun_fact = gets.chomp
user_input_planet = (Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact))
# planet(user_input_planet)
# puts sol_system.new_planet.summary
add_planet(user_input_planet)
end
end