diff --git a/solar_system_wave1.rb b/solar_system_wave1.rb new file mode 100644 index 00000000..7ea45f5e --- /dev/null +++ b/solar_system_wave1.rb @@ -0,0 +1,51 @@ +# Create a SolarSystem class with an @planets instance variable. +# Create an initialize method which should take a collection of planet names and store them in an @planets instance variable. +# Create a method that adds a planet to the list (not using user input). +# Create a method which will return, not print, a list of the planets as a String. +# Write code to test your SolarSystem +# Modify SolarSystem's initialize method to take a list of hashes where each planet is sent as a hash with at least 5 attributes. + +# Define the class +class SolarSystem + +# reader for planet + def planets + return @planets + end + +# writer for planet + def planets=(new_planet) + @planets = new_planet + end + + attr_accessor :order + +# initialize method + def initialize(planets) + @planets = planets + end + + def self.planets + return @planets + end + + @planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"] + +end + +# code to test +SolarSystem.planets.each_with_index do |planet, i| + puts "#{i+1}. #{planet}" +end + + + +mercury = { planets: "MercuryA", radius: 1516, moons: 0, rotation_period: 58.646, orbit_period: 0.2408467, rings: "No" } +venus = { planets: "VenusA", radius: 3760, moons: 0, rotation_period: -243.018, orbit_period: 0.61519726, rings: "No"} +earth = { planets: "EarthA", radius: 6371, moons: 1, rotation_period: 0.99726968, orbit_period: 1.0000174, rings: "No" } +mars = { planets: "MarsA", radius: 3389, moons: 2, rotation_period: 1.026, orbit_period: 1.8808476, rings: "No" } +jupiter = { planets: "JupiterA", radius: 69911 , moons: 69, rotation_period: 0.41354, orbit_period: 11.862615, rings: "Yes" } +saturn = { planets: "SaturnA", radius: 58232, moons: 62, rotation_period: 0.444, orbit_period: 29.447498, rings: "Yes" } +uranus = { planets: "UranusA", radius: 25362 , moons: 27, rotation_period: -0.718, orbit_period: 84.016846, rings: "Yes" } +neptune = { planets: "NeptuneA", radius: 24622 , moons: 14, rotation_period: 0.671, orbit_period: 164.79132, rings: "Yes" } + diff --git a/solar_system_wave1b.rb b/solar_system_wave1b.rb new file mode 100644 index 00000000..0f6331e5 --- /dev/null +++ b/solar_system_wave1b.rb @@ -0,0 +1,134 @@ +# Create a SolarSystem class with an @planets instance variable. +# Create an initialize method which should take a collection of planet names and store them in an @planets instance variable. +# Create a method that adds a planet to the list (not using user input). +# Create a method which will return, not print, a list of the planets as a String. +# Write code to test your SolarSystem +# Modify SolarSystem's initialize method to take a list of hashes where each planet is sent as a hash with at least 5 attributes. + +# Define the class +class SolarSystem + +# reader for planet + def planets + return @planets + end + +# writer for planet + def planets=(new_planet) + @planets = new_planet + end + + #attr_accessor :order, + attr_accessor :radius + attr_accessor :moons + attr_accessor :rotation_period + attr_accessor :orbit_period + attr_accessor :rings + + +# initialize method + def initialize(planets_hash) + @planets = planets_hash[:planets] + @radius = planets_hash[:radius] + @moons = planets_hash[:moons] + @rotation_period = planets_hash[:rotation_period] + @orbit_period = planets_hash[:orbit_period] + @rings = planets_hash[:rings] + end + + def self.planets + return @planets + end + + #@planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"] + +end + +puts +mercury = SolarSystem.new ({ + planets: "MercuryA", + radius: 1516, + moons: 0, + rotation_period: 58.646, + orbit_period: 0.2408467, + rings: "No" +}) + + +venus = SolarSystem.new ({ + planets: "VenusA", + radius: 3760, + moons: 0, + rotation_period: -243.018, + orbit_period: 0.61519726, + rings: "No" +}) + + +earth = SolarSystem.new ({ + planets: "EarthA", + radius: 6371, + moons: 1, + rotation_period: 0.99726968, + orbit_period: 1.0000174, + rings: "No" +}) + +mars = SolarSystem.new ({ + planets: "MarsA", + radius: 3389, + moons: 2, + rotation_period: 1.026, + orbit_period: 1.8808476, + rings: "No" +}) + +jupiter = SolarSystem.new ({ + planets: "JupiterA", + radius: 69911 , + moons: 69, + rotation_period: 0.41354, + orbit_period: 11.862615, + rings: "Yes" +}) + +saturn = SolarSystem.new ({ + planets: "SaturnA", + radius: 58232, + moons: 62, + rotation_period: 0.444, + orbit_period: 29.447498, + rings: "Yes" +}) +uranus = SolarSystem.new ({ + planets: "UranusA", + radius: 25362 , + moons: 27, + rotation_period: -0.718, + orbit_period: 84.016846, + rings: "Yes" +}) +neptune = SolarSystem.new ({ + planets: "NeptuneA", + radius: 24622 , + moons: 14, + rotation_period: 0.671, + orbit_period: 164.79132, + rings: "Yes" }) + +puts mercury.planets +puts +puts venus.planets +puts +puts earth.planets +puts +puts mars.planets +puts +puts jupiter.planets +puts +puts saturn.planets +puts +puts uranus.planets +puts +puts neptune.planets +puts diff --git a/solar_system_wave2.rb b/solar_system_wave2.rb new file mode 100644 index 00000000..dcb754f2 --- /dev/null +++ b/solar_system_wave2.rb @@ -0,0 +1,76 @@ +# Create a SolarSystem class with an @planets instance variable. +# Create an initialize method which should take a collection of planet names and store them in an @planets instance variable. +# Create a method that adds a planet to the list (not using user input). +# Create a method which will return, not print, a list of the planets as a String. +# Write code to test your SolarSystem +# Modify SolarSystem's initialize method to take a list of hashes where each planet is sent as a hash with at least 5 attributes. + +# Define the class +class SolarSystem + + def initialize(planets) + @planets = planets + end + + attr_reader :planets + + def puts_self + puts self + end + + def self.planets + return @planets + end + + def planet + puts + end + arr_planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"] + + @planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"] + +end + +class Planet + + attr_accessor :name + attr_accessor :order + attr_accessor :radius + attr_accessor :moons + + def initialize(name, order, radius, moons) + @name = name + @order = order + @radius = radius + @moons = moons + end + + def puts_self + puts self + end + + def self.name + return @name + end + + # instance method + def summary + return " #{order}. #{name} - Radius: #{radius}km. Number of Moons: #{moons}." + end + + def self.summary + return @summary + end + +end + + planet_arr = [ + Planet.new("Mercury", 1, 2440 , 0), + Planet.new("Venus", 2, 6052 , 0), + Planet.new("Earth", 3, 6371 ,1) + ] + +puts +planet_arr.each do |i| + puts i.summary +end diff --git a/solar_system_wave3.rb b/solar_system_wave3.rb new file mode 100644 index 00000000..5a17db55 --- /dev/null +++ b/solar_system_wave3.rb @@ -0,0 +1,109 @@ +class SolarSystem + + def initialize(planets) + @planets = planets + end + + attr_reader :planets + + def puts_self + puts self + end + + def self.planets + return @planets + end +end + +class Planet + + attr_accessor :name, :order, :radius, :moons + + def initialize(name, order, radius, moons) + @name = name + @order = order + @radius = radius + @moons = moons + end + + def puts_self + puts self + end + + def self.name + return @name + end + + # instance method + def contents + return " #{order}. #{name}" + end + + def self.contents + return @contents + end + + def summary + return " #{name} - Radius: #{radius}km. Number of Moons: #{moons}." + end + + def self.summary + return @summary + end + +end + +# input planets + planet_arr = [ + Planet.new("Mercury", 1, 2440, 0), + Planet.new("Venus", 2, 6052, 0), + Planet.new("Earth", 3, 6371, 1), + Planet.new("Mars", 4, 3390, 2), + Planet.new("Jupiter", 5, 69911, 2), + Planet.new("Saturn", 6, 58232, 2), + Planet.new("Uranus", 7, 25362, 2), + Planet.new("Neptune", 8, 24622, 2), + ] + +#outputs planets +puts +print "Welcome to this Solar System directory! Here is a list of planets: " +puts +planet_arr.each_with_index do |planet, i| + puts "#{i+1}. #{planet.name}" +end + +puts +print "Type LEARN to learn about a planet. Type NEW to add a new planet: " +selection = gets.chomp.upcase! +if selection == "LEARN" + print "Please type the number of the planet you'd like to learn about: " + learn_option = gets.chomp.to_i + counter = 0 + until learn_option >= 0 && learn_option <= planet_arr.length + counter += 1 + if counter == 3 + puts "Too many incorrect attempts. Goodbye" + exit + else + print "You did not type a valid number. Please try again: " + learn_option = gets.chomp.to_i + end + end + puts planet_arr[learn_option-1].summary +elsif selection == "NEW" + print "What's the planet name? " + input_name = gets.chomp.upcase! + print "What's the radius of #{input_name} in kilometers? " + input_radius = gets.chomp.to_i + print "How many moons does #{input_name} have? " + input_moons = gets.chomp.to_i + planet_arr << Planet.new(input_name, 0, input_radius, input_moons) + puts "Here's an updated list of planets: " + planet_arr.each_with_index do |planet, i| + puts "#{i+1}. #{planet.summary} " +else + puts "You did not enter a valid option. Goodbye" +end + +end