From 6645bcf00f55bff3db60fe112a745692aa5e07cf Mon Sep 17 00:00:00 2001 From: MyriamWD Date: Mon, 25 Feb 2019 16:28:13 -0800 Subject: [PATCH 1/4] Create main.rb planet.rb solar_system.rb. Wave 2 --- lib/main.rb | 39 +++++++++++++++++++++++++++++++++++++++ lib/planet.rb | 30 ++++++++++++++++++++++++++++++ lib/solar_system.rb | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 lib/main.rb create mode 100644 lib/planet.rb create mode 100644 lib/solar_system.rb diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..f8af2dd5 --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,39 @@ +require_relative "planet" +require_relative "solar_system" + +def main + cybertron = Planet.new("Cybertron", "Silver", 10234, 890890, "Autobots don't live there anymore - see Transformers") + arrakis = Planet.new("Arrakis", "Orange", 52345234, 1243123, "It's not a planet of arrays - see Dune") + solar_system = SolarSystem.new("sol") + solar_system.add_planet(cybertron) + solar_system.add_planet(arrakis) + list = solar_system.list_planets + puts list + + # puts arrakis.name + # puts cybertron.fun_fact + # puts cybertron.summary +end + +# NameError: uninitialized constant SolarSystem +# What does this error mean? What do you need to do to fix it? +# It means that the require_relative "solar_system" is missing. reference? +# + +main + +#--------------------- +# Create two diferent instances of planet +# Print put some of the planet attributes +# Not necessary go simple +# # information = [{name: "", +# # color: "", +# # mass_kg: 0, +# # distance_from_sun_km: 0, +# # fun_fact: ""}] +# # information.each_with_index do |attribute, index| +# # puts "Please enter the planet's #{attribute.keys}" +# # information[index][attribute.keys] << gets.chomp +# # end + +# planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) diff --git a/lib/planet.rb b/lib/planet.rb new file mode 100644 index 00000000..6443f6a4 --- /dev/null +++ b/lib/planet.rb @@ -0,0 +1,30 @@ + +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 "In a galaxy far away (not start wars) + there is a planet called #{name}. It appears + to be #{color} and its mass is #{mass_kg}. + The distance between #{name} and the + sun is #{distance_from_sun_km} kms. + Something that you might not know is that #{fun_fact}." + end + + # Question: Why do we puts in main but not in Planet#summary? + # Because in Planet#summary we are modeling the behavior and state + # for the new instances of the class. In main we actually + # use the state and behavior +end + +#earth = Planet.new("Mars", "Red", 5.972e24, 1.496e8, "Only planet to support life") + +#puts earth.name diff --git a/lib/solar_system.rb b/lib/solar_system.rb new file mode 100644 index 00000000..33fbe88e --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,34 @@ + + +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + def add_planet(planet) # Elle's idea + @planets << planet # Elle's idea + # Create a method SolarSystem#add_planet, which will take an instance + # of Planet as a parameter and add it to the list of planets. + + # var = Planet.summary + # puts var + # @planets << var + end + + def list_planets + # which will return (not puts) a string containing a list of all the + # planets in the system. The string should be formatted in this style: + # ________ somehow print the index and the name of the planet _______ + string = "Planets orbitting #{star_name}\n" + @planets.each_with_index do |planet, index| + string += "#{index + 1}. #{planet.name}\n" + end + return string + end + + def find_planet_by_name + end +end From 14288aed54bd250ce0e2da760820e8ce291358ac Mon Sep 17 00:00:00 2001 From: MyriamWD Date: Mon, 25 Feb 2019 17:14:35 -0800 Subject: [PATCH 2/4] Find planet by name --- lib/main.rb | 8 ++++++++ lib/solar_system.rb | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index f8af2dd5..1a67a402 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,3 +1,4 @@ +require "awesome_print" require_relative "planet" require_relative "solar_system" @@ -10,6 +11,13 @@ def main list = solar_system.list_planets puts list + puts "So you want to find a planet, mmm... tell me the name, please?" + planet_name = gets.chomp.capitalize + found_planet = solar_system.find_planet_by_name(planet_name) + + puts found_planet + puts found_planet.summary + # puts arrakis.name # puts cybertron.fun_fact # puts cybertron.summary diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 33fbe88e..cd213093 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -29,6 +29,11 @@ def list_planets return string end - def find_planet_by_name + def find_planet_by_name(planet_name) + var = "" + @planets.each do |planet| + return planet if planet.name == planet_name + end + # return planet_name if @planets.include?(planet_name) end end From aba9259749696932b86d4132a7255e27b535e3d4 Mon Sep 17 00:00:00 2001 From: MyriamWD Date: Tue, 26 Feb 2019 14:01:12 -0800 Subject: [PATCH 3/4] Wave 3 -> steps 1 and 2 --- lib/main.rb | 44 +++++++++++++++++++++++++++++++++++--------- lib/solar_system.rb | 3 ++- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 1a67a402..aabbe6f1 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -3,21 +3,47 @@ require_relative "solar_system" def main + option = "" + # found_planet = [] + + solar_system_name = "Puppy Kingdom" + solar_system = SolarSystem.new(solar_system_name) cybertron = Planet.new("Cybertron", "Silver", 10234, 890890, "Autobots don't live there anymore - see Transformers") - arrakis = Planet.new("Arrakis", "Orange", 52345234, 1243123, "It's not a planet of arrays - see Dune") - solar_system = SolarSystem.new("sol") + arrakis = Planet.new("Arrakis", "Orange", 52345234, 1243123, "it is not a planet of arrays - see Dune") + pa = Planet.new("Pa", "Purple", 8999333, 555666890, "Apes RUlE!! - see... you know this one, don't you?") + planet_express = Planet.new("Planet-express", "Lot of them", 234, 12, "it is mailing company from the future.") solar_system.add_planet(cybertron) solar_system.add_planet(arrakis) - list = solar_system.list_planets - puts list + solar_system.add_planet(pa) + solar_system.add_planet(planet_express) + # list = solar_system.list_planets + # puts list - puts "So you want to find a planet, mmm... tell me the name, please?" - planet_name = gets.chomp.capitalize - found_planet = solar_system.find_planet_by_name(planet_name) + # puts found_planet - puts found_planet - puts found_planet.summary + while option != "4" + puts "What would you like to do next? + 1. List Planets + 2. Planet Details + 3. Add a Planet + 4. Exit " + option = gets.chomp.capitalize + case option + when "1" + list = solar_system.list_planets + puts list + gets.chomp + when "2" + puts "So you want to find a planet, mmm... tell me the name, please?" + planet_name = gets.chomp.capitalize + found_planet = solar_system.find_planet_by_name(planet_name) + puts found_planet.summary + when "3" + when "4" + break + end + end # puts arrakis.name # puts cybertron.fun_fact # puts cybertron.summary diff --git a/lib/solar_system.rb b/lib/solar_system.rb index cd213093..5aaa5a0d 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -22,6 +22,7 @@ def list_planets # which will return (not puts) a string containing a list of all the # planets in the system. The string should be formatted in this style: # ________ somehow print the index and the name of the planet _______ + string = "Planets orbitting #{star_name}\n" @planets.each_with_index do |planet, index| string += "#{index + 1}. #{planet.name}\n" @@ -30,7 +31,7 @@ def list_planets end def find_planet_by_name(planet_name) - var = "" + # var = "" @planets.each do |planet| return planet if planet.name == planet_name end From c682f10a84598f58356365199e75c819833cd0a9 Mon Sep 17 00:00:00 2001 From: MyriamWD Date: Wed, 27 Feb 2019 00:47:19 -0800 Subject: [PATCH 4/4] Wave 3 --- lib/main.rb | 94 ++++++++++++++++++++++++++------------------- lib/planet.rb | 14 +++---- lib/solar_system.rb | 41 ++++++++++++-------- 3 files changed, 84 insertions(+), 65 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index aabbe6f1..b339abc8 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -4,70 +4,86 @@ def main option = "" - # found_planet = [] - solar_system_name = "Puppy Kingdom" solar_system = SolarSystem.new(solar_system_name) cybertron = Planet.new("Cybertron", "Silver", 10234, 890890, "Autobots don't live there anymore - see Transformers") - arrakis = Planet.new("Arrakis", "Orange", 52345234, 1243123, "it is not a planet of arrays - see Dune") - pa = Planet.new("Pa", "Purple", 8999333, 555666890, "Apes RUlE!! - see... you know this one, don't you?") + arrakis = Planet.new("Arrakis", "Orange", 52345234, 12, "it is not a planet of arrays - see Dune") + pa = Planet.new("P-apes", "Purple", 8999333, 5, "Apes RUlE!! - see... you know this one, don't you?") planet_express = Planet.new("Planet-express", "Lot of them", 234, 12, "it is mailing company from the future.") solar_system.add_planet(cybertron) solar_system.add_planet(arrakis) solar_system.add_planet(pa) solar_system.add_planet(planet_express) - # list = solar_system.list_planets - # puts list - - # puts found_planet - - while option != "4" - puts "What would you like to do next? + puts "\n-------- Welcome to Planets --------" + while option != "5" + puts "\nWhat would you like to do next? 1. List Planets 2. Planet Details 3. Add a Planet - 4. Exit " + 4. Calculate Distance + 5. Exit " option = gets.chomp.capitalize case option when "1" list = solar_system.list_planets puts list + puts "\nEnter to continue..." gets.chomp when "2" - puts "So you want to find a planet, mmm... tell me the name, please?" + puts "\nPlease, tell me the name of the planet..." planet_name = gets.chomp.capitalize - found_planet = solar_system.find_planet_by_name(planet_name) - puts found_planet.summary + match = false + found_planet = solar_system.find_planet_by_name(planet_name, match) + if found_planet == false + puts "Planet not found :(" + else + puts found_planet.summary + end + puts "\nEnter to continue..." + gets.chomp when "3" + puts "\nPlease input the following information to create a planet..." + puts "- Name:" + name = gets.chomp.capitalize + puts "- Color:" + color = gets.chomp.downcase + puts "- Mass (Kg):" + mass = gets.chomp.to_f + while mass <= 0 + puts "Please enter a number higher than 0... + - Mass (Kg):" + mass = gets.chomp.to_f + end + puts "- Distance from #{solar_system_name} (Kms):" + distance_from_sun_km = gets.chomp.to_f + while distance_from_sun_km <= 0 + puts "Please enter a number higher than 0... + - Distance from #{solar_system_name} (Kms):" + distance_from_sun_km = gets.chomp.to_f + end + puts "- Fun Fact" + fun_fact = gets.chomp.downcase + planet = Planet.new(name, color, mass, distance_from_sun_km, fun_fact) + solar_system.add_planet(planet) + puts "\nEnter to continue..." + gets.chomp when "4" + puts "\nCalculate Planets Distance" + temporary_distance = 0 + 2.times do |i| + puts "What is the planet N #{i + 1}?" + planet_q = gets.chomp.capitalize + temporary_distance = (temporary_distance - solar_system.distance_between(planet_q)).abs + end + puts " The distance between the two planets is: #{temporary_distance}Kms" + puts "\nEnter to continue..." + gets.chomp + when "5" + puts "Bye..." break end end - # puts arrakis.name - # puts cybertron.fun_fact - # puts cybertron.summary end -# NameError: uninitialized constant SolarSystem -# What does this error mean? What do you need to do to fix it? -# It means that the require_relative "solar_system" is missing. reference? -# - main - -#--------------------- -# Create two diferent instances of planet -# Print put some of the planet attributes -# Not necessary go simple -# # information = [{name: "", -# # color: "", -# # mass_kg: 0, -# # distance_from_sun_km: 0, -# # fun_fact: ""}] -# # information.each_with_index do |attribute, index| -# # puts "Please enter the planet's #{attribute.keys}" -# # information[index][attribute.keys] << gets.chomp -# # end - -# planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) diff --git a/lib/planet.rb b/lib/planet.rb index 6443f6a4..4afed3cc 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -11,20 +11,16 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "In a galaxy far away (not start wars) + return "\nIn a galaxy far away (not start wars) there is a planet called #{name}. It appears to be #{color} and its mass is #{mass_kg}. The distance between #{name} and the sun is #{distance_from_sun_km} kms. Something that you might not know is that #{fun_fact}." end - - # Question: Why do we puts in main but not in Planet#summary? - # Because in Planet#summary we are modeling the behavior and state - # for the new instances of the class. In main we actually - # use the state and behavior end -#earth = Planet.new("Mars", "Red", 5.972e24, 1.496e8, "Only planet to support life") - -#puts earth.name +# Question: Why do we puts in main but not in Planet#summary? +# Because in Planet#summary we are modeling the behavior and state +# for the new instances of the class. In main we actually +# use the state and behavior diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 5aaa5a0d..c63e6605 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -8,33 +8,40 @@ def initialize(star_name) @planets = [] end - def add_planet(planet) # Elle's idea - @planets << planet # Elle's idea - # Create a method SolarSystem#add_planet, which will take an instance - # of Planet as a parameter and add it to the list of planets. - - # var = Planet.summary - # puts var - # @planets << var + def add_planet(planet) + @planets << planet end def list_planets - # which will return (not puts) a string containing a list of all the - # planets in the system. The string should be formatted in this style: - # ________ somehow print the index and the name of the planet _______ - - string = "Planets orbitting #{star_name}\n" + string = "\nPlanets orbitting #{star_name}\n" @planets.each_with_index do |planet, index| string += "#{index + 1}. #{planet.name}\n" end return string end - def find_planet_by_name(planet_name) - # var = "" + def find_planet_by_name(planet_name, match) + @planets.each do |planet| + # return planet.name == planet_name ? planet : false + # return planet if planet.name == planet_name + if planet.name == planet_name + match = true + return planet + + # else + # return + end + end + if match == false + return false + end + end + + def distance_between(planet_q) @planets.each do |planet| - return planet if planet.name == planet_name + if planet.name == planet_q + return planet.distance_from_sun_km + end end - # return planet_name if @planets.include?(planet_name) end end