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

Danielle Metzner - Solar System #36

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
88 changes: 88 additions & 0 deletions 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

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!

#... 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")
28 changes: 28 additions & 0 deletions planet.rb
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
45 changes: 45 additions & 0 deletions planet_spec.rb
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
35 changes: 35 additions & 0 deletions solar_system.rb
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

Choose a reason for hiding this comment

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

It's convention to put the attr_reader and attr_accessor etc above initialize and below the class name


#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}

Choose a reason for hiding this comment

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

You could also use .find instead of .select

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

Choose a reason for hiding this comment

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

nice 🥇

end
end