Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
90c2895
Complete Wave 1 - Planet class created
seaweeddol Feb 18, 2020
332ada3
Write tests to ensure number inputs are greater than zero
seaweeddol Feb 18, 2020
39397c2
Write code to run program
seaweeddol Feb 18, 2020
4a6f305
Complete Wave 2 - SolarSystem class created
seaweeddol Feb 19, 2020
27a37cf
add code to run SolarSystem class methods
seaweeddol Feb 19, 2020
7957b81
get float instead of integer from number prompts
seaweeddol Feb 19, 2020
0996441
add distance_between method
seaweeddol Feb 19, 2020
9ad1be4
complete wave 3 - add user prompt options
seaweeddol Feb 19, 2020
7cd86b8
add test for find_planet_by_name method
seaweeddol Feb 19, 2020
8657c1a
add test for distance_between method
seaweeddol Feb 19, 2020
26f9fb9
refactor distance_between method to use abs method
seaweeddol Feb 19, 2020
ae4389f
add additional user options to control loop
seaweeddol Feb 19, 2020
b3bc69a
update find_planet_by_name method to return instance of planet instea…
seaweeddol Feb 19, 2020
777dbbb
update planet details option to print summary
seaweeddol Feb 19, 2020
8c98de4
add distance between option to control loop
seaweeddol Feb 19, 2020
e149d3b
refactor distance between option to account for invalid inputs
seaweeddol Feb 20, 2020
9de4ab6
refactor add planet option to reduce repetition
seaweeddol Feb 20, 2020
a53e71d
fix distance_from_sun_km loop to check correct instance variable
seaweeddol Feb 20, 2020
310bef0
refactor to allow for easier scaling as more options are added
seaweeddol Feb 20, 2020
eeb29af
refactor planet details option to handle invalid input
seaweeddol Feb 20, 2020
05f71a3
fix test to pass
seaweeddol Feb 20, 2020
caa7fdb
fix broken functionality
seaweeddol Feb 20, 2020
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
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['test/test.rb']
end

task default: :test
30 changes: 30 additions & 0 deletions lib/planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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
until @mass_kg > 0
print "Please enter a mass kg greater than 0: "
@mass_kg = gets.chomp.to_f
end

@distance_from_sun_km = distance_from_sun_km
until @distance_from_sun_km > 0
print "Please enter a distance from sun greater than 0: "
@distance_from_sun_km = gets.chomp.to_f
end

@fun_fact = fun_fact
end

def summary
return " ~~ #{@name} ~~
Color: #{@color}
Mass (kg): #{@mass_kg}
Distance from sun (km): #{@distance_from_sun_km}
Fun Fact: #{@fun_fact}"
end
end
38 changes: 38 additions & 0 deletions lib/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative './planet.rb'

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
planet_list = "Planets orbiting #{@star_name}"
@planets.each_with_index do |planet, i|
planet_list += "\n #{i + 1}. #{@planets[i].name}"
end

return planet_list
end

def find_planet_by_name(planet_input)
planet_input.capitalize!()

planet_lookup = @planets.find_index {|planet| planet.name.capitalize() == planet_input}

return planet_lookup ? @planets[planet_lookup] : "The planet #{planet_input} doesn't exist"
end

def distance_between(planet_one, planet_two)
planet_one_distance = planet_one.distance_from_sun_km
planet_two_distance = planet_two.distance_from_sun_km

return (planet_one_distance - planet_two_distance).abs
end
end
73 changes: 73 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require_relative 'lib/planet.rb'
require_relative 'lib/solar_system.rb'

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

earth = Planet.new('Earth', 'blue-green', 753, 1.496e8, 'Only planet known to support life')
jupiter = Planet.new('Jupiter', 'blue-red', 1.898e27, 817e6, 'Jupiter is the largest planet in our Solar System.')

solar_system.add_planet(earth)
solar_system.add_planet(jupiter)

input = ""
until input == "exit"
puts "\nWhat would you like to do?"
options = ["planet [details]", "[list] planets", "[add] planet", "[distance] between planets", "exit"]

options.each_with_index do |option, i|
puts "#{i + 1}. #{option}"
end

input = gets.chomp.downcase

case input
when "1", "details", "planet details"
print "\nWhat planet do you want information on? "
planet = solar_system.find_planet_by_name(gets.chomp)

puts planet.respond_to?(:summary) ? planet.summary : planet

when "2", "list", "list planets"
puts "\n#{solar_system.list_planets}"

when "3", "add", "add planet"
info = {
:name => "Planet Name: ",
:color => "Color: ",
:mass_kg => "Mass (kg): ",
:distance_from_sun_km => "Distance from sun (km): ",
:fun_fact => "Fun fact: "
}

puts "Please enter the following information:"

info.each do |key, value|
print value
info[key] = gets.chomp
end

solar_system.add_planet(Planet.new(info[:name].capitalize, info[:color], info[:mass_kg].to_f, info[:distance_from_sun_km].to_f, info[:fun_fact]))

when "4", "distance", "distance between", "distance between planets"
planets = []

2.times do |i|
until planets[i].respond_to?(:summary)
print "Planet #{i + 1}: "
planets[i] = solar_system.find_planet_by_name(gets.chomp)
end
end

puts "#{planets[0].name} and #{planets[1].name} are #{solar_system.distance_between(planets[0], planets[1])}km apart."

when "5", "exit"
input = "exit"

else
puts "That's not a valid option, please try again."
end
end
end

main
50 changes: 50 additions & 0 deletions test/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'

require_relative '../lib/planet'
require_relative '../lib/solar_system'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe 'Planet' do
describe 'initialize method' do
it 'only takes numbers larger than zero' do
earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life')

expect(earth.mass_kg).must_be :>, 0
expect(earth.distance_from_sun_km).must_be :>, 0
end
end
end

describe 'SolarSystem' do
# initialize variables to use in tests
solar_system = SolarSystem.new('Sol')

earth = Planet.new('Earth', 'blue-green', 5, 20, 'Only planet known to support life')
jupiter = Planet.new('Jupiter', 'blue-red', 1.898e27, 45, 'Jupiter is the largest planet in our Solar System.')
pluto = Planet.new('Pluto', 'black', 1.898e27, 75, 'Pluto is the cutest planet.')

solar_system.add_planet(earth)
solar_system.add_planet(jupiter)
solar_system.add_planet(pluto)

describe 'find_planet_by_name method' do
it 'returns planet info when valid planet is provided' do
earth_found = solar_system.find_planet_by_name('Earth')

expect(earth_found).must_equal earth
end
end

describe 'distance_between method' do
it 'returns correct distance between planets' do
distance_between_earth_jupiter = solar_system.distance_between(earth, jupiter)
distance_between_pluto_earth = solar_system.distance_between(pluto, earth)

expect(distance_between_earth_jupiter).must_equal 25
expect(distance_between_pluto_earth).must_equal 55
end
end
end