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

Roshni - Solar System #41

Open
wants to merge 8 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
77 changes: 77 additions & 0 deletions lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require_relative 'planet'
require_relative 'solar_system'

def main
solar_system = SolarSystem.new("Sun")
mercury = Planet.new("Mercury", "light-grey", 3.285e23, 5.800e7, "if you lived on Mercury you'd have a birthday every 3 months.")
venus = Planet.new("Venus", "white", 4.867e24, 1.080e8, "it is named after the Roman goddess of love and beauty.")
earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "it is the only planet known to support life.")
mars = Planet.new("Mars", "red", 6.39e23, 2.066e8, "it has the highest mountain in our solar system (24km).")
jupiter = Planet.new("Jupiter", "yellow-orange", 1.898e27, 7.680e8, "it has 79 moons.")
saturn = Planet.new("Saturn", "yellow-brown", 5.68e26, 1.426e9, "one of Saturn's rings, discovered in 2009, could fit a billion earths.")
uranus = Planet.new("Uranus", "blue-green", 8.681e25, 2.959e9, "it was the first planet discovered by a telescope in 1781.")
neptune = Planet.new("Neptune", "blue", 1.024e26, 4.476e9, "a year on Neptune is equivalent to 165 years on Earth.")

solar_system.add_planet(mercury)
solar_system.add_planet(venus)
solar_system.add_planet(earth)
solar_system.add_planet(mars)
solar_system.add_planet(jupiter)
solar_system.add_planet(saturn)
solar_system.add_planet(uranus)
solar_system.add_planet(neptune)

list = solar_system.list_planets

puts "Welcome to the Solar System Program! You may: \n 1. Check out a list of the planets \n 2. Learn about a planet \n 3. Add a planet \n 4. Calculate the distance between two planets \n 5. Exit"

user_input = nil

until user_input == 5
puts
print "Please enter a number to continue: "
user_input = gets.chomp.to_i
until user_input >= 1 && user_input <= 5
print "Sorry, that is not a valid option. Please check the list and try again: "
user_input = gets.chomp.to_i
end

case user_input
when 1
puts list
when 2
puts "Which planet do you want details on?"
planet = gets.chomp
info = solar_system.find_planet_by_name(planet)
puts info.summary
when 3
puts "Add a planet by entering the following information."
print "Name: "
name = gets.chomp.capitalize
print "Color: "
color = gets.chomp.downcase
print "Mass (Kg): "
mass = gets.chomp.to_f
print "Distance From Sun (Km): "
distance = gets.chomp.to_f
print "Fun Fact: "
fact = gets.chomp.downcase
user_planet = Planet.new(name, color, mass, distance, fact)
solar_system.add_planet(user_planet)
puts "Your planet has been added to the solar system! Here is the updated list: "
list = solar_system.list_planets
print list
when 4
print "Enter the first planet: "
first_planet = gets.chomp.capitalize
print "Enter the second planet: "
second_planet = gets.chomp.capitalize
puts "The distance between #{first_planet} and #{second_planet} is #{solar_system.distance_between(first_planet, second_planet)} km."
when 5
puts "Thank you for using the Solar System Program. Goodbye!"
exit
end
end
end

main
23 changes: 23 additions & 0 deletions lib/planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 = mass_kg
@distance_from_sun_km = distance_from_sun_km
@fun_fact = fun_fact

if mass_kg <= 0
raise ArgumentError.new("Mass must be greater than 0 kg.")
end

if distance_from_sun_km <= 0
raise ArgumentError.new("Distance from the sun must be greater than 0 km.")
end
end

def summary
return "#{@name} is #{@color}, weighs #{@mass_kg} kg, and is #{@distance_from_sun_km} km from the sun. A fun fact is that #{@fun_fact}"
end
end
37 changes: 37 additions & 0 deletions lib/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require_relative 'planet'

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_string = "Planets orbiting the #{@star_name} \n"
@planets.each_with_index do |planet, index|
output_string += "#{index + 1}. #{planet.name} \n"
end
return output_string
end

