From 05c06f8172f591faeab2fb68741b468eb318d1fb Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Tue, 18 Feb 2020 14:25:49 -0800 Subject: [PATCH 1/5] Added Planet class and main method for Wave 1 --- main.rb | 12 ++++++++++++ planet.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 main.rb create mode 100644 planet.rb diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..7536ae7a --- /dev/null +++ b/main.rb @@ -0,0 +1,12 @@ +require_relative 'planet' + +# method; creates two different instances of Planet and prints out some of their attributes. +def main + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + mars = Planet.new('Mars', 'red', 0.65e24, 2.279e8, 'The only planet that humans can somewhat survive on (with the help of technology and science, of course)') + puts earth.summary + puts mars.summary +end + +# invoke method +main \ No newline at end of file diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..dc214e84 --- /dev/null +++ b/planet.rb @@ -0,0 +1,17 @@ +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}: This planet is #{@color}-colored, is #{@mass_kg}kg, and is #{@distance_from_sun_km}km from the sun. Fun fact: #{@fun_fact}.\n" + end + +end \ No newline at end of file From 7896b7973988e6b82524de806bc602d30e65baac Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Tue, 18 Feb 2020 15:37:28 -0800 Subject: [PATCH 2/5] Added SolarSystem class for Wave 2 --- main.rb | 29 ++++++++++++++++++++++------- planet.rb | 2 +- solar_system.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 solar_system.rb diff --git a/main.rb b/main.rb index 7536ae7a..a886fc86 100644 --- a/main.rb +++ b/main.rb @@ -1,12 +1,27 @@ require_relative 'planet' +require_relative 'solar_system' + +solar_system = SolarSystem.new('Sol') # method; creates two different instances of Planet and prints out some of their attributes. -def main - earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') - mars = Planet.new('Mars', 'red', 0.65e24, 2.279e8, 'The only planet that humans can somewhat survive on (with the help of technology and science, of course)') - puts earth.summary - puts mars.summary -end +# def main +# earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') +# mars = Planet.new('Mars', 'red', 0.65e24, 2.279e8, 'The only planet that humans can somewhat survive on (with the help of technology and science, of course)') +# puts earth.summary +# puts mars.summary +# end # invoke method -main \ No newline at end of file +# main + +earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') +solar_system.add_planet(earth) + +list = solar_system.list_planets +puts list + +found_planet = solar_system.find_planet_by_name('eArth') + +# found_planet is an instance of class Planet +puts found_planet +puts found_planet.summary \ No newline at end of file diff --git a/planet.rb b/planet.rb index dc214e84..6324ccb9 100644 --- a/planet.rb +++ b/planet.rb @@ -1,5 +1,5 @@ class Planet - + # readable, not writeable attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..2cb6337e --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,43 @@ +class SolarSystem + # readable, not writeable + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + # methods + def add_planet(planet) + # take an instance of Planet as a parameter and add it to the list of planets. + planets << planet + end + + def list_planets + # return (not puts) string containing a list of all the planets in the system. + string = "Planets orbiting #{star_name}\n" + num = 0 + planets.each do |item| + num +=1 + string << "#{num}. #{item.name}\n" + end + return string + end + + def find_planet_by_name(string) + # takes the name of a planet as a string, and returns the corresponding instance of Planet + string = string.downcase.capitalize + + planets.each do |item| + if item.name == string + return item + end + end + # cannot find planet scenario + if planets.include?(string) == false + return "Planet not found" + end + + end + +end \ No newline at end of file From f6e5604abdae37324f4ac0628ababa52e7b63d6d Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Wed, 19 Feb 2020 12:00:10 -0800 Subject: [PATCH 3/5] Added functionality for Wave 3 --- main.rb | 63 ++++++++++++++++++++++++++++++++++--------------- solar_system.rb | 4 ++-- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/main.rb b/main.rb index a886fc86..e99aaf19 100644 --- a/main.rb +++ b/main.rb @@ -1,27 +1,52 @@ require_relative 'planet' require_relative 'solar_system' -solar_system = SolarSystem.new('Sol') +def main + solar_system = SolarSystem.new('Sol') + 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') + solar_system.add_planet(earth) # add more planets + solar_system.add_planet(pluto) + + puts "Welcome. Please choose from the following options: \n" + options = "1: Add Planet \n2: Planet Details \n3: Exit \n" + puts options + + while input = gets.chomp.downcase + if input == "exit" || input == "3" + break + elsif input == "1" || input == "add planet" # needs to be separate method + puts "Please enter details(name, color, mass_kg, distance_from_sun_km, fun_fact)" + # planet_details = gets.chomp # etc, make array or hash? + puts "Please enter planet name:" + planet_name = gets.chomp.capitalize + puts "Please enter planet color:" + planet_color = gets.chomp + puts "Please enter planet mass" + planet_mass = gets.chomp + puts "Please enter planet's distance from the sun:" + planet_distance = gets.chomp + puts "Please enter a fun fact about this planet:" + planet_fact = gets.chomp + new_planet = Planet.new(planet_name, planet_color, planet_mass, planet_distance, planet_fact) + solar_system.add_planet(new_planet) -# method; creates two different instances of Planet and prints out some of their attributes. -# def main -# earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') -# mars = Planet.new('Mars', 'red', 0.65e24, 2.279e8, 'The only planet that humans can somewhat survive on (with the help of technology and science, of course)') -# puts earth.summary -# puts mars.summary -# end + p solar_system -# invoke method -# main + puts "What would you like to do next?\n" + puts options + elsif input == "2" || input == "planet details" + puts "Please enter planet's name" + planet_name = gets.chomp + your_planet = solar_system.find_planet_by_name(planet_name) + puts your_planet.summary -earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') -solar_system.add_planet(earth) + puts "What would you like to do next?\n" + puts options + end + + end -list = solar_system.list_planets -puts list +end -found_planet = solar_system.find_planet_by_name('eArth') - -# found_planet is an instance of class Planet -puts found_planet -puts found_planet.summary \ No newline at end of file +main \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb index 2cb6337e..8f816838 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -30,10 +30,10 @@ def find_planet_by_name(string) planets.each do |item| if item.name == string - return item + return item #.name end end - # cannot find planet scenario + # cannot find planet scenario, needs to handle nil summary if planets.include?(string) == false return "Planet not found" end From 77bdb97d8238072924276f45359edf489649bac9 Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Wed, 19 Feb 2020 21:27:30 -0800 Subject: [PATCH 4/5] Refactored Wave 3 functionality; created show_planet_details and add_new_planet methods. --- main.rb | 79 +++++++++++++++++++++++++++++++------------------ solar_system.rb | 2 +- 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/main.rb b/main.rb index e99aaf19..0adc0aa6 100644 --- a/main.rb +++ b/main.rb @@ -2,51 +2,74 @@ require_relative 'solar_system' def main + # Our solar system solar_system = SolarSystem.new('Sol') + # Planets: thanks to Emily Cowan for sharing her planets data! 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') - solar_system.add_planet(earth) # add more planets + 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') + # Add planets to solar system + solar_system.add_planet(earth) solar_system.add_planet(pluto) + solar_system.add_planet(mercury) + solar_system.add_planet(venus) + solar_system.add_planet(mars) + solar_system.add_planet(jupiter) + solar_system.add_planet(saturn) + solar_system.add_planet(uranus) + solar_system.add_planet(neptune) puts "Welcome. Please choose from the following options: \n" options = "1: Add Planet \n2: Planet Details \n3: Exit \n" puts options - + # Start loop while input = gets.chomp.downcase - if input == "exit" || input == "3" + if input == "3" || input == "exit" break - elsif input == "1" || input == "add planet" # needs to be separate method - puts "Please enter details(name, color, mass_kg, distance_from_sun_km, fun_fact)" - # planet_details = gets.chomp # etc, make array or hash? - puts "Please enter planet name:" - planet_name = gets.chomp.capitalize - puts "Please enter planet color:" - planet_color = gets.chomp - puts "Please enter planet mass" - planet_mass = gets.chomp - puts "Please enter planet's distance from the sun:" - planet_distance = gets.chomp - puts "Please enter a fun fact about this planet:" - planet_fact = gets.chomp - new_planet = Planet.new(planet_name, planet_color, planet_mass, planet_distance, planet_fact) - solar_system.add_planet(new_planet) - - p solar_system - + elsif input == "1" || input == "add planet" + # Add Planet + add_new_planet(solar_system) puts "What would you like to do next?\n" puts options elsif input == "2" || input == "planet details" - puts "Please enter planet's name" - planet_name = gets.chomp - your_planet = solar_system.find_planet_by_name(planet_name) - puts your_planet.summary - + # Planet Details + show_planet_details(solar_system) puts "What would you like to do next?\n" puts options - end - + end end +end + +# Wave 3 method: Planet Details +def show_planet_details(solar_system) + # show list of planets? + puts "Please enter planet's name:\n" + puts solar_system.list_planets + planet_name = gets.chomp + your_planet = solar_system.find_planet_by_name(planet_name) + puts your_planet.summary +end +# Wave 3 method: Add Planet +def add_new_planet(solar_system) + puts "Please enter planet name:" + planet_name = gets.chomp.capitalize + puts "Please enter planet color:" + planet_color = gets.chomp + puts "Please enter planet mass" + planet_mass = gets.chomp + puts "Please enter planet's distance from the sun:" + planet_distance = gets.chomp + puts "Please enter a fun fact about this planet:" + planet_fact = gets.chomp + new_planet = Planet.new(planet_name, planet_color, planet_mass, planet_distance, planet_fact) + solar_system.add_planet(new_planet) end main \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb index 8f816838..99f008f1 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -14,7 +14,7 @@ def add_planet(planet) end def list_planets - # return (not puts) string containing a list of all the planets in the system. + # return string containing a list of all the planets in the system. string = "Planets orbiting #{star_name}\n" num = 0 planets.each do |item| From ae35c10352f08c8d429431bd29cedcbbdb2430cd Mon Sep 17 00:00:00 2001 From: Yolotzin Dunbar Date: Wed, 19 Feb 2020 21:31:07 -0800 Subject: [PATCH 5/5] Cleaned up comments and spacing --- planet.rb | 2 -- solar_system.rb | 7 +------ 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/planet.rb b/planet.rb index 6324ccb9..1d4097e1 100644 --- a/planet.rb +++ b/planet.rb @@ -1,5 +1,4 @@ class Planet - # readable, not writeable attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @@ -13,5 +12,4 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) def summary return "#{@name}: This planet is #{@color}-colored, is #{@mass_kg}kg, and is #{@distance_from_sun_km}km from the sun. Fun fact: #{@fun_fact}.\n" end - end \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb index 99f008f1..f6db2a00 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -1,5 +1,4 @@ class SolarSystem - # readable, not writeable attr_reader :star_name, :planets def initialize(star_name) @@ -7,7 +6,6 @@ def initialize(star_name) @planets = [] end - # methods def add_planet(planet) # take an instance of Planet as a parameter and add it to the list of planets. planets << planet @@ -27,17 +25,14 @@ def list_planets def find_planet_by_name(string) # takes the name of a planet as a string, and returns the corresponding instance of Planet string = string.downcase.capitalize - planets.each do |item| if item.name == string return item #.name end end - # cannot find planet scenario, needs to handle nil summary + # cannot find planet scenario if planets.include?(string) == false return "Planet not found" end - end - end \ No newline at end of file