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

Marj - solar-system #40

Open
wants to merge 17 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
16 changes: 16 additions & 0 deletions lib/planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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

# Add an instance method to `Planet` called `summary`. This method should _return_ (not `puts`) a string containing a nicely-formatted description of the planet. Exercise your `summary` method in the `main` method.
def summary
return "Welcome to planet #{name} - #{distance_from_sun_km}km from the sun. We spin a #{color} mass of #{mass_kg}. Did you know, #{name} is [the] #{fun_fact}!"
end
end
45 changes: 45 additions & 0 deletions lib/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class SolarSystem
attr_reader :star_name, :planets

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

def add_planet(planet_instance)
@planets << planet_instance
end

def list_planets
list = "Planets orbiting the star #{star_name}:"
if @planets.length == 0
list += "\nCurrently no planets orbiting the #{star_name}"
else
@planets.length.times do |index|
list_item = "\n #{index + 1}. #{@planets[index].name}"
list += list_item
end
end
return list
end

def find_planet_by_name(planet)
planet = planet.downcase
planet_instance = nil
count = 0
@planets.length.times do |index|
if @planets[index].name.downcase == planet
planet_instance = @planets[index]
count += 1
# else
# not sure why when the else clause is added, it defaults to this
# planet_instance = "No planet by that name."
end

if count > 1
planet_instance = 'More than one instance of this planet'
end
end
return planet_instance
end
end
47 changes: 47 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_relative 'lib/planet'
require_relative 'lib/solar_system'

def main
# 1. Create a `SolarSystem` and add some `Planet`s
puts "What would you like to name the star of your new solar system?"
star_name = gets.chomp
new_system = SolarSystem.new(star_name)
puts "Your new solar system now has a star called:", new_system.star_name
# 1. Enter a control loop that repeatedly asks the user what to do next. The two options at this point are `list planets` and `exit`.
continue = 0
until continue == 4
puts "Please choose a number: [1] List Planets, [2] Get Planet Details, [3] Add a Planet [4] Exit"
continue = gets.chomp.to_i
case continue
when 1
puts new_system.list_planets
when 2
puts new_system.list_planets
next if new_system.planets.length == 0
puts "Which planet would you like to learn more about?"
planet_name = gets.chomp
new_system.find_planet_by_name(planet_name)
when 3
puts "You've chosen to add a planet to your solar system. Please type:"
puts "Name:"
name = gets.chomp
puts "Color:"
color = gets.chomp
puts "Mass (KG):"
mass_kg = gets.chomp
puts "Distance From the Sun:"
distance_from_sun_km = gets.chomp
puts "Fun Fact:"
fun_fact = gets.chomp

new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact)
new_system.add_planet(new_planet)
when 4
continue == 4
else
puts "Not a valid option."
end
end
end

main