From 7f333314c546dd94c8bfbe1556b2b0e4f433bf21 Mon Sep 17 00:00:00 2001 From: Laura Date: Tue, 21 Aug 2018 13:27:48 -0700 Subject: [PATCH 1/4] wave1 --- main.rb | 9 +++++++++ planet.rb | 15 +++++++++++++++ 2 files changed, 24 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..34e6294f --- /dev/null +++ b/main.rb @@ -0,0 +1,9 @@ +require_relative 'planet' + +def main + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + mercury = Planet.new('Mercury', "grey", 5.343242, 2.2342, "planet closest to the sun") + puts earth.summary +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..d3aed710 --- /dev/null +++ b/planet.rb @@ -0,0 +1,15 @@ +class Planet + 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 + attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + + def summary + return "planet #{name} has a #{color} color, a mass of #{mass_kg}. It's #{distance_from_sun_km} away from the sun, and fun fact: #{fun_fact}" + end + +end From aaa3cba7a13d2efbb42b941e90d787527076760e Mon Sep 17 00:00:00 2001 From: Laura Date: Tue, 21 Aug 2018 14:04:54 -0700 Subject: [PATCH 2/4] wave2 --- main.rb | 10 ++++++++-- solar_system.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 solar_system.rb diff --git a/main.rb b/main.rb index 34e6294f..8d96a0f8 100644 --- a/main.rb +++ b/main.rb @@ -1,9 +1,15 @@ require_relative 'planet' +require_relative 'solar_system' +require 'pry' def main earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') - mercury = Planet.new('Mercury', "grey", 5.343242, 2.2342, "planet closest to the sun") - puts earth.summary + solar_system = SolarSystem.new('Sol') + solar_system.add_planet(earth) + list = solar_system.list_planets + found_planet = solar_system.find_planet_by_name('Earth') + puts found_planet + puts found_planet.summary end main diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..5bda19f7 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,30 @@ +require 'pry' +class SolarSystem + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + attr_reader :star_name, :planets + + def add_planet(planet) + @planets << planet + end + + def list_planets + string = "Planets orbiting #{@star_name}: \n" + list = planets.map.with_index do |planet, index| + "#{index+1}. #{planet.name}\n" + end + return string + list.join + end + + def find_planet_by_name(target_planet) + target_planet = planets.map do |planet| + if planet.name.upcase == target_planet.upcase + planet + end + end + return target_planet[0] + end +end From 14d99816abc50335933594d981bc06de2f39e3f7 Mon Sep 17 00:00:00 2001 From: Laura Date: Wed, 22 Aug 2018 11:31:40 -0700 Subject: [PATCH 3/4] fixed nil error in find_planet when having more than one planet by changing .map to .select --- main.rb | 67 +++++++++++++++++++++++++++++++++++++++++++++---- planet.rb | 1 + solar_system.rb | 9 ++----- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/main.rb b/main.rb index 8d96a0f8..6e245e3a 100644 --- a/main.rb +++ b/main.rb @@ -3,13 +3,70 @@ require 'pry' def main - earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') solar_system = SolarSystem.new('Sol') + 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 - found_planet = solar_system.find_planet_by_name('Earth') - puts found_planet - puts found_planet.summary + + puts "What would you like to do? (A/B/C/D)" + puts "option A: list plannets" + puts "option B: exit" + puts "option C: planet details" + puts "option D: add planet" + decision = gets.chomp + if decision.upcase == "C" + puts "which planet would you like to learn more about?" + planet = gets.chomp + found_planet = solar_system.find_planet_by_name(planet) + puts found_planet.summary + elsif decision.upcase == "D" + puts "So, you want to add a planet, I need to know more about it." + puts "Name?" + name = gets.chomp + puts "color?" + color = gets.chomp + puts "mass?" + mass = gets.chomp + puts "distance_from_sun_km?" + distance = gets.chomp + puts "fun_fact?" + fun_fact = gets.chomp + planet = Planet.new(name, color, mass, distance, fun_fact) + solar_system.add_planet(planet) + elsif decision.upcase == "A" + list = solar_system.list_planets + puts list + end + until decision.upcase == "B" + puts "What would you like to do? (A/B/C/D)" + puts "option A: list plannets" + puts "option B: exit" + puts "option C: planet details" + puts "option D: add planet" + decision = gets.chomp + if decision.upcase == "C" + puts "which planet would you like to learn more about?" + planet = gets.chomp + found_planet = solar_system.find_planet_by_name(planet) + puts found_planet.summary + elsif decision.upcase == "D" + puts "So, you want to add a planet, I need to know more about it." + puts "Name?" + name = gets.chomp + puts "color?" + color = gets.chomp + puts "mass?" + mass = gets.chomp + puts "distance_from_sun_km?" + distance = gets.chomp + puts "fun_fact?" + fun_fact = gets.chomp + planet = Planet.new(name, color, mass, distance, fun_fact) + solar_system.add_planet(planet) + elsif decision.upcase == "A" + list = solar_system.list_planets + puts list + end + end end main diff --git a/planet.rb b/planet.rb index d3aed710..28e759b0 100644 --- a/planet.rb +++ b/planet.rb @@ -1,3 +1,4 @@ +require 'pry' class Planet def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @name = name diff --git a/solar_system.rb b/solar_system.rb index 5bda19f7..e1492505 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -18,13 +18,8 @@ def list_planets end return string + list.join end - + # map adds a nill if it does not exist def find_planet_by_name(target_planet) - target_planet = planets.map do |planet| - if planet.name.upcase == target_planet.upcase - planet - end - end - return target_planet[0] + planets.select { |planet| return planet if planet.name.upcase == target_planet.upcase } end end From 3bc8c81d94fe4897c94592d0b39646e1f371c2ef Mon Sep 17 00:00:00 2001 From: Laura Date: Wed, 22 Aug 2018 11:38:39 -0700 Subject: [PATCH 4/4] refactored main.rb --- main.rb | 39 ++++++++------------------------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/main.rb b/main.rb index 6e245e3a..2f467dec 100644 --- a/main.rb +++ b/main.rb @@ -13,41 +13,14 @@ def main puts "option C: planet details" puts "option D: add planet" decision = gets.chomp - if decision.upcase == "C" - puts "which planet would you like to learn more about?" - planet = gets.chomp - found_planet = solar_system.find_planet_by_name(planet) - puts found_planet.summary - elsif decision.upcase == "D" - puts "So, you want to add a planet, I need to know more about it." - puts "Name?" - name = gets.chomp - puts "color?" - color = gets.chomp - puts "mass?" - mass = gets.chomp - puts "distance_from_sun_km?" - distance = gets.chomp - puts "fun_fact?" - fun_fact = gets.chomp - planet = Planet.new(name, color, mass, distance, fun_fact) - solar_system.add_planet(planet) - elsif decision.upcase == "A" - list = solar_system.list_planets - puts list - end - until decision.upcase == "B" - puts "What would you like to do? (A/B/C/D)" - puts "option A: list plannets" - puts "option B: exit" - puts "option C: planet details" - puts "option D: add planet" - decision = gets.chomp + while decision.upcase != "B" if decision.upcase == "C" puts "which planet would you like to learn more about?" planet = gets.chomp found_planet = solar_system.find_planet_by_name(planet) puts found_planet.summary + puts "What would you like to do? (A/B/C/D)\noption A: list plannets\noption B: exit\noption C: planet details\noption D: add planet" + decision = gets.chomp elsif decision.upcase == "D" puts "So, you want to add a planet, I need to know more about it." puts "Name?" @@ -56,15 +29,19 @@ def main color = gets.chomp puts "mass?" mass = gets.chomp - puts "distance_from_sun_km?" + puts "distance from sun in km?" distance = gets.chomp puts "fun_fact?" fun_fact = gets.chomp planet = Planet.new(name, color, mass, distance, fun_fact) solar_system.add_planet(planet) + puts "What would you like to do? (A/B/C/D)\noption A: list plannets\noption B: exit\noption C: planet details\noption D: add planet" + decision = gets.chomp elsif decision.upcase == "A" list = solar_system.list_planets puts list + puts "What would you like to do? (A/B/C/D)\noption A: list plannets\noption B: exit\noption C: planet details\noption D: add planet" + decision = gets.chomp end end end