From 04739bb54c891f9382c7fd4742e3e41537d8af9d Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Mon, 25 Feb 2019 14:04:45 -0800 Subject: [PATCH 01/11] Created planet.rb and made a planet class. --- planet.rb | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 planet.rb diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..cabb01f8 --- /dev/null +++ b/planet.rb @@ -0,0 +1,3 @@ +class Planet + +end \ No newline at end of file From d764542d65a870caa9b6ba839446db45f59d08f9 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Mon, 25 Feb 2019 14:15:36 -0800 Subject: [PATCH 02/11] =?UTF-8?q?Created=20constructor=20method=20(initial?= =?UTF-8?q?ize)=20for=20the=20Planet=20class.=20Set=20all=20instance=20var?= =?UTF-8?q?iables=20to=20readable=20only.=E2=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- planet.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/planet.rb b/planet.rb index cabb01f8..a29edb2f 100644 --- a/planet.rb +++ b/planet.rb @@ -1,3 +1,11 @@ class Planet - -end \ No newline at end of file + 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 +end From acfd41b48bb4a3d6616d29143832accdcff1a154 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Mon, 25 Feb 2019 14:27:41 -0800 Subject: [PATCH 03/11] Created main.rb. Defined main method. --- main.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 main.rb diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..fdf69289 --- /dev/null +++ b/main.rb @@ -0,0 +1,6 @@ +require_relative "planet.rb" + +def main +end + +main From c5d735e65c9410485d002804736420f11d2938bd Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Mon, 25 Feb 2019 14:48:17 -0800 Subject: [PATCH 04/11] Wrote code so that the main method creates two instances of Planet and prints some of their attributes. Hard coded, for testing functionality. --- main.rb | 7 ++++++- planet.rb | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/main.rb b/main.rb index fdf69289..c68cd71c 100644 --- a/main.rb +++ b/main.rb @@ -1,6 +1,11 @@ require_relative "planet.rb" def main + jupiter = Planet.new("Jupiter", "reddish", 578944636, 75436.32, "This planet has been a planet since the big bang.") + + saturn = Planet.new("Saturn", "multi-colored striped", 39435845, 222343, "This planet has rings of gas, dust and rock.") + + return "#{jupiter.name} is #{jupiter.color}. #{jupiter.fun_fact} #{saturn.name} is #{saturn.color}. #{saturn.fun_fact} " end -main +puts main diff --git a/planet.rb b/planet.rb index a29edb2f..7f079892 100644 --- a/planet.rb +++ b/planet.rb @@ -8,4 +8,7 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @distance_from_sun_km = distance_from_sun_km @fun_fact = fun_fact end + + def summary + end end From 1a2f0546c1416e48617a5d24d6ac73849cdce44c Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Mon, 25 Feb 2019 15:01:45 -0800 Subject: [PATCH 05/11] Wave 1, Step 4. Added summary method to planet.rb and invoke it in main.rb. Main.rb now outputs a description of the two planets. --- main.rb | 2 +- planet.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/main.rb b/main.rb index c68cd71c..4ed89f78 100644 --- a/main.rb +++ b/main.rb @@ -5,7 +5,7 @@ def main saturn = Planet.new("Saturn", "multi-colored striped", 39435845, 222343, "This planet has rings of gas, dust and rock.") - return "#{jupiter.name} is #{jupiter.color}. #{jupiter.fun_fact} #{saturn.name} is #{saturn.color}. #{saturn.fun_fact} " + return "#{jupiter.summary} #{saturn.summary}" end puts main diff --git a/planet.rb b/planet.rb index 7f079892..df07dbfa 100644 --- a/planet.rb +++ b/planet.rb @@ -10,5 +10,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary + return "#{name} is #{color}. #{fun_fact} Its mass is #{mass_kg} kg and it is #{distance_from_sun_km} km from the sun." end end From eb54ece45d077451c37a40d69b73b7d13d2f1a27 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Mon, 25 Feb 2019 16:28:05 -0800 Subject: [PATCH 06/11] Wave 1, Setp 4. Created a class SolarSystem in new file. Print a list of planets in a given solar system. --- main.rb | 9 +++++++++ planet.rb | 8 ++++++++ solar_system.rb | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 solar_system.rb diff --git a/main.rb b/main.rb index 4ed89f78..ca3dce06 100644 --- a/main.rb +++ b/main.rb @@ -1,11 +1,20 @@ require_relative "planet.rb" +require_relative "solar_system.rb" def main jupiter = Planet.new("Jupiter", "reddish", 578944636, 75436.32, "This planet has been a planet since the big bang.") saturn = Planet.new("Saturn", "multi-colored striped", 39435845, 222343, "This planet has rings of gas, dust and rock.") + solar_system_1 = SolarSystem.new("Sol") + solar_system_1.add_planet(jupiter) + solar_system_1.add_planet(saturn) + + list = solar_system_1.list_planets + puts list + return "#{jupiter.summary} #{saturn.summary}" end puts main + diff --git a/planet.rb b/planet.rb index df07dbfa..c84149c1 100644 --- a/planet.rb +++ b/planet.rb @@ -7,6 +7,14 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @mass_kg = mass_kg @distance_from_sun_km = distance_from_sun_km @fun_fact = fun_fact + + unless mass_kg > 0 + raise ArgumentError, "Mass must be greater than zero." + end + + unless distance_from_sun_km > 0 + raise ArgumentError, "Distance from Sun must be greater than zero." + end end def summary diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..c06c9144 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,21 @@ +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = [] + end + + def add_planet(planet) + @planets << planet + end + + def list_planets + planet_list = "Planets orbiting #{@star_name}\n" + @planets.each_with_index do |item, index| + planet_list += "#{index + 1}. #{item.name}\n" + end + + return planet_list + end +end From 4652a17aa39fc7001ef55e6d21ca6288f99c20b9 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Tue, 26 Feb 2019 20:51:19 -0800 Subject: [PATCH 07/11] Wave 2, step 2 done. Implemented control loop, lets user choose a planet to learn about. --- main.rb | 32 ++++++++++++++++++++++++++++---- solar_system.rb | 8 ++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/main.rb b/main.rb index ca3dce06..1b01737c 100644 --- a/main.rb +++ b/main.rb @@ -6,15 +6,39 @@ def main saturn = Planet.new("Saturn", "multi-colored striped", 39435845, 222343, "This planet has rings of gas, dust and rock.") + largo = Planet.new("Largo", "fushia", 4332, 435, "This planet is bright and sparsely populated. Life is not carbon-based.") + + office_space = Planet.new("Office Space", "blue-grey", 4534534, 66685995, "Residents of this planet must submit one TPS Report every Friday. Visitors may be required to work on the weekend.") + + playground = Planet.new("Playground", "orange", 686869939393, 8899494, "This planet is located at the edge of the universe.") + solar_system_1 = SolarSystem.new("Sol") solar_system_1.add_planet(jupiter) solar_system_1.add_planet(saturn) + solar_system_1.add_planet(largo) + solar_system_1.add_planet(office_space) + solar_system_1.add_planet(playground) list = solar_system_1.list_planets - puts list + user_cont_choice = "LIST PLANETS" + until user_cont_choice != "LIST PLANETS" + puts "Hello! What would you like to do? You can LIST PLANETS or EXIT." + user_cont_choice = gets.chomp.upcase + if user_cont_choice.upcase == "EXIT" + break + end + puts list + puts "\nWhich planet would you like to learn about? (enter its name)" + user_planet_choice = gets.chomp + user_choice_planet = solar_system_1.find_planet_by_name(user_planet_choice) + puts user_choice_planet.summary + puts "-------" + end - return "#{jupiter.summary} #{saturn.summary}" + found_planet = solar_system_1.find_planet_by_name("Jupiter") + # puts found_planet.name + # puts found_planet.summary + # return "#{jupiter.summary} #{saturn.summary} #{playground.summary}" end -puts main - +main diff --git a/solar_system.rb b/solar_system.rb index c06c9144..66ab79f1 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -18,4 +18,12 @@ def list_planets return planet_list end + + def find_planet_by_name(planet_name) + @planets.each do |this_planet| + if this_planet.name.downcase == planet_name.downcase + return this_planet + end + end + end end From caad24afe0ab8c5f7d2acbe5e85ebd8661260c09 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Wed, 27 Feb 2019 07:07:30 -0800 Subject: [PATCH 08/11] Added a planet option to the control loop. Base-level functionality only to incorporate this option. Need to accept details for the planet. --- main.rb | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/main.rb b/main.rb index 1b01737c..c0490777 100644 --- a/main.rb +++ b/main.rb @@ -20,25 +20,30 @@ def main solar_system_1.add_planet(playground) list = solar_system_1.list_planets - user_cont_choice = "LIST PLANETS" - until user_cont_choice != "LIST PLANETS" - puts "Hello! What would you like to do? You can LIST PLANETS or EXIT." - user_cont_choice = gets.chomp.upcase - if user_cont_choice.upcase == "EXIT" - break + user_choice = "LIST PLANETS" + until user_choice == "EXIT" + puts "Hello! What would you like to do? You can LIST PLANETS, ADD A PLANET or EXIT." + user_choice = gets.chomp.upcase + if user_choice.upcase == "EXIT" + break #<-- TRY CHANGING THIS TO EXIT + end + if user_choice.upcase == "LIST PLANETS" + puts list + puts "\nWhich planet would you like to learn about? (enter its name)" + user_planet_choice = gets.chomp + user_choice_planet = solar_system_1.find_planet_by_name(user_planet_choice) + puts user_choice_planet.summary + puts "-------" + elsif user_choice.upcase == "ADD A PLANET" + user_added_planet = gets.chomp + puts "You added this planet: #{user_added_planet}" end - puts list - puts "\nWhich planet would you like to learn about? (enter its name)" - user_planet_choice = gets.chomp - user_choice_planet = solar_system_1.find_planet_by_name(user_planet_choice) - puts user_choice_planet.summary - puts "-------" end - found_planet = solar_system_1.find_planet_by_name("Jupiter") + # found_planet = solar_system_1.find_planet_by_name("Jupiter") # puts found_planet.name # puts found_planet.summary # return "#{jupiter.summary} #{saturn.summary} #{playground.summary}" end -main +puts main From 040775c4e260f6db87ced1689c84f73ba684a50f Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Wed, 27 Feb 2019 08:02:22 -0800 Subject: [PATCH 09/11] Wave 3, step 3. User can crete a new instance of Planet by entering details about it. --- main.rb | 10 ++++++---- solar_system.rb | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/main.rb b/main.rb index c0490777..be051cde 100644 --- a/main.rb +++ b/main.rb @@ -20,12 +20,12 @@ def main solar_system_1.add_planet(playground) list = solar_system_1.list_planets - user_choice = "LIST PLANETS" + user_choice = "" until user_choice == "EXIT" puts "Hello! What would you like to do? You can LIST PLANETS, ADD A PLANET or EXIT." user_choice = gets.chomp.upcase if user_choice.upcase == "EXIT" - break #<-- TRY CHANGING THIS TO EXIT + exit end if user_choice.upcase == "LIST PLANETS" puts list @@ -35,8 +35,10 @@ def main puts user_choice_planet.summary puts "-------" elsif user_choice.upcase == "ADD A PLANET" - user_added_planet = gets.chomp - puts "You added this planet: #{user_added_planet}" + new_planet = solar_system_1.add_planet_from_user_input + solar_system_1.planets << new_planet + else + puts "Oops, that's an invalid input! Please try again." end end diff --git a/solar_system.rb b/solar_system.rb index 66ab79f1..de155028 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -1,5 +1,6 @@ class SolarSystem - attr_reader :star_name, :planets + attr_reader :star_name + attr_accessor :planets def initialize(star_name) @star_name = star_name @@ -26,4 +27,24 @@ def find_planet_by_name(planet_name) end end end + + def add_planet_from_user_input + puts "Please enter some details about this planet." + print "Name: " + @name = gets.chomp.capitalize + print "Color: " + @color = gets.chomp + print "Mass in kilograms: " + @mass_kg = gets.to_i + print "Distance from its sun: " + @distance_from_sun_km = gets.to_i + puts "Please enter one fun fact about this planet." + @fun_fact = gets.chomp + + return user_added_planet = Planet.new(@name, @color, @mass_kg, @distance_from_sun_km, @fun_fact) + # add_planet(user_added_planet) + # @planets << planet + # return user_added_planet + # puts "You added this planet: #{user_added_planet.name}" + end end From 5d3a913423db86bc7eff9c1601ac3a75dc070895 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Wed, 27 Feb 2019 08:55:54 -0800 Subject: [PATCH 10/11] Fixed user control loop to update list after user adds a planet (added main.rb. --- main.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/main.rb b/main.rb index be051cde..9ebe58ef 100644 --- a/main.rb +++ b/main.rb @@ -37,6 +37,7 @@ def main elsif user_choice.upcase == "ADD A PLANET" new_planet = solar_system_1.add_planet_from_user_input solar_system_1.planets << new_planet + list = solar_system_1.list_planets else puts "Oops, that's an invalid input! Please try again." end From 53f8f3b50efc582b3b6ed51f727ef3fc4ccc716b Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Wed, 27 Feb 2019 10:26:13 -0800 Subject: [PATCH 11/11] Put planets back into attr_reader. Had moved it to an accessor for troubleshooting. Just fixing this detail. --- solar_system.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solar_system.rb b/solar_system.rb index de155028..5beec0d4 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -1,6 +1,5 @@ class SolarSystem - attr_reader :star_name - attr_accessor :planets + attr_reader :star_name, :planets def initialize(star_name) @star_name = star_name