From fc28e067d9e9631037cfc24781f610a5b5dcb171 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 26 Feb 2019 13:28:31 -0800 Subject: [PATCH 1/9] Adds main --- main.rb | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 main.rb diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..05db465a --- /dev/null +++ b/main.rb @@ -0,0 +1,76 @@ +require_relative 'planet.rb' +require_relative 'solar_system.rb' + +def main + # Instantiation + solar_system = SolarSystem.new("Sol") + + mercury = Planet.new("Mercury", "light grey", "3.3011×10^23", "57.91 million", "It has no moons") + venus = Planet.new("Venus", "red-orange", "4.867 × 10^24", "108.2 million", "One day on Venus is longer than one year on Earth") + earth = Planet.new("Earth", "blue-green", "5.972e24", "1.496e8", "Only planet known to support life") + mars = Planet.new("Mars", "red", "6.39 × 10^23", "227.9 million", "It's our next destination") + jupiter = Planet.new("Jupiter", "white, red, orange, brown and yellow", "1.898 × 10^27", "778.5 million", "Jupiter can fit 1,300 Earths") + saturn = Planet.new("Saturn", "yellowing-brown", "5.683 × 10^26", "1.434 billion", "It is the least dense planet in the solar system") + uranus = Planet.new("Uranus", "pale blue", "8.681 × 10^25", "2.871 billion", "Uranus’ moons are named after characters created by William Shakespeare and Alexander Pope") + neptune = Planet.new("Neptune", "bright azure blue", "1.024 × 10^26", "4.495 billion", "Neptune has an average surface temperature of -214°C – approximately -353°F.") + + # Add planets to solar system + solar_system.add_planet(mercury) + solar_system.add_planet(venus) + solar_system.add_planet(earth) + solar_system.add_planet(mars) + solar_system.add_planet(jupiter) + solar_system.add_planet(saturn) + solar_system.add_planet(uranus) + solar_system.add_planet(neptune) + + # puts "" + # puts earth.summary + # puts "" + # puts mars.summary + + list = solar_system.list_planets + # List out all the planets orbiting Sol + # for i in (0..list.length - 1) + # print list[i] + # end + + + # Find planet by name + # found_planet = solar_system.find_planet_by_name("Saturn") + # puts "\n\nThe planet you are looking for has been found: #{found_planet}" + + # WAVE 3 + # Loop to ask user what they want to do + + loop do + puts "\n\nWhat would you like to do?" + puts "List planets planet details Exit" + answer = gets.chomp.downcase + + # list planets + if (answer == "list planets") + for i in (0..list.length - 1) + print list[i] + end + elsif (answer == "planet details") + puts "Which planet would you like to know about?" + planet_choice = gets.chomp.downcase + puts "Planet choice: #{planet_choice}" + + found_planet = solar_system.find_planet_by_name(planet_choice.capitalize) + + puts "Found planet: #{found_planet}" + + puts "Here are some details about #{found_planet}: " + print found_planet.summary + + else + break + end + end + +end # main class end + + +main From af4d881ee379cca331e0e94624a2f75f84713be3 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 26 Feb 2019 13:28:48 -0800 Subject: [PATCH 2/9] Adds planet --- planet.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 planet.rb diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..a807fe56 --- /dev/null +++ b/planet.rb @@ -0,0 +1,17 @@ +# WAVE 1 +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 + @fun_fact = fun_fact + end + + # Summary + def summary + return "#{self.name} is the third planet and is #{self.distance_from_sun_km} kilometers away from the Sun. It's color is #{self.color}. It's mass is #{self.mass_kg} kilograms and it is the #{self.fun_fact}!" + end +end \ No newline at end of file From 5c410b81f3de666d7667f51cc777a21875d5b6ff Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 26 Feb 2019 13:29:06 -0800 Subject: [PATCH 3/9] Adds solar system --- solar_system.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 solar_system.rb diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..5b4df158 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,45 @@ +# Wave 2 +require_relative 'planet.rb' + +class SolarSystem + attr_reader :star_name, :planets + # Constructor + def initialize(star_name) + @star_name = star_name + @planets = Array.new + end + + # Take an instance of Planet as a parameter and add it to the list of planets. + def add_planet(planet) + @planets << planet.name + end + + puts + + def planet_list + @planets.each do |planet| + # return planet # returns the planet at index 0 and then exits because return breaks a method + next + end + return @planets + end + + def list_planets + list = "" + for i in (0..@planets.length - 1) + list += "\n#{i + 1}. #{@planets[i]}" + end + return list + end + + def find_planet_by_name(planet_name) + # found_planet = "" + for i in (0..@planets.length - 1) + if @planets[i] == planet_name + # found_planet = planet_name + return @planets[i] + end + end + # return found_planet + end +end \ No newline at end of file From a64af46e36d1c635cd2afa421d1a5e2b7037a4e3 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 26 Feb 2019 21:35:25 -0800 Subject: [PATCH 4/9] Modifed list planets loop and changed its location in main. --- main.rb | 45 +++++++++++++++------------------------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/main.rb b/main.rb index 05db465a..51b80806 100644 --- a/main.rb +++ b/main.rb @@ -2,17 +2,18 @@ require_relative 'solar_system.rb' def main + puts "Welcome to my Solar System!" # Instantiation solar_system = SolarSystem.new("Sol") - mercury = Planet.new("Mercury", "light grey", "3.3011×10^23", "57.91 million", "It has no moons") - venus = Planet.new("Venus", "red-orange", "4.867 × 10^24", "108.2 million", "One day on Venus is longer than one year on Earth") - earth = Planet.new("Earth", "blue-green", "5.972e24", "1.496e8", "Only planet known to support life") - mars = Planet.new("Mars", "red", "6.39 × 10^23", "227.9 million", "It's our next destination") - jupiter = Planet.new("Jupiter", "white, red, orange, brown and yellow", "1.898 × 10^27", "778.5 million", "Jupiter can fit 1,300 Earths") - saturn = Planet.new("Saturn", "yellowing-brown", "5.683 × 10^26", "1.434 billion", "It is the least dense planet in the solar system") - uranus = Planet.new("Uranus", "pale blue", "8.681 × 10^25", "2.871 billion", "Uranus’ moons are named after characters created by William Shakespeare and Alexander Pope") - neptune = Planet.new("Neptune", "bright azure blue", "1.024 × 10^26", "4.495 billion", "Neptune has an average surface temperature of -214°C – approximately -353°F.") + mercury = Planet.new("Mercury", "light grey", 3.3011e23, 5.791000000e1, "It has no moons") + venus = Planet.new("Venus", "red-orange", 4.867e24, 1.082000000e2, "One day on Venus is longer than one year on Earth") + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "its the only planet known to support life") + mars = Planet.new("Mars", "red", 6.39e23, 2.279000000e2, "It's our next destination") + jupiter = Planet.new("Jupiter", "white, red, orange, brown and yellow", 1.898e27, 7.785000000e2, "Jupiter can fit 1,300 Earths") + saturn = Planet.new("Saturn", "yellowing-brown", 5.683e26, 1.434000000000e0, "It is the least dense planet in the solar system") + uranus = Planet.new("Uranus", "pale blue", 8.681e25, 2.871000000000e0, "Uranus’ moons are named after characters created by William Shakespeare and Alexander Pope") + neptune = Planet.new("Neptune", "bright azure blue", 1.024e26, 4.495000000000, "Neptune has an average surface temperature of -214°C – approximately -353°F.") # Add planets to solar system solar_system.add_planet(mercury) @@ -24,47 +25,31 @@ def main solar_system.add_planet(uranus) solar_system.add_planet(neptune) - # puts "" - # puts earth.summary - # puts "" - # puts mars.summary - - list = solar_system.list_planets - # List out all the planets orbiting Sol - # for i in (0..list.length - 1) - # print list[i] - # end - - - # Find planet by name - # found_planet = solar_system.find_planet_by_name("Saturn") - # puts "\n\nThe planet you are looking for has been found: #{found_planet}" - # WAVE 3 # Loop to ask user what they want to do loop do puts "\n\nWhat would you like to do?" - puts "List planets planet details Exit" + puts "List planets Planet details Add planet Exit" answer = gets.chomp.downcase # list planets if (answer == "list planets") + list = solar_system.list_planets for i in (0..list.length - 1) print list[i] end + elsif (answer == "planet details") puts "Which planet would you like to know about?" planet_choice = gets.chomp.downcase puts "Planet choice: #{planet_choice}" - found_planet = solar_system.find_planet_by_name(planet_choice.capitalize) - puts "Found planet: #{found_planet}" - - puts "Here are some details about #{found_planet}: " + puts "Here are some details about #{found_planet.name}:\n" print found_planet.summary - + elsif (answer == "add planet") + solar_system.discovered_planet else break end From 780faeb85b2852c31ed19a99f72a624400f196c1 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 26 Feb 2019 21:36:19 -0800 Subject: [PATCH 5/9] Added ArgumentError for mass and distance from sun being < 0 and edited summary output --- planet.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/planet.rb b/planet.rb index a807fe56..163540b0 100644 --- a/planet.rb +++ b/planet.rb @@ -3,6 +3,15 @@ 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) + + while mass_kg <= 0 + raise ArgumentError.new("Planet mass cannot be less than or equal to 0") + end + + while distance_from_sun_km <= 0 + raise ArgumentError.new("Distance from sun cannot be less than or equal to 0. There would be no planet left!") + end + @name = name @color = color @mass_kg = mass_kg @@ -12,6 +21,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) # Summary def summary - return "#{self.name} is the third planet and is #{self.distance_from_sun_km} kilometers away from the Sun. It's color is #{self.color}. It's mass is #{self.mass_kg} kilograms and it is the #{self.fun_fact}!" + return "#{self.name} is #{self.distance_from_sun_km} kilometers away from the Sun. It's color is #{self.color}. It's mass is #{self.mass_kg} kilograms and #{self.fun_fact}!" end end \ No newline at end of file From 37d3d22b80faf6604800f003779e8a9ca8f5f0e4 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 26 Feb 2019 21:36:59 -0800 Subject: [PATCH 6/9] Added a new method to add a new planet --- solar_system.rb | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/solar_system.rb b/solar_system.rb index 5b4df158..d951b2aa 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -11,7 +11,11 @@ def initialize(star_name) # Take an instance of Planet as a parameter and add it to the list of planets. def add_planet(planet) - @planets << planet.name + planets = @planets.map { |i| i.name.downcase } + if planets.include?(planet.name.downcase) + raise ArgumentError.new("Planet already exists. A solar system cannot have duplicate planets") + end + @planets.push(planet) end puts @@ -27,19 +31,36 @@ def planet_list def list_planets list = "" for i in (0..@planets.length - 1) - list += "\n#{i + 1}. #{@planets[i]}" + list += "\n#{i + 1}. #{@planets[i].name}" end return list end - def find_planet_by_name(planet_name) - # found_planet = "" - for i in (0..@planets.length - 1) - if @planets[i] == planet_name - # found_planet = planet_name - return @planets[i] - end + def find_planet_by_name(name) + @planets.each do |planet| + if planet.name.downcase == name.downcase + return planet + end end - # return found_planet + return nil + end + + def discovered_planet + + puts "What is the name of your planet?" + name = gets.chomp.capitalize + puts "What is the color of your planet?" + color = gets.chomp.downcase + puts "How much does your planet weigh in kg?" + mass_kg = gets.chomp.to_f + puts "How far is your planet from the sun in km?" + distance_from_sun_km = gets.chomp.to_f + puts "What is a fun fact about your planet?" + fun_fact = gets.chomp.downcase + + new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) + add_planet(new_planet) + + # puts new_planet.name end end \ No newline at end of file From 1a66e7c6f1d14fdcdc115a79045f41b0c9e84eac Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Tue, 26 Feb 2019 21:37:32 -0800 Subject: [PATCH 7/9] Added 3 tests --- planet_spec.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 planet_spec.rb diff --git a/planet_spec.rb b/planet_spec.rb new file mode 100644 index 00000000..3a47dddb --- /dev/null +++ b/planet_spec.rb @@ -0,0 +1,33 @@ +require 'minitest/autorun' +require 'minitest/reporters' +require_relative 'planet.rb' + +Minitest::Reporters.use! + +describe "class Planet" do + it "will raise ArgumentError if mass is < 0" do + expect { + Planet.new("Earth", "blue-green", -10, 100000, "only planet known to support life") + }.must_raise ArgumentError + end + + it "will raise ArgumentError if distance from Sun is < 0" do + expect { + Planet.new("Earth", "blue-green", 1000000, 0, "only planet known to support life") + }.must_raise ArgumentError + end +end + +describe "class solar system" do + it "will raise ArgumentError if planet already exists" do + # solar_system = SolarSystem.new("SailorMoon") + + expect { + mars = Planet.new("Mars", "red", "6.39 × 10^23", "227.9 million", "It's our next destination") + solar_system.add_planet(mars) + + another_mars = Planet.new("Mars", "red", "6.39 × 10^23", "227.9 million", "It's our next destination") + solar_system.add_planet(another_mars) + }.must_raise ArgumentError + end +end \ No newline at end of file From 71336765e062f971585d3a0cda4e170be03e9510 Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Wed, 27 Feb 2019 10:24:40 -0800 Subject: [PATCH 8/9] Modified add_planet method in solar_system to not use map --- solar_system.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solar_system.rb b/solar_system.rb index d951b2aa..6b95e927 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -9,10 +9,10 @@ def initialize(star_name) @planets = Array.new end - # Take an instance of Planet as a parameter and add it to the list of planets. + # Take an instance of Planet as a parameter and add it to the list of planets.exit def add_planet(planet) - planets = @planets.map { |i| i.name.downcase } - if planets.include?(planet.name.downcase) + # planets = @planets.map { |i| i.name.downcase } + if @planets.include?(planet.name.downcase) raise ArgumentError.new("Planet already exists. A solar system cannot have duplicate planets") end @planets.push(planet) From aca4da9c446f9eb7158ac7b8e1a1f54d5c14962b Mon Sep 17 00:00:00 2001 From: Faiza Husain Date: Wed, 27 Feb 2019 10:25:53 -0800 Subject: [PATCH 9/9] Modiefies spacing in main and replaces break with exit --- main.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.rb b/main.rb index 51b80806..9599994a 100644 --- a/main.rb +++ b/main.rb @@ -48,10 +48,12 @@ def main puts "Found planet: #{found_planet}" puts "Here are some details about #{found_planet.name}:\n" print found_planet.summary + elsif (answer == "add planet") solar_system.discovered_planet + else - break + exit end end