From a392c0df87411042565aa76904b948e92d1e2be7 Mon Sep 17 00:00:00 2001 From: Ting Wong Date: Tue, 14 Feb 2017 19:58:48 -0800 Subject: [PATCH] Create Queues - Ting Wong - Solar System.rb --- Queues - Ting Wong - Solar System.rb | 113 +++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Queues - Ting Wong - Solar System.rb diff --git a/Queues - Ting Wong - Solar System.rb b/Queues - Ting Wong - Solar System.rb new file mode 100644 index 00000000..b7cb8300 --- /dev/null +++ b/Queues - Ting Wong - Solar System.rb @@ -0,0 +1,113 @@ +# WAVE 1 + +class Planet + attr_reader :name, :diameter, :mass, :gravity, :length_day, :rate_of_solar_rotation, :distance_from_the_sun + # TW: Used reader as we won't need to edit after initialization + + def initialize(planet_hash) + @name = planet_hash[:name] + @diameter = planet_hash[:diameter] + @mass = planet_hash[:mass] + @gravity = planet_hash[:gravity] + @length_day = planet_hash[:length_day] + @rate_of_solar_rotation = planet_hash[:rate_of_solar_rotation] + @distance_from_the_sun = planet_hash[:distance_from_the_sun] + end + + def distance_to(planet_neighbour) + distance = (@distance_from_the_sun - planet_neighbour.distance_from_the_sun).abs.round(2) + puts "The distance from #{@name} to #{planet_neighbour.name} is #{distance} 10^6 km." + return distance + end + + def summary + puts " Planet: #{@name};\n Diameter: #{@diameter}km;\n Mass: #{@mass} 10^24kg;\n Gravity: #{@gravity}m/s2;\n Length of Day: #{@length_day} hours;\n Rate of Solar Rotation: #{@rate_of_solar_rotation};\n Distance from Sun: #{@distance_from_the_sun} 10^6 km" + end +end + +mars = Planet.new(name: "mars", diameter: 6792, mass: 0.64171, gravity: 3.7, length_day: 24.7, rate_of_solar_rotation: 24.6, distance_from_the_sun: 227.9) +mercury = Planet.new(name: "mercury", diameter: 4879, mass: 0.33, gravity: 3.7, length_day: 4222.6, rate_of_solar_rotation: 1407.6, distance_from_the_sun: 57.9) +venus = Planet.new(name: "venus", diameter: 12104, mass: 4.87, gravity: 8.9, length_day: -5832.5, rate_of_solar_rotation: 2802.0, distance_from_the_sun: 57.9) +earth = Planet.new(name: "earth", diameter: 12756, mass: 9.8, gravity: 23.9, length_day: 24.0, rate_of_solar_rotation: 23.9, distance_from_the_sun: 149.6) +moon = Planet.new(name: "moon", diameter: 3475, mass: 0.073, gravity: 1.6, length_day: 655.7, rate_of_solar_rotation: 708.7, distance_from_the_sun: 0.384) +# TW: Manual creation of 5 planets with planet details + +while true + puts "Enter the name or number of the planet you'd like to learn about, or type 6 to exit." + puts "1. Mars, 2. Mercury, 3. Venus, 4. Earth, 5. Moon, 6. Exit" + user_choice = gets.chomp + if user_choice.to_i == 1 || user_choice.downcase == "mars" + then mars.summary + # TW: Chose "if" statement over "case" statements to manage integers and capitalization + elsif user_choice.to_i == 2 || user_choice.downcase == "mercury" + then mercury.summary + elsif user_choice.to_i == 3 || user_choice.downcase == "venus" + then venus.summary + elsif user_choice.to_i == 4 || user_choice.downcase == "earth" + earth.summary + elsif user_choice.to_i == 5 || user_choice.downcase == "moon" + moon.summary + elsif user_choice.to_i == 6 || user_choice.downcase == "exit" + then break + else + puts "That is not an option. Try again." + end +end + +# WAVE 2 +class SolarSystem + attr_reader :formation_year + + def initialize(formation) + @planets = [] + @formation_year = formation + end + + def planet_object(planet_name) + @planets.each do |planet_object| + if planet_object.name == planet_name + then return planet_object + end + end + end + + def add_planet(planet) + @planets.push planet + end + + def add_planet_list(planets) + @planets.concat(planets) + end + + def local_year(planet_name) + planet = planet_object(planet_name) + local_year = (@formation_year / planet.rate_of_solar_rotation.to_f).round(2) + puts "Local year: #{local_year} earth days" + return local_year + end + +end + +solar_system = SolarSystem.new(1000) +# TW: Use of SolarSystem class, setting formation_year to 1000. + +solar_system.add_planet(Planet.new(name: "secret moon", diameter: 3475, mass: 0.073, gravity: 1.6, length_day: 655.7, rate_of_solar_rotation: 27.3, distance_from_the_sun: 0.384)) +# TW: Use of add_planet class + +list_of_planets = [ + Planet.new(name: "secret marsmars", diameter: 6792, mass: 0.64171, gravity: 3.7, length_day: 24.7, rate_of_solar_rotation: 687, distance_from_the_sun: 227.9), + Planet.new(name: "secret mercurymercury", diameter: 4879, mass: 0.33, gravity: 3.7, length_day: 4222.6, rate_of_solar_rotation: 88, distance_from_the_sun: 57.9), + Planet.new(name: "secret venusvenus", diameter: 12104, mass: 4.87, gravity: 8.9, length_day: -5832.5, rate_of_solar_rotation: 224.7, distance_from_the_sun: 57.9), + Planet.new(name: "secret earthearth", diameter: 12756, mass: 9.8, gravity: 23.9, length_day: 24.0, rate_of_solar_rotation: 365, distance_from_the_sun: 149.6) +] +# TW: Manual creation of a list of planets + +solar_system.add_planet_list(list_of_planets) +# TW: Adding list of planets to solar_system variable + + +mars.distance_to(earth) +# TW: Calculating distance between two planets. Moved it to the planet class as it functionally makes more sense there than in a SolarSystem class + +solar_system.local_year("secret marsmars") +# TW: Calculating local_year