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

Fire - Anna #42

Open
wants to merge 15 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.byebug_history
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
71 changes: 71 additions & 0 deletions lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'byebug'
require_relative 'planet'
require_relative 'solar_system'

def main
earth = Planet.new(
'Earth',
'blue-green',
5.972e+24,
149.6e+6,
"Only planet known to support life"
)

moon = Planet.new(
'Moon',
'grey',
7.348e+22,
150e+6,
"Earth's only natural satellite"
)

##############################################

solar_system = SolarSystem.new('Sun')

solar_system.add_planet(earth)
solar_system.add_planet(moon)

##############################################

continue = true
while continue
puts "This program has created a solar system with several planets. From here, you can:\n"
puts "1. List the planets in the solar system (enter 1)\n"
puts "2. Display details about a single planet in the system (enter 2)\n"
puts "3. Add a planet to the system (enter 3)\n"
puts "4. Calculate the distance between two planets (in km) (enter 4)\n"
puts "5. Exit (enter 5)\n"

response = gets.chomp.to_i

case response
when 1
puts solar_system.list_planets
when 2
puts "Please enter the name of the planet you'd like to know more about:"
planet_inspect = gets.chomp
planet_inspect = solar_system.find_planet_by_name(planet_inspect)
puts planet_inspect.summary
when 3
solar_system.user_add_planet
when 4
puts "Please enter the name of the first planet:"
planet_1 = gets.chomp
puts "Please enter the name of the second planet:"
planet_2 = gets.chomp

solar_system.find_planet_by_name(planet_1)
solar_system.find_planet_by_name(planet_2)
distance = solar_system.distance_between(planet_1, planet_2)
puts "The distance between #{planet_1} and #{planet_2} is approximately: #{distance} km."
when 5
puts "OK! Terminating program."
continue = false
else
puts "That was not a valid response."
end
end
end

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

if (mass_kg.class == Float || mass_kg.class == Integer) && mass_kg > 0
@mass_kg = mass_kg
else
raise ArgumentError, "For #{self.name}, this isn't the right type of input for mass in kg - please enter an number greater than 0."
end

if (distance_from_sun_km.class == Float || distance_from_sun_km.class == Integer) && distance_from_sun_km > 0
@distance_from_sun_km = distance_from_sun_km
else
raise ArgumentError, "For #{self.name}, this isn't the right type of input for distance from sun in km - please enter an number greater than 0."
end

@fun_fact = fun_fact
end

def summary
stats = "<< Summary of planet: #{name.upcase} >>\n" +
"Color: #{color}\n" +
"Mass (kg): #{mass_kg}\n" +
"Distance from sun (km): #{distance_from_sun_km}\n" +
"Fun fact: #{fun_fact}"
return stats
end

end
64 changes: 64 additions & 0 deletions lib/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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
summary_list = "Planets orbiting <#{@star_name}>\n"
@planets.each.with_index(1) { |planet, idx| summary_list << "#{idx}. #{planet.name}\n"}
return summary_list
end

def check_valid_planet(planet)
@planets.each do |one_planet|
if one_planet.name.upcase == planet.upcase
return one_planet
end
end
raise ArgumentError, "#{planet} is not part of the given solar system."
end

def find_planet_by_name(planet)
found_planet = check_valid_planet(planet)
return found_planet
end

def distance_between(planet_1, planet_2)
planet_1 = check_valid_planet(planet_1)
planet_2 = check_valid_planet(planet_2)
distance_bw_planets = (planet_1.distance_from_sun_km - planet_2.distance_from_sun_km).abs
return distance_bw_planets
end

def user_add_planet
puts "We need the following details about the planet:"
puts "What is the name of the planet?"
planet_name = gets.chomp
puts "What color is the planet?"
planet_color = gets.chomp
puts "What is the mass (in kg) of the planet?"
planet_mass_kg = gets.chomp.to_i
puts "What is the planet's distance from the sun (in km)?"
planet_dist_km = gets.chomp.to_i
puts "What is a fun fact about the planet?"
planet_fun_fact = gets.chomp

new_planet = Planet.new(
planet_name,
planet_color,
planet_mass_kg,
planet_dist_km,
planet_fun_fact
)

add_planet(new_planet)
end
end
75 changes: 75 additions & 0 deletions test/planet_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'minitest/autorun'
require 'minitest/spec'
require 'minitest/reporters'
require 'minitest/pride'

require_relative '../lib/planet'

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

describe 'Correct initialize' do

it 'raises an ArgumentError for invalid measurement inputs' do
expect {
Planet.new(
'Earth',
'blue-green',
'very big',
149.6e+6,
"Only planet known to support life"
)
}.must_raise ArgumentError

expect {
Planet.new(
'Earth',
'blue-green',
5.972e+24,
'very far',
"Only planet known to support life"
)
}.must_raise ArgumentError

expect {
Planet.new(
'Earth',
'blue-green',
'',
'very far',
"Only planet known to support life"
)
}.must_raise ArgumentError
end

it 'raises an ArgumentError for measurement inputs <= 0' do
expect {
Planet.new(
'Earth',
'blue-green',
5.972e+24,
-149.6e+6,
"Only planet known to support life"
)
}.must_raise ArgumentError

expect {
Planet.new(
'Earth',
'blue-green',
-5.972e+24,
'very far',
"Only planet known to support life"
)
}.must_raise ArgumentError

expect {
Planet.new(
'Earth',
'blue-green',
0,
900,
"Only planet known to support life"
)
}.must_raise ArgumentError
end
end
44 changes: 44 additions & 0 deletions test/solar_system_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'minitest/autorun'
require 'minitest/spec'
require 'minitest/reporters'
require 'minitest/pride'

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

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

describe 'checking solar system planets' do
# Arrange
earth = Planet.new(
'Earth',
'blue-green',
5.972e+24,
149.6e+6,
"Only planet known to support life"
)

moon = Planet.new(
'Moon',
'grey',
7.348e+22,
150e+6,
"Earth's only natural satellite"
)
solar_system = SolarSystem.new('Sun')

solar_system.add_planet(earth)
solar_system.add_planet(moon)

it 'raises an ArgumentError for invalid planets in solar system' do
expect {
solar_system.check_valid_planet('not a real planet')
}.must_raise ArgumentError
end

it 'raises an ArgumentError for invalid planets in solar system' do
expect {
solar_system.check_valid_planet('')
}.must_raise ArgumentError
end
end