diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..84f9e195 --- /dev/null +++ b/main.rb @@ -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!" + 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' + 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 + 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 diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..f96566d6 --- /dev/null +++ b/planet.rb @@ -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 diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..21a355f0 --- /dev/null +++ b/solar_system.rb @@ -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