Skip to content
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 @@
/lib/wip.rb
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
68 changes: 68 additions & 0 deletions lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require_relative "solar_system/planet"
require_relative "solar_system/solar_system"

def welcome
puts "Welcome to the Solar System app!"
puts "================================"
end

def menu
puts "\nWhat would you like to do next?"
puts "Please, type the command number"
puts "1: list all planets"
puts "2: show planet detail"
puts "3: add new planet"
puts "4: exit"
end

def get_planet_detail(solar_system)
print "\nEnter planet name: "
input = gets.chomp
found_planets = solar_system.find_planet_by_name(input)
if found_planets.length == 0
puts "Planet not found."
else
found_planets.each do |planet|
puts planet.summary
puts "============================================="
end
end
end

def main
welcome

solar_system = SolarSystem.new("Sun")

earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Home planet to human race.")
earth_2 = Planet.new("Earth", "blue-green-2", 5.972e24, 1.496e8, "Duplicate Earth! Ha-ha!")
tatooine = Planet.new("Tatooine", "brown-yellow", 6.4171e23, 23e7, "Though its proximity to the suns makes life difficult, it is located near key hyperspace routes, making it a smuggler and gangster haven.")

solar_system.add_planet(earth)
solar_system.add_planet(earth_2)
solar_system.add_planet(tatooine)

menu
input = gets.chomp

while input != "4"
case input
when "1"
puts solar_system.list_planets
menu
input = gets.chomp
when "2"
get_planet_detail(solar_system)
menu
input = gets.chomp
when "3"
solar_system.create_planet
menu
input = gets.chomp
when "4"
break
end
end
end

main
18 changes: 18 additions & 0 deletions lib/solar_system/planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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)
raise ArgumentError.new("Planet's mass should be greater than zero.") unless mass_kg.respond_to?(:>) && mass_kg > 0
raise ArgumentError.new("Distance from Sun should be greater than zero.") unless distance_from_sun_km.respond_to?(:>) && distance_from_sun_km > 0

@name = name
@color = color
@mass_kg = mass_kg
@distance_from_sun_km = distance_from_sun_km
@fun_fact = fun_fact
end

def summary
"\n#{name.upcase}\nColor: #{color}\nMass: #{mass_kg}kg\nDistance from sun: #{distance_from_sun_km}km\nFun fact: #{fun_fact}"
end
end
46 changes: 46 additions & 0 deletions lib/solar_system/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require_relative "../validate"

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
output = "\nPlanets orbiting #{@star_name}:\n"
@planets.each_with_index do |planet, i|
output = output + "#{i+1}. #{planet.name}\n"
end
return output
end

def find_planet_by_name(planet_name)
found_planets = @planets.select { |planet| planet.name.downcase == planet_name.downcase}
# raise ArgumentError.new("Unable to find a planet with this name") unless found_planets.length > 0
return found_planets
end

def create_planet
puts "\nPlease, provide planet data."
print "Planet name: "
name = gets.chomp
print "Planet color: "
color = gets.chomp
print "Planet mass (kg): "
mass = validate(gets.chomp)
print "Distance from #{star_name}: "
distance = validate(gets.chomp)
print "Fun fact (huh?): "
fun_fact = gets.chomp

new_planet = Planet.new(name, color, mass, distance, fun_fact)
add_planet(new_planet)
puts "\nPlanet added."
end
end
7 changes: 7 additions & 0 deletions lib/validate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def validate(input)
while input !~ /\A[1-9]\d+(\.\d+)?\z/ # positive number
print "Please, enter a valid number: "
input = gets.chomp
end
return input.to_i
end
47 changes: 47 additions & 0 deletions test/planet_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'minitest'
require 'minitest/spec'
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/pride'

require_relative '../lib/main.rb'

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

describe 'Planet' do
it 'must raise an error if mass_kg is a negative number' do
expect {
Planet.new("Earth", "blue-green", -5.972e24, 1.496e8, "Home planet to human race.")
}.must_raise ArgumentError
end

it 'must raise an error if distance from Sun is a negative number' do
expect {
Planet.new("Earth", "blue-green", 5.972e24, -1.496e8, "Home planet to human race.")
}.must_raise ArgumentError
end

it 'must raise an error if mass_kg is zero' do
expect {
Planet.new("Earth", "blue-green", 0, 1.496e8, "Home planet to human race.")
}.must_raise ArgumentError
end

it 'must raise an error if distance from Sun is zero' do
expect {
Planet.new("Earth", "blue-green", 5.972e24, 0, "Home planet to human race.")
}.must_raise ArgumentError
end

it 'must raise an error if mass_kg is not a number' do
expect {
Planet.new("Earth", "blue-green", "a lot", 1.496e8, "Home planet to human race.")
}.must_raise ArgumentError
end

it 'must raise an error if distance from Sun is not a number' do
expect {
Planet.new("Earth", "blue-green", 5.972e24, "I don't know", "Home planet to human race.")
}.must_raise ArgumentError
end
end