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

Ports - Faiza Husain #28

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
63 changes: 63 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require_relative 'planet.rb'
require_relative 'solar_system.rb'

def main
puts "Welcome to my Solar System!"
# Instantiation
solar_system = SolarSystem.new("Sol")

mercury = Planet.new("Mercury", "light grey", 3.3011e23, 5.791000000e1, "It has no moons")
venus = Planet.new("Venus", "red-orange", 4.867e24, 1.082000000e2, "One day on Venus is longer than one year on Earth")
earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "its the only planet known to support life")
mars = Planet.new("Mars", "red", 6.39e23, 2.279000000e2, "It's our next destination")
jupiter = Planet.new("Jupiter", "white, red, orange, brown and yellow", 1.898e27, 7.785000000e2, "Jupiter can fit 1,300 Earths")
saturn = Planet.new("Saturn", "yellowing-brown", 5.683e26, 1.434000000000e0, "It is the least dense planet in the solar system")
uranus = Planet.new("Uranus", "pale blue", 8.681e25, 2.871000000000e0, "Uranus’ moons are named after characters created by William Shakespeare and Alexander Pope")
neptune = Planet.new("Neptune", "bright azure blue", 1.024e26, 4.495000000000, "Neptune has an average surface temperature of -214°C – approximately -353°F.")

# Add planets to solar system
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)

# WAVE 3
# Loop to ask user what they want to do

loop do
puts "\n\nWhat would you like to do?"
puts "List planets Planet details Add planet Exit"
answer = gets.chomp.downcase

# list planets
if (answer == "list planets")
list = solar_system.list_planets
for i in (0..list.length - 1)
print list[i]
end

elsif (answer == "planet details")
puts "Which planet would you like to know about?"
planet_choice = gets.chomp.downcase
puts "Planet choice: #{planet_choice}"
found_planet = solar_system.find_planet_by_name(planet_choice.capitalize)
puts "Found planet: #{found_planet}"
puts "Here are some details about #{found_planet.name}:\n"
print found_planet.summary

elsif (answer == "add planet")
solar_system.discovered_planet

else
exit
end
end

end # main class end


main
26 changes: 26 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# WAVE 1
class Planet
attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact
# Constructor
def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact)

while mass_kg <= 0
raise ArgumentError.new("Planet mass cannot be less than or equal to 0")
end

while distance_from_sun_km <= 0
raise ArgumentError.new("Distance from sun cannot be less than or equal to 0. There would be no planet left!")
end

@name = name
@color = color
@mass_kg = mass_kg
@distance_from_sun_km = distance_from_sun_km
@fun_fact = fun_fact
end

# Summary
def summary
return "#{self.name} is #{self.distance_from_sun_km} kilometers away from the Sun. It's color is #{self.color}. It's mass is #{self.mass_kg} kilograms and #{self.fun_fact}!"
end
end
33 changes: 33 additions & 0 deletions planet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative 'planet.rb'

Minitest::Reporters.use!

describe "class Planet" do
it "will raise ArgumentError if mass is < 0" do
expect {
Planet.new("Earth", "blue-green", -10, 100000, "only planet known to support life")
}.must_raise ArgumentError
end

it "will raise ArgumentError if distance from Sun is < 0" do
expect {
Planet.new("Earth", "blue-green", 1000000, 0, "only planet known to support life")
}.must_raise ArgumentError
end
end

describe "class solar system" do
it "will raise ArgumentError if planet already exists" do
# solar_system = SolarSystem.new("SailorMoon")

expect {
mars = Planet.new("Mars", "red", "6.39 × 10^23", "227.9 million", "It's our next destination")
solar_system.add_planet(mars)

another_mars = Planet.new("Mars", "red", "6.39 × 10^23", "227.9 million", "It's our next destination")
solar_system.add_planet(another_mars)
}.must_raise ArgumentError
end
end
66 changes: 66 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Wave 2
require_relative 'planet.rb'

class SolarSystem
attr_reader :star_name, :planets
# Constructor
def initialize(star_name)
@star_name = star_name
@planets = Array.new
end

# Take an instance of Planet as a parameter and add it to the list of planets.exit
def add_planet(planet)
# planets = @planets.map { |i| i.name.downcase }
if @planets.include?(planet.name.downcase)
raise ArgumentError.new("Planet already exists. A solar system cannot have duplicate planets")
end
@planets.push(planet)
end

puts

def planet_list
@planets.each do |planet|
# return planet # returns the planet at index 0 and then exits because return breaks a method
next
end
return @planets
end

def list_planets
list = ""
for i in ([email protected] - 1)
list += "\n#{i + 1}. #{@planets[i].name}"
end
return list
end

def find_planet_by_name(name)
@planets.each do |planet|
if planet.name.downcase == name.downcase
return planet
end
end
return nil
end

def discovered_planet

puts "What is the name of your planet?"
name = gets.chomp.capitalize
puts "What is the color of your planet?"
color = gets.chomp.downcase
puts "How much does your planet weigh in kg?"
mass_kg = gets.chomp.to_f
puts "How far is your planet from the sun in km?"
distance_from_sun_km = gets.chomp.to_f
puts "What is a fun fact about your planet?"
fun_fact = gets.chomp.downcase

new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact)
add_planet(new_planet)

# puts new_planet.name
end
end