diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..12737a57 --- /dev/null +++ b/planet.rb @@ -0,0 +1,39 @@ +class Planet + attr_reader :name, :mass, :diameter, :length_of_day, :distance_from_sun, :mean_temperature, :number_of_moons, :ring_system, :orbital_period + + def initialize(planet_hash) + @name = planet_hash[:name] + @mass = planet_hash[:mass] + @diameter = planet_hash[:diameter] + @length_of_day = planet_hash[:length_of_day] + @distance_from_sun = planet_hash[:distance_from_sun] + @mean_temperature = planet_hash[:mean_temperature] + @number_of_moons = planet_hash[:number_of_moons] + @ring_system = planet_hash[:ring_system] + @orbital_period = planet_hash[:orbital_period] + end + + def print_formatted + puts "*" * 44 + puts "Planet name: #{@name}" + puts "Mass: #{@mass} * 10^24 kg" + puts "Diameter: #{@diameter} km" + puts "Length of day: #{@length_of_day} hours" + puts "Distance from sun: #{@distance_from_sun} * 10^6 km" + puts "Mean temperature: #{@mean_temperature} ˚C" + puts "Number of moons: #{@number_of_moons}" + puts "Has a ring system: #{@ring_system}" + puts "Its orbital period #{@orbital_period} days" + puts "*" * 44 + end + + def calc_year(universe_start) + t = Time.now + years_around = t.year - universe_start + # how many Earth days since the beginning of the universe + # + days_around = years_around * 365.2 + planet_age = days_around / @orbital_period + puts "The age of the planet #{@name} is #{planet_age.round(3)} years." + end +end diff --git a/planet_data.csv b/planet_data.csv new file mode 100644 index 00000000..e8d070ba --- /dev/null +++ b/planet_data.csv @@ -0,0 +1,10 @@ +name,mass,diameter,length_of_day,distance_from_sun,mean_temperature,number_of_moons,ring_system +Mercury,0.33,4879,4222.6,57.9,167,0,FALSE +Venus,4.87,"12,104",2802,108.2,464,0,FALSE +Earth,5.97,"12,756",24,149.6,15,1,FALSE +Mars,0.642,6792,24.7,227.9,-65,2,FALSE +Jupiter,1898,"142,984",9.9,778.6,-110,67,TRUE +Saturn,568,"120,536",10.7,1433.5,-140,62,TRUE +Uranus,86.8,"51,118",17.2,2872.5,-195,27,TRUE +Neptune,102,"49,528",16.1,4495.1,-200,14,TRUE +Pluto,0.0146,2370,153.3,5906.4,-225,5,FALSE diff --git a/planet_program.rb b/planet_program.rb new file mode 100644 index 00000000..e0371546 --- /dev/null +++ b/planet_program.rb @@ -0,0 +1,150 @@ +require './planet' +require './solar_system' + +planets = [ + { + name: "Mercury", + mass: 0.330, + diameter: 4879, + length_of_day: 4222.6, + distance_from_sun: 57.9, + mean_temperature: 167, + number_of_moons: 0, + ring_system: false, + orbital_period: 88.0, + }, + { + name: "Venus", + mass: 4.87, + diameter: 12104, + length_of_day: 2802, + distance_from_sun: 108.2, + mean_temperature: 464, + number_of_moons: 0, + ring_system: false, + orbital_period: 224.7, + }, + { + name: "Earth", + mass: 5.97, + diameter: 12756, + length_of_day: 24.0, + distance_from_sun: 149.6, + mean_temperature: 15, + number_of_moons: 1, + ring_system: false, + orbital_period: 365.2, + }, + { + name: "Mars", + mass: 0.642, + diameter: 6782, + length_of_day: 24.7, + distance_from_sun: 227.9, + mean_temperature: -65, + number_of_moons: 2, + ring_system: false, + orbital_period: 687.0, + }, + { + name: "Jupiter", + mass: 1898, + diameter: 142984, + length_of_day: 9.9, + distance_from_sun: 778.6, + mean_temperature: -110, + number_of_moons: 67, + ring_system: true, + orbital_period: 4331, + }, + { + name: "Saturn", + mass: 568, + diameter: 120536, + length_of_day: 10.7, + distance_from_sun: 1433.5, + mean_temperature: -140, + number_of_moons: 62, + ring_system: true, + orbital_period: 10747, + }, + { + name: "Uranus", + mass: 86.8, + diameter: 51118, + length_of_day: 17.2, + distance_from_sun: 2872.5, + mean_temperature: -195, + number_of_moons: 27, + ring_system: true, + orbital_period: 30589, + }, + { + name: "Neptune", + mass: 102, + diameter: 49528, + length_of_day: 16.1, + distance_from_sun: 4495.1, + mean_temperature: -200, + number_of_moons: 14, + ring_system: true, + orbital_period: 59800, + }, +] + +dwarf_planet = { + name: "Pluto", + mass: 0.0146, + diameter: 2370, + length_of_day: 153.3, + distance_from_sun: 5906.4, + mean_temperature: -225, + number_of_moons: 5, + ring_system: false, + orbital_period: 90560, +} + +# Create the universe object +universe = SolarSystem.new() +# Create a single planet object, Pluto the dwarf planet +dwarf = Planet.new(dwarf_planet) +# Add the single planet, pluto, to the universe +universe.add_a_planet(dwarf) +# Create an empty array to hold all the planet objects +planet_objects = [] +# Go through the planet hash (above) and create planet objects for each +# Push each of the planet objects to the planet_objects array +planets.each do |planet_hash| + planet_objects.push(Planet.new(planet_hash)) +end +# Add the array of the other planet objects to the universe +universe.add_many_planets(planet_objects) + +# Print the distance between two random planets +puts "*" * 44 +puts "Here are a few fun facts:" +num_planets = universe.planets.length +universe.distance(universe.planets[rand(num_planets)], universe.planets[rand(num_planets)]) +universe.distance(universe.planets[rand(num_planets)], universe.planets[rand(num_planets)]) +universe.distance(universe.planets[rand(num_planets)], universe.planets[rand(num_planets)]) +universe.distance(universe.planets[rand(num_planets)], universe.planets[rand(num_planets)]) + +puts "*" * 44 +print "Press enter for more solar system fun." +gets + +# Calculating planet ages with default universe start date of 1979 +puts "This universe was invented in #{universe.formation_year}" +universe.planets.each do |planet| + planet.calc_year(universe.formation_year) +end + +puts "*" * 44 +print "Press enter for more solar system fun." +gets + +# Print the universe fact sheet from assignment wave 1 +puts "*" * 44 +print "Press enter for more solar system fun." +gets +universe.print_fact_sheet diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..bf93361f --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,54 @@ +class SolarSystem + attr_accessor :planets, :formation_year + + def initialize(formation_year = 1979) + @planets = [] + @formation_year = formation_year + end + + def add_a_planet(planet) + @planets.push(planet) + end + + def add_many_planets(planet_array) + @planets = @planets + planet_array + end + + def distance(planet1, planet2) + distance = (planet1.distance_from_sun - planet2.distance_from_sun).abs + puts "The distance between #{planet1.name} and #{planet2.name } is #{distance} * 10^6 km." + return distance + end + + def print_fact_sheet + # Print a menu of options + puts "PLANET FACT SHEET".center(64) + puts "Information from http://nssdc.gsfc.nasa.gov/planetary/factsheet/" + puts "Type the number of the planet to learn more" + @planets.length.times do |num| + puts "#{num + 1}. #{@planets[num].name}" + end + puts "0. Exit" + # Get user input and convert it to the + # planet's index in @planets array + choice_index = 100 + while choice_index >= 0 + print "Your choice: " + choice_index = gets.chomp.to_i - 1 + # Validate user input + while choice_index < -1 || choice_index >= @planets.length + puts "Invalid entry. Please enter a number from 0 to #{@planets.length} * 10^6 km" + print "Your choice: " + choice_index = gets.chomp.to_i - 1 + end + # Print fun planet facts based on user input + # Or if the option was -1 then quit + if choice_index == -1 + exit + else + @planets[choice_index].print_formatted + end + end + end + +end