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

Leaves - Katie (All Waves Complete) #51

Open
wants to merge 2 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
78 changes: 78 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require_relative 'planet'
require_relative 'solar-system'
require 'pry'

#Wave 3 Main
def main
solar_system = SolarSystem.new('Sol')
earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life')
mars = Planet.new('Mars', 'red', 6.39e23, 2.279e8, 'Named after the Roman God of War')
mercury = Planet.new('Mercury', 'gray', 3.3011e23, 77e7, 'Has the shortest orbital period, 87.97 days')

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

choice = 0

while choice != 4
puts "\nSolar System Program"
puts "Menu:"
puts "1. List Planets"
puts "2. Planet Details"
puts "3. Add Planet"
puts "4. Exit"
print "Enter Menu Choice: "
choice = gets.chomp.to_i
case choice
when 1 #List Planets
list = solar_system.list_planets
puts "\n#{list}"
when 2 #Planet Details
print "\nEnter Planet Name: "
planet = gets.chomp.to_s.capitalize
found_planet = solar_system.find_planet_by_name(planet)
puts "\n#{found_planet}"
when 3 #Add Planet
print "\nEnter Planet Name: "
name = gets.chomp.to_s.capitalize
if solar_system.check_for_planet(name) == false
print "Enter Color: "
color = gets.chomp.to_s
check = 0
while check != 1
print "Enter Mass in kg: "
mass = gets.chomp.to_f
if mass > 0
check = 1
else
puts "Invalid Mass, must be greater than 0."
end
end
check = 0
while check != 1
print "Distance from the Sun in km: "
distance = gets.chomp.to_f
if distance > 0
check = 1
else
puts "Invalid Distance, must be greater than 0."
end
end
print "Enter Fun Fact: "
fact = gets.chomp.to_s
new_planet = Planet.new(name, color, mass, distance, fact)
solar_system.add_planet(new_planet)
else
puts "Invalid Entry - Planet Already in Solar System"
end
when 4 #Exit
return
else
puts "Invalid Menu Choice"
end
end
end

main

17 changes: 17 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 > 0 ? @mass_kg = mass_kg : (raise "Error - Invalid Mass Value")
distance_from_sun_km > 0 ? @distance_from_sun_km = distance_from_sun_km : (raise "Error - Invalid Distance Value")
@fun_fact = fun_fact
end

def summary
"Planet: #{@name}\nColor: #{@color}\nMass: #{@mass_kg} kg\nDistance from Sun: #{@distance_from_sun_km} km\nFun Fact: #{@fun_fact}\n\n"
end

end
44 changes: 44 additions & 0 deletions solar-system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class SolarSystem

attr_reader :star_name, :planets

def initialize(star_name)
@star_name = star_name
@planets = []
end

def add_planet(planet)
@planets.push(planet)

end

def list_planets
intro = "Planets Orbiting #{@star_name}: \n"
count = 1
@planets.each do |planet|
intro += "#{count}. #{planet.name}\n"
count += 1
end

return intro
end

def find_planet_by_name(planet)
@planets.each do |planet_name|
if planet_name.name == planet
return planet_name.summary
end
end
return "No Information Found"
end

def check_for_planet(planet)
@planets.each do |planet_name|
if planet_name.name == planet
return true
end
end
return false
end
end