def find_planet_by_name(planet_name)
found_planet = @planets.find { |planet| planet.name == planet_name.capitalize}
if found_planet.nil?
raise ArgumentError.new("Sorry, that planet does not exist in this solar system.")
end
return found_planet
end

def distance_between(first_planet, second_planet)
first_planet_distance = find_planet_by_name(first_planet).distance_from_sun_km
second_planet_distance = find_planet_by_name(second_planet).distance_from_sun_km
distance_between_planets = (first_planet_distance - second_planet_distance).abs
return distance_between_planets
end
end
69 changes: 69 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require_relative 'planet'
require_relative 'solar_system'

def main
solar_system = SolarSystem.new("Sun")
mercury = Planet.new("Mercury", "light-grey", 3.285e23, 5.800e7, "if you lived on Mercury you'd have a birthday every 3 months.")
venus = Planet.new("Venus", "white", 4.867e24, 1.080e8, "it is named after the Roman goddess of love and beauty.")
earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "it is the only planet known to support life.")
mars = Planet.new("Mars", "red", 6.39e23, 2.066e8, "it has the highest mountain in our solar system (24km).")
jupiter = Planet.new("Jupiter", "yellow-orange", 1.898e27, 7.680e8, "it has 79 moons.")
saturn = Planet.new("Saturn", "yellow-brown", 5.68e26, 1.426e9, "one of Saturn's rings, discovered in 2009, could fit a billion earths.")
uranus = Planet.new("Uranus", "blue-green", 8.681e25, 2.959e9, "it was the first planet discovered by a telescope in 1781.")
neptune = Planet.new("Neptune", "blue", 1.024e26, 4.476e9, "a year on Neptune is equivalent to 165 years on Earth.")

solar_system.add_planet(mercury)
solar_system.add_planet(venus)
solar_system.add_planet(earth)
solar_system.add_planet(mars)
solar_system.add_planet(jupiter)
solar_system.add_planet(saturn)
solar_system.add_planet(uranus)
solar_system.add_planet(neptune)

list = solar_system.list_planets

puts "Welcome to the Solar System Program! You may: \n 1. See a list of the planets \n 2. See details for a planet \n 3. Add a planet \n 4. Exit"

user_input = nil

until user_input == 4
puts
print "Please enter a number to continue: "
user_input = gets.chomp.to_i
until user_input >= 1 && user_input <= 4
print "Sorry, that is not a valid option. Please check the list and try again: "
user_input = gets.chomp.to_i
end

case user_input
when 1
puts list
when 2
puts "Which planet do you want details on?"
planet = gets.chomp
info = solar_system.find_planet_by_name(planet)
puts info.summary
when 3
puts "Add a planet by entering the following information."
print "Name: "
name = gets.chomp
print "Color: "
color = gets.chomp
print "Mass (Kg): "
mass = gets.chomp
print "Distance From Sun (Km): "
distance = gets.chomp
print "Fun Fact: "
fact = gets.chomp
user_planet = Planet.new(name, color, mass, distance, fact)
solar_system.add_planet(user_planet)
puts "Awesome! Your planet has been added to the solar system."
when 4
puts "Thank you for using the Solar System Program. Goodbye!"
exit
end
end
end

main
15 changes: 15 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 = mass_kg
@distance_from_sun_km = distance_from_sun_km
@fun_fact = fun_fact
end

def summary
return "#{@name} is #{@color}, weighs #{@mass_kg} kg, and is #{@distance_from_sun_km} km from the sun. A fun fact is that #{@fun_fact}"
end
end
30 changes: 30 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative 'planet'

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_string = "Planets orbiting the #{@star_name} \n"
@planets.each_with_index do |planet, index|
output_string += " #{index + 1}. #{planet.name} \n"
end
return output_string
end

def find_planet_by_name(planet_name)
found_planet = @planets.find { |planet| planet.name == planet_name.capitalize}
if found_planet.nil?
raise ArgumentError.new("Sorry, that planet does not exist in this solar system.")
end
return found_planet
end
end
36 changes: 36 additions & 0 deletions test/planet_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'

