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

Solar System #43

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

def main

jupiter = Planet.new('Jupiter', 'brown', 4.972e24, 1.506e8, 'Only planet known how to be happy in life')
venus = Planet.new('Venus', 'gray', 2.972e24, 1.126e8, 'Only planet known how to travel around the world')
#===================
# create an instance of SolarSystem, add all your Planets to it
solar_system = SolarSystem.new('Sol')

all_planets = [jupiter, venus]
all_planets.each do |planet|
solar_system.add_planet(planet)
end

while true
puts "please select one: \n- list planets \n- planet details \n- add planet \n- enter exit to quit!"
print "? "
user_input = gets.chomp

case user_input

when "list planets"
puts solar_system.list_planets

when "planet details"
puts "which planet(name of the planet)?"
planet_name = gets.chomp.capitalize!
planet_summary = solar_system.find_planet_by_name(planet_name)
puts planet_summary.summary

when "add planet"
puts "enter the name of the planet you would like to add: "
name = gets.chomp.capitalize!
puts "color of the planet? "
color = gets.chomp.capitalize!
puts "planet's mass in kg?"
mass_kg = gets.chomp.to_i
puts "planet distance from the sun?"
distance_from_sun_km = gets.chomp.to_i
puts "a fun fact about the planet?"
fun_fact = gets.chomp
new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact)
solar_system.add_planet(new_planet)

when "exit"
break
else
puts "wrong input"
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 @@
class Planet
#info about single 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

# readable from outside the class, but not writable.
attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact

def summary
return "#{@name} is a #{@color}. it has a mass of #{@mass_kg} - distance from sun: #{@distance_from_sun_km} - fun fact: #{@fun_fact}"
end
end

# earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life')
# puts earth.name
# puts earth.fun_fact

35 changes: 35 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require_relative 'planet'

class SolarSystem
# constructor
def initialize(star_name)
@star_name = star_name
@planets = []
end

attr_reader :planets, :star_name

# take an instance of Planet as a parameter and add it to the list of planets.
def add_planet(planet)
@planets << planet
end

def list_planets
list_of_all_planets = "Planets orbiting #{@star_name}: \n"
@planets.each_with_index do |planet, i|
list_of_all_planets += "#{i + 1}. #{planet.name} \n"
end
return list_of_all_planets
end

# takes the name of a planet as a string, and returns the corresponding instance of Planet
def find_planet_by_name(planet_name)
# instance_of_planet = @planets.find { |planet| planet.name.downcase == planet.name.downcase}
@planets.each do |planet|
if planet.name == planet_name
return planet
end
end
end
end