diff --git a/Wave_1.rb b/Wave_1.rb new file mode 100644 index 00000000..6a1eec75 --- /dev/null +++ b/Wave_1.rb @@ -0,0 +1,86 @@ +# hashes of planet information +planet_a = { + name: "Mercury", + size: 1516, #miles in diameter + color: "dark gray", + temp: 332, # degrees Fahrenheit average + speed: 112000, #mph + distance: 36 #million miles average from the sun +} +planet_b = { + name: "Jupiter", + size: 43441, #miles in diameter + color: "dark orange", + temp: -234, # degrees Fahrenheit average + speed: 47051, #mph + distance: 483 #million miles average from the sun +} +planet_c = { + name: "Mars", + size: 4222, #miles in diameter + color: "golden brown", + temp: -80, # degrees Fahrenheit average + speed: 86871, #mph + distance: 141 #million miles average from the sun +} +planet_d = { + name: "Venus", + size: 3760, #miles in diameter + color: "light yellow", + temp: 864, # degrees Fahrenheit average + speed: 78341, #mph + distance: 67, #million miles average from the sun +} +planet_e = { + name: "Earth", + size: 8000, #miles in diameter + color: "blue", + temp: 61, # degrees Fahrenheit average + speed: 67000, #mph + distance: 93, #mil lion miles average from the sun +} + +planet_list = ["Mercury", "Jupiter", "Mars", "Venus", "Earth"] + +class SolarSystem + attr_accessor :planets + + def initialize(planets) + @planets = planets + end + + # method to add new planets + def add_planet(planet) + @planets.push(planet) + end + + # method that returns the array as a string + def list + list = "" + i = 0 + @planets.each do |planet| + list << "#{i+1}. #{planet}\n" + i += 1 + end + return list + end + + def summary + return "#{planets}" + end + +end + +# test the SolarSystem class with methods to add a new planet +# set up the list from the array +my_solar_system = SolarSystem.new(planet_list) +my_solar_system.add_planet('Hoth') +the_list_to_print = my_solar_system.list + +# prints out the list of planets from the planet_list array +puts the_list_to_print + +# prints out the hashes +my_solar_system = SolarSystem.new( [ planet_a, planet_b, planet_c, planet_d, planet_e ] ) +the_list_to_print = my_solar_system.list +puts the_list_to_print diff --git a/Wave_2.rb b/Wave_2.rb new file mode 100644 index 00000000..1c8d42f0 --- /dev/null +++ b/Wave_2.rb @@ -0,0 +1,65 @@ +class SolarSystem + + attr_accessor :planets + + def initialize(planets) + @planets = planets + end + + # method that returns the array as a string + def list + list = "" + i = 0 + @planets.each do |planet| + list << "Planet ##{i+1} - \n#{planet.attributes}\n" + i += 1 + end + return list + end + +end + +class Planet + + attr_accessor :names, :sizes, :colors, :temps, :speeds, :year_length, :distances + + # initialize method which takes several arguments + def initialize(name, size, color, temp, speed, year_length, distance) + @names = name + @sizes = size + @colors = color + @temps = temp + @speeds = speed + @year_length = year_length + @distances = distance + # @@array << self + end + + # method that returns the Planet's attributes reading the instance variables + def attributes + list = "name: #{@names} + size: #{@sizes} miles in diameter + color: #{@colors} + tempuratur: #{@temps} degrees Fahrenheit + speed: #{@speeds} mph + year_length: #{@year_length} days to go around the sun + distance: #{@distances} million miles from the sun" + return list + end + +end + +# creating instance for each planet as an Planet object +planet_a = Planet.new("Mercury", 1516, "dark gray", 332, 112000, 87.97, 36) +planet_b = Planet.new("Jupiter", 43441, "dark orange", -234, 47051, 4332.82, 483) +planet_c = Planet.new("Mars", 4222, "golden brown", -80, 86871, 686.98, 141) +planet_d = Planet.new("Venus", 3760, "light yellow", 864, 78341, 224.70, 67) +planet_e = Planet.new("Earth", 8000, "blue", 61, 67000, 365.26, 93) + +# creating an array of planets +my_planets = [planet_a, planet_b, planet_c, planet_d, planet_e] + +# calling an intance of SolarSystem class +my_solar_system = SolarSystem.new(my_planets) +the_list_to_print = my_solar_system.list +puts the_list_to_print diff --git a/Wave_3.rb b/Wave_3.rb new file mode 100644 index 00000000..99dca916 --- /dev/null +++ b/Wave_3.rb @@ -0,0 +1,167 @@ +# Solar System class +class SolarSystem + + attr_accessor :planets + + def initialize(my_planets) + @planets = my_planets + end + + # method that returns the array as a string + def list_planets + list = "" + i = 0 + @planets.each do |planet| + list << "#{i+1} - #{planet.name}\n" + i += 1 + end + + list << "#{i + 1} - Create a new planet!" + return list + end + + # adds a new to the solar system + def add new_planet + @planets << new_planet + end + +end + + +# Planet class +class Planet + + attr_accessor :name, :size, :color, :temp, :speed, :year_length, :distance + + # initialize planet method + def initialize(planet_attributes) + @names = planet_attributes[:name] + @sizes = planet_attributes[:size] + @colors = planet_attributes[:color] + @temps = planet_attributes[:temp] + @speeds = planet_attributes[:speed] + @year_length = planet_attributes[:year_length] + @distances = planet_attributes[:distance] + end + + # method that returns each planet's name from the hashes + def name + return "#{@names}" + end + + # method that returns the Planet's attributes reading the instance variables + def attributes + list = "name: #{@names} + size: #{@sizes} miles in diameter + color: #{@colors} + temperature: #{@temps} degrees Fahrenheit + speed: #{@speeds} mph + year_length: #{@year_length} days to go around the sun + distance: #{@distances} million miles from the sun" + return list + end + +end + +# a method to create a new planet from user input +def create_planet + new_planet ={} + + print "\nWhat is the name of the planet? " + new_planet[:name] = gets.chomp.downcase.capitalize + + print "What is the size of #{new_planet[:name]} in diameter? " + new_planet[:size] = gets.chomp.to_i + + print "What is the color of #{new_planet[:name]}? " + new_planet[:color] = gets.chomp + + print "What is the temperature of #{new_planet[:name]}? " + new_planet[:temp] = gets.chomp + + print "How many days to go around the sun? " + new_planet[:year_length] = gets.chomp + + print "How far is #{new_planet[:name]} from the sun? " + new_planet[:distance] = gets.chomp.to_i + + new_planet = Planet.new(new_planet) + + return new_planet +end + +# a list of my planets in hashes +planet_a = Planet.new({ + name: "Mercury", + size: 1516, #miles in diameter + color: "dark gray", + temp: 332, # degrees Fahrenheit average + speed: 112000, #mph + year_length: 87.97, #days + distance: 36 #million miles average from the sun +}) +planet_b = Planet.new({ + name: "Jupiter", + size: 43441, #miles in diameter + color: "dark orange", + temp: -234, # degrees Fahrenheit average + speed: 47051, #mph + year_length: 4332.82, #days + distance: 483 #million miles average from the sun +}) +planet_c = Planet.new({ + name: "Mars", + size: 4222, #miles in diameter + color: "golden brown", + temp: -80, # degrees Fahrenheit average + speed: 86871, #mph + year_length: 686.98, #days + distance: 141 #million miles average from the sun +}) +planet_d = Planet.new({ + name: "Venus", + size: 3760, #miles in diameter + color: "light yellow", + temp: 864, # degrees Fahrenheit average + speed: 78341, #mph + year_length: 224.70, #days + distance: 67, #million miles average from the sun +}) +planet_e = Planet.new({ + name: "Earth", + size: 8000, #miles in diameter + color: "blue", + temp: 61, # degrees Fahrenheit average + speed: 67000, #mph + year_length: 365.26, #days + distance: 93, #mil lion miles average from the sun +}) + +# store instances in an array +my_planets = [planet_a, planet_b, planet_c, planet_d, planet_e] + +# creating my solar system +my_solar_system = SolarSystem.new(my_planets) +the_list_to_print = my_solar_system.list_planets + + +# welcome message +puts "Welcome to my Solar System!" +puts "My solar system include: \n#{the_list_to_print}!" + +# prompt input from user +print "Would you like to learn about one of my planets or create your own planet? +Please select a number from above. > " +user_input = gets.chomp.to_i + +# if statement to read the creat planet method +# else list characteristics of chosen planet +if user_input == my_planets.count + 1 + new_planet = create_planet + my_solar_system.add(new_planet) + print "\nA new planet called #{new_planet.name} has been" + puts " added to my solar system. Thanks for visiting!" +else + puts "\nHere's the information for #{my_planets[user_input - 1].name}" + puts my_planets[user_input - 1].attributes +end