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

def main
solar_system = SolarSystem.new("Sol")

mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.")
solar_system.add_planet(mercury)

venus = Planet.new("Venus", "yellow", 4.87e24, 108.2e6, "it probably had a moon, but collided with it after another impact reversed the planet's spin.")
solar_system.add_planet(venus)

earth = Planet.new("Earth", "blue", 5.97e24, 149.6e6, "it has more life than the rest of the known universe, as far as we know.")
solar_system.add_planet(earth)

mars = Planet.new("Mars", "red", 0.64e24, 227.9e6, "it has liquid water.")
solar_system.add_planet(mars)

jupiter = Planet.new("Jupiter", "orange", 1898e24, 778.3e6, "its gravity breaks apart many comets.")
solar_system.add_planet(jupiter)

saturn = Planet.new("Saturn", "gold", 569e24, 1427e6, "it's not known how or when its rings formed.")
solar_system.add_planet(saturn)

uranus = Planet.new("Uranus", "blue", 86.8e24, 2871e6, "it also has large storms in its atmosphere, like Jupiter.")
solar_system.add_planet(uranus)

neptune = Planet.new("Neptune", "blue", 102e24, 5913e6, "the wind can go as fast as 1,700 km/hr - faster than the speed of sound.")
solar_system.add_planet(neptune)

continue = true

while continue == true
puts "What would you like to do next?"
puts "list planets | planet details | add planet | distance between planets | exit"
get_input = gets.chomp
if get_input == "exit"
puts "See ya!"
return continue == false
elsif get_input == "list planets"
puts solar_system.list_planets
elsif get_input == "planet details"
puts "Which planet would you like to learn about?"
input_name = gets.chomp
puts solar_system.find_planet_by_name(input_name)
elsif get_input == "add planet"
solar_system.add_new_planet
puts "Planet added to the solar system!"
elsif get_input == "distance between planets"
puts "What's the first planet you're interested in?"
planet1_name = gets.chomp
puts "Sweet, what's the second planet?"
planet2_name = gets.chomp
puts solar_system.distance_between(planet1_name, planet2_name)
else
puts "Invalid prompt."
end
end
end

puts "Welcome to the solar system!"
main

# Moved to solar_system.rb for now

# def add_new_planet
# puts "Okay, what's the name of the planet we're missing?"
# name = gets.chomp
# puts "Cool, what color is it?"
# color = gets.chomp
# puts "What about its mass in kilograms?"
# mass_kg = gets.chomp
# until mass_kg.to_f > 0
# puts "Must be a number greater than zero."
# mass_kg = gets.chomp
# end
# puts "Now I need its distance from the sun in kilometers."
# distance_from_sun_km = gets.chomp
# until distance_from_sun_km.to_f > 0
# puts "Must be a number greater than zero."
# distance_from_sun_km = gets.chomp
# end
# puts "And finally, what's a fun fact about this planet?"
# fun_fact = gets.chomp

# return new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact)
# end
15 changes: 15 additions & 0 deletions lib/planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 a #{@color} planet that weighs #{mass_kg} kilograms and orbits #{@distance_from_sun_km} kilometers from the sun. A cool fact about #{@name} is that #{fun_fact}"
end
end
74 changes: 74 additions & 0 deletions lib/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require_relative "planet"

class SolarSystem
attr_reader :star_name, :planets

def initialize(star_name)
@star_name = star_name
@planets = []
@planet_print = ["Planets orbiting #{star_name}"]
#@new_planet
end

def add_planet(planet)
@planets << planet
end

def add_new_planet
print "Okay, what's the name of the planet we're missing?\n"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should be moved to main.rb That way you would separate the user interface role with the job of keeping and tracking the list of planets.

new_name = gets.chomp
print "Cool, what color is it?\n"
new_color = gets.chomp
print "What about its mass in kilograms?\n"
new_mass_kg = gets.chomp.to_f
until new_mass_kg > 0
print "Must be a number greater than zero.\n"
new_mass_kg = gets.chomp.to_f
end
print "Now I need its distance from the sun in kilometers.\n"
new_distance = gets.chomp.to_f
until new_distance > 0
print "Must be a number greater than zero.\n"
new_distance = gets.chomp.to_f
end
print "And finally, what's a fun fact about this planet?\n"
new_fun_fact = gets.chomp

new_planet = Planet.new(new_name, new_color, new_mass_kg, new_distance, new_fun_fact)
add_planet(new_planet)
end

def list_planets
@planets.each_index do |index|
@planet_print << "#{index + 1}. #{@planets[index].name}"
end
return @planet_print
end

def find_planet_by_name(input_name)
@planets.each do |each_planet|
if each_planet.name.casecmp(input_name) == 0
return each_planet.summary
end
end
return "Invalid planet."
end

def find_planet_distance_by_name(planet)
@planets.each do |each_planet|
if each_planet.name.casecmp(planet) == 0
return each_planet.distance_from_sun_km
end
end
return "Invalid planet."
end

def distance_between(planet1_name, planet2_name)
planet1_position = find_planet_distance_by_name(planet1_name)
planet2_position = find_planet_distance_by_name(planet2_name)
if planet1_position.is_a?(Float) == false || planet2_position.is_a?(Float) == false
return "Invalid planet entered."
end
return "#{(planet2_position - planet1_position).abs} kilometers"
end
end
25 changes: 25 additions & 0 deletions specs/planet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
gem "minitest", ">= 5.0.0"
require "minitest"
require "minitest/spec"
require "minitest/autorun"
require "minitest/reporters"
require "minitest/pride"

require_relative "../lib/planet"

describe "Planets tests" do
it "Tests mass is greater than zero" do
mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.")
expect(mercury.mass_kg).must_be :>, 0
end

it "Tests distance is greater than zero" do
mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.")
expect(mercury.distance_from_sun_km).must_be :>, 0
end

it "Test planet variable assignment and summary" do
mercury = Planet.new("Mercury", "grey", 0.33e24, 57.9e6, "it has ice in craters that never receive sunlight.")
expect(mercury.summary).must_equal "Mercury is a grey planet that weighs 3.3e+23 kilograms and orbits 57900000.0 kilometers from the sun. A cool fact about Mercury is that it has ice in craters that never receive sunlight."
end
end
17 changes: 17 additions & 0 deletions specs/solar_system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
gem "minitest", ">= 5.0.0"
require "minitest"
require "minitest/spec"
require "minitest/autorun"
require "minitest/reporters"
require "minitest/pride"

# require_relative "../lib/planet"
require_relative "../lib/solar_system"

describe "Solar system tests" do
it "Tests planet finding" do
solar_system = SolarSystem.new("Sol")
@planets = ["earth", "mars"]
expect(solar_system.find_planet_by_name("mars2")).must_match "Invalid planet."
end
end