-
Notifications
You must be signed in to change notification settings - Fork 48
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
Danielle Metzner - Solar System #36
base: master
Are you sure you want to change the base?
Changes from all commits
7f7f916
ca80407
ed1e547
9db02f0
0386bfa
69bd536
f2989b9
f1715e6
4f59e5b
8d5083b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require_relative 'planet' | ||
require_relative 'solar_system' | ||
|
||
def main | ||
#... Do stuff with planets .. | ||
solar_system = SolarSystem.new("Sol") | ||
earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') | ||
saturn = Planet.new('Saturn','rust',5.683e26,1.433e9,"Saturn has at least 53 moons, 9 moons awaiting confirmation") | ||
mars = Planet.new('Mars', 'red', 6.39e23, 227.9e6, 'The planet is named after Mars, the Roman god of war. ') | ||
|
||
solar_system.add_planet(earth) | ||
solar_system.add_planet(saturn) | ||
solar_system.add_planet(mars) | ||
# puts earth.name | ||
# puts earth.color | ||
# puts saturn.name | ||
# puts saturn.color | ||
# puts earth.summary | ||
# puts saturn.summary | ||
print "What do you want to do next, 'list planets', 'planet details','add planet', 'exit'>> " | ||
option = gets.chomp.downcase | ||
until option == "exit" | ||
if option == "list planets" | ||
list = solar_system.list_planets | ||
puts list | ||
print "What do you want to do next, 'list planets', 'planet details','add planet','distance between' 'exit'>> " | ||
option = gets.chomp.downcase | ||
#PLANET DETAILS ---------------------------- | ||
elsif option == "planet details" | ||
print "What planet do you wish to learn more about? >> " | ||
planet = gets.chomp | ||
found_planet = solar_system.find_planet_by_name(planet) | ||
until found_planet != nil | ||
list = solar_system.list_planets | ||
print "That planet is not in our solar system!\n" | ||
puts list | ||
print "\nPlease choose another >>" | ||
planet = gets.chomp | ||
found_planet = solar_system.find_planet_by_name(planet) | ||
end | ||
puts found_planet.summary | ||
print "What do you want to do next, 'list planets', 'planet details','add planet','distance between' 'exit'>> " | ||
option = gets.chomp.downcase | ||
#ADD PLANET ---------------------------- | ||
elsif option == "add planet" | ||
print "What's the planet name? >> " | ||
new_name = gets.chomp | ||
print "What color is #{new_name}? >> " | ||
new_color = gets.chomp | ||
print "What is the mass of the #{new_name}? >>" | ||
new_mass_kg = gets.chomp.to_f | ||
until new_mass_kg > 0 | ||
print "Please insert a mass greater than 0 >> " | ||
new_mass_kg = gets.chomp.to_f | ||
end | ||
print "How far away is #{new_name} from the sun? >> " | ||
new_distance_from_sun_km = gets.chomp.to_f | ||
until new_distance_from_sun_km > 0 | ||
print "Please insert a distance greater than 0 >> " | ||
new_distance_from_sun_km = gets.chomp.to_f | ||
end | ||
print "Do you know a fun fact about #{new_name}? >> " | ||
new_fun_fact = gets.chomp | ||
new_planet = Planet.new(new_name,new_color,new_mass_kg,new_distance_from_sun_km,new_fun_fact) | ||
solar_system.add_planet(new_planet) | ||
print "What do you want to do next, 'list planets', 'planet details','add planet','distance between' 'exit'>> " | ||
option = gets.chomp.downcase | ||
#DISTANCE BETWEEN ---------------------------- | ||
elsif option == "distance between" | ||
print "What 2 planets do you want to know the distance between?\n\\Planet 1: >> " | ||
planet1 = gets.chomp! | ||
print "Planet 2: >> " | ||
planet2 = gets.chomp! | ||
distance = solar_system.distance_between(planet1,planet2) | ||
puts "The total distance between #{planet1} and #{planet2} is #{distance} km." | ||
print "What do you want to do next, 'list planets', 'planet details','add planet','distance between' 'exit'>> " | ||
option = gets.chomp.downcase | ||
else | ||
print "I'm sorry, please select a valid option:'list planets','planet details','add planet' 'exit'>> " | ||
option = gets.chomp.downcase | ||
end | ||
end | ||
end | ||
|
||
main | ||
|
||
#earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') | ||
#saturn = Planet.new('Saturn','rust',5.683e26,1.433e9,"Saturn has at least 53 moons, 9 moons awaiting confirmation") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#Planet class | ||
|
||
class Planet | ||
|
||
#attr_accessor - Lets you do both Read and Write | ||
attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact | ||
#attr_writer | ||
|
||
def initialize(name, color, mass_kg, distance_from_sun_km,fun_fact) | ||
@name = name.capitalize | ||
@color = color | ||
@fun_fact = fun_fact | ||
raise ArgumentError.new("Planet mass must be an integer or float") unless mass_kg.class == Integer || mass_kg.class == Float | ||
raise ArgumentError.new("Planet mass should be greater than 1") unless mass_kg > 0 | ||
@mass_kg = mass_kg | ||
raise ArgumentError.new("Planet distance must be an integer or float") unless distance_from_sun_km.class == Integer || distance_from_sun_km.class == Float | ||
raise ArgumentError.new("Planet distance from the sun should be greater than 1") unless distance_from_sun_km > 0 | ||
@distance_from_sun_km = distance_from_sun_km | ||
end | ||
|
||
def summary | ||
return "Facts about #{name}:\n \ | ||
#{name}'s color is #{color}\n \ | ||
#{name} weighs #{mass_kg} kg's\n \ | ||
#{name} is #{distance_from_sun_km} km from the sun\n \ | ||
Fun fact: #{fun_fact} \n" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# csv_practice_spec.rb | ||
|
||
require 'minitest' | ||
require 'minitest/spec' | ||
require 'minitest/autorun' | ||
require 'minitest/reporters' | ||
require 'minitest/pride' | ||
require 'pry' | ||
|
||
require_relative 'planet' | ||
|
||
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
||
describe "Checking values to make sure they are greater than zero" do | ||
it "Test on zero mass" do | ||
expect{earth = Planet.new('Earth', 'blue-green', 0 , 1.496e8, 'Only planet known to support life') | ||
}.must_raise ArgumentError | ||
end | ||
it "Test on zero distance" do | ||
expect{earth = Planet.new('Earth', 'blue-green', 1 , 0, 'Only planet known to support life') | ||
}.must_raise ArgumentError | ||
end | ||
it "Test on negative mass" do | ||
expect{earth = Planet.new('Earth', 'blue-green', -1 , 1.496e8, 'Only planet known to support life') | ||
}.must_raise ArgumentError | ||
end | ||
it "Test on negative distance" do | ||
expect{earth = Planet.new('Earth', 'blue-green', 1 , -1.496e8, 'Only planet known to support life') | ||
}.must_raise ArgumentError | ||
end | ||
it "Test on string entered in mass" do | ||
expect{earth = Planet.new('Earth', 'blue-green', 'far far away' , -1.496e8, 'Only planet known to support life') | ||
}.must_raise ArgumentError | ||
end | ||
it "Test on string entered in distance" do | ||
expect{earth = Planet.new('Earth', 'blue-green', 1 , 'far far away', 'Only planet known to support life') | ||
}.must_raise ArgumentError | ||
end | ||
end | ||
|
||
# describe | ||
## Arrange | ||
# sol = SolarSystem.new("Sol") | ||
# expect(sol.name).must_equl "Sol" | ||
# end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
class SolarSystem | ||
def initialize(star_name) | ||
@star_name = star_name | ||
@planets = [] | ||
end | ||
|
||
attr_reader :star_name, :planets | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's convention to put the |
||
|
||
#Adds a planet to the solar system | ||
def add_planet(new_planet) | ||
@planets << new_planet | ||
end | ||
|
||
#Creates a numbered list of all the planets in the solar system | ||
def list_planets | ||
planet_list = ["Planets orbiting #{@star_name}"] | ||
@planets.each_with_index do |planet, index| | ||
planet_list << "#{index+1}. #{planet.name}" | ||
end | ||
return planet_list | ||
end | ||
|
||
#Finds the planet with the same name and returns the planet object | ||
def find_planet_by_name(planet_name) | ||
found_planet = @planets.select {|planet| planet.name.downcase == planet_name.downcase} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could also use |
||
return found_planet[0] | ||
end | ||
|
||
#Calculates the distance between two planets | ||
def distance_between(planet1, planet2) | ||
distance = find_planet_by_name(planet2).distance_from_sun_km - find_planet_by_name(planet1).distance_from_sun_km | ||
return distance.abs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice 🥇 |
||
end | ||
end |
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.
This method is just crying to be broken into smaller methods!