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

Water - Jessica C #33

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

def main
solar_system = SolarSystem.new('Zeus')
tatooine = Planet.new('Tatooine', 'orange-ish', 6.34e48, 1.29e13, 'has two moons')
pluto = Planet.new('Pluto', 'purple', 10, 2.8e293, 'counts as a planet in some books')
solar_system.add_planet(tatooine)
solar_system.add_planet(pluto)

option = ""

until option == "exit"
puts "Please choose one of these options:
1. list planets
2. get planet details
3. add planet
4. exit"

option = gets.chomp.downcase

case option
when "list planets"
puts solar_system.list_planets
when "get planet details"
puts "Which planet's details do you want to know? "
get_planet = gets.chomp
puts solar_system.find_planet_by_name(get_planet).summary
when "add planet"
puts "Please enter new planet name"
name = gets.chomp
puts "Enter planet color"
color = gets.chomp
puts "Enter planet mass"
mass = gets.chomp.to_f
puts "Enter planet distance from sun"
distance = gets.chomp
puts "Enter a fun fact about the planet"
fact = gets.chomp

solar_system.add_planet(Planet.new(name, color, mass, distance, fact))
end
end

end

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

def summary
return "#{@name} is #{@color}, has a mass of #{@mass_kg} kg, is #{@distance_from_sun_km} km from the sun and #{@fun_fact}"
end


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

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

def add_planet(planet)
@planets << planet
end

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

def find_planet_by_name(find_planet)
@planets.each {|planet| return planet if planet.name.downcase == find_planet.downcase}
end
end