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

Leaves -- Janice #41

Open
wants to merge 14 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
22 changes: 22 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 = check_number_validity(mass_kg, "mass")
@distance_from_sun_km = check_number_validity(distance_from_sun_km, "distance from sun")
@fun_fact = fun_fact
end

def summary
return "\nThe glorious planet #{@name} is #{@color}-colored and has a mass of #{@mass_kg} kilograms. It is #{@distance_from_sun_km} kilometers from the sun. And wouldn't you like to know a fun fact about #{@name}? #{@fun_fact}"
end

def check_number_validity(num, measure)
if num < 0
raise ArgumentError, "Number is negative."
end
end
end
54 changes: 54 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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
return "\nPlanets orbiting #{star_name}: \n#{gen_list(@planets).join( )}"
end

def gen_list(items_to_list)
count = 1
list = []
items_to_list.each do |item|
list << "#{count}. #{item.name} \n"
count += 1
end
return list
end

def find_planet_by_name(planet_sought)
test_for_duplicates = @planets.select {|test_against| test_against.name.downcase == planet_sought.downcase}
if test_for_duplicates.length > 1
puts "Sorry, there is more than one planet by that name in our Solar System!"
return nil
else
@planets.each do |test_against|
if test_against.name.downcase == planet_sought.downcase
return test_against
end
end
end
end

def produce_planet_details(planet_sought)
found_planet = find_planet_by_name(planet_sought)
while found_planet.class == Array
puts "Sorry, I don't recognize that planet. Please try again."
print "Which planet are you curious about? "

Choose a reason for hiding this comment

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

let your cli handle this puts-type stuff, to keep this more of a single responsibility.

planet_sought = gets.chomp
found_planet = find_planet_by_name(planet_sought)
end
if found_planet == nil
return "Please try something else."
end
return found_planet.summary
end
end
88 changes: 88 additions & 0 deletions solar_system_main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require_relative 'planet'
require_relative 'solar_system'

def main
solar_system = SolarSystem.new('Sun')

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

venus = Planet.new('Venus', 'cream', 4.867e24, 108.2e8, 'It has the longest rotation period of any planet in the solar system.')
solar_system.add_planet(venus)

mars = Planet.new('Mars', 'red', 6.42e23, 227.9e8, 'It has a thin atmosphere composed primarily of carbon dioxide.')
solar_system.add_planet(mars)

mercury = Planet.new('Mercury', 'grey', 3.3e23, 57.9e8, 'Its orbital period around the sun is the shortest of all the planets in the solar system.')
solar_system.add_planet(mercury)


print "\nHello! Welcome to the Solar System! Would you like to (A)-List Planets (B)-Read Planet Details (C)-Add a Planet or (D)-Exit: "
choice = gets.chomp.upcase

until ['A', 'B', 'C', 'D'].include?(choice)
print "Invalid input. Please enter 'A', 'B', 'C' or 'D': "
choice = gets.chomp.upcase
end

case choice
when "A"
puts solar_system.list_planets
when "B"
print "\nWhich planet are you curious about? "
planet_sought = gets.chomp
puts solar_system.produce_planet_details(planet_sought)
when "C"
begin
solar_system.add_planet(collect_planet_details)
rescue ArgumentError => exception
puts exception
end

when "D"
puts "You are now leaving the Solar System!"
exit
end

until choice == 'D'
print "\nNow would you like to (A)-List Planets (B)-Read Planet Details (C)-Add a Planet or (D)-Exit: "
choice = gets.chomp.upcase

until ['A', 'B', 'C', 'D'].include?(choice)
print "Invalid input. Please enter 'A', 'B', 'C' or 'D': "
choice = gets.chomp.upcase
end

case choice
when "A"
puts solar_system.list_planets
when "B"
print "\nWhich planet are you curious about? "
planet_sought = gets.chomp
puts solar_system.produce_planet_details(planet_sought)
when "C"
solar_system.add_planet(collect_planet_details)
when "D"
puts "You are now leaving the Solar System!"
exit
end
end
end

def collect_planet_details
print "\nWhat is your planet's name? "
planet = gets.chomp.capitalize
print "What color is #{planet}? "
color = gets.chomp
print "What is the mass (in kg) of #{planet}? "
mass_kg = gets.chomp.to_f
print "What is #{planet}'s distance from the sun? "
distance_from_sun_km = gets.chomp.to_f
print "What is a fun fact about #{planet}? "
fun_fact = gets.chomp
added_planet = Planet.new(planet, color, mass_kg, distance_from_sun_km, fun_fact)
puts "\nSummary of the latest planet added to our solar system: #{added_planet.summary}"
return added_planet
end

main
35 changes: 35 additions & 0 deletions solar_system_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'minitest'
require 'minitest/spec'
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/pride'

require_relative 'planet'

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

describe 'Planet' do
it 'outputs a summary' do
# Arrange
earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'It\'s the only planet known to support life.')
# Act
summary = earth.summary
# Assert
expect(summary).must_include "The glorious planet Earth is blue-green-colored and has a mass of 5.972e+24 kilograms. It is 149600000.0 kilometers from the sun. And wouldn't you like to know a fun fact about Earth? It's the only planet known to support life."
end

it 'requests a positive number if a negative number is given for mass' do
# Arrange
earth = Planet.new('Earth', 'blue-green', -5.972e24, 1.496e8, 'It\'s the only planet known to support life.')
# Act
new_mass = earth.mass_kg
# Assert
expect(new_mass).must_include "Positive number for #{measure}, please: "

end


it 'requests a positive number if a negative number is given for distance' do
end

end