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

Katrina - Edges - Solar System #28

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
123 changes: 123 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
require_relative 'planet'
require_relative 'solar_system'
require 'pry'

# Welcome message to user
def welcome_message
puts "=" * 80
puts "\t\t\tHello and Welcome to THE MAGIC SCHOOL BUS!"
Copy link

Choose a reason for hiding this comment

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

🚌

puts "\tOn today's adventure, we will be exploring the Solar System of Anirtak..."
puts "=" * 80
puts "Let's choose from our options below! "
puts "|| A || : Explore a list of all the planets"
puts "|| B || : Discover all the details about a planet"
puts "|| C || : Add your own planet"
puts "|| D || : Find the distance between two planets"
puts "|| Q || : Hop off the bus, and exit!"

main
end

# Main method that interacts with user
def main
xandar = Planet.new("XANDAR","aquamarine powder blue", 6.39e23, 2.279e8,
"It's been known to host living creatures that are closely related to smurfs")
onurb = Planet.new("ONURB", "shades of lemon, light salmon, and honeydew",
1.898e27, 7.785e8, "It does not have a true surface, as the planet is mostly swirling gases and liquids")

solar_system = SolarSystem.new("Anirtak")
solar_system.add_planet(xandar)
solar_system.add_planet(onurb)

# loop until user quits
user_play = "Y"
while user_play == "Y"
print "Enter A, B, C, D or Q: "
user_input = gets.chomp.strip.upcase
while user_input !='A' &&
user_input != 'B' &&
user_input != 'C' &&
user_input != 'D' &&
user_input != 'Q'
print "EEP. Please try again: "
user_input = gets.chomp.strip.upcase
end
list = solar_system.list_planets

if user_input == 'A'
Copy link

Choose a reason for hiding this comment

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

Instead of checking what user_input is equal to with a bunch of if/elsif/else clauses, what else could we do?

puts list

elsif user_input == 'B'
puts "Let's begin our wild adventure! "
puts "What planet would you like to learn about?"
puts list
print "Please enter the name of the planet: "
user_planet = gets.chomp.strip.upcase
found_planet = solar_system.find_planet_by_name(user_planet)

if found_planet
puts "Your choice is OUT OF THIS WORLD! Here's everything you need to know about #{found_planet.name}"
puts found_planet.summary
end

elsif user_input == 'C'
puts "Let's make a planet!"
print "What is the name of the planet? "
new_planet_name = gets.chomp.strip.upcase

print "What is #{new_planet_name}'s color: "
new_planet_color = gets.chomp.strip

print "What is #{new_planet_name}'s mass in kg? "
new_planet_mass = gets.chomp.strip.to_f
while new_planet_mass <= 0 || new_planet_mass.to_s == "0.0"
puts "Invalid, please enter a positive number"
print "What is #{new_planet_name}'s mass in kg? "
new_planet_mass = gets.chomp.strip.to_f
end

print "What is #{new_planet_name}'s distance from the sun in km? "
new_planet_distance_sun = gets.chomp.strip.to_f
while new_planet_distance_sun <= 0 || new_planet_distance_sun.to_s == "0.0"
puts "Invalid, please enter a positive number"
print "What is #{new_planet_name}'s distance from the sun in km? "
new_planet_distance_sun = gets.chomp.strip.to_f
end

print "What is a fun fact about #{new_planet_name}? "
new_planet_fact = gets.chomp.strip

new_planet = Planet.new(new_planet_name, new_planet_color, new_planet_mass,
new_planet_distance_sun, new_planet_fact)
solar_system.add_planet(new_planet)

elsif user_input == 'D'
puts list
puts "Please choose two planets from the above list"
print "Please enter the name of the first planet: "
user_first_planet = gets.chomp.strip.upcase
print "Please enter the name of the second_planet: "
user_second_planet = gets.chomp.strip.upcase

first_user_planet_found = solar_system.find_planet_by_name(user_first_planet)
second_user_planet_found = solar_system.find_planet_by_name(user_second_planet)

if first_user_planet_found && second_user_planet_found
Copy link

Choose a reason for hiding this comment

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

Handling the idea of "what if a planet isn't found" wasn't a requirement, so I usually don't comment on this, but here you are deliberately checking to see if the planets were found so I wanted to chime in...

Your SolarSystem#find_planet_by_name method actually will never return nil! Therefore, this line of code if first_user_planet_found && second_user_planet_found will never be found. Instead, if your method find_planet_by_name doesn't find a planet, it will return the @planets array.

puts solar_system.distance_between(first_user_planet_found, second_user_planet_found)
end

elsif user_input == 'Q'
puts "Thank you for playing! Come back again for another adventure!"
puts
exit
end

puts "Would you like to keep exploring (Y/N)?"
user_play = gets.chomp.upcase
end

puts "Thank you for playing! Come back again for another adventure!"
exit
end

welcome_message
26 changes: 26 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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)
unless mass_kg > 0
raise ArgumentError, "Mass of planet must be greater than 0"
end

unless distance_from_sun_km > 0
raise ArgumentError, "Distance from sun must be greater than 0"
end

@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 of planet: #{name.upcase}\nColor: #{color}\nMass in kilograms: #{mass_kg}\nDistance from the sun in kilometers: #{distance_from_sun_km}\nFun fact: #{fun_fact}"
# note - ruby syntax if i put them on separate lines, there are tabs in the output
# vs using \n there are no tabs?
end

end
43 changes: 43 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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
list = ""

@planets.each_with_index do |planet, i|
each_planet = "\n#{i + 1}. #{planet.name}"
list << each_planet
end

return "Planets orbiting #{star_name}" + list
end

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

def distance_between(first_planet="", second_planet="")
first_planet_distance = first_planet.distance_from_sun_km
second_planet_distance = second_planet.distance_from_sun_km

distance_between_planets =
(first_planet_distance - second_planet_distance).abs

return "Distance between #{first_planet.name} and #{second_planet.name} is #{distance_between_planets} kilometers"
end
end