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

Ana- Solar System Final #50

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

#Initialize Solar System
def create_solar_system
#Create an instance of SolarSystem and add planet Wave 2, Part 4

solar_system =SolarSystem.new('Sun')
#Each planet is an instance of the Solar System

mercury =Planet.new('Mercury','gray',3.285e23, '69.306 ', 'Mercury has the most craters in the Solar System.')
solar_system.add_planets(mercury)

venus =Planet.new('Venus', 'white',"4.867e24","107.84 ","Venus spins clockwise on its axis")
solar_system.add_planets(venus)

earth =Planet.new('Earth', 'blue-green',5.972e24, 1.496e8, 'Only planet known to support life')
solar_system.add_planets(earth)

mars =Planet.new('Mars','red','6.39e23', '210.69', 'The NASA Curiosity Rover landed on Mars')
solar_system.add_planets(mars)

jupiter =Planet.new('Jupiter','orange-white','1.898e27','767.92 ','Jupiter is the largest planet in our solar system')
solar_system.add_planets(jupiter)

return solar_system
end

#Method to learn more about each planet, Wave 3, part 1 & 2
def get_planet_details(solar_system)
print "What is the name of the planet you want to learn about?"
planet_name =gets.chomp.strip

# the found planet is an instance of planet class
found_planet = solar_system.find_planet_by_name(planet_name)

puts found_planet.summary
end

#Method that will ask user to create a new planet, Wave 3 Part 3
def user_add_planet(solar_system)
print "What planet do you want to add?"
name = gets.chomp
print "what is the color of the planet?"
color = gets.chomp
print "what is the mass in kg of the planet? "
mass_kg = gets.chomp.to_i
print "what is the distance from the sun for the planet?"
distance_sun = gets.chomp.to_i
print "what is a fun fact about the planet?"
fun_fact= gets.chomp

user_planet = Planet.new(name,color,mass_kg,distance_sun,fun_fact)
solar_system.add_planets(user_planet)

end

def main
my_solar_system = create_solar_system
puts "Welcome to Solar System, please select a choice: "
puts "List planets\nPlanet details\nAdd planet\nExit\n"

user_input = gets.chomp.downcase.strip

until user_input =="exit"
case user_input
when "list planets"
puts my_solar_system.list_planets
when "planet details"
puts get_planet_details(my_solar_system)
when "add planet"
user_add_planet(my_solar_system)
else
puts "Invalid option.Program will exit."
end
puts"Please select a valid choice:"
user_input = gets.chomp.downcase.strip
end
end
main
19 changes: 19 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#Wave 1 - Create a class Planet
class Planet
attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact
#Create a constructor which takes 5 parameters Wave 1, Part 2
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

#Create a method that returns a string with the summary of a planet Wave 4 - Part 4
def summary
summary ="Planet #{name} is the color #{color} and it weights #{mass_kg}. and its #{distance_from_sun_km} km away from the sun. Here's a fun fact: #{fun_fact}."
return summary
end
end
34 changes: 34 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#Wave 2
# Create a new class called SolarSystem Wave 1, Part 1
class SolarSystem
attr_reader :star_name, :planets

#create a constructor with a parameter star_name
def initialize(star_name)
@star_name = star_name
@planets = []
end

#create a method to add planet with a parameter called planet --> Wave 2, Part 2
def add_planets(planet)
@planets << planet
end

#Create a method to return a string with a list of planets --> Wave 2, Part 3
def list_planets
list= "Planets orbiting the #{star_name}:\n"
@planets.each_with_index do |planet,i|
list += "\n#{i + 1}.#{planet.name}"
end
list
end

#Create a method to find the planet by name Wave 2, Part 5

def find_planet_by_name(planet_name)
@planets.each {|planet|return planet if planet.name.upcase ==planet.name.upcase}

raise ArgumentError, "#{planet_name} is not in solar system"
end

end