require_relative '../lib/planet.rb'

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

describe 'Planet' do
it 'creates an instance of Planet' do
name = 'Earth'
color = 'blue-green'
mass_kg = 5.972e24
distance_from_star_km = 1.496e8
fun_fact = "it is the only planet known to support life."

earth = Planet.new(name, color, mass_kg, distance_from_star_km, fun_fact)
expect(earth).must_be_instance_of Planet
end

it 'raises an error when the mass or distance from the sun are less than or equal to 0' do
expect {Planet.new('Earth','blue-green',0,1.496e8 ,"it is the only planet known to support life.")}.must_raise ArgumentError
expect {Planet.new('Earth','blue-green',5.972e24,0,"it is the only planet known to support life.")}.must_raise ArgumentError
end

it 'returns a summary for an instance of Planet' do
name = 'Earth'
color = 'blue-green'
mass_kg = 5.972e24
distance_from_star_km = 1.496e8
fun_fact = "it is the only planet known to support life."

earth = Planet.new(name, color, mass_kg, distance_from_star_km, fun_fact)
expect(earth.summary).must_equal "Earth is blue-green, weighs 5.972e+24 kg, and is 149600000.0 km from the sun. A fun fact is that it is the only planet known to support life."
end
end
79 changes: 79 additions & 0 deletions test/solar_system_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'

require_relative '../lib/solar_system.rb'

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

describe 'Solar System' do
describe 'initialize solar system' do
it 'creates an instance of Solar System' do
star_name = 'Sun'
solar_system = SolarSystem.new(star_name)
expect(solar_system).must_be_instance_of SolarSystem
end
end
end

describe 'add planet' do
it 'adds a planet' do
solar_system = SolarSystem.new("Sun")

earth = Planet.new('Earth','blue-green',5.972e24,1.496e8,"it is the only planet known to support life.")
mars = Planet.new("Mars", "red", 6.39e23, 2.066e8, "it has the highest mountain in our solar system (24km).")

solar_system.add_planet(earth)
solar_system.add_planet(mars)

expect(solar_system.planets.length).must_equal 2
end
end

describe 'list planet' do
it 'lists the planets' do
solar_system = SolarSystem.new('Sun')
expect(solar_system.list_planets).must_be_instance_of String

earth = Planet.new('Earth','blue-green',5.972e24,1.496e8,"it is the only planet known to support life.")
mars = Planet.new("Mars", "red", 6.39e23, 2.066e8, "it has the highest mountain in our solar system (24km).")
solar_system.add_planet(earth)
solar_system.add_planet(mars)
expect(solar_system.list_planets).must_equal "Planets orbiting the Sun \n1. Earth \n2. Mars \n"
end
end

describe 'finds a planet by name' do
it 'raises an error if the planet cannot be found' do
solar_system = SolarSystem.new("Sun")

earth = Planet.new('Earth','blue-green',5.972e24,1.496e8,"it is the only planet known to support life.")
mars = Planet.new("Mars","red",6.39e23,2.066e8,"it has the highest mountain in our solar system (24km).")

solar_system.add_planet(earth)
solar_system.add_planet(mars)

expect{solar_system.find_planet_by_name("Mercury")}.must_raise ArgumentError
end

it 'if the planet can be found it must be an instance of the class Planet' do
solar_system = SolarSystem.new("Sun")
earth = Planet.new('Earth','blue-green',5.972e24,1.496e8,"it is the only planet known to support life.")
solar_system.add_planet(earth)
expect(solar_system.find_planet_by_name("Earth")).must_be_instance_of Planet
end
end

describe 'distance between' do
it 'calculates the distance between two planets' do
solar_system = SolarSystem.new("Sun")

earth = Planet.new("Earth",'blue-green',5.972e24,1.496e8,"it is the only planet known to support life.")
mars = Planet.new("Mars","red",6.39e23,2.066e8,"it has the highest mountain in our solar system (24km).")

solar_system.add_planet(earth)
solar_system.add_planet(mars)

expect(solar_system.distance_between("Earth", "Mars")).must_be_close_to 57000000
end
end