-
Notifications
You must be signed in to change notification settings - Fork 48
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be above initialize and below |
||
|
||
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sweet 👍 |
||
end | ||
end |
There was a problem hiding this comment.
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.