Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3d6b471
created new file w/ Planet class.
qqdipps Feb 25, 2019
6f7f64a
added planet constructor
qqdipps Feb 25, 2019
d2bbfbe
changed planet param to positional, added main file for main method.
qqdipps Feb 25, 2019
7ef773f
added minitest setup, beg writing tests.
qqdipps Feb 25, 2019
e1a5fd9
finished writind first wave of tests.
qqdipps Feb 25, 2019
7cfe570
uncommented constructor, all tests pass.
qqdipps Feb 25, 2019
77f41e7
added summary test of string
qqdipps Feb 25, 2019
8eb900c
fixed typos
qqdipps Feb 25, 2019
a59c2a2
added summary method.
qqdipps Feb 25, 2019
703f0f2
adjusted case of planet params.
qqdipps Feb 25, 2019
2fe3046
added method test for summary of planet attributes.
qqdipps Feb 25, 2019
9d4f910
added return summary statements. all tests are up to date and passing.
qqdipps Feb 25, 2019
3d7521c
added tests for edge case for mass and distance == 0.
qqdipps Feb 25, 2019
ba9f58e
added error checking for params of distance and mass
qqdipps Feb 25, 2019
8f694fa
added beg tests
qqdipps Feb 25, 2019
52b002f
added SolarSystem class
qqdipps Feb 25, 2019
a641b90
added constructor, intial tests pass.
qqdipps Feb 25, 2019
063a527
added add_planet tests and methods. test pass.
qqdipps Feb 26, 2019
bb81e72
added require_relative path for solar_system.rb
qqdipps Feb 26, 2019
d523f39
added tests and methods for list_planets, add_planets in solar_system.
qqdipps Feb 26, 2019
4dc77be
added solar_system model to main
qqdipps Feb 26, 2019
54b279c
raise exception if not of type Planet when adding planet, added test …
qqdipps Feb 26, 2019
a974a7c
added find_planet_by_name methods with tests to confirm. tests pass.
qqdipps Feb 26, 2019
543f8e7
changed variables in main, added find_planet_by_name method in main.
qqdipps Feb 26, 2019
9e6efff
added distance between method and tests, test pass.
qqdipps Feb 26, 2019
b6fcf04
added is_a_Planet? method.
qqdipps Feb 26, 2019
b077a0f
created layout of main.
qqdipps Feb 26, 2019
b9c2ce7
streamed lined main outline
qqdipps Feb 26, 2019
5d1389a
added missing methods, put in module, still working out main structure
qqdipps Feb 26, 2019
d34dcab
added main helper methods, and exception handling for CLI
qqdipps Feb 27, 2019
710e344
adding style
qqdipps Feb 27, 2019
e5abe55
fixed tests, updated style, added param checking for adding planet if…
qqdipps Feb 27, 2019
a111dc4
final txt ascii art added
qqdipps Feb 27, 2019
2b4f52a
added file for art
qqdipps Feb 27, 2019
d9fad69
fixed broken file paths
qqdipps Feb 27, 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["specs/*_spec.rb"]
end

task default: :test
21 changes: 21 additions & 0 deletions art/distance.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


________
`---. `.
\ `.
)_______`.
.' //`---...___
/ || // || `-._
)`-| =//= || || || / ).`.
_............_ ).-| || || `........'`-._ (o)
.-' `.----`. _...'.....__ || || _____ ||-\__.'
.' | Inter- | ).---.) /_______..--' || || ----- _ ||_/
.'_ |Plantery| || || `-------' || || =\\= \_.'
| | | Travel | |'---'| )`-| || || _..-'
'--'_____________|_____| ).-| =\\= || \\ _.-' ||
|[]--------------/ / __==\ \\ _.-' /o'\
\ .--. / _...--'' '-.__......_.-' \__/
`-._//'o\\___.'---'' \ .'
LGB \__/ -' / /
___.' /
`-------'
9 changes: 9 additions & 0 deletions art/goodbye.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


__
\ \_____
###[==_____>
/_/ __
\ \_____
###[==_____>
/_/
12 changes: 12 additions & 0 deletions art/planet.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

.::.
.:' .:
,MMM8&&&.:' .:'
MMMMM88&&&& .:'
MMMMM88&&&&&&:'
MMMMM88&&&&&&
.:MMMMM88&&&&&&
.:' MMMMM88&&&&
.:' .:'MMM8&&&'
:' .:'
'::' jgs
11 changes: 11 additions & 0 deletions art/solar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


* +
' |
() .-.,="``"=. - o -
'=/_ \ |
* | '=._ |
\ `=./`, '
. '=.__.=' `=' *
+ +
O * ' . jgs
17 changes: 17 additions & 0 deletions art/unicorn.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
. + . . . . . .
. . . *
. * . . . . . . + .
"You Are Here" . . + . . .
. | . . . . . .
| . . . +. + .
\|/ . . . .
. . V . * . . . . + .
+ . . . +
. . + .+. .
. . . + . . . . .
. . . . . . . . ! /
* . . . + . . - O -
. . . + . . * . . / |
. + . . . .. + .
. . . . * . * . +.. . *
. . . . . . . . + . . +
163 changes: 163 additions & 0 deletions lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
require "colorize"
require_relative "planet.rb"
require_relative "solar_system.rb"

def main
display(load_solar_system_program)
continue = true
while continue
puts options
continue = do_option(get_option)
end
display(good_bye)
end

def display(string, char = true, lag = 0.03)
if char
count = 0
string.each_char do |ch|
print ch
sleep lag if count < 100
count += 1
end
else
puts string
end
return true
end

def load_solar_system_program
@solar_system = SolarSystem.new("Err")

create_system(solar_system)
return "Welcome to the".cyan + " Solar System Program.".light_magenta.bold \
+ read_file("./art/solar.txt").yellow + "\n\nYou are in the Solar System of #{solar_system.name}.\n".cyan
end

def solar_system
return @solar_system
end

def create_system(solar_system)
solar_system.add_planet(Planet.new("Willerr", "blue", 5.972e24, 1.496e28,
"raining rainbows"))
solar_system.add_planet(Planet.new("Saverr", "amber", 9.34e54, 2.345e23,
"saving time by reversing spin"))
solar_system.add_planet(Planet.new("Katerr", "green", 2.342e33, 4.234e43,
"being made of dolphins"))
solar_system.add_planet(Planet.new("Singherr", "onyx", 4.23e23, 46.2e23,
"emmitting song-like sounds"))
end

def options
string_of_options = "\n\nEnter ".green + "LIST".cyan.on_blue.italic + " to display all the planets in system.".green \
+ "\nEnter ".green + "ADD ".cyan.on_blue.italic + " to add planets".green \
+ "\nEnter ".green + "CALC".cyan.on_blue.italic + " to calculate the distance between two planets".green \
+ "\nEnter ".green + "INFO".cyan.on_blue.italic + " to display information about a planet".green \
+ "\nEnter ".green + "Q ".cyan.on_blue.italic + " to quit program\n".green
return string_of_options
end

def get_option
print "\nPlease Enter Selection: ".cyan
user_input = gets.chomp.upcase
return user_input if ["LIST", "ADD", "CALC", "INFO", "Q"].include?(user_input)
display("Hmm... ", true, 0.5)
puts "I don't understand \"#{user_input}\".".red.bold.underline
sleep 1
get_option
end

def do_option(option_key)
case option_key
when "LIST"
display(solar_system.list_planets.yellow)
when "ADD"
user_add_planet
when "CALC"
user_calc_distance
when "INFO"
display(user_info_planet.cyan)
sleep 1
when "Q"
return false
end
end

def user_add_planet
display("\nTo add a planet follow the prompts: \n".yellow)
print "Name? ".yellow
name = gets.chomp.capitalize
print "Color? ".yellow
color = gets.chomp.downcase
print "Mass in kg? ".yellow
mass = gets.chomp.to_i
print "Distance from sun in km? ".yellow
distance = gets.chomp.to_i
print "Fun fact? ".yellow
fun_fact = gets.chomp
begin
solar_system.add_planet(Planet.new(name, color, mass, distance, fun_fact))
display("\nCongratulations #{solar_system.planets[-1].name} Succesfully Added!!".cyan.on_blue)
puts read_file("./art/planet.txt").light_magenta
rescue ArgumentError => error_message
display("\nError: #{error_message} \nReselect option from main menu to try again.".red.underline.bold)
end
return true
end

def user_calc_distance
print "\nEnter first planet: ".yellow
planet1 = get_planet
return true if !planet1
print "Enter second planet: ".yellow
planet2 = get_planet
return true if !planet2
begin
distance = solar_system.distance_between(planet1, planet2)
puts read_file("./art/distance.txt").blue.bold
display("\nDirections from #{planet1.name.upcase} to #{planet2.name.upcase}:".light_magenta)
display("ONWARD -> ".cyan.on_blue)
display("%0.3e km ".cyan.on_blue % [distance])
display("\nIt will take %0.2e light years, expected day of arrival: May 4th".magenta % [distance / 1.057e13])
sleep 1
rescue ArgumentError => error_message
display("\nError: #{error_message} \nReselect option from main menu to try again.".red.underline.bold)
end
return true
end

def user_info_planet
print "\nWhich planet would you like more information on? ".yellow
planet = get_planet
return planet.summary if planet
return "Try using the numeric tags associated with planets from LIST command.".red
end

def get_planet
planet_name = gets.chomp.capitalize
begin
planet = solar_system.find_planet_by_name(planet_name)
rescue ArgumentError
planet = planet_name.to_i.to_s == planet_name ? solar_system.planets[planet_name.to_i - 1] : nil
if !solar_system.is_a_Planet?(planet)
display("Hmm... ", true, 0.5)
puts "#{planet_name} is not in the system of #{solar_system.name}".cyan
end
end
return planet
end

def read_file(file)
read = ""
File.open(file, "r") do |f|
read += f.read
end
return read
end

def good_bye
return "\n\nGoodbye! Visit the #{solar_system.name} system again soon! Safe Travels!\n".magenta.bold.italic.underline + read_file("./art/unicorn.txt").light_magenta.bold + "\n"
end

main
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.capitalize
@color = color.downcase
@mass_kg = mass_kg > 0 ? mass_kg : (raise ArgumentError.new("Mass must be greater than 0."))
@distance_from_sun_km = distance_from_sun_km > 0 ? distance_from_sun_km : (raise ArgumentError.new("Distance must be greater than 0."))
@fun_fact = fun_fact.downcase
end

def summary
return "\n#{name} is a #{color} planet that is %0.2e km from it's sun. \nIt has a mass of %0.2e kg and \nis known for #{fun_fact}." % [distance_from_sun_km, mass_kg]
end
end
46 changes: 46 additions & 0 deletions lib/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class SolarSystem
attr_reader :name, :planets

def initialize(name)
@name = name.capitalize
@planets = Array.new
end

def add_planet(planet)
raise ArgumentError.new("#{planet.capitalize} is not a Planet") if !is_a_Planet?(planet)
if planets.any? do |logged_planet|
planet.name == logged_planet.name
end
raise ArgumentError.new("#{planet.name}\'s name is in use.")
else
@planets << planet
end
return true
end

def list_planets
string_planets = "\nPlanets orbiting #{self.name}\n"
@planets.each_with_index do |planet, i|
string_planets += "#{i + 1}. #{planet.name}\n"
end
return string_planets
end

def find_planet_by_name(name)
@planets.each do |planet|
return planet if planet.name == name.capitalize
end
raise ArgumentError.new("No planet by #{name} found.")
end

def distance_between(planet1, planet2)
if is_a_Planet?(planet1) && is_a_Planet?(planet2)
return (planet1.distance_from_sun_km - planet2.distance_from_sun_km).abs.to_i
end
raise ArgumentError.new("#{planet1} and/or #{planet2} not of type Planet")
end

def is_a_Planet?(planet)
return planet.instance_of?(Planet)
end
end
57 changes: 57 additions & 0 deletions specs/planet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
gem "minitest", ">= 5.0.0"
require "minitest/pride"

Choose a reason for hiding this comment

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

Fabulous use of minitest-pride!

require "minitest/autorun"
require_relative "../lib/planet"

describe "Planet" do
let (:earth) {
Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life")
}

it "is an instance of Planet" do
expect(earth).must_be_instance_of Planet
end

it "has a name" do
expect(earth.name).must_equal "Earth"
end

it "has a color" do
expect(earth.color).must_equal "blue-green"
end

it "has a mass" do
expect(earth.mass_kg).must_be_close_to 5.972e24, 0.01
end

it "has a distance from sun" do
expect(earth.distance_from_sun_km).must_be_close_to 1.496e8, 0.01
end

it "has a fun fact" do
expect(earth.fun_fact).must_equal "only planet known to support life"
end

describe "summary" do
it "returns a string" do
expect(earth.summary).must_be_kind_of String
end
it "returns a summary of planet's attributes" do
expect(earth.summary).must_equal "\nEarth is a blue-green planet that is 1.50e+08 km from it's sun. \nIt has a mass of 5.97e+24 kg and \nis known for only planet known to support life."
end
end

describe "test edge case for distance and mass" do
it "0 mass will raise argument error" do
expect {
Planet.new("Invisible", "clear", 0, 234230, " located everywhere, but nowhere")
}.must_raise ArgumentError
end

it "0 distance will raise argument error" do
expect {
Planet.new("Rock", "jelly", 34535555, 0, " located everywhere, but nowhere")
}.must_raise ArgumentError
end
end
end
Loading