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

# Returns the details for the planet using method from Solar System
def display(solar_system)
puts "Which planet would you like to see?"
display_planet = gets.chomp.downcase
begin
puts solar_system.find_planet_by_name(display_planet).summary()
rescue NoMethodError => e
puts "Planet not in solar systen. Please enter a different planet or add new planet to the solar system."
end
end

# Verifies that the data is a number
def verify_number(data_to_verify)
until (Float(data_to_verify) rescue nil) != nil && data_to_verify.to_f > 0
puts "Input not accepted, please enter a number:"
data_to_verify = gets.chomp
end
data_to_verify.to_f
end

# Prompts for user input to create a new Planet
def user_create_planet(solar_system)
puts "What is the name of the planet?"
user_planet_name = gets.chomp.capitalize

puts "What color is #{user_planet_name}?"
user_planet_color = gets.chomp.downcase

puts "What is its mass in kilograms?"
user_planet_mass = verify_number(gets.chomp)

puts "What distance is the planet from the sun (in kilometers)?"
user_planet_distance = verify_number(gets.chomp)

puts "Can you share a fun fact about #{user_planet_name}?"
user_planet_fun_fact = gets.chomp.capitalize

user_planet = Planet.new(user_planet_name,user_planet_color, user_planet_mass,user_planet_distance,user_planet_fun_fact)
solar_system.add_planet(user_planet)
end

# Finds distance between two planets using Solar System
def display_distance_between(solar_system)
puts "Please enter the name of the first planet."
planet_one = gets.chomp

puts "Please enter the name of the second planet."
planet_two = gets.chomp

puts "The distance between #{planet_one} and #{planet_two} is #{solar_system.distance_between(planet_one, planet_two)} km."
end

# Control panel to call in methods for user to select options
def main
solar_system = SolarSystem.new('Sol')

earth = Planet.new('Earth', 'blue-green', 5.972e24, 4.769e9, 'Only planet known to support life')
solar_system.add_planet(earth)

venus = Planet.new('Venus', 'White', 6.392e24, 2.982e9, 'Sister planet to Earth')
solar_system.add_planet(venus)

puts "What action would you like to take?"
user_control = gets.chomp.downcase

# Prompts user for options until they choose to exit
until user_control == 'exit'
case user_control
when 'list planets'
puts solar_system.list_planets
when 'planet details'
display(solar_system)
when 'add planet'
user_create_planet(solar_system)
when 'find distance'
display_distance_between(solar_system)
else
puts "Please enter a valid option."
end

puts "What action would you like to take next?"
user_control = gets.chomp.downcase
end

end

main
44 changes: 44 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'pry'

class Planet
attr_reader :name, :color, :fun_fact, :mass_kg, :distance_from_sun_km

# Creates constructor for planet attributes
def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact)
@name = name
@color = color
self.mass_kg = mass_kg
self.distance_from_sun_km = distance_from_sun_km
@fun_fact = fun_fact
end

# Creates summary using details about the planet
def summary()
return "#{@name} is a wonderful planet! It is a beautiful #{color} color! #{@name} has a mass of #{mass_kg} kg and is #{distance_from_sun_km} km from the sun. You might be surprised to find that: #{fun_fact}."
end

# Verifies that mass is in numbers
def mass_kg=(value)
begin
value > 0
@mass_kg=(value)
rescue
puts "Mass entered is not in number format, please enter as a number."
user_input = gets.chomp.to_f
@mass_kg = user_input
end
end

# Verifies that distance is in numbers
def distance_from_sun_km=(value)
begin
value > 0
@distance_from_sun_km=(value)
rescue
puts "Distance entered is not in number format, please enter as a number."
user_input = gets.chomp.to_f
@distance_from_sun_km = user_input
end
end

end
37 changes: 37 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'pry'

class SolarSystem
attr_reader :star_name, :planets

# Creates constructor for solar system attributes
def initialize(star_name)
@star_name = star_name
@planets = []
end

# Adds a planet object to solar system
def add_planet(planet)
@planets << planet
end

# Lists out all planets in the solar system
def list_planets()
planets_list = @planets.each_with_index.map {|planet,index|
"#{index + 1}. #{planet.name}"}
planets_list.unshift("Planets orbiting #{@star_name}:\n").to_s

return planets_list
end

# Returns the planet object by name
def find_planet_by_name(planet_name)
@planets.detect {|planet|
planet.name.downcase.include? planet_name.downcase}
end

# Calculates the distance between two planets
def distance_between(planet_one, planet_two)
return (find_planet_by_name(planet_one).distance_from_sun_km - find_planet_by_name(planet_two).distance_from_sun_km).abs
end
end