From 3519dcf4a1141d0af3774b11601f9e5bebd6a150 Mon Sep 17 00:00:00 2001 From: Dreedle Date: Thu, 1 Oct 2015 11:23:44 -0700 Subject: [PATCH] Adding planet class and solar system program. --- planet_class_file.rb | 41 +++++++++++++++ solar_system_program.rb | 110 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 planet_class_file.rb create mode 100644 solar_system_program.rb diff --git a/planet_class_file.rb b/planet_class_file.rb new file mode 100644 index 00000000..d02e5f92 --- /dev/null +++ b/planet_class_file.rb @@ -0,0 +1,41 @@ +class Planet +#year length is given in units of earth years +#diameter is given in units of earth diameters +#distance_from_star is given in millions of miles + + attr_reader :name, :year_length, :number_of_moons, :diameter, :distance_from_star , :moon_names, :state_of_matter, :fun_facts + + def initialize(single_planet_hash) + @name = single_planet_hash[:name] + @year_length = single_planet_hash[:year_length] + @number_of_moons = single_planet_hash[:number_of_moons] + @diameter = single_planet_hash[:diameter] + @distance_from_star = single_planet_hash[:distance_from_star] + @moon_names = single_planet_hash[:moon_names] #moon names is an array + @state_of_matter = single_planet_hash[:state_of_matter] + @fun_facts = single_planet_hash[:fun_facts] + end + + def dig_hole_through_planet (shovelfulls = 100) + progress = (diameter*100)/shovelfulls + if progress < 100 + puts "You ran out of air before digging all the way through. :(" + else + puts "SUCCESS! You dug a hole through the planet!" + end + end + + def add_moon(moon) + @moon_names.push(moon) + end + + + def print_planet_info + puts "\n#{@name} is a planet that is #{@distance_from_star} million miles from its star." + puts "Its diameter is #{@diameter} times that of Earth's." + puts "One #{@name} year is equal to #{@year_length} Earth years." + puts "The surface is #{@state_of_matter}." + puts "#{@fun_facts} How about that?" + puts + end +end diff --git a/solar_system_program.rb b/solar_system_program.rb new file mode 100644 index 00000000..b0397c73 --- /dev/null +++ b/solar_system_program.rb @@ -0,0 +1,110 @@ +require "./planet_class_file.rb" +require "./solar_system_class.rb" + +#start with just one planet +pluto = + {name: "Pluto", + year_length: 5, + numbers_of_moons: 3, + diameter: 23, + distance_from_star: 6422, + moon_names:[], + state_of_matter: "furry", + fun_facts: "For some reason this planet can't talk, but planet Goofy can." + } + +#then move on to an array of planet info +#question- is there an easy way to keep all this info in a different file? +#I don't like it to be here. + +more_planets = [ + {name: "Oobleck", + year_length: 2, + numbers_of_moons: 2, + diameter: 3.34, + distance_from_star: 105, + moon_names:[], + state_of_matter: "sometimes solid, sometimes liquid", + fun_facts: "You can run across the surface, but if you walk you'll sink." + }, + + {name: "Pizza", + year_length: 3, + numbers_of_moons: 1, + diameter: 465, + distance_from_star: 231, + moon_names:[], + state_of_matter: "gaseous", + fun_facts: "There are little green aliens inside a crane game on Pizza Planet." + }, + + {name: "Captain", + year_length: 34, + numbers_of_moons: 0, + diameter: 65.5, + distance_from_star: 24, + moon_names:[], + state_of_matter: "plasma", + fun_facts: "Captain Planet is gonna take pollution down to zero." + }, + + {name: "Xerox", + year_length: 2, + numbers_of_moons: 2, + diameter: 34.3, + distance_from_star: 16, + moon_names:["Copy 1", "Copy 2"], + state_of_matter: "black ink", + fun_facts: "You can make copies on planet Xerox." + }, + + {name: "Janet", + year_length: 33, + numbers_of_moons: 3, + diameter: 105, + distance_from_star: 1020, + moon_names:["Riff Raff", "Frank N. Furter", "Brad"], + state_of_matter: "hot mess", + fun_facts: "Dammit, Janet. I love you." + } + ] + +#first make planets from the info we have +#start with just one +pluto_p = Planet.new(pluto) + +# then make a whole array of planets +starter_planets_array = [] + +more_planets.each do |planet| + starter_planets_array.push(Planet.new(planet)) +end + +#test +# starter_planets_array.each do |planet| +# puts planet.name +# end + +#ok, now make a solar system. +bowler_system = SolarSystem.new("Bowler system", "Bowling ball") +puts "We have created the #{bowler_system.system_name}." +puts + +bowler_system.add_one_planet(pluto_p) + +#Great. Now, from the array of planets we made, add those planets to the bowler system as well. +bowler_system.add_array_of_planets(starter_planets_array) + +#now play around with the solar system a little +bowler_system.planets_array.each do |planetn| + puts planetn.name +end +puts + +bowler_system.planets_array.each do |planetp| + puts planetp.dig_hole_through_planet +end + +bowler_system.planets_array.each do |planetz| + puts planetz.print_planet_info +end