From 2f4649f531a9ce427d7d37c4febc6dde064d2c09 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Thu, 8 Feb 2018 14:23:28 -0800 Subject: [PATCH 1/6] Create file --- solar-system.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 solar-system.rb diff --git a/solar-system.rb b/solar-system.rb new file mode 100644 index 00000000..e69de29b From 4c057e11f081e5c3f711bfdb3f2aba89cd8a3202 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Thu, 8 Feb 2018 15:38:58 -0800 Subject: [PATCH 2/6] Create initialize --- solar-system.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/solar-system.rb b/solar-system.rb index e69de29b..3067eaef 100644 --- a/solar-system.rb +++ b/solar-system.rb @@ -0,0 +1,18 @@ +# Define a solar system class +class Solar_System + +attr_accessor :planets +def initialize(planets) + @planets = planets +end + +def summary + return @planets +end + +end + + +planet_array =["Mercury", "Venus", "Earth"] +our_solar_system = Solar_System.new(planet_array) +puts our_solar_system.summary From 769bfd2659ca4b57017ee18fbec59de990709a0b Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Thu, 8 Feb 2018 15:54:54 -0800 Subject: [PATCH 3/6] Add Plannets --- solar-system.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/solar-system.rb b/solar-system.rb index 3067eaef..ac8c2093 100644 --- a/solar-system.rb +++ b/solar-system.rb @@ -6,6 +6,11 @@ def initialize(planets) @planets = planets end +def add(new_planet) + @planets.push(new_planet) + +end + def summary return @planets end @@ -15,4 +20,5 @@ def summary planet_array =["Mercury", "Venus", "Earth"] our_solar_system = Solar_System.new(planet_array) +our_solar_system.add("Mars") puts our_solar_system.summary From 2f1a81ac6b7bcb1cc6c89d9b09f3b05751950d6f Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Mon, 12 Feb 2018 04:05:17 -0800 Subject: [PATCH 4/6] final --- solar-system.rb | 216 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 203 insertions(+), 13 deletions(-) diff --git a/solar-system.rb b/solar-system.rb index ac8c2093..2334b5e3 100644 --- a/solar-system.rb +++ b/solar-system.rb @@ -1,24 +1,214 @@ -# Define a solar system class -class Solar_System +# Define the class Solar System +class SolarSystem + attr_reader :planets, :age -attr_accessor :planets -def initialize(planets) - @planets = planets + # Initialize @planets as an array of planet instances + def initialize(planets) + @planets = planets + @age = 4600 # in million of years + end + + # Method to add planets to the array + def add_planet(planet_to_add) + @planets << planet_to_add + end + + # Create an array of planet names + def planet_name_array + @planet_names = [] + @planets.each do |planet_object| + planet_name = planet_object.name.downcase + @planet_names << planet_name + end + @planet_names + end + + # Generates a list of planet names + def list_planets + planet_list = '' + i = 1 + @planet_names.each do |planet_name| + planet_list << "#{i}. #{planet_name.capitalize}\n" + i += 1 + end + planet_list + end + + # This method finds the distance assuming they are in a straight line out from the sun. + def find_distance_between_planets(planet_1, planet_2) + information_hash = {} + information_hash[:planet_1] = @planets[planet_1.to_i].name + information_hash[:planet_2] = @planets[planet_2.to_i].name + distance = @planets[planet_1.to_i].distance_from_sun - @planets[planet_2.to_i].distance_from_sun + information_hash[:distance] = distance.abs + + information_hash + end +end # End of Solar System class + +class Planet + attr_reader :name, :diameter, :distance_from_sun, :year_length, :color + + def initialize(attributes = {}) + @name = attributes[:name] + @diameter = attributes[:diameter] # measured in km + @distance_from_sun = attributes[:distance_from_sun] # measured in million km + @year_length = attributes[:year_length] # measured in Earth years + @color = attributes[:color] + end + + def return_attributes + planet_summary = " + Name: #{@name} + Diameter: #{@diameter} + Color: #{@color} + Length of Year (in Earth years): #{@year_length} + Distance from the sun: #{@distance_from_sun} million km" + planet_summary + end +end # End of Planet class + +# Initial message used to let the determine how they want to interact with the program. +def get_choice + puts "\nThank you for using our Solar System Explorer! Here are your options: + (A) Add a planet to the list + (B) View a planet + (C) Exit the program" + print "\nPlease select a letter: " + choice = gets.chomp.upcase + choice end -def add(new_planet) - @planets.push(new_planet) +# If the user enters a planet name the first time, it will be accepted. If the user does not enter a correct number or name it will prompt them to enter an existing number until they do. +def valid_entry(solar_system) + print 'Select a planet: ' + planet_number = gets.chomp + # If user does not enter a valid name or number they will be prompted to enter again. + until (planet_number.to_i.to_s == planet_number) && (planet_number.to_i <= solar_system.num_of_planets && planet_number.to_i > 0) + print 'Please select a valid number to represent a planet: ' + planet_number = gets.chomp + end + planet_number = planet_number.to_i - 1 + planet_number end -def summary - return @planets +# (Choice B) +def view_planet_summary(solar_system) + puts "Choose a planet to view some if it's information: " + puts solar_system.list_planets + puts + planet_chosen = valid_entry(solar_system) + puts solar_system.planets[planet_chosen].return_attributes end +# (Choice A) +def create_new_planet + planet = {} + planet[:name] = '' + while planet[:name] == '' + puts 'Please enter the name of the planet you would like to add: ' + planet[:name] = gets.chomp.capitalize + end + puts 'Please provide the following attributes.' + print 'Diameter(in km): ' + planet[:diameter] = gets.chomp + print "Distance from the Sun (or it's star) (units: million km): " + planet[:distance_from_sun] = gets.chomp + print 'Length of Year (units: Earth years): ' + planet[:year_length] = gets.chomp + print 'Color: ' + planet[:color] = gets.chomp + + planet_object = Planet.new(planet) + planet_object end +# (Choice B) -planet_array =["Mercury", "Venus", "Earth"] -our_solar_system = Solar_System.new(planet_array) -our_solar_system.add("Mars") -puts our_solar_system.summary +def view_planet_summary(solar_system) + puts "Choose a planet to view some if it's information: " + puts solar_system.list_planets + puts solar_system.planets[planet_chosen].return_attributes +end + +# (Choice C) + +def leave_program + puts 'Thank you! Have a nice day!' + exit +end + +# Pre-populated solar system +# distance from sun is represented in million km. +planets_in_ss = [ + { + name: 'Mercury', + diameter: 4_878, + color: 'gray', + year_length: 0.241, + distance_from_sun: 57.9 + }, + { + name: 'Venus', + diameter: 12_104, + color: 'yellow', + year_length: 0.615, + distance_from_sun: 108.2 + }, + { + name: 'Earth', + diameter: 12_760, + color: 'blue', + year_length: 1, + distance_from_sun: 149.6 + }, + { + name: 'Mars', + diameter: 6_787, + color: 'red', + year_length: 1.88, + distance_from_sun: 227.9 + }, + { + name: 'Jupiter', + diameter: 139_822, + color: 'orange and white', + year_length: 11.86, + distance_from_sun: 778.3 + }, + { + name: 'Saturn', + diameter: 120_500, + color: 'pale gold', + year_length: 29.46, + star_system: 'Solar', + distance_from_sun: 1427 + } + +] + +# Creates an array of planet objects based on the planet hashes defined above. +solar_system_planets = [] +planets_in_ss.each do |planet_hash| + solar_system_planets << Planet.new(planet_hash) +end + +# Creates a new solar system instance based on the planet instances +solar_system_object = SolarSystem.new(solar_system_planets) + +# User interface +loop do + choice = get_choice + case choice + when 'A' + new_planet = create_new_planet + solar_system_object.add_planet(new_planet) + when 'B' + view_planet_summary(solar_system_object) + when 'C' + leave_program + else + puts "\nI'm sorry that's not a valid selection. Please try again." + end +end From f5ab92abe959bf78f112981814cc67629dd86036 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Mon, 12 Feb 2018 20:34:22 -0800 Subject: [PATCH 5/6] Fixed error with the conditionals --- solar-system.rb | 57 ++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/solar-system.rb b/solar-system.rb index 2334b5e3..aee6fdb3 100644 --- a/solar-system.rb +++ b/solar-system.rb @@ -1,11 +1,12 @@ # Define the class Solar System class SolarSystem - attr_reader :planets, :age + attr_reader :planets, :age, :planet_names # Initialize @planets as an array of planet instances def initialize(planets) @planets = planets @age = 4600 # in million of years + @planet_names = [] end # Method to add planets to the array @@ -14,24 +15,24 @@ def add_planet(planet_to_add) end # Create an array of planet names - def planet_name_array - @planet_names = [] - @planets.each do |planet_object| - planet_name = planet_object.name.downcase - @planet_names << planet_name - end - @planet_names - end + # def planet_name_array + # @planet_names = [] + # @planets.each do |planet_object| + # planet_name = planet_object.name.downcase + # @planet_names << planet_name + # end + # @planet_names + # end # Generates a list of planet names def list_planets planet_list = '' - i = 1 - @planet_names.each do |planet_name| - planet_list << "#{i}. #{planet_name.capitalize}\n" + i = 0 + @planets.each do |planet| i += 1 + planet_list << "#{i}. #{planet.name.capitalize}\n" end - planet_list + return planet_list end # This method finds the distance assuming they are in a straight line out from the sun. @@ -42,7 +43,7 @@ def find_distance_between_planets(planet_1, planet_2) distance = @planets[planet_1.to_i].distance_from_sun - @planets[planet_2.to_i].distance_from_sun information_hash[:distance] = distance.abs - information_hash + return information_hash end end # End of Solar System class @@ -64,7 +65,7 @@ def return_attributes Color: #{@color} Length of Year (in Earth years): #{@year_length} Distance from the sun: #{@distance_from_sun} million km" - planet_summary + return planet_summary end end # End of Planet class @@ -76,7 +77,7 @@ def get_choice (C) Exit the program" print "\nPlease select a letter: " choice = gets.chomp.upcase - choice + return choice end # If the user enters a planet name the first time, it will be accepted. If the user does not enter a correct number or name it will prompt them to enter an existing number until they do. @@ -84,22 +85,14 @@ def valid_entry(solar_system) print 'Select a planet: ' planet_number = gets.chomp + # If user does not enter a valid name or number they will be prompted to enter again. - until (planet_number.to_i.to_s == planet_number) && (planet_number.to_i <= solar_system.num_of_planets && planet_number.to_i > 0) + until (planet_number.to_i.to_s == planet_number) && (planet_number.to_i <= solar_system.planets.length && planet_number.to_i > 0) print 'Please select a valid number to represent a planet: ' planet_number = gets.chomp end planet_number = planet_number.to_i - 1 - planet_number -end - -# (Choice B) -def view_planet_summary(solar_system) - puts "Choose a planet to view some if it's information: " - puts solar_system.list_planets - puts - planet_chosen = valid_entry(solar_system) - puts solar_system.planets[planet_chosen].return_attributes + return planet_number end # (Choice A) @@ -110,7 +103,7 @@ def create_new_planet puts 'Please enter the name of the planet you would like to add: ' planet[:name] = gets.chomp.capitalize end - puts 'Please provide the following attributes.' + puts "Please provide the following attributes." print 'Diameter(in km): ' planet[:diameter] = gets.chomp print "Distance from the Sun (or it's star) (units: million km): " @@ -121,14 +114,14 @@ def create_new_planet planet[:color] = gets.chomp planet_object = Planet.new(planet) - planet_object + return planet_object end # (Choice B) - def view_planet_summary(solar_system) - puts "Choose a planet to view some if it's information: " + puts "Choose a planet to view some of it's information: " puts solar_system.list_planets + planet_chosen = valid_entry(solar_system) puts solar_system.planets[planet_chosen].return_attributes end @@ -198,7 +191,7 @@ def leave_program solar_system_object = SolarSystem.new(solar_system_planets) # User interface -loop do +while true choice = get_choice case choice when 'A' From 23d6fdce6213964499b907690dcec49979f0eedb Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Mon, 12 Feb 2018 20:40:23 -0800 Subject: [PATCH 6/6] Fixed indentation and used Atom beutify --- solar-system.rb | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/solar-system.rb b/solar-system.rb index aee6fdb3..16f538af 100644 --- a/solar-system.rb +++ b/solar-system.rb @@ -14,16 +14,6 @@ def add_planet(planet_to_add) @planets << planet_to_add end - # Create an array of planet names - # def planet_name_array - # @planet_names = [] - # @planets.each do |planet_object| - # planet_name = planet_object.name.downcase - # @planet_names << planet_name - # end - # @planet_names - # end - # Generates a list of planet names def list_planets planet_list = '' @@ -32,7 +22,7 @@ def list_planets i += 1 planet_list << "#{i}. #{planet.name.capitalize}\n" end - return planet_list + planet_list end # This method finds the distance assuming they are in a straight line out from the sun. @@ -43,7 +33,7 @@ def find_distance_between_planets(planet_1, planet_2) distance = @planets[planet_1.to_i].distance_from_sun - @planets[planet_2.to_i].distance_from_sun information_hash[:distance] = distance.abs - return information_hash + information_hash end end # End of Solar System class @@ -65,7 +55,7 @@ def return_attributes Color: #{@color} Length of Year (in Earth years): #{@year_length} Distance from the sun: #{@distance_from_sun} million km" - return planet_summary + planet_summary end end # End of Planet class @@ -77,7 +67,7 @@ def get_choice (C) Exit the program" print "\nPlease select a letter: " choice = gets.chomp.upcase - return choice + choice end # If the user enters a planet name the first time, it will be accepted. If the user does not enter a correct number or name it will prompt them to enter an existing number until they do. @@ -85,14 +75,13 @@ def valid_entry(solar_system) print 'Select a planet: ' planet_number = gets.chomp - # If user does not enter a valid name or number they will be prompted to enter again. until (planet_number.to_i.to_s == planet_number) && (planet_number.to_i <= solar_system.planets.length && planet_number.to_i > 0) print 'Please select a valid number to represent a planet: ' planet_number = gets.chomp end planet_number = planet_number.to_i - 1 - return planet_number + planet_number end # (Choice A) @@ -103,7 +92,7 @@ def create_new_planet puts 'Please enter the name of the planet you would like to add: ' planet[:name] = gets.chomp.capitalize end - puts "Please provide the following attributes." + puts 'Please provide the following attributes.' print 'Diameter(in km): ' planet[:diameter] = gets.chomp print "Distance from the Sun (or it's star) (units: million km): " @@ -114,7 +103,7 @@ def create_new_planet planet[:color] = gets.chomp planet_object = Planet.new(planet) - return planet_object + planet_object end # (Choice B) @@ -191,7 +180,7 @@ def leave_program solar_system_object = SolarSystem.new(solar_system_planets) # User interface -while true +loop do choice = get_choice case choice when 'A'