Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queues Natalia Kuleniuk Solar System #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added new_file.txt
Empty file.
134 changes: 134 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
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 the same time:
def self.store_planet(*planets)
@@all_planets += planets
end
#Method print info about planet:
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

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 a list and info about planets in SOLAR SYSTEM: \n "
@planets.each do |planet|
planet.print_info_about_planet
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?"
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'
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