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

require_relative 'planet'
require_relative 'solar_system'

# Method used if user chooses to add a new planet
def new_planet
puts "What is the name of the planet?"
new_planet_name = gets.chomp

puts "What color is the planet?"
new_planet_color = gets.chomp
while new_planet_color.empty?
puts "How about a color?"
new_planet_color = gets.chomp
end

puts "What is the mass?(numbers only)"
new_planet_mass = gets.chomp.to_f
while new_planet_mass <= 0.0
puts "Mass cannot be less than 0. Try again."
new_planet_mass = gets.chomp.to_f
end

puts "What is the distance from the sun?(numbers only)"
new_planet_distance = gets.chomp.to_i
while new_planet_distance <= 0.0
puts "Distance cannot be less than 0. Try again."
new_planet_distance = gets.chomp.to_i
end

puts "What is a fun fact about this planet?"
new_planet_fact = gets.chomp

return Planet.new(new_planet_name, new_planet_color, new_planet_mass, new_planet_distance, new_planet_fact)
end

# Find all planets within the solar system
def planet_details(current_planet, solar_system)
solar_system.planets.each do |planet|
if current_planet == planet.name
return planet.summary
end
end
return "That planet does not exist!"
end

# CLI for user with planet details
def main
solar_system = SolarSystem.new("Sun")
solar_system.add_planet(Planet.new("Mercury", "gray", 3.285e23, 57904197.12 ,"Mercury is named after the messenger of the Roman gods."))
solar_system.add_planet(Planet.new("Venus", "pale yellow", 4.867e24, 10821.23, "Venus is the second brightest natural object in the sky."))
solar_system.add_planet(Planet.new('Earth', 'blue', 5.972e24, 1.496e8, "Only planet known to support life."))
solar_system.add_planet(Planet.new("Mars", "reddish brown",6.39e23 , 227883110.4, "Pieces of Mars have been found on Earth."))
solar_system.add_planet(Planet.new("Jupiter", "orange", 1.898e27, 778600627.2, "Jupiter has at least 67 moons in satellite around the planet."))

# Display to user available options to continue
puts "Select a number from this COMMAND LIST to continue:
1. List all planets
2. Display planet details
3. Add a new planet
4. exit"
input = gets.chomp

while input != "4"
if input == "1"
puts solar_system.list_planets
puts "Select a number from the orginal command list to continue:"
input = gets.chomp

elsif input == "2"
puts "Which planet's details would you like to see?"
selected_planet = gets.chomp.capitalize
puts planet_details(selected_planet, solar_system)
puts "Select a number from the command list:"
input = gets.chomp

elsif input == '3'
solar_system.add_planet(new_planet)
solar_system.planets.each do |planet|
# puts planet.summary
end
puts "Select a number from the orginal command list to continue:"
input = gets.chomp

else
puts "Invalid selection type a valid number to continue:
1. List all planets
2. Display planet details
3. Add a new planet
4. exit"
input = gets.chomp
end
end

end

main()
37 changes: 37 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

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.capitalize
@color = color
@mass_kg = valid_mass(mass_kg)
@distance_from_sun_km = valid_distance(distance_from_sun_km)
@fun_fact = fun_fact
end

# Check that planet's mass is no less than 0
def valid_mass(mass_kg)
if mass_kg <= 0
raise ArgumentError "0 is an invalid input"
else
mass_kg.to_s
end
end

# Check that planet's distance to the sun is not less than 0
def valid_distance(distance_from_sun_km)
if distance_from_sun_km <= 0
raise ArgumentError "0 is an invalid input"
else
distance_from_sun_km.to_s
end
end

# Display the planet's details
def summary
return "\nThe planet's name is #{@name}, it has a recognizable #{@color} color.
It weighs about #{@mass_kg} kilograms and is #{@distance_from_sun_km} kilometers from the sun.
Fun fact about #{@name}: #{@fun_fact}.\n"
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 @@

class SolarSystem

attr_reader :star_name, :planets

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

# Add planet to list of planets
def add_planet(planet)
@planets << planet
end

# List that displays all available planets and their star
def list_planets
puts "Planets that orbit the #{self.star_name} are:"
@planet_list = self.planets.each_with_index.map{ |planet, i| " #{i += 1}.#{planet.name} " }
return @planet_list
end

# Locate planet by name
def find_planet_by_name(planet_name)
input = planet_name.capitalize
@planets.each do |planet|
if input == planet.name
return planet
end
return "Planet not found"
end
end

end