From 96816ac4510e89225751f3d0e05e1b9bf52bd35f Mon Sep 17 00:00:00 2001 From: Natalia K Date: Tue, 14 Feb 2017 20:37:28 -0800 Subject: [PATCH 1/4] Create solar_system.rb --- solar_system.rb | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 solar_system.rb diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..dad6e691 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,138 @@ +class Planet + attr_accessor :name, :diameter, :mass, :day_length, :moons, :av_density, :distance_from_sun + def initialize (planets_info_hash) + @name = planets_info_hash[:name] + @diameter = planets_info_hash[:diameter] + @mass = planets_info_hash[:mass] + @day_length = planets_info_hash[:day_length] + @moons = planets_info_hash[:moons] + @av_density = planets_info_hash[:av_density] + @distance_from_sun = planets_info_hash[:distance_from_sun] + end + + @@all_planets = [] # To store all planets +# Method returns array of planets: + def self.all_planets + @@all_planets + end + +# Method adds planet to array of planets +# Method could take multiple arguments, so I could add all planet object in one time + def self.store_planet(*planets) + @@all_planets += planets + end +#Method print info about planet: + def self.print_info_about_planet(num) + puts "Here is your info about planet:" + puts "Name: #{Planet.all_planets[num].name}" + puts "Diameter: #{Planet.all_planets[num].diameter}" + puts "Mass: #{Planet.all_planets[num].mass}" + puts "Length of a day: #{Planet.all_planets[num].day_length}" + puts "Number of moons: #{Planet.all_planets[num].moons}" + puts "Average density: #{Planet.all_planets[num].av_density}" + puts "Distance from the sun (in km): #{Planet.all_planets[num].distance_from_sun}" + end +end + +class SolarSystem + + def initialize() + @planets = [] + end + + def add_planet(planet) + @planets << planet + end + + def add_planets(list_of_planet) + @planets += list_of_planet + end +# Display all planets in Solar System: + def display_solar_system + puts "\n Here is a list of planets in SOLAR SYSTEM: \n " + @planets.each do |planet| + puts "Here is your info about planet: " + puts "Name: #{planet.name} " + puts "Diameter (km) : #{planet.diameter}" + puts "Mass (of Earth mass): #{planet.mass} " + puts "Length of a day: #{planet.day_length}" + puts "Number of moons: #{planet.moons}" + puts "Average density: #{planet.av_density}" + puts "Distance from the sun (in km): #{planet.distance_from_sun} \n\n" + end + end + +# Method asks for input and calculate distance between planets: + def self.calculate_distance + while true + puts "Distance between planet calculation:" + Planet.all_planets.length.times do |i| + puts "#{Planet.all_planets[i].name} " + end + puts "Enter the NAME of first planet:" + planet1 = gets.chomp.downcase + puts "Enter the NAME of second planet:" + planet2 = gets.chomp.downcase + if planet1 == planet2 + puts "Planets should have different names! \n" + next + end + # Match planet name with planet object: + Planet.all_planets.each do |planet| + planet1 = planet if planet.name.downcase == planet1 + planet2 = planet if planet.name.downcase == planet2 + end + if planet1.distance_from_sun > planet2.distance_from_sun + result = planet1.distance_from_sun - planet2.distance_from_sun + else + result = planet2.distance_from_sun - planet1.distance_from_sun + end + puts "Distance between #{planet1.name} and #{planet2.name} is #{result}" + puts "Would you like to get distance between another planets? (yes/no) " + answer = gets.chomp.downcase + break if answer == 'no' + end + end # End of Calculate Diatance method + +end # End of SolarSystem class + +# Create instances of class Planet +mercury = Planet.new(name: "Mercury", diameter: 4378 , mass: 0.055, day_length: "58 days", moons: 0, av_density: 5.43, distance_from_sun: 57910006) +venus = Planet.new(name: "Venus", diameter: 12046, mass: 0.815,day_length: "243 days", moons: 0, av_density: 5.24, distance_from_sun: 108199995) +earth = Planet.new(name: "Earth", diameter: 12756, mass: 1.000, day_length: "24 hours", moons: 0, av_density: 5.43, distance_from_sun: 149599951) +mars = Planet.new(name: "Mars", diameter: 6794, mass: 0.107, day_length: "24 hours 37 mins", moons: 2, av_density: 3.93, distance_from_sun: 227939920) +jupiter = Planet.new(name: "Jupiter", diameter: 142800, mass: 318, day_length: "9.9 hours", moons: 67, av_density: 1.33, distance_from_sun: 778330257) +saturn = Planet.new(name: "Saturn", diameter: 120000, mass: 95, day_length: "10 hours 34 mins", moons: 0, av_density: 0.687, distance_from_sun: 1429400028) +uranus = Planet.new(name: "Uranus", diameter: 51260, mass: 14.5, day_length: "17 hours 14 mins", moons: 0, av_density: 1.27, distance_from_sun: 2870989228) +neptune = Planet.new(name: "Neptune", diameter: 49598, mass: 17.2, day_length: "16 hours 6 minutes", moons: 14, av_density: 1.63, distance_from_sun: 4504299579) +pluto = Planet.new(name: "Pluto",diameter: 2290, mass: 0.002, day_length: "6 days 9 hours", moons: 5, av_density: 2.03, distance_from_sun: 74000000000) + +# Add all planets to array: +Planet.store_planet(mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto) + +#Display a list of planets: +puts "Here is a list of all planets:" +Planet.all_planets.length.times do |i| + puts "#{i+1}. #{Planet.all_planets[i].name} " +end + +while true + print "What planet do you want to get info about? (Enter corresponding number): " + num = gets.chomp.to_i - 1 + Planet.print_info_about_planet(num) + puts "Would you like to get information about another planet?" + answer = gets.chomp.downcase + break if answer == 'no' +end + +solar_system = SolarSystem.new # Create instance of Solar System class +solar_system.add_planet(mercury) # Add one planet to Solar System +solar_system.add_planet(venus) +# Display all planets: +#solar_system.display_solar_system +list_of_planets = [earth, mars, jupiter, saturn, uranus, neptune, pluto] +solar_system.add_planets(list_of_planets) # Add list of planets to the Solar System +#solar_system.display_solar_system + +# Call method to calculate distance between planets: +SolarSystem.calculate_distance From f6f387b2e8902e4cc9bce88b39558adb4a911e0d Mon Sep 17 00:00:00 2001 From: Natalia K Date: Wed, 15 Feb 2017 10:00:02 -0800 Subject: [PATCH 2/4] Update solar_system.rb add some changes to not the repeat the same block of code twice --- solar_system.rb | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/solar_system.rb b/solar_system.rb index dad6e691..f859b1d9 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -22,15 +22,14 @@ def self.store_planet(*planets) @@all_planets += planets end #Method print info about planet: - def self.print_info_about_planet(num) - puts "Here is your info about planet:" - puts "Name: #{Planet.all_planets[num].name}" - puts "Diameter: #{Planet.all_planets[num].diameter}" - puts "Mass: #{Planet.all_planets[num].mass}" - puts "Length of a day: #{Planet.all_planets[num].day_length}" - puts "Number of moons: #{Planet.all_planets[num].moons}" - puts "Average density: #{Planet.all_planets[num].av_density}" - puts "Distance from the sun (in km): #{Planet.all_planets[num].distance_from_sun}" + def print_info_about_planet + puts "Name: #{@name}" + puts "Diameter: #{@diameter}" + puts "Mass: #{@mass}" + puts "Length of a day: #{@day_length}" + puts "Number of moons: #{@moons}" + puts "Average density: #{@av_density}" + puts "Distance from the sun (in km): #{@distance_from_sun} \n\n" end end @@ -50,15 +49,9 @@ def add_planets(list_of_planet) # Display all planets in Solar System: def display_solar_system puts "\n Here is a list of planets in SOLAR SYSTEM: \n " + @planets.each do |planet| - puts "Here is your info about planet: " - puts "Name: #{planet.name} " - puts "Diameter (km) : #{planet.diameter}" - puts "Mass (of Earth mass): #{planet.mass} " - puts "Length of a day: #{planet.day_length}" - puts "Number of moons: #{planet.moons}" - puts "Average density: #{planet.av_density}" - puts "Distance from the sun (in km): #{planet.distance_from_sun} \n\n" + planet.print_info_about_planet end end @@ -93,7 +86,7 @@ def self.calculate_distance break if answer == 'no' end end # End of Calculate Diatance method - + end # End of SolarSystem class # Create instances of class Planet @@ -117,9 +110,13 @@ def self.calculate_distance end while true - print "What planet do you want to get info about? (Enter corresponding number): " - num = gets.chomp.to_i - 1 - Planet.print_info_about_planet(num) + print "What planet do you want to get info about?" + planet_name_input = gets.chomp + Planet.all_planets.each do |planet| + if planet.name.downcase == planet_name_input.downcase + planet.print_info_about_planet + end + end puts "Would you like to get information about another planet?" answer = gets.chomp.downcase break if answer == 'no' @@ -127,9 +124,9 @@ def self.calculate_distance solar_system = SolarSystem.new # Create instance of Solar System class solar_system.add_planet(mercury) # Add one planet to Solar System -solar_system.add_planet(venus) +#solar_system.add_planet(venus) # Display all planets: -#solar_system.display_solar_system +solar_system.display_solar_system list_of_planets = [earth, mars, jupiter, saturn, uranus, neptune, pluto] solar_system.add_planets(list_of_planets) # Add list of planets to the Solar System #solar_system.display_solar_system From ead2749715682730ab8c462d19d63d311e987c24 Mon Sep 17 00:00:00 2001 From: Natalia K Date: Wed, 15 Feb 2017 10:08:55 -0800 Subject: [PATCH 3/4] Update solar_system.rb --- solar_system.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/solar_system.rb b/solar_system.rb index f859b1d9..0fb00ce2 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -17,7 +17,7 @@ def self.all_planets end # Method adds planet to array of planets -# Method could take multiple arguments, so I could add all planet object in one time +# Method could take multiple arguments, so I could add all planet object in the same time: def self.store_planet(*planets) @@all_planets += planets end @@ -48,8 +48,7 @@ def add_planets(list_of_planet) end # Display all planets in Solar System: def display_solar_system - puts "\n Here is a list of planets in SOLAR SYSTEM: \n " - + puts "\n Here is a a list and info about planets in SOLAR SYSTEM: \n " @planets.each do |planet| planet.print_info_about_planet end @@ -124,7 +123,7 @@ def self.calculate_distance solar_system = SolarSystem.new # Create instance of Solar System class solar_system.add_planet(mercury) # Add one planet to Solar System -#solar_system.add_planet(venus) +solar_system.add_planet(venus) # Display all planets: solar_system.display_solar_system list_of_planets = [earth, mars, jupiter, saturn, uranus, neptune, pluto] From 7f1e27caa8c3066870505ea482b82c3171bb3f8d Mon Sep 17 00:00:00 2001 From: natalia-ku Date: Thu, 16 Feb 2017 09:37:11 -0800 Subject: [PATCH 4/4] new file --- new_file.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 new_file.txt diff --git a/new_file.txt b/new_file.txt new file mode 100644 index 00000000..e69de29b