From c223b36c8b873b8cb370639281b2ad790c7e1e49 Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Mon, 19 Aug 2019 17:23:41 -0700 Subject: [PATCH 1/5] Wave 1 --- main.rb | 14 ++++++++++++++ planet.rb | 25 +++++++++++++++++++++++++ test.rb | 19 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 main.rb create mode 100644 planet.rb create mode 100644 test.rb diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..d671d684 --- /dev/null +++ b/main.rb @@ -0,0 +1,14 @@ +require_relative 'planet' + +# manually enters planet information and creates new intances of Planet class +def main + planet_1 = Planet.new("earth", "blue", 34, 23409, "It's made of bleu cheese") + + planet_2 = Planet.new("mars", "orange", 790, 704307, "It's made of mustard") + + return "#{planet_1.summary}\n #{planet_2.summary}" + +end + +# puts information for the planets +puts main \ No newline at end of file diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..3c380397 --- /dev/null +++ b/planet.rb @@ -0,0 +1,25 @@ +# creates a class +class Planet + + # reader attributes + attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + + # constructor + 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 + + # prints self + def print_self + print "#{self}" + end + + # writes summary + def summary + return "Let's learn about #{name}. It is #{color}, it has a mass of #{mass_kg}, and it orbits #{distance_from_sun_km} kilometers from the sun. Fun fact: #{fun_fact}. " + end +end \ No newline at end of file diff --git a/test.rb b/test.rb new file mode 100644 index 00000000..afec9061 --- /dev/null +++ b/test.rb @@ -0,0 +1,19 @@ +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' + +require_relative '../main.rb' +require_relative '..planet.rb' + +# Get that nice colorized output +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe 'planets' do + describe 'attributes entered' do + # checks that the name has at least 1 letter + # checks that the color has only letters + # checks that fun fact has an input + # checks that mass is not negative + # checks that the distance from the sun is not negative + end +end From f61adb0167fe411b129de194d8d22270390d4992 Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Tue, 20 Aug 2019 14:07:36 -0700 Subject: [PATCH 2/5] Wave 1 revised, initial Wave 2 --- main.rb | 42 +++++++++++++++++++++++++++++++++++------- planet.rb | 18 +++++++----------- solar_system.rb | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 solar_system.rb diff --git a/main.rb b/main.rb index d671d684..203ceb9e 100644 --- a/main.rb +++ b/main.rb @@ -1,14 +1,42 @@ +# WAVE 1 require_relative 'planet' +require_relative 'solar_system' -# manually enters planet information and creates new intances of Planet class -def main - planet_1 = Planet.new("earth", "blue", 34, 23409, "It's made of bleu cheese") +# WAVE 1 + +def main # Creates solar system and populates it + solar_system_1 = SolarSystem.new("Red Dwarf") + + planet_1 = Planet.new(name: "Din", color: "Pink", mass_kg: 237923789, distance_from_sun_km: 3278997897823575978987, fun_fact: "It's made out of cotton candy") + + solar_system_1.add_planet(planet_1) + + planet_2 = Planet.new(name: "Minnie", color: "orange", mass_kg: 780978, distance_from_sun_km: 9877984531234354, fun_fact: "It's made out of mustard") - planet_2 = Planet.new("mars", "orange", 790, 704307, "It's made of mustard") + solar_system_1.add_planet(planet_2) + + # displays the planet summeries + solar_system_1.planets.each do |planet| + puts planet.summary + end + + # displays the solar system list for the user + solar_system_1.list_planets.each do |line| + puts line + end + + print "Do you want to look up a planet? (Y or N): " + lookup = gets.chomp.upcase + if lookup == "Y" + planet_search = nil + print "Enter the name of the planet you want to search for: " + planet_search = gets.chomp.upcase + puts solar_system_1.find_planet_by_name(planet_search) + + end - return "#{planet_1.summary}\n #{planet_2.summary}" end -# puts information for the planets -puts main \ No newline at end of file + +main diff --git a/planet.rb b/planet.rb index 3c380397..519f615f 100644 --- a/planet.rb +++ b/planet.rb @@ -5,21 +5,17 @@ class Planet attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact # constructor - 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 + # here the initial attributes are + def initialize(name:, color:, mass_kg:, distance_from_sun_km:, fun_fact:) + @name = name.upcase + @color = color.downcase + @mass_kg = mass_kg.to_b + @distance_from_sun_km = distance_from_sun_km.to_i @fun_fact = fun_fact end - # prints self - def print_self - print "#{self}" - end - # writes summary def summary - return "Let's learn about #{name}. It is #{color}, it has a mass of #{mass_kg}, and it orbits #{distance_from_sun_km} kilometers from the sun. Fun fact: #{fun_fact}. " + return "Let's learn about #{@name.capitalize}. It is #{@color}, it has a mass of #{@mass_kg} kg, and it orbits #{@distance_from_sun_km} kilometers from the sun. Fun fact: #{@fun_fact}. " end end \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..d693248a --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,32 @@ +class SolarSystem + + attr_reader :star_name, :planets + + def initialize (star_name) + @star_name = star_name + @planets = [] + end + + def add_planet (planet) + @planets.push planet + end + + def list_planets + place = @planets.map.with_index do |planet, index| + "#{index + 1}. #{planet.name.capitalize}" + end + return place + end + + def find_planet_by_name(planet_name) + planet_name.upcase + @planets.each do |planet| + if planet_name.upcase == planet.name.upcase + return planet.summary + else + return "I couldn't find that planet, hmm." + end + end + end + +end From b6dbcb56310cd7894df5e805354b6491246c411d Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Tue, 20 Aug 2019 17:24:18 -0700 Subject: [PATCH 3/5] Modified Wave 2 --- main.rb | 6 ------ planet.rb | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/main.rb b/main.rb index 203ceb9e..efa91f68 100644 --- a/main.rb +++ b/main.rb @@ -1,9 +1,6 @@ -# WAVE 1 require_relative 'planet' require_relative 'solar_system' -# WAVE 1 - def main # Creates solar system and populates it solar_system_1 = SolarSystem.new("Red Dwarf") @@ -32,11 +29,8 @@ def main # Creates solar system and populates it print "Enter the name of the planet you want to search for: " planet_search = gets.chomp.upcase puts solar_system_1.find_planet_by_name(planet_search) - end - end - main diff --git a/planet.rb b/planet.rb index 519f615f..ce426a09 100644 --- a/planet.rb +++ b/planet.rb @@ -9,7 +9,7 @@ class Planet def initialize(name:, color:, mass_kg:, distance_from_sun_km:, fun_fact:) @name = name.upcase @color = color.downcase - @mass_kg = mass_kg.to_b + @mass_kg = mass_kg.to_i @distance_from_sun_km = distance_from_sun_km.to_i @fun_fact = fun_fact end From ec345d071ec036e023f10144bb09bf146aa08a75 Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Tue, 20 Aug 2019 21:22:22 -0700 Subject: [PATCH 4/5] Wave 3 --- main.rb | 65 +++++++++++++++++++++++++++++++++++++++---------- planet.rb | 1 + solar_system.rb | 7 ++++-- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/main.rb b/main.rb index efa91f68..a79bed1c 100644 --- a/main.rb +++ b/main.rb @@ -2,6 +2,9 @@ require_relative 'solar_system' def main # Creates solar system and populates it + + loop_options = ["list planets", "planet details", "add planet", "exit"] + solar_system_1 = SolarSystem.new("Red Dwarf") planet_1 = Planet.new(name: "Din", color: "Pink", mass_kg: 237923789, distance_from_sun_km: 3278997897823575978987, fun_fact: "It's made out of cotton candy") @@ -12,23 +15,59 @@ def main # Creates solar system and populates it solar_system_1.add_planet(planet_2) - # displays the planet summeries - solar_system_1.planets.each do |planet| - puts planet.summary + puts "What would you like to do next?" + loop_options.each do |option| + puts "\t #{option}" + end + + def planet_details + print "Enter the name of the planet you want to search for: " + planet_search_term = gets.chomp.upcase + return_statement = find_planet_by_name(planet_search_term) + return return_statement end - # displays the solar system list for the user - solar_system_1.list_planets.each do |line| - puts line + def add_planet_manually + print "What is the name of the planet you want to enter: " + name_manual = gets.chomp + print "What is its color? " + color_manual = gets.chomp + print "What is its mass: " + mass_manual = gets.chomp.to_i + print "What is its distance from the sun (km): " + distance_from_sun_km_manual = gets.chomp.to_i + print "What's a fun fact about this planet? " + fun_fact_manual = gets.chomp + + user_planet = Planet.new(name: "#{name_manual}", color: "#{color_manual}", mass_kg: "#{mass_manual}", distance_from_sun_km: "#{distance_from_sun_km_manual}", fun_fact: "#{fun_fact_manual}") + + return user_planet + end - print "Do you want to look up a planet? (Y or N): " - lookup = gets.chomp.upcase - if lookup == "Y" - planet_search = nil - print "Enter the name of the planet you want to search for: " - planet_search = gets.chomp.upcase - puts solar_system_1.find_planet_by_name(planet_search) + eval = gets.chomp.downcase + until eval == "exit" do + if eval == "list planets" + solar_system_1.list_planets.each do |line| + puts line + end + eval = nil + elsif eval == "planet details" + puts solar_system_1.planet_details + eval = nil + elsif eval == "add planet" + solar_system_1.add_planet(add_planet_manually) + solar_system_1.list_planets.each do |line| + puts line + end + eval = nil + else + puts "What would you like to do next? " + loop_options.each do |option| + puts "\t #{option}" + end + eval = gets.chomp.downcase + end end end diff --git a/planet.rb b/planet.rb index ce426a09..fe3d9ad8 100644 --- a/planet.rb +++ b/planet.rb @@ -18,4 +18,5 @@ def initialize(name:, color:, mass_kg:, distance_from_sun_km:, fun_fact:) def summary return "Let's learn about #{@name.capitalize}. It is #{@color}, it has a mass of #{@mass_kg} kg, and it orbits #{@distance_from_sun_km} kilometers from the sun. Fun fact: #{@fun_fact}. " end + end \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb index d693248a..223f5295 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -23,10 +23,13 @@ def find_planet_by_name(planet_name) @planets.each do |planet| if planet_name.upcase == planet.name.upcase return planet.summary - else - return "I couldn't find that planet, hmm." end end end + + + + + end From 64dd9ed10507ad61013768c42040ccede0660b80 Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Tue, 20 Aug 2019 22:26:54 -0700 Subject: [PATCH 5/5] Refactor for clarity --- main.rb | 28 ++++++++++++++++++++-------- planet.rb | 4 ++-- solar_system.rb | 7 +------ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/main.rb b/main.rb index a79bed1c..e4a238c1 100644 --- a/main.rb +++ b/main.rb @@ -3,8 +3,10 @@ def main # Creates solar system and populates it + # These loop options will be used later in the method to list for the user what they can do loop_options = ["list planets", "planet details", "add planet", "exit"] + # Create and populate a solar system with 2 planets solar_system_1 = SolarSystem.new("Red Dwarf") planet_1 = Planet.new(name: "Din", color: "Pink", mass_kg: 237923789, distance_from_sun_km: 3278997897823575978987, fun_fact: "It's made out of cotton candy") @@ -15,11 +17,8 @@ def main # Creates solar system and populates it solar_system_1.add_planet(planet_2) - puts "What would you like to do next?" - loop_options.each do |option| - puts "\t #{option}" - end - + # method that calls "find_planet_by_name" method from solar_system.rb + # placed here because it needs to access to previously entered information def planet_details print "Enter the name of the planet you want to search for: " planet_search_term = gets.chomp.upcase @@ -27,6 +26,8 @@ def planet_details return return_statement end + # method that manually enters planets into the previously established solar_system + # placed here because it needs to access to previously entered information def add_planet_manually print "What is the name of the planet you want to enter: " name_manual = gets.chomp @@ -45,17 +46,28 @@ def add_planet_manually end + # Asks user what to pursue next using the options from LN 6 + # Calls secondary methods depending on the user input + puts "What would you like to do next?" + loop_options.each do |option| + puts "\t #{option}" + end eval = gets.chomp.downcase + + # Creates a loop that traps the user until they enter "exit" + # The user can enter multiple planets manually + # The planets previously entered by the user are available in subsequent loops until eval == "exit" do - if eval == "list planets" + case eval + when "list planets" solar_system_1.list_planets.each do |line| puts line end eval = nil - elsif eval == "planet details" + when "planet details" puts solar_system_1.planet_details eval = nil - elsif eval == "add planet" + when "add planet" solar_system_1.add_planet(add_planet_manually) solar_system_1.list_planets.each do |line| puts line diff --git a/planet.rb b/planet.rb index fe3d9ad8..fa6d309b 100644 --- a/planet.rb +++ b/planet.rb @@ -16,7 +16,7 @@ def initialize(name:, color:, mass_kg:, distance_from_sun_km:, fun_fact:) # writes summary def summary - return "Let's learn about #{@name.capitalize}. It is #{@color}, it has a mass of #{@mass_kg} kg, and it orbits #{@distance_from_sun_km} kilometers from the sun. Fun fact: #{@fun_fact}. " + return "Let's learn about #{@name.capitalize}. It is #{@color}, it has a mass of #{@mass_kg} kg, and it orbits #{@distance_from_sun_km} kilometers from the sun. Fun fact: #{@fun_fact}. \n" end -end \ No newline at end of file +end diff --git a/solar_system.rb b/solar_system.rb index 223f5295..825860cb 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -13,7 +13,7 @@ def add_planet (planet) def list_planets place = @planets.map.with_index do |planet, index| - "#{index + 1}. #{planet.name.capitalize}" + "#{ index + 1 }. #{planet.name.capitalize}" end return place end @@ -27,9 +27,4 @@ def find_planet_by_name(planet_name) end end - - - - - end