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
f067120
Wave 1: Create a class called Planet, create two instances of Planet,…
Feb 18, 2020
5a12584
Wave 2: Create solar_system.rb, create a new class SolarSystem
Feb 18, 2020
1d55776
Wave 2: Create SolarSystem#add_planet method
Feb 18, 2020
d5ae823
Wave 2: Create a method SolarSystem#list_planets
Feb 19, 2020
968270d
Wave 2: Update driver code in main.rb to create an instance of SolarS…
Feb 19, 2020
0e95b50
Add a newline between each planet summary
Feb 19, 2020
84c20c7
Added more instances of Planet, return summaries
Feb 19, 2020
0b9f220
Edited the summary method
Feb 19, 2020
4a81992
Wave 2: Refactored the add_planet and list_planets methods
Feb 19, 2020
58e9bef
Wave 2: Fixed the find_planet_by_name method, returns planet.summary
Feb 19, 2020
b7ae21f
Wave 2: Fixed the find_planet_by_name method so it returns the whole …
Feb 19, 2020
474ffbf
Clean up comments
Feb 19, 2020
0cd2998
Wave 3: Restructure main to create a SolarSystem, add some Planets, e…
Feb 19, 2020
7d943a7
Wave 3: Add a planet details option to the control loop
Feb 19, 2020
ae58a2f
Wave 3: Add an add planet option to the control loop
Feb 19, 2020
4bc5da3
Wave 3: Change attr_reader to attr_accessor
Feb 19, 2020
46e34b6
Optionals: Create test file and Rakefile
Feb 19, 2020
a9d66f4
Clean up the printing of the fun facts
Feb 20, 2020
460c5af
Wave 3: Refactor, add separate methods for the planet details and add…
Feb 20, 2020
ce0a042
Fixed error, escape quote on line 22
Feb 20, 2020
6d9a9cb
Optionals: Unsuccessful attempt at adding minitests for Planet
Feb 20, 2020
139e4ba
Changed attr_accessor to attr_reader
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
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/ruby -wKU

task :default => :run

task :run do
ruby 'test/planet_test.rb'
end
75 changes: 75 additions & 0 deletions lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require_relative 'planet'
require_relative 'solar_system'

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

mercury = Planet.new("Mercury", "red", 0.330e24, 57e6, "Mercury is the smallest planet in our Solar System")
solar_system.add_planet(mercury)

venus = Planet.new("Venus", "yellow", 4.87e24, 108e6, "Venus is the hottest planet in our solar system")
solar_system.add_planet(venus)

earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Earth is the only planet known to support life")
solar_system.add_planet(earth)

mars = Planet.new("Mars", "red", 6.39e23, 230.26e6, "Mars is home to the largest volcano in our solar system, named Olympus Mons")
solar_system.add_planet(mars)

jupiter = Planet.new("Jupiter", "blue-red", 1.898e27, 817e6, "Jupiter is the largest planet in our solar system")
solar_system.add_planet(jupiter)

saturn = Planet.new("Saturn", "pink", 5.69e26, 1.5e9, "Saturn\'s rings are made primarily of \"water ice\" mixed with dust and other chemicals")
solar_system.add_planet(saturn)

uranus = Planet.new("Uranus", "blue", 8.68e25, 3e9, "Uranus is the coldest planet in our solar system")
solar_system.add_planet(uranus)

neptune = Planet.new("Neptune", "dark blue", 1.02e26, 4.5e9, "Neptune is a planet with a distinct blue color, which is due to the absorption of red light by methane in the atmosphere")
solar_system.add_planet(neptune)

def planet_details_ui(solar_system)
puts "\nWhat planet would you like to learn about?"
found_planet = solar_system.find_planet_by_name(gets.chomp)
return found_planet.summary
end

def add_planet_ui(solar_system)
puts "\nPlease enter the planet's name:"
name = gets.chomp.capitalize
puts "Please enter the planet's color:"
color = gets.chomp
puts "Please enter the planet's mass (kg):"
mass_kg = gets.chomp.to_i
puts "Please enter the planet's distance from the sun (km):"
distance_from_sun_km = gets.chomp.to_i
puts "Please enter a fun fact about the planet:"
fun_fact = gets.chomp

# The following line assigns each new planet to the variable new_planet, which is not ideal. I tried looking into how to dynamically set variable names in Ruby, but it seems like this functionality is too complex than it's worth here.
new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact)
solar_system.add_planet(new_planet)

return "\nThank you! #{name} has been added to #{solar_system.star_name}."
end

puts "Welcome to the Solar System Explorer!"

loop do
puts "\nWhat would you like to do? \n1) list planets \n2) planet details \n3) add planet \n4) exit"
option = gets.chomp.downcase

case option
when "1", "list planets"
puts solar_system.list_planets
when "2", "planet details"
puts planet_details_ui(solar_system)
when "3", "add planet"
puts add_planet_ui(solar_system)
when "4", "exit"
break
end
end
end

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

raise ArgumentError.new("Mass and distance from sun must be greater than zero") if @mass_kg < 0.0000001 || @distance_from_sun_km < 0.0000001
end

def summary
return "\n#{self.name} is a #{self.color} planet with a mass of #{self.mass_kg} kg and a distance from the sun of #{self.distance_from_sun_km} km. #{self.fun_fact}.\n"
end
end
27 changes: 27 additions & 0 deletions lib/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class SolarSystem
attr_reader :star_name, :planets

def initialize(star_name)
@star_name = star_name
@planets = Array.new
end

def add_planet(planet)
@planets << planet
end

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

def find_planet_by_name(planet_name)
raise ArgumentError.new("Argument must be a string") unless planet_name.is_a? String

@planets.each do |planet|
return planet if planet.name.downcase == planet_name.downcase
end
end
end
22 changes: 22 additions & 0 deletions test/planet_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'minitest'
require 'minitest/spec'
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/pride'
require 'pry'

require_relative '../lib/main'

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

describe "create two different instances of the Planet class" do
it "raises an ArgumentError if mass_kg is not greater than zero" do
earth = Planet.new("Earth", "blue-green", 0, 1.496e8, "Earth is the only planet known to support life")
expect(earth).must_raise ArgumentError
end

it "raises an ArgumentError if distance_from_sun_km is not greater than zero" do
earth = Planet.new("Earth", "blue-green", 5.972e24, 0, "Earth is the only planet known to support life")
expect(earth).must_raise ArgumentError
end
end