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

def main # Creates solar system and populates it

# These loop options will be used later in the method to list for the user what they can do
loop_options = ["list planets", "planet details", "add planet", "exit"]

# Create and populate a solar system with 2 planets
solar_system_1 = SolarSystem.new("Red Dwarf")

planet_1 = Planet.new(name: "Din", color: "Pink", mass_kg: 237923789, distance_from_sun_km: 3278997897823575978987, fun_fact: "It's made out of cotton candy")

solar_system_1.add_planet(planet_1)

planet_2 = Planet.new(name: "Minnie", color: "orange", mass_kg: 780978, distance_from_sun_km: 9877984531234354, fun_fact: "It's made out of mustard")

solar_system_1.add_planet(planet_2)

# method that calls "find_planet_by_name" method from solar_system.rb
# placed here because it needs to access to previously entered information
def planet_details
print "Enter the name of the planet you want to search for: "
planet_search_term = gets.chomp.upcase
return_statement = find_planet_by_name(planet_search_term)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is really stirring my curiosity. find_planet_by_name is a method defined on the solar_system class so I'm not sure how this is working without calling it on an instance of solar_system.

return return_statement
end

# method that manually enters planets into the previously established solar_system
# placed here because it needs to access to previously entered information
def add_planet_manually
print "What is the name of the planet you want to enter: "
name_manual = gets.chomp
print "What is its color? "
color_manual = gets.chomp
print "What is its mass: "
mass_manual = gets.chomp.to_i
print "What is its distance from the sun (km): "
distance_from_sun_km_manual = gets.chomp.to_i
print "What's a fun fact about this planet? "
fun_fact_manual = gets.chomp

user_planet = Planet.new(name: "#{name_manual}", color: "#{color_manual}", mass_kg: "#{mass_manual}", distance_from_sun_km: "#{distance_from_sun_km_manual}", fun_fact: "#{fun_fact_manual}")

return user_planet

end

# Asks user what to pursue next using the options from LN 6
# Calls secondary methods depending on the user input
puts "What would you like to do next?"
loop_options.each do |option|
puts "\t #{option}"
end
eval = gets.chomp.downcase

# Creates a loop that traps the user until they enter "exit"
# The user can enter multiple planets manually
# The planets previously entered by the user are available in subsequent loops
until eval == "exit" do
case eval
when "list planets"
solar_system_1.list_planets.each do |line|
puts line
end
eval = nil
when "planet details"
puts solar_system_1.planet_details

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is really stirring my curiosity. planet_details is not a method defined on the solar_system class so I'm not sure how this is working.

eval = nil
when "add planet"
solar_system_1.add_planet(add_planet_manually)
solar_system_1.list_planets.each do |line|
puts line
end
eval = nil
else
puts "What would you like to do next? "
loop_options.each do |option|
puts "\t #{option}"
end
eval = gets.chomp.downcase
end
end

end

main
22 changes: 22 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# creates a class
class Planet

# reader attributes
attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact

# constructor
# here the initial attributes are
def initialize(name:, color:, mass_kg:, distance_from_sun_km:, fun_fact:)
@name = name.upcase
@color = color.downcase
@mass_kg = mass_kg.to_i
@distance_from_sun_km = distance_from_sun_km.to_i
@fun_fact = fun_fact
end

# writes summary
def summary
return "Let's learn about #{@name.capitalize}. It is #{@color}, it has a mass of #{@mass_kg} kg, and it orbits #{@distance_from_sun_km} kilometers from the sun. Fun fact: #{@fun_fact}. \n"
end

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

attr_reader :star_name, :planets

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

def add_planet (planet)
@planets.push planet
end

def list_planets
place = @planets.map.with_index do |planet, index|
"#{ index + 1 }. #{planet.name.capitalize}"
end
return place
end

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

end
19 changes: 19 additions & 0 deletions test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'

require_relative '../main.rb'
require_relative '..planet.rb'

# Get that nice colorized output
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe 'planets' do
describe 'attributes entered' do
# checks that the name has at least 1 letter
# checks that the color has only letters
# checks that fun fact has an input
# checks that mass is not negative
# checks that the distance from the sun is not negative
end
end