From cbed3b7224e91519d0b1d2af4f925d7638967ab4 Mon Sep 17 00:00:00 2001 From: Nara Date: Mon, 25 Feb 2019 14:11:01 -0800 Subject: [PATCH 1/8] Finished Wave 1 --- main.rb | 9 +++++++++ planet.rb | 16 ++++++++++++++++ 2 files changed, 25 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..81937947 --- /dev/null +++ b/main.rb @@ -0,0 +1,9 @@ +require_relative "planet.rb" +def main + earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') + puts earth.name + mars = Planet.new('Mars', "red", 6.39e23, 2.279e8,'Mars is named after a mythological figure - the Roman god of war') + 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..3eebd5e4 --- /dev/null +++ b/planet.rb @@ -0,0 +1,16 @@ +class Planet + attr_accessor :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 "The planet name is #{@name}. Color of the planet is #{@color}. Planet weight is #{@mass_kg} kg. Distance from the sun is #{@distance_from_sun_km} km. #{@fun_fact}." + end +end + +# earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') \ No newline at end of file From 68620dc09436a272a47b22371daffda1fbbf8160 Mon Sep 17 00:00:00 2001 From: Nara Date: Mon, 25 Feb 2019 15:42:42 -0800 Subject: [PATCH 2/8] Completed Wave 2 --- main.rb | 13 +++++++++++-- solar_system.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 solar_system.rb diff --git a/main.rb b/main.rb index 81937947..e9d8cf6c 100644 --- a/main.rb +++ b/main.rb @@ -1,9 +1,18 @@ require_relative "planet.rb" +require_relative "solar_system.rb" def main + solar_system = SolarSystem.new('Sol') earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') - puts earth.name + solar_system.add_planet(earth) mars = Planet.new('Mars', "red", 6.39e23, 2.279e8,'Mars is named after a mythological figure - the Roman god of war') - puts mars.summary + solar_system.add_planet(mars) + list = solar_system.list_planets + puts list + + found_planet = solar_system.find_planet_by_name('Earth') + puts found_planet + puts found_planet.summary + end main \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..4c12aa71 --- /dev/null +++ b/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) + @planets << planet + end + def list_planets + puts "Planets orbiting #{@star_name}" + counter = 0 + all_planets = "" + @planets.each do |planet| + all_planets += "#{counter += 1}.#{planet.name} " + end + return all_planets.split(' ') + end + + def find_planet_by_name(name) + @planets.each do |planet| + if planet.name.downcase == name.downcase + return planet + end + end + end +end + +# solar_system = SolarSystem.new('Sol') +# puts solar_system.star_name +# solar_system.add_planet('earth') +# solar_system.add_planet('mars') +# puts "#{solar_system.planets}" From 51d144bcd1c8fbc375c85d5d1382f31707021409 Mon Sep 17 00:00:00 2001 From: Nara Date: Mon, 25 Feb 2019 16:49:17 -0800 Subject: [PATCH 3/8] Updated Wave 2 methods --- main.rb | 22 +++++++++++++++++----- solar_system.rb | 7 +++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/main.rb b/main.rb index e9d8cf6c..70892f28 100644 --- a/main.rb +++ b/main.rb @@ -6,13 +6,25 @@ def main solar_system.add_planet(earth) mars = Planet.new('Mars', "red", 6.39e23, 2.279e8,'Mars is named after a mythological figure - the Roman god of war') solar_system.add_planet(mars) - list = solar_system.list_planets - puts list + venus = Planet.new('Venus', "yellow-white", 4.867e24, 1.082e8,'Venus is the second brightest natural object in the sky') + solar_system.add_planet(venus) + # list = solar_system.list_planets + # puts list - found_planet = solar_system.find_planet_by_name('Earth') + found_planet = solar_system.find_planet_by_name('Venuss') puts found_planet - puts found_planet.summary + puts found_planet.summary if found_planet.is_a? Planet -end + while true + puts "What would you like to do next? You can list planets or exit." + user_input = gets.chomp.downcase + if user_input == "exit" + return + elsif user_input == "list planets" + list = solar_system.list_planets + puts list + end + end +end main \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb index 4c12aa71..e132389a 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -19,11 +19,18 @@ def list_planets end def find_planet_by_name(name) + found = false @planets.each do |planet| if planet.name.downcase == name.downcase + found = true return planet + else + found = false end end + if found == false + return "This is not a planet!" + end end end From 2a420b674a3d81fd9580809873664aaf4bf29d91 Mon Sep 17 00:00:00 2001 From: Nara Date: Mon, 25 Feb 2019 22:23:47 -0800 Subject: [PATCH 4/8] Completed Wave 3 --- main.rb | 31 ++++++++++++++++++++++++------- planet.rb | 5 ++--- solar_system.rb | 7 +++++-- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/main.rb b/main.rb index 70892f28..6c3204c7 100644 --- a/main.rb +++ b/main.rb @@ -8,21 +8,38 @@ def main solar_system.add_planet(mars) venus = Planet.new('Venus', "yellow-white", 4.867e24, 1.082e8,'Venus is the second brightest natural object in the sky') solar_system.add_planet(venus) - # list = solar_system.list_planets - # puts list - - found_planet = solar_system.find_planet_by_name('Venuss') - puts found_planet - puts found_planet.summary if found_planet.is_a? Planet while true - puts "What would you like to do next? You can list planets or exit." + puts "What would you like to do? You can get planet details, list planets, add planet or exit." user_input = gets.chomp.downcase if user_input == "exit" return + elsif user_input == "list planets" list = solar_system.list_planets puts list + + elsif user_input == "planet details" + puts "What is the name of the planet you wish to learn about?" + name = gets.chomp + found_planet = solar_system.find_planet_by_name(name) + puts found_planet + puts found_planet.summary if found_planet.is_a? Planet + + elsif user_input == "add planet" + puts "What is the name of the planet?" + name = gets.chomp.downcase + puts "What color is the planet?" + color = gets.chomp.downcase + puts "What is the mass of the planet in kg?" + mass_kg = gets.chomp.to_i + puts "What is the distance from the sun in km?" + distance_from_sun_km = gets.chomp.to_i + puts "Give us a fun fact about the planet!" + fun_fact = gets.chomp + + name = Planet.new(name.capitalize, color, mass_kg, distance_from_sun_km, fun_fact) + solar_system.add_planet(name) end end diff --git a/planet.rb b/planet.rb index 3eebd5e4..cf8411fc 100644 --- a/planet.rb +++ b/planet.rb @@ -1,6 +1,7 @@ class Planet - attr_accessor :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) + raise ArgumentError.new("Both mass_kg and distance_from_sun_km must be a number that is greater than 0") if mass_kg.zero? || distance_from_sun_km.zero? @name = name @color = color @mass_kg = mass_kg @@ -12,5 +13,3 @@ def summary return "The planet name is #{@name}. Color of the planet is #{@color}. Planet weight is #{@mass_kg} kg. Distance from the sun is #{@distance_from_sun_km} km. #{@fun_fact}." end end - -# earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') \ No newline at end of file diff --git a/solar_system.rb b/solar_system.rb index e132389a..35ea89e1 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -8,13 +8,14 @@ def initialize(star_name) def add_planet(planet) @planets << planet end + def list_planets - puts "Planets orbiting #{@star_name}" counter = 0 all_planets = "" @planets.each do |planet| all_planets += "#{counter += 1}.#{planet.name} " end + puts "Planets orbiting #{@star_name}" return all_planets.split(' ') end @@ -29,8 +30,10 @@ def find_planet_by_name(name) end end if found == false - return "This is not a planet!" + return "#{name} is not a planet!" end + + end end From 6a372fb6b31a43025533f277da22641d593e33b1 Mon Sep 17 00:00:00 2001 From: Nara Date: Tue, 26 Feb 2019 13:47:16 -0800 Subject: [PATCH 5/8] Added colorize and terminal-table gems --- main.rb | 7 +++++-- planet.rb | 1 + solar_system.rb | 23 +++++++++++------------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/main.rb b/main.rb index 6c3204c7..7f738d2b 100644 --- a/main.rb +++ b/main.rb @@ -1,5 +1,7 @@ require_relative "planet.rb" require_relative "solar_system.rb" +require 'terminal-table' + def main solar_system = SolarSystem.new('Sol') earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life') @@ -8,6 +10,8 @@ def main solar_system.add_planet(mars) venus = Planet.new('Venus', "yellow-white", 4.867e24, 1.082e8,'Venus is the second brightest natural object in the sky') solar_system.add_planet(venus) + venus2 = Planet.new('Venus', "green-white", 4.867e24, 1.082e8,'Venus2 is the second brightest natural object in the sky') + solar_system.add_planet(venus2) while true puts "What would you like to do? You can get planet details, list planets, add planet or exit." @@ -23,7 +27,6 @@ def main puts "What is the name of the planet you wish to learn about?" name = gets.chomp found_planet = solar_system.find_planet_by_name(name) - puts found_planet puts found_planet.summary if found_planet.is_a? Planet elsif user_input == "add planet" @@ -44,4 +47,4 @@ def main end end -main \ No newline at end of file +main diff --git a/planet.rb b/planet.rb index cf8411fc..6c884a57 100644 --- a/planet.rb +++ b/planet.rb @@ -1,3 +1,4 @@ +require 'terminal-table' 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) diff --git a/solar_system.rb b/solar_system.rb index 35ea89e1..d6573156 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -1,3 +1,6 @@ +require 'terminal-table' +require 'colorize' + class SolarSystem attr_reader :star_name, :planets def initialize(star_name) @@ -11,12 +14,16 @@ def add_planet(planet) def list_planets counter = 0 - all_planets = "" + rows = [] @planets.each do |planet| - all_planets += "#{counter += 1}.#{planet.name} " + rows << ["#{counter += 1}".colorize(:light_blue), "#{planet.name} ".colorize(:light_green)] + end + table = Terminal::Table.new do |t| + t.title = "Planets orbiting #{@star_name}".colorize(:color => :red) + t.rows = rows + t.style = {:width => 30, :padding_left => 3, :border_x => "=", :border_i => "x"} end - puts "Planets orbiting #{@star_name}" - return all_planets.split(' ') + return table end def find_planet_by_name(name) @@ -32,13 +39,5 @@ def find_planet_by_name(name) if found == false return "#{name} is not a planet!" end - - end end - -# solar_system = SolarSystem.new('Sol') -# puts solar_system.star_name -# solar_system.add_planet('earth') -# solar_system.add_planet('mars') -# puts "#{solar_system.planets}" From e79aa8d7a90e4e02455c6de0f717cfb78017cb48 Mon Sep 17 00:00:00 2001 From: Nara Date: Tue, 26 Feb 2019 16:52:09 -0800 Subject: [PATCH 6/8] Added user input handler --- main.rb | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/main.rb b/main.rb index 7f738d2b..dee8b203 100644 --- a/main.rb +++ b/main.rb @@ -27,24 +27,27 @@ def main puts "What is the name of the planet you wish to learn about?" name = gets.chomp found_planet = solar_system.find_planet_by_name(name) + puts found_planet if !found_planet.is_a? Planet puts found_planet.summary if found_planet.is_a? Planet - - elsif user_input == "add planet" - puts "What is the name of the planet?" - name = gets.chomp.downcase - puts "What color is the planet?" - color = gets.chomp.downcase - puts "What is the mass of the planet in kg?" - mass_kg = gets.chomp.to_i - puts "What is the distance from the sun in km?" - distance_from_sun_km = gets.chomp.to_i - puts "Give us a fun fact about the planet!" - fun_fact = gets.chomp - name = Planet.new(name.capitalize, color, mass_kg, distance_from_sun_km, fun_fact) + elsif user_input == "add planet" + name = input_handle('name').capitalize + color = input_handle('color') + mass_kg = input_handle('mass_kg').to_i + distance_from_sun_km = input_handle('distance_from_sun_km').to_i + fun_fact = input_handle('fun_fact') + name = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) solar_system.add_planet(name) end end - +end +def input_handle(variable) + puts "What is the #{variable} of the planet" + variable = gets.chomp.downcase + while variable =='' + puts "Please type in a valid input" + variable = gets.chomp.downcase + end + return variable end main From fd3e490d6cb0cf2773fe97f0ab01a6965490469c Mon Sep 17 00:00:00 2001 From: Nara Date: Tue, 26 Feb 2019 22:03:07 -0800 Subject: [PATCH 7/8] Added a method to handle input validations. --- main.rb | 47 ++++++++++++++++++++++++++++++----------------- planet.rb | 3 ++- solar_system.rb | 4 ++-- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/main.rb b/main.rb index dee8b203..1b25a6dc 100644 --- a/main.rb +++ b/main.rb @@ -1,20 +1,41 @@ require_relative "planet.rb" require_relative "solar_system.rb" -require 'terminal-table' + +def input_handle(variable) + puts "What is the #{variable} of the planet" + variable = gets.chomp.downcase + while variable =='' + puts "Please type in a valid input." + variable = gets.chomp.downcase + end + return variable +end + +def validate_integer(variable) + puts "What is the #{variable} of the planet" + variable = gets.chomp.downcase.to_i + while variable <= 0 + puts "Input must a number greater than zero. Please try again." + variable = gets.chomp.downcase.to_i + end + return variable +end def main 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, 2.279e8,'Mars is named after a mythological figure - the Roman god of war') - solar_system.add_planet(mars) venus = Planet.new('Venus', "yellow-white", 4.867e24, 1.082e8,'Venus is the second brightest natural object in the sky') + mercury = Planet.new('Mercury', "dark gray", 3.30e23, 5.8e7,'Only two spacecraft have ever visited Mercuryß') + jupiter = Planet.new('Jupiter', "white, red, orange, brown, and yellow", 1.898e27, 7.785e8,'It is made primarily of gases and is therefore known as a "gas giant"') + solar_system.add_planet(earth) + solar_system.add_planet(mars) solar_system.add_planet(venus) - venus2 = Planet.new('Venus', "green-white", 4.867e24, 1.082e8,'Venus2 is the second brightest natural object in the sky') - solar_system.add_planet(venus2) + solar_system.add_planet(mercury) + solar_system.add_planet(jupiter) while true - puts "What would you like to do? You can get planet details, list planets, add planet or exit." + puts "What would you like to do? Choose from these options: [planet details], [list planets], [add planet] or [exit]." user_input = gets.chomp.downcase if user_input == "exit" return @@ -33,21 +54,13 @@ def main elsif user_input == "add planet" name = input_handle('name').capitalize color = input_handle('color') - mass_kg = input_handle('mass_kg').to_i - distance_from_sun_km = input_handle('distance_from_sun_km').to_i + mass_kg = validate_integer('mass_kg') + distance_from_sun_km = validate_integer('distance_from_sun_km') fun_fact = input_handle('fun_fact') name = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) solar_system.add_planet(name) end end end -def input_handle(variable) - puts "What is the #{variable} of the planet" - variable = gets.chomp.downcase - while variable =='' - puts "Please type in a valid input" - variable = gets.chomp.downcase - end - return variable -end + main diff --git a/planet.rb b/planet.rb index 6c884a57..a3b91b50 100644 --- a/planet.rb +++ b/planet.rb @@ -1,4 +1,5 @@ require 'terminal-table' +require 'colorize' 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) @@ -11,6 +12,6 @@ def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) end def summary - return "The planet name is #{@name}. Color of the planet is #{@color}. Planet weight is #{@mass_kg} kg. Distance from the sun is #{@distance_from_sun_km} km. #{@fun_fact}." + return "The planet name is #{@name}. Color of the planet is #{@color}. Planet mass is #{@mass_kg} kg. Distance from the sun is #{@distance_from_sun_km} km. #{@fun_fact}.".colorize(:magenta) end end diff --git a/solar_system.rb b/solar_system.rb index d6573156..012e59ff 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -21,7 +21,7 @@ def list_planets table = Terminal::Table.new do |t| t.title = "Planets orbiting #{@star_name}".colorize(:color => :red) t.rows = rows - t.style = {:width => 30, :padding_left => 3, :border_x => "=", :border_i => "x"} + t.style = {:width => 40, :padding_left => 3, :border_x => "=", :border_i => "x"} end return table end @@ -37,7 +37,7 @@ def find_planet_by_name(name) end end if found == false - return "#{name} is not a planet!" + return "#{name} is not a planet. However, you can create your own planet!" end end end From b895a766340558c6804ea01563df2ca4a79ed800 Mon Sep 17 00:00:00 2001 From: Nara Date: Tue, 26 Feb 2019 22:10:45 -0800 Subject: [PATCH 8/8] Added capitalize method to show a user input in a sentence --- main.rb | 4 ++-- solar_system.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main.rb b/main.rb index 1b25a6dc..fa5cbe45 100644 --- a/main.rb +++ b/main.rb @@ -52,12 +52,12 @@ def main puts found_planet.summary if found_planet.is_a? Planet elsif user_input == "add planet" - name = input_handle('name').capitalize + name = input_handle('name') color = input_handle('color') mass_kg = validate_integer('mass_kg') distance_from_sun_km = validate_integer('distance_from_sun_km') fun_fact = input_handle('fun_fact') - name = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) + name = Planet.new(name.capitalize, color, mass_kg, distance_from_sun_km, fun_fact) solar_system.add_planet(name) end end diff --git a/solar_system.rb b/solar_system.rb index 012e59ff..73263a83 100644 --- a/solar_system.rb +++ b/solar_system.rb @@ -37,7 +37,7 @@ def find_planet_by_name(name) end end if found == false - return "#{name} is not a planet. However, you can create your own planet!" + return "#{name.capitalize} is not a planet. However, you can create your own planet!" end end end