From 065b041b5634b3a8a2db3c436cbd695b6b789474 Mon Sep 17 00:00:00 2001 From: Emily Cowan Date: Thu, 20 Feb 2020 10:22:04 -0800 Subject: [PATCH] Moved files over to correct repo --- .DS_Store | Bin 0 -> 6148 bytes main.rb | 106 ++++++++++++++++++++++++++++++++++++++++++++++++ planet.rb | 36 ++++++++++++++++ solar_system.rb | 56 +++++++++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 .DS_Store create mode 100644 main.rb create mode 100644 planet.rb create mode 100644 solar_system.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..71bb84a31344546a8d6ed55f58a28e3aacff3ffc GIT binary patch literal 6148 zcmeHKu};H441EqowCX@d*fKI9F(afe&9=h$)bE{byiWc*g10V4o?s$w#r=@31y+L9!C_KD)JQDBN0o^gtKE7}~tkpW%1 zAs+F-qeOH4%B!lL6;)j@LSK(_>dBWEj(`KM@f1&3aJ82xwxT~7;Tq8cyr5$If^}A1 z8FJCexWNRM$Z<{7H)`vSTzmf3>X8(z&~Mt zJzJ$e3~1IEFb0f)H3Ry6NU4fhL>JJ1I%xbBfY_tk3&;Ais5xoGETRj@GZYD_#E>d+ ziyX+r>bb23%E| 0 + puts "That is not a valid mass. Please enter the mass for the planet in kg." + detail = gets.chomp.to_i + end + puts "Please enter the distance of the planet from the sun in km:" + distance = gets.chomp.to_i + until detail > 0 + puts "That is not a valid distance. Please enter the distance of the planet from the sun in km." + detail = gets.chomp.to_i + end + puts "Please enter a fact about the planet:" + fact = gets.chomp + solar_system.add_planet(Planet.new(name, color, mass, distance, fact)) + main +end + +def distance_between(solar_system) + puts "Please name the first planet:" + planet_one = gets.chomp + puts "Please name the second planet" + planet_two = gets.chomp + + # until all_planets.include?() + + puts "The distance between #{planet_one} and #{planet_two} is: #{solar_system.distance_between(planet_one, planet_two)}km" + main +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..c94ea6da --- /dev/null +++ b/planet.rb @@ -0,0 +1,36 @@ +class Planet + attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + + def initialize ( + name, + color, + mass_kg, + distance_from_sun_km, + fun_fact + ) + @name = name + @color = color + @mass_kg = mass_kg + @distance_from_sun_km = distance_from_sun_km + @fun_fact = fun_fact + end + + def summary + return "#{@name.to_s.capitalize} is a #{@color} planet that weighs #{@mass_kg}kg. +It is approximately #{@distance_from_sun_km}km away from the sun, and a fact about is: #{@fun_fact}" + end +end + +# earth = Planet.new('earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') +# pluto = Planet.new('pluto', 'varied, grey-orange', 1.30900e22, 5.906e9, 'Named after the Roman god of the underworld') +# mercury = Planet.new('mercury', 'yellow-blue', 3.285e23, 5.791e7, 'mercury is the fasest planet. Completing a full circle around the sun in 88 days') +# venus = Planet.new('venus', 'yellow', 4.867e24, 1.089e8, 'venus is the hottest planet in our solar system') +# mars = Planet.new('mars', 'red', 6.39e23, 1.496e8, 'it is suspected that billions of years ago mars was much warmer and had water') +# jupiter = Planet.new('jupiter', 'white and red', 1.898e27, 7.779e8, 'jupiter is almost twice as large as any other planet in the solar system') +# saturn = Planet.new('saturn', 'yellow', 5.683e26, 1.433e9, 'saturn has the largest and most complex rings of any planet in our solar system') +# uranus = Planet.new('uranus', 'light blue', 8.681e25, 2.877e9, 'uranus rotates at an almost 90-degree angle from the plane of its orbit') +# neptune = Planet.new('neptune', 'blue', 1.024e26, 4.503e9, 'neptune was the first planet located through mathematical calculations') +# menu_selection = [1..9] +# all_planets = [earth, pluto, mercury, venus, mars, jupiter, saturn, uranus, neptune] +# solar_system = SolarSystem.new('Sol') +# solar_system.add_multiple_planets(all_planets) \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..6c1c900a --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,56 @@ +class SolarSystem + attr_reader :star_name, :planets + def initialize (star_name) + @star_name = star_name + @planets = [] + end + + def add_planet(planet) + @planets << planet + @planets.sort_by! { |planet| planet.distance_from_sun_km} + end + + def add_multiple_planets(planets) + planets.each do |planet| + @planets << planet + end + @planets.sort_by! { |planet| planet.distance_from_sun_km} + end + + def list_planets + list = "Planets orbiting #{@star_name}:\n" + @planets.each_with_index do |planet, index| + list += "#{index + 1}. #{planet.name.to_s.capitalize}\n" + end + return list + end + + def find_planet_by_name(planet_name) + planet_name = planet_name.capitalize + planets.each do |item| + if item.name == planet_name + return item + end + end + end + + def include?(planet) + planet_strings = @planets.map {|index| index.name} + if planet_strings.include?(planet.capitalize) + return true + else + return false + end + end + + def distance_between(selection_one, selection_two) + planet_one = self.find_planet_by_name(selection_one) + planet_two = self.find_planet_by_name(selection_two) + if planet_one.distance_from_sun_km > planet_two.distance_from_sun_km + distance_between = planet_one.distance_from_sun_km - planet_two.distance_from_sun_km + else + distance_between = planet_two.distance_from_sun_km - planet_one.distance_from_sun_km + end + return distance_between + end +end \ No newline at end of file