From e898aca52972ef31f49726725d42e679c913d226 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Mon, 21 Sep 2020 21:48:56 -0700 Subject: [PATCH 01/17] add class planet and main file for wave1 --- main.rb | 14 ++++++++++++++ planet.rb | 15 +++++++++++++++ 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..cacd252a --- /dev/null +++ b/main.rb @@ -0,0 +1,14 @@ +require_relative 'planet' + +def main + # this method should create two different instances of Planet and print out some of their attributes + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + puts "#{ earth.name } is #{ earth.color }" + puts earth.summary + + mars = Planet.new('Mars', 'red', 6.39e23, 14.16e8, 'The second-smallest planet in the solar system') + puts "#{ mars.name } is #{ mars.color }" + puts mars.summary +end + +main \ No newline at end of file diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..68944cce --- /dev/null +++ b/planet.rb @@ -0,0 +1,15 @@ +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 } is #{ @color }, and it weights #{ @mass_kg } kg and #{ @distance_from_sun_km } km away from sun. The fun fact about it: #{ @fun_fact }" + end +end From df3c62aac98dfe2e675757e10ecce80b8ef3574c Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Mon, 21 Sep 2020 23:54:03 -0700 Subject: [PATCH 02/17] revise summary format --- planet.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planet.rb b/planet.rb index 68944cce..4a6ecd83 100644 --- a/planet.rb +++ b/planet.rb @@ -10,6 +10,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "#{ @name } is #{ @color }, and it weights #{ @mass_kg } kg and #{ @distance_from_sun_km } km away from sun. The fun fact about it: #{ @fun_fact }" + return "#{ @name } is #{ @color }, and it weights #{ @mass_kg } kg and #{ @distance_from_sun_km } km away from sun. \nThe fun fact about it: #{ @fun_fact.downcase }\n\n" end end From 8b12c08112efe6edf86c3457108624c8268bb721 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Mon, 21 Sep 2020 23:54:54 -0700 Subject: [PATCH 03/17] add class solar_system and revise main file for wave2 --- main.rb | 18 ++++++++++++++++-- solar_system.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 solar_system.rb diff --git a/main.rb b/main.rb index cacd252a..46814bef 100644 --- a/main.rb +++ b/main.rb @@ -1,14 +1,28 @@ require_relative 'planet' +require_relative 'solar_system' def main - # this method should create two different instances of Planet and print out some of their attributes + # Create two different instances of Planet and print out some of their attributes earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') puts "#{ earth.name } is #{ earth.color }" puts earth.summary mars = Planet.new('Mars', 'red', 6.39e23, 14.16e8, 'The second-smallest planet in the solar system') - puts "#{ mars.name } is #{ mars.color }" + puts "#{ mars.name } is #{ mars.fun_fact.downcase }" puts mars.summary + + # create an instance of SolarSystem, add all your Planets to it, and then print the list + solar_system = SolarSystem.new("Sol") + saturn = Planet.new('Saturn', 'pale yellow', 5.683e26, 92.802e8, 'The second-largest planet in the solar system') + solar_system.add_planet(saturn) + + list = solar_system.list_planets + puts list + + # Exercise SolarSystem#find_planet_by_name + found_planet = solar_system.find_planet_by_name('saturn') + puts found_planet + puts found_planet.summary if found_planet.class == Planet end main \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..5d7f0fe2 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,27 @@ +class SolarSystem + attr_reader :star_name, :planets + + def initialize(star_name) + @star_name = star_name + @planets = Array.new + end + + def add_planet(planet) + @planets.push(planet) + end + + def list_planets + my_string = "Planets orbiting <#{ @star_name }>\n" + @planets.each_with_index do |planet, index| + my_string += "#{ index + 1 } #{ planet.name }\n" + end + return my_string + end + + def find_planet_by_name(planet_name) + array_planet = @planets.select { |planet| planet.name.downcase == planet_name.downcase} + found_planet = nil + array_planet.each { |element| found_planet = element } + return found_planet + end +end \ No newline at end of file From 5d2518061535fd1229d5bb78d178b556a9f56fb1 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Tue, 22 Sep 2020 11:28:24 -0700 Subject: [PATCH 04/17] revise main file for wave3 --- main.rb | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/main.rb b/main.rb index 46814bef..b3115cb7 100644 --- a/main.rb +++ b/main.rb @@ -2,6 +2,8 @@ require_relative 'solar_system' def main +=begin + # For wave 1 # Create two different instances of Planet and print out some of their attributes earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') puts "#{ earth.name } is #{ earth.color }" @@ -19,10 +21,61 @@ def main list = solar_system.list_planets puts list + # For wave 2 # Exercise SolarSystem#find_planet_by_name found_planet = solar_system.find_planet_by_name('saturn') puts found_planet puts found_planet.summary if found_planet.class == Planet +=end + + # For wave 3 + 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) + + mars = Planet.new('Mars', 'red', 6.39e23, 14.16e8, 'The second-smallest planet in the solar system') + solar_system.add_planet(mars) + + saturn = Planet.new('Saturn', 'pale yellow', 5.683e26, 92.802e8, 'The second-largest planet in the solar system') + solar_system.add_planet(saturn) + + options = ["list planets", "planet details", "add planet", "exit"] + stop_loop = false + until stop_loop + puts "\nWhat do you want to see?" + options.each { |option| puts "- #{ option }" } + user_response = gets.chomp.downcase + + if user_response == "list planets" + puts "\n", list = solar_system.list_planets + elsif user_response == "planet details" + puts "\nYay, let's see more details about the planets. Which planet do you want to see?" + puts list = solar_system.list_planets + user_planet = gets.chomp.downcase + found_planet = solar_system.find_planet_by_name(user_planet) + puts "Sorry, we don't have this planet." if found_planet.nil? + puts "\n", found_planet.summary if found_planet.class == Planet + elsif user_response == "add planet" + puts "\nSo exciting, you're to add a new planet!" + new_planet = Hash.new + elements_to_give = ["name", "color", "mass_kg", "distance_from_sun_km", "fun_fact"] + elements_to_give.each do |element| + print "What's the #{ element } of this planet: " + user_new_planet = gets.chomp.downcase + new_planet[element] = user_new_planet + end + + user_planet = Planet.new(new_planet["name"].capitalize, new_planet["color"], new_planet["mass_kg"], new_planet["distance_from_sun_km"], new_planet["fun_fact"]) + solar_system.add_planet(user_planet) + + puts "\nNow, want to see your new planet on the main menu? Please go check \"list planets\"" + elsif user_response == "exit" + stop_loop = true + else + puts "Please enter the options listed." + end + end + end main \ No newline at end of file From 2a7e6bd411a4f6744669bd3f11310ad142f8b67a Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Tue, 22 Sep 2020 18:51:00 -0700 Subject: [PATCH 05/17] add a test for class planet for wave1 --- planet.rb => lib/planet.rb | 8 +++- main.rb | 81 -------------------------------------- solar_system.rb | 27 ------------- test/planet_test.rb | 38 ++++++++++++++++++ 4 files changed, 44 insertions(+), 110 deletions(-) rename planet.rb => lib/planet.rb (52%) delete mode 100644 main.rb delete mode 100644 solar_system.rb create mode 100644 test/planet_test.rb diff --git a/planet.rb b/lib/planet.rb similarity index 52% rename from planet.rb rename to lib/planet.rb index 4a6ecd83..cd616dd7 100644 --- a/planet.rb +++ b/lib/planet.rb @@ -2,10 +2,14 @@ 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 + raise ArgumentError.new("Mass(kg) should be a number that greater than zero: #{ @mass_kg }") if (@mass_kg.to_i != @mass_kg || @mass_kg.to_f != @mass_kg) || @mass_kg <= 0 + @distance_from_sun_km = distance_from_sun_km + raise ArgumentError.new("Distance from sun (km) should be a number that greater than zero: #{ @distance_from_sun_km }") if (@distance_from_sun_km.to_i != @distance_from_sun_km || @distance_from_sun_km.to_f != @distance_from_sun_km) || @distance_from_sun_km <= 0 + + @name = name + @color = color @fun_fact = fun_fact end diff --git a/main.rb b/main.rb deleted file mode 100644 index b3115cb7..00000000 --- a/main.rb +++ /dev/null @@ -1,81 +0,0 @@ -require_relative 'planet' -require_relative 'solar_system' - -def main -=begin - # For wave 1 - # Create two different instances of Planet and print out some of their attributes - earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') - puts "#{ earth.name } is #{ earth.color }" - puts earth.summary - - mars = Planet.new('Mars', 'red', 6.39e23, 14.16e8, 'The second-smallest planet in the solar system') - puts "#{ mars.name } is #{ mars.fun_fact.downcase }" - puts mars.summary - - # create an instance of SolarSystem, add all your Planets to it, and then print the list - solar_system = SolarSystem.new("Sol") - saturn = Planet.new('Saturn', 'pale yellow', 5.683e26, 92.802e8, 'The second-largest planet in the solar system') - solar_system.add_planet(saturn) - - list = solar_system.list_planets - puts list - - # For wave 2 - # Exercise SolarSystem#find_planet_by_name - found_planet = solar_system.find_planet_by_name('saturn') - puts found_planet - puts found_planet.summary if found_planet.class == Planet -=end - - # For wave 3 - 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) - - mars = Planet.new('Mars', 'red', 6.39e23, 14.16e8, 'The second-smallest planet in the solar system') - solar_system.add_planet(mars) - - saturn = Planet.new('Saturn', 'pale yellow', 5.683e26, 92.802e8, 'The second-largest planet in the solar system') - solar_system.add_planet(saturn) - - options = ["list planets", "planet details", "add planet", "exit"] - stop_loop = false - until stop_loop - puts "\nWhat do you want to see?" - options.each { |option| puts "- #{ option }" } - user_response = gets.chomp.downcase - - if user_response == "list planets" - puts "\n", list = solar_system.list_planets - elsif user_response == "planet details" - puts "\nYay, let's see more details about the planets. Which planet do you want to see?" - puts list = solar_system.list_planets - user_planet = gets.chomp.downcase - found_planet = solar_system.find_planet_by_name(user_planet) - puts "Sorry, we don't have this planet." if found_planet.nil? - puts "\n", found_planet.summary if found_planet.class == Planet - elsif user_response == "add planet" - puts "\nSo exciting, you're to add a new planet!" - new_planet = Hash.new - elements_to_give = ["name", "color", "mass_kg", "distance_from_sun_km", "fun_fact"] - elements_to_give.each do |element| - print "What's the #{ element } of this planet: " - user_new_planet = gets.chomp.downcase - new_planet[element] = user_new_planet - end - - user_planet = Planet.new(new_planet["name"].capitalize, new_planet["color"], new_planet["mass_kg"], new_planet["distance_from_sun_km"], new_planet["fun_fact"]) - solar_system.add_planet(user_planet) - - puts "\nNow, want to see your new planet on the main menu? Please go check \"list planets\"" - elsif user_response == "exit" - stop_loop = true - else - puts "Please enter the options listed." - end - end - -end - -main \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb deleted file mode 100644 index 5d7f0fe2..00000000 --- a/solar_system.rb +++ /dev/null @@ -1,27 +0,0 @@ -class SolarSystem - attr_reader :star_name, :planets - - def initialize(star_name) - @star_name = star_name - @planets = Array.new - end - - def add_planet(planet) - @planets.push(planet) - end - - def list_planets - my_string = "Planets orbiting <#{ @star_name }>\n" - @planets.each_with_index do |planet, index| - my_string += "#{ index + 1 } #{ planet.name }\n" - end - return my_string - end - - def find_planet_by_name(planet_name) - array_planet = @planets.select { |planet| planet.name.downcase == planet_name.downcase} - found_planet = nil - array_planet.each { |element| found_planet = element } - return found_planet - end -end \ No newline at end of file diff --git a/test/planet_test.rb b/test/planet_test.rb new file mode 100644 index 00000000..8d269820 --- /dev/null +++ b/test/planet_test.rb @@ -0,0 +1,38 @@ +require 'minitest' +require 'minitest/spec' +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/pride' + +require_relative '../lib/planet' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe 'tests for planet class' do + it 'mass_kg and distance_from_sun_km are both a number greater than zero' do + # Arrange && Act + tiny_planet = Planet.new('Tiny planet', 'pale pink', 1, 10000, 'Pale pink color is becuase of cherry blossom') + + # Assert + expect(tiny_planet.mass_kg).must_equal 1 + expect(tiny_planet.distance_from_sun_km).must_equal 10000 + end + + it 'raises an ArgumentError for mass_kg is not a number greater than 0' do + + expect {Planet.new('Tiny planet', 'pale pink', 'super tiny planet', 10000, 'Pale pink color is becuase of cherry blossom')}.must_raise ArgumentError + + expect {Planet.new('Tiny planet', 'pale pink', -100, 10000, 'Pale pink color is becuase of cherry blossom')}.must_raise ArgumentError + + expect {Planet.new('Tiny planet', 'pale pink', '', 10000, 'Pale pink color is becuase of cherry blossom')}.must_raise ArgumentError + end + + it 'raises an ArgumentError for distance_from_sun_km is not a number greater than 0' do + + expect {Planet.new('Tiny planet', 'pale pink', 1, 'not so far away', 'Pale pink color is becuase of cherry blossom')}.must_raise ArgumentError + + expect {Planet.new('Tiny planet', 'pale pink', 1, 0, 'Pale pink color is becuase of cherry blossom')}.must_raise ArgumentError + + expect {Planet.new('Tiny planet', 'pale pink', 1, nil, 'Pale pink color is becuase of cherry blossom')}.must_raise ArgumentError + end +end \ No newline at end of file From fe0fe92e6b441c33624b673332588a6247b4bb88 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Tue, 22 Sep 2020 19:55:46 -0700 Subject: [PATCH 06/17] add distance_between method in solar_system --- lib/solar_system.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lib/solar_system.rb diff --git a/lib/solar_system.rb b/lib/solar_system.rb new file mode 100644 index 00000000..20ecf9ee --- /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 = Array.new + end + + def add_planet(planet) + @planets.push(planet) + end + + def list_planets + my_string = "Planets orbiting <#{ @star_name }>\n" + @planets.each_with_index do |planet, index| + my_string += "#{ index + 1 } #{ planet.name }\n" + end + return my_string + end + + def find_planet_by_name(planet_name) + array_planet = @planets.select { |planet| planet.name.downcase == planet_name.downcase} + found_planet = nil + array_planet.each { |element| found_planet = element } + return found_planet + end + + def distance_between(planet1, planet2) + first_planet = find_planet_by_name(planet1) + second_planet = find_planet_by_name(planet2) + distance_between_two_planets = (first_planet.distance_from_sun_km - second_planet.distance_from_sun_km).abs + return distance_between_two_planets + end +end \ No newline at end of file From 467efab324015e01199b57336b7067383ea8df39 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Tue, 22 Sep 2020 19:56:14 -0700 Subject: [PATCH 07/17] add a test for class solar_system for wave2 --- test/solar_system_test.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/solar_system_test.rb diff --git a/test/solar_system_test.rb b/test/solar_system_test.rb new file mode 100644 index 00000000..2ff5d95d --- /dev/null +++ b/test/solar_system_test.rb @@ -0,0 +1,27 @@ +require 'minitest' +require 'minitest/spec' +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/pride' + +require_relative '../lib/planet' +require_relative '../lib/solar_system' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe 'tests for solar_system class' do + it 'has the distance between two planets' do + # Arrange + first_planet = Planet.new('Tiny planet', 'pale pink', 1, 10_000, 'Pale pink color is becuase of cherry blossom') + second_planet = Planet.new('Huge planet', 'green and blue', 100_000_000, 100_000, 'The biggest volumn of planet') + + # Act + space_system = SolarSystem.new("Sol") + space_system.add_planet(first_planet) + space_system.add_planet(second_planet) + distance_between_them = space_system.distance_between(first_planet.name, second_planet.name) + + # Assert + expect(distance_between_them).must_equal 90_000 + end +end \ No newline at end of file From 1496c9343b39ded2e57c87fe2bea6ee41a8b46e1 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Tue, 22 Sep 2020 21:12:27 -0700 Subject: [PATCH 08/17] address a number in string data type --- test/planet_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/planet_test.rb b/test/planet_test.rb index 8d269820..3672d525 100644 --- a/test/planet_test.rb +++ b/test/planet_test.rb @@ -16,6 +16,14 @@ # Assert expect(tiny_planet.mass_kg).must_equal 1 expect(tiny_planet.distance_from_sun_km).must_equal 10000 + + tiny_planet2 = Planet.new('Tiny planet', 'pale pink', "4", 10000, 'Pale pink color is becuase of cherry blossom') + expect(tiny_planet2.mass_kg.to_f).must_equal 4.0 + + tiny_planet3 = Planet.new('Tiny planet', 'pale pink', 1, "40000", 'Pale pink color is becuase of cherry blossom') + expect(tiny_planet3.distance_from_sun_km.to_f).must_equal 40000.0 + + end it 'raises an ArgumentError for mass_kg is not a number greater than 0' do From c5e79683ca046180d4a6f89cf3f453cfa2323778 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Tue, 22 Sep 2020 21:13:26 -0700 Subject: [PATCH 09/17] revise if statement for ArgumentError --- lib/planet.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/planet.rb b/lib/planet.rb index cd616dd7..28f336a8 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -3,10 +3,11 @@ class Planet def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @mass_kg = mass_kg - raise ArgumentError.new("Mass(kg) should be a number that greater than zero: #{ @mass_kg }") if (@mass_kg.to_i != @mass_kg || @mass_kg.to_f != @mass_kg) || @mass_kg <= 0 + + raise ArgumentError.new("Mass(kg) should be a number that greater than zero: #{ @mass_kg }") if !(@mass_kg.to_f > 0) @distance_from_sun_km = distance_from_sun_km - raise ArgumentError.new("Distance from sun (km) should be a number that greater than zero: #{ @distance_from_sun_km }") if (@distance_from_sun_km.to_i != @distance_from_sun_km || @distance_from_sun_km.to_f != @distance_from_sun_km) || @distance_from_sun_km <= 0 + raise ArgumentError.new("Distance from sun (km) should be a number that greater than zero: #{ @distance_from_sun_km }") if !(@distance_from_sun_km.to_f > 0) @name = name @color = color From a3cd6e041abfef40e1e6a38bdb12d6d17ef86c0c Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Tue, 22 Sep 2020 21:45:13 -0700 Subject: [PATCH 10/17] add rescue if user enters a bad value --- lib/main.rb | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 lib/main.rb diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..af03cf61 --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,88 @@ +require_relative 'planet' +require_relative 'solar_system' + +def main +=begin + # For wave 1 + # Create two different instances of Planet and print out some of their attributes + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + puts "#{ earth.name } is #{ earth.color }" + puts earth.summary + + mars = Planet.new('Mars', 'red', 6.39e23, 14.16e8, 'The second-smallest planet in the solar system') + puts "#{ mars.name } is #{ mars.fun_fact.downcase }" + puts mars.summary + + # For wave 2 + # create an instance of SolarSystem, add all your Planets to it, and then print the list + solar_system = SolarSystem.new("Sol") + saturn = Planet.new('Saturn', 'pale yellow', 5.683e26, 92.802e8, 'The second-largest planet in the solar system') + solar_system.add_planet(saturn) + + list = solar_system.list_planets + puts list + + # Exercise SolarSystem#find_planet_by_name + found_planet = solar_system.find_planet_by_name('saturn') + puts found_planet + puts found_planet.summary if found_planet.class == Planet +=end + + # For wave 3 + 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) + + mars = Planet.new('Mars', 'red', 6.39e23, 14.16e8, 'The second-smallest planet in the solar system') + solar_system.add_planet(mars) + + saturn = Planet.new('Saturn', 'pale yellow', 5.683e26, 92.802e8, 'The second-largest planet in the solar system') + solar_system.add_planet(saturn) + + options = ["list planets", "planet details", "add planet", "exit"] + stop_loop = false + until stop_loop + puts "\nWhat do you want to see?" + options.each { |option| puts "- #{ option }" } + user_response = gets.chomp.downcase + + if user_response == "list planets" + puts "\n", list = solar_system.list_planets + elsif user_response == "planet details" + puts "\nYay, let's see more details about the planets. Which planet do you want to see?" + puts list = solar_system.list_planets + user_planet = gets.chomp.downcase + found_planet = solar_system.find_planet_by_name(user_planet) + puts "Sorry, we don't have this planet." if found_planet.nil? + puts "\n", found_planet.summary if found_planet.class == Planet + elsif user_response == "add planet" + puts "\nSo exciting, you're to add a new planet!" + new_planet = Hash.new + elements_to_give = ["name", "color", "mass_kg", "distance_from_sun_km", "fun_fact"] + elements_to_give.each do |element| + print "What's the #{ element } of this planet: " + user_new_planet = gets.chomp.downcase + new_planet[element] = user_new_planet + end + + rescue_status = false + begin + user_planet = Planet.new(new_planet["name"].capitalize, new_planet["color"], new_planet["mass_kg"], new_planet["distance_from_sun_km"], new_planet["fun_fact"]) + rescue ArgumentError => exception + puts "\n#{ exception.message }" + puts "Your new planet is not added, please try again." + rescue_status = true + end + + solar_system.add_planet(user_planet) + puts "\nNow, want to see your new planet on the main menu? Please go check \"list planets\"" if rescue_status == false + elsif user_response == "exit" + stop_loop = true + else + puts "Please enter the options listed." + end + end + +end + +main \ No newline at end of file From 5977046346b4f08bf732e6fd291a64d82d740265 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Tue, 22 Sep 2020 22:27:39 -0700 Subject: [PATCH 11/17] revise the data dype for mass_by & distance_from_sun_km --- lib/planet.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/planet.rb b/lib/planet.rb index 28f336a8..30dc96c7 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -5,9 +5,11 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) @mass_kg = mass_kg raise ArgumentError.new("Mass(kg) should be a number that greater than zero: #{ @mass_kg }") if !(@mass_kg.to_f > 0) + @mass_kg = mass_kg.to_f @distance_from_sun_km = distance_from_sun_km raise ArgumentError.new("Distance from sun (km) should be a number that greater than zero: #{ @distance_from_sun_km }") if !(@distance_from_sun_km.to_f > 0) + @distance_from_sun_km = distance_from_sun_km.to_f @name = name @color = color From d3dc287b9975f61e021f95daa2516ec3aa484d38 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Tue, 22 Sep 2020 22:28:05 -0700 Subject: [PATCH 12/17] add distance_between option --- lib/main.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index af03cf61..5f4bae85 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -39,7 +39,7 @@ def main saturn = Planet.new('Saturn', 'pale yellow', 5.683e26, 92.802e8, 'The second-largest planet in the solar system') solar_system.add_planet(saturn) - options = ["list planets", "planet details", "add planet", "exit"] + options = ["list planets", "planet details", "add planet", "distance between planets", "exit"] stop_loop = false until stop_loop puts "\nWhat do you want to see?" @@ -76,6 +76,23 @@ def main solar_system.add_planet(user_planet) puts "\nNow, want to see your new planet on the main menu? Please go check \"list planets\"" if rescue_status == false + elsif user_response == "distance between planets" + puts "\n Which two planets do you want to see their distance?" + puts list = solar_system.list_planets + two_planet = 1 + planets_for_distance = Array.new + until two_planet == 3 + print "Planet #{two_planet}: " + distance_planet = gets.chomp.downcase + found_planet = solar_system.find_planet_by_name(distance_planet) + if found_planet.nil? + puts "Sorry, we don't have this planet: #{ distance_planet }" + else + planets_for_distance.push(distance_planet) + two_planet += 1 + end + end + puts "The distance between #{ planets_for_distance[0] } and #{ planets_for_distance[1] } is #{solar_system.distance_between(planets_for_distance[0], planets_for_distance[1])}km." elsif user_response == "exit" stop_loop = true else From 229054fd0d7c211219949e5b24faaba4352ec4f1 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Wed, 23 Sep 2020 14:18:00 -0700 Subject: [PATCH 13/17] debug add planet --- lib/main.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.rb b/lib/main.rb index 5f4bae85..30d2496f 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -74,7 +74,7 @@ def main rescue_status = true end - solar_system.add_planet(user_planet) + solar_system.add_planet(user_planet) if rescue_status == false puts "\nNow, want to see your new planet on the main menu? Please go check \"list planets\"" if rescue_status == false elsif user_response == "distance between planets" puts "\n Which two planets do you want to see their distance?" From 8253afd49f6183423dd7df30bf80fdad012ef668 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Wed, 23 Sep 2020 16:05:05 -0700 Subject: [PATCH 14/17] delete redundant lines --- lib/planet.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/planet.rb b/lib/planet.rb index 30dc96c7..aced9783 100644 --- a/lib/planet.rb +++ b/lib/planet.rb @@ -2,13 +2,10 @@ 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) - @mass_kg = mass_kg - - raise ArgumentError.new("Mass(kg) should be a number that greater than zero: #{ @mass_kg }") if !(@mass_kg.to_f > 0) + raise ArgumentError.new("Mass(kg) should be a number that greater than zero: #{ mass_kg }") if !(mass_kg.to_f > 0) @mass_kg = mass_kg.to_f - @distance_from_sun_km = distance_from_sun_km - raise ArgumentError.new("Distance from sun (km) should be a number that greater than zero: #{ @distance_from_sun_km }") if !(@distance_from_sun_km.to_f > 0) + raise ArgumentError.new("Distance from sun (km) should be a number that greater than zero: #{ distance_from_sun_km }") if !(distance_from_sun_km.to_f > 0) @distance_from_sun_km = distance_from_sun_km.to_f @name = name From 3aad36af695795fa0483e86b846d4ab5ce49f686 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Wed, 23 Sep 2020 16:23:09 -0700 Subject: [PATCH 15/17] revise find_planet_by_name method --- lib/solar_system.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/solar_system.rb b/lib/solar_system.rb index 20ecf9ee..1317628b 100644 --- a/lib/solar_system.rb +++ b/lib/solar_system.rb @@ -12,23 +12,19 @@ def add_planet(planet) def list_planets my_string = "Planets orbiting <#{ @star_name }>\n" - @planets.each_with_index do |planet, index| - my_string += "#{ index + 1 } #{ planet.name }\n" - end + @planets.each { |planet| my_string += "- #{ planet.name }\n" } return my_string end def find_planet_by_name(planet_name) - array_planet = @planets.select { |planet| planet.name.downcase == planet_name.downcase} - found_planet = nil - array_planet.each { |element| found_planet = element } + found_planet = @planets.select { |planet| planet.name.downcase == planet_name.downcase } return found_planet end def distance_between(planet1, planet2) first_planet = find_planet_by_name(planet1) second_planet = find_planet_by_name(planet2) - distance_between_two_planets = (first_planet.distance_from_sun_km - second_planet.distance_from_sun_km).abs + distance_between_two_planets = (first_planet[0].distance_from_sun_km - second_planet[0].distance_from_sun_km).abs return distance_between_two_planets end end \ No newline at end of file From 050a8f21e4567d62bee9afe2a3cca22978750a91 Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Wed, 23 Sep 2020 16:23:44 -0700 Subject: [PATCH 16/17] revise planet details --- lib/main.rb | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index 30d2496f..becc101d 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -12,7 +12,7 @@ def main mars = Planet.new('Mars', 'red', 6.39e23, 14.16e8, 'The second-smallest planet in the solar system') puts "#{ mars.name } is #{ mars.fun_fact.downcase }" puts mars.summary - + # For wave 2 # create an instance of SolarSystem, add all your Planets to it, and then print the list solar_system = SolarSystem.new("Sol") @@ -24,8 +24,10 @@ def main # Exercise SolarSystem#find_planet_by_name found_planet = solar_system.find_planet_by_name('saturn') - puts found_planet - puts found_planet.summary if found_planet.class == Planet + # puts found_planet + # puts found_planet.summary if found_planet.class == Planet + puts "We couldn't find this planet, please try agian" if found_planet.nil? + found_planet.each { |planet| puts "#{ planet.summary }\n"} =end # For wave 3 @@ -43,19 +45,29 @@ def main stop_loop = false until stop_loop puts "\nWhat do you want to see?" - options.each { |option| puts "- #{ option }" } + options.each_with_index { |option, index| puts "#{ index+1 }. #{ option }" } user_response = gets.chomp.downcase - if user_response == "list planets" + case user_response + when "list planets", "1" puts "\n", list = solar_system.list_planets - elsif user_response == "planet details" + when "planet details", "2" puts "\nYay, let's see more details about the planets. Which planet do you want to see?" puts list = solar_system.list_planets + print "==> " user_planet = gets.chomp.downcase found_planet = solar_system.find_planet_by_name(user_planet) - puts "Sorry, we don't have this planet." if found_planet.nil? - puts "\n", found_planet.summary if found_planet.class == Planet - elsif user_response == "add planet" + + while found_planet.count == 0 + puts "\nSorry, we don't have this planet. Please try again.", list = solar_system.list_planets + print "==> " + user_planet = gets.chomp.downcase + found_planet = solar_system.find_planet_by_name(user_planet) + end + + puts "\nWe have #{ found_planet.count } planet(s) named #{ user_planet.capitalize }" + found_planet.each_with_index { |planet, index| puts "#{ index+1 }. #{ planet.summary }"} + when "add planet", "3" puts "\nSo exciting, you're to add a new planet!" new_planet = Hash.new elements_to_give = ["name", "color", "mass_kg", "distance_from_sun_km", "fun_fact"] @@ -76,7 +88,7 @@ def main solar_system.add_planet(user_planet) if rescue_status == false puts "\nNow, want to see your new planet on the main menu? Please go check \"list planets\"" if rescue_status == false - elsif user_response == "distance between planets" + when "distance between planets", "4" puts "\n Which two planets do you want to see their distance?" puts list = solar_system.list_planets two_planet = 1 @@ -93,7 +105,7 @@ def main end end puts "The distance between #{ planets_for_distance[0] } and #{ planets_for_distance[1] } is #{solar_system.distance_between(planets_for_distance[0], planets_for_distance[1])}km." - elsif user_response == "exit" + when "exit", "5" stop_loop = true else puts "Please enter the options listed." From 81dae8ee503f670f58a3ba0ef077c1f6c91348dc Mon Sep 17 00:00:00 2001 From: Ting-Yi Liu <46391352+ichbinorange@users.noreply.github.com> Date: Wed, 23 Sep 2020 17:10:09 -0700 Subject: [PATCH 17/17] revise add planet & distance between planets --- lib/main.rb | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index becc101d..a5c3be3b 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -65,8 +65,8 @@ def main found_planet = solar_system.find_planet_by_name(user_planet) end - puts "\nWe have #{ found_planet.count } planet(s) named #{ user_planet.capitalize }" - found_planet.each_with_index { |planet, index| puts "#{ index+1 }. #{ planet.summary }"} + puts "\nThe information for planet #{ user_planet.capitalize } is as below:" + found_planet.each { |planet| puts "#{ planet.summary }"} when "add planet", "3" puts "\nSo exciting, you're to add a new planet!" new_planet = Hash.new @@ -74,6 +74,13 @@ def main elements_to_give.each do |element| print "What's the #{ element } of this planet: " user_new_planet = gets.chomp.downcase + if element == "name" + check_name = solar_system.find_planet_by_name(user_new_planet) + until check_name.count == 0 + print "Sorry, this name is used, please try another name ==> " + user_new_planet = gets.chomp.downcase + end + end new_planet[element] = user_new_planet end @@ -92,19 +99,21 @@ def main puts "\n Which two planets do you want to see their distance?" puts list = solar_system.list_planets two_planet = 1 - planets_for_distance = Array.new + planets_for_distance = Hash(first: "", second:"") until two_planet == 3 print "Planet #{two_planet}: " - distance_planet = gets.chomp.downcase - found_planet = solar_system.find_planet_by_name(distance_planet) - if found_planet.nil? - puts "Sorry, we don't have this planet: #{ distance_planet }" + user_distance_planet = gets.chomp.downcase + found_planet = solar_system.find_planet_by_name(user_distance_planet) + + if found_planet.nil? || found_planet.count == 0 + puts "Sorry, we don't have this planet: #{ user_distance_planet }" else - planets_for_distance.push(distance_planet) + planets_for_distance[:first] = user_distance_planet if two_planet == 1 + planets_for_distance[:second] = user_distance_planet if two_planet == 2 two_planet += 1 end end - puts "The distance between #{ planets_for_distance[0] } and #{ planets_for_distance[1] } is #{solar_system.distance_between(planets_for_distance[0], planets_for_distance[1])}km." + puts "\nThe distance between #{ planets_for_distance[:first] } and #{ planets_for_distance[:second] } is #{ solar_system.distance_between(planets_for_distance[:first], planets_for_distance[:second]) }km." when "exit", "5" stop_loop = true else