forked from AdaGold/solar-system
-
Notifications
You must be signed in to change notification settings - Fork 49
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
jaitch
wants to merge
14
commits into
Ada-C12:master
Choose a base branch
from
jaitch:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Leaves -- Janice #41
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7a761ae
get code to working state for Wave 1
janicewhuang 2152925
get main to working state for Wave 1
janicewhuang 75e8322
has one test working successfully
janicewhuang b02f0a7
initialize Wave 2 code
janicewhuang 1f1f4f5
play around with testing for validity check but conflicting instructi…
janicewhuang 01cd99c
revert to validity method call within constructor. tried to move to m…
janicewhuang 50429af
add list planet function (still needs formatting help)
janicewhuang a7c0fd1
baby steps on find_planet_by_name
janicewhuang 5c57cd0
futz further
janicewhuang 14734be
Add wave 3 functions; need to work on big loop
janicewhuang 41ed590
make big loop w/o calling 'main' at end of every choice so new planet…
janicewhuang 167fecb
solve duplicate planet problem, all working
janicewhuang 8eb19a1
move I/O from class methods to main
janicewhuang 01d91c9
add rescue for exception
janicewhuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? " | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.