Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f5365c3
Added file planet.rb for Planet class
tofuandeve Aug 19, 2019
5b01ef4
Implemented constructor for Planet class
tofuandeve Aug 19, 2019
0e17d8c
Added main.rb with 2 instances of Planet
tofuandeve Aug 19, 2019
216da74
Implemented summary method for Planet and modified main.rb to use sum…
tofuandeve Aug 19, 2019
b662bf4
Refactored constructor for error check
tofuandeve Aug 19, 2019
8b7983b
Added tests and Rakefile
tofuandeve Aug 19, 2019
e024889
Created SolarSystem class
tofuandeve Aug 19, 2019
a51a176
Implemented add_planet method for SolarSystem class
tofuandeve Aug 20, 2019
93e12dc
Implemented list_planets for SolarSystem class
tofuandeve Aug 20, 2019
415faae
Updated main.rb to use SolarSystem class
tofuandeve Aug 20, 2019
5258146
Added type error check for constructor and updated list_planets metho…
tofuandeve Aug 20, 2019
1e8a2c5
Implemented find_planet_by_name method for SolarSystem class
tofuandeve Aug 20, 2019
9a97d8f
Added tests for SolarSystem class Wave 2
tofuandeve Aug 20, 2019
2d95031
Modified Planet class constructor to take in keyword arguments and up…
tofuandeve Aug 20, 2019
d3244c0
Added test for constructor with keyword arguments in Planet class
tofuandeve Aug 20, 2019
33e26a4
Fixed naming issue for test in SolarSystem class and replaced expect …
tofuandeve Aug 20, 2019
e44fbf2
Implemented distance_between method for SolarSystem class and added t…
tofuandeve Aug 20, 2019
71a0e76
Modified distance_between method to take in 2 strings as arguments an…
tofuandeve Aug 20, 2019
2fc6f2b
Updated SolarSystem class and its tests
tofuandeve Aug 21, 2019
d55c784
Implemented wave 3: Modified main.rb
tofuandeve Aug 21, 2019
8ea3909
Updated indentation for solar-system folder
tofuandeve Aug 21, 2019
f573b30
Updated spacing for main.rb
tofuandeve Aug 21, 2019
1006676
Updated main.rb
tofuandeve Aug 21, 2019
2cb1ded
Updated relative path for main.rb
tofuandeve Aug 21, 2019
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['tests/*_test.rb']
end

task default: :test
20 changes: 20 additions & 0 deletions lib/planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Planet
attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact

def initialize(name: , color: , mass_kg:, distance:, fact:)
@name = name
@color = color

raise ArgumentError.new("Planet mass_kg must be a positive number") if mass_kg <= 0
@mass_kg = mass_kg

raise ArgumentError.new("Distance must be a positive number") if distance <= 0
@distance_from_sun_km = distance

@fun_fact = fact
end

def summary
return "Planet's name is #{@name}, it's #{@color}. Fun fact about #{name}: #{fun_fact}"
end
end
39 changes: 39 additions & 0 deletions lib/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative 'planet'

class SolarSystem
attr_reader :star_name, :planets

def initialize(star_name)
raise ArgumentError.new("Input must be a string for star name") if !(star_name.instance_of? String)
@star_name = star_name
@planets = Array.new
end

def add_planet(planet)
raise ArgumentError.new("Input must be a Planet!") if !(planet.instance_of? Planet)
@planets << planet
end

def list_planets
planets_list = "Planets orbiting #{@star_name}\n"
@planets.length.times do |index|
planets_list << "#{index + 1}. #{planets[index].name.capitalize}\n"
end
return planets_list
end

def find_planet_by_name(planet_name)
raise ArgumentError.new("Planet name must be a string") if !(planet_name.instance_of? String)
planets_by_name = @planets.select {|obj| obj.name == planet_name.capitalize}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice!

Here's a possible refactoring: using find instead of select. It will give back the element
that matches first. If there is no match, then it will give back nil

planet = (planets_by_name.empty?) ? nil : planets_by_name[0]
return planet
end

def distance_between(planet_name1, planet_name2)
raise ArgumentError.new("Arguments must be strings") if !(planet_name1.instance_of? String) || !(planet_name2.instance_of? String)
first_planet = find_planet_by_name(planet_name1)
second_planet = find_planet_by_name(planet_name2)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great work re-using the other instance methods!

raise ArgumentError.new("Planet doesn't exist in this Solar System") if first_planet == nil || second_planet == nil
return (first_planet.distance_from_sun_km - second_planet.distance_from_sun_km).to_i.abs
end
end
128 changes: 128 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require_relative 'lib/solar_system'

def get_positive_number_from_user
input = Integer(gets.chomp) rescue false
while !input || input <= 0
print "Invalid input. Input must be a positive number: "
input = Integer(gets.chomp) rescue false
end
return input
end

def get_string_input_from_user()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Really thorough and thoughtful logic in here. It anticipates a lot of interesting things users can do. Well done

input = gets.chomp.strip.capitalize
while input.empty?
print "Input cannot be empty"
input = gets.chomp.strip.capitalize
end
return input
end

def print_decisions(decisions)
puts "Here are some options you can choose: "
decisions.length.times do |index|
puts "#{index + 1}. #{decisions[index].capitalize}"
end
end

def get_decision(decisions)
puts "\nWhat would you like to do next?"
print_decisions(decisions)

decision = get_string_input_from_user
while !(decisions.include? decision)
puts "Hey, that's not a valid decision"
print_decisions(decisions)
decision = get_string_input_from_user
end
return decision
end

def create_planet
print "Please enter in your planet's name: "
name = get_string_input_from_user

print "Please enter in your planet's color: "
color = get_string_input_from_user

print "Please enter in your planet's mass_kg: "
mass_kg = get_positive_number_from_user

print "Please enter in your planet's distance from the sun: "
distance = get_positive_number_from_user

print "Please enter in your planet's fun fact: "
fact = get_string_input_from_user

return Planet.new(name: name, color: color, mass_kg: mass_kg, distance: distance, fact: fact)
end

def get_2_planet_names_from_user(solar_system)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I love how your code looks in this method! It looks so nice!!

puts "Please enter in name of 2 planets of which you want to calculate the distance between: "
user_planet_names = Array.new

2.times do
print "Planet name: "
planet = get_string_input_from_user

while solar_system.find_planet_by_name(planet) == nil
puts "#{planet} doesn't exist in our Solar System. This is our Solar System:"
puts solar_system.list_planets
print "Planet name: "
planet = get_string_input_from_user
end

user_planet_names << planet
end
return user_planet_names
end

def main
solar_system = SolarSystem.new('Sol')
print "How many planets would you like to enter in: "
number_of_planets = get_positive_number_from_user

number_of_planets.times do |i|
planet = create_planet
solar_system.add_planet(planet)
end

available_decisions = ["Planet details", "Add planet", "List planets", "Calculate distance", "Exit"]
while true
user_decision = get_decision(available_decisions)

case user_decision
when "Calculate distance"
planet_names = get_2_planet_names_from_user(solar_system)
distance = solar_system.distance_between(planet_names[0],planet_names[1])
puts "The distance between #{planet_names[0].capitalize} and #{planet_names[1].capitalize} is #{distance} km"

when "Planet details"
print "Please enter in name of a planet you want to look up in our Solar System: "
user_planet = get_string_input_from_user
planet = solar_system.find_planet_by_name(user_planet)

result_message = ""
if planet == nil
result_message << "Oops, #{user_planet} doesn't exist in our Solar System\n"
result_message << solar_system.list_planets
else
result_message << "This is the first result we found in our Solar System about #{user_planet}:\n #{planet.summary}"
end

puts result_message

when "Add planet"
planet = create_planet()
solar_system.add_planet(planet)
puts solar_system.list_planets

when "List planets"
puts solar_system.list_planets

else
exit
end
end
end
main
62 changes: 62 additions & 0 deletions tests/planet_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'

require_relative '../lib/planet'

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

describe "Planet class" do

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Really really wonderful tests! They're really thorough and cover all of the important parts. Keep it up!

describe "Constructor" do
it "Error checks input being passed in for mass_kg" do

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor nitpick: It may be helpful to include in the test name that this error happens for negative mass_kg (same as the next distance test)

expect {Planet.new(name:'Earth', color: 'blue-green', mass_kg: -5.972e24, distance: 1.496e8, fact: 'Only planet known to support life')}.must_raise ArgumentError
end

it "Error checks input being passed in for distance" do
expect {Planet.new(name:'Earth', color: 'blue-green', mass_kg: 5.972e24, distance: -1.496e8, fact: 'Only planet known to support life')}.must_raise ArgumentError
end

it "Initializes instance variables" do
earth = Planet.new(name:'Earth', color: 'blue-green', mass_kg: 5.972e24, distance: 1.496e8, fact: 'Only planet known to support life')
expect (earth.name).must_equal 'Earth'
expect (earth.fun_fact).must_equal 'Only planet known to support life'
expect (earth.color).must_equal 'blue-green'
expect (earth.mass_kg).must_equal 5.972e24
expect (earth.distance_from_sun_km).must_equal 1.496e8
end

it "Does not allow reassigning instance variables" do
earth = Planet.new(name:'Earth', color: 'blue-green', mass_kg: 5.972e24, distance: 1.496e8, fact: 'Only planet known to support life')
expect (earth.name).must_equal 'Earth'
expect (earth.fun_fact).must_equal 'Only planet known to support life'
expect (earth.color).must_equal 'blue-green'
expect (earth.mass_kg).must_equal 5.972e24
expect (earth.distance_from_sun_km).must_equal 1.496e8

expect {earth.color = 'pink'}.must_raise NoMethodError
end

it "Takes in arguments not in order" do
earth = Planet.new(color: 'blue-green', mass_kg: 5.972e24, fact: 'Only planet known to support life', name:'Earth', distance: 1.496e8)

expect (earth.name).must_equal 'Earth'
expect (earth.fun_fact).must_equal 'Only planet known to support life'
expect (earth.color).must_equal 'blue-green'
expect (earth.mass_kg).must_equal 5.972e24
expect (earth.distance_from_sun_km).must_equal 1.496e8
end
end

describe "summary method" do
it "Returns a string" do
earth = Planet.new(name:'Earth', color: 'blue-green', mass_kg: 5.972e24, distance: 1.496e8, fact: 'Only planet known to support life')
expect (earth.summary).must_be_instance_of String
end

it "Summarizes planet's information" do
earth = Planet.new(name:'Earth', color: 'blue-green', mass_kg: 5.972e24, distance: 1.496e8, fact: 'Only planet known to support life')
expect (earth.summary).must_equal "Planet's name is Earth, it's blue-green. Fun fact about Earth: Only planet known to support life"
end
end
end

Loading