Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laura_solar_system #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
49 changes: 49 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require_relative 'planet'
require_relative 'solar_system'
require 'pry'

def main

Choose a reason for hiding this comment

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

This method begs to be broken up into smaller simpler methods.

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)

puts "What would you like to do? (A/B/C/D)"
puts "option A: list plannets"
puts "option B: exit"
puts "option C: planet details"
puts "option D: add planet"
decision = gets.chomp
while decision.upcase != "B"
if decision.upcase == "C"
puts "which planet would you like to learn more about?"
planet = gets.chomp
found_planet = solar_system.find_planet_by_name(planet)
puts found_planet.summary
puts "What would you like to do? (A/B/C/D)\noption A: list plannets\noption B: exit\noption C: planet details\noption D: add planet"
decision = gets.chomp
elsif decision.upcase == "D"
puts "So, you want to add a planet, I need to know more about it."
puts "Name?"
name = gets.chomp
puts "color?"
color = gets.chomp
puts "mass?"
mass = gets.chomp
puts "distance from sun in km?"
distance = gets.chomp
puts "fun_fact?"
fun_fact = gets.chomp
planet = Planet.new(name, color, mass, distance, fun_fact)
solar_system.add_planet(planet)
puts "What would you like to do? (A/B/C/D)\noption A: list plannets\noption B: exit\noption C: planet details\noption D: add planet"
decision = gets.chomp
elsif decision.upcase == "A"
list = solar_system.list_planets
puts list
puts "What would you like to do? (A/B/C/D)\noption A: list plannets\noption B: exit\noption C: planet details\noption D: add planet"
decision = gets.chomp
end
end
end

main
16 changes: 16 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'pry'
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
attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact

Choose a reason for hiding this comment

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

This should be above initialize and below class Planet


def summary
return "planet #{name} has a #{color} color, a mass of #{mass_kg}. It's #{distance_from_sun_km} away from the sun, and fun fact: #{fun_fact}"
end

end
25 changes: 25 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'pry'
class SolarSystem
def initialize(star_name)
@star_name = star_name
@planets = []
end

attr_reader :star_name, :planets

Choose a reason for hiding this comment

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

ditto with above


def add_planet(planet)
@planets << planet
end

def list_planets
string = "Planets orbiting #{@star_name}: \n"
list = planets.map.with_index do |planet, index|

Choose a reason for hiding this comment

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

Awesome 🥇

"#{index+1}. #{planet.name}\n"
end
return string + list.join
end
# map adds a nill if it does not exist
def find_planet_by_name(target_planet)
planets.select { |planet| return planet if planet.name.upcase == target_planet.upcase }

Choose a reason for hiding this comment

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

sweet 👍

end
end