From f68a94ff5d2b5e5056368dad93b2ebbcb8ae7f75 Mon Sep 17 00:00:00 2001 From: vertige Date: Mon, 14 Aug 2017 14:50:27 -0700 Subject: [PATCH 1/3] Clarify instructions Misleading instructions, later clarified in class, that created a lot of confusion about Wave 1. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b58c75ff..e0ded512 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Let's make a planetary system! 6. Hoth ``` - Write code to test your SolarSystem -- Instead of Strings for planets, modify SolarSystem's `initialize` method to take a list of hashes where each planet is sent as a hash with at least 5 attributes. +- Instead of Strings for planets, modify `class SolarSystem` methods to take a list of hashes where each planet is sent as a hash with at least 5 attributes. ## Optional Enhancements - Give each planet a `year_length` attribute which is the length of time the planet takes to go around it's star. From 0267742beb9de3de6810d6fb3638a820f93fae2c Mon Sep 17 00:00:00 2001 From: vertige Date: Wed, 16 Aug 2017 07:21:00 -0700 Subject: [PATCH 2/3] Jessica Owens -- Carets --- 02.1_Solar_System.rb | 172 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 02.1_Solar_System.rb diff --git a/02.1_Solar_System.rb b/02.1_Solar_System.rb new file mode 100644 index 00000000..74a6554f --- /dev/null +++ b/02.1_Solar_System.rb @@ -0,0 +1,172 @@ +### Solar System + +## Setup Classes +class SolarSystem + AGE = 4_600 + attr_reader :planets + + def initialize(planets_array) + @planets = planets_array + end + + def add_planet(planet) + @planets << planet + end + + def planet_list + display = Array.new + + @planets.each_index do |i| + display << "#{i + 1}. #{planets[i].name}\n" + end + + return display + end + +end # End of Class SolarSystem + +class Planet + EARTH_YEAR = 365.2564 + + attr_accessor :user_name, :user_days, :user_age, :user_distance, :user_mass, :user_moons, :user_life + attr_reader :name, :earth_days, :earth_age, :distance_from_the_sun, :earth_mass, :has_moons, :known_life, :rel_age + + def initialize(name, year, age, distance, mass, moons, life) + @name = name + @earth_days = year + @earth_age = age + @distance_from_the_sun = distance + @earth_mass = mass + @has_moons = moons + @known_life = life + end + + def user_input #no input checks + puts "Please answer the following:" + + print "Planet name: " + @user_name = gets.chomp.capitalize + + print "Please enter its year length (in Earth days): " + @user_days = gets.chomp + + print "Please enter its age (in millions of Earth years): " + @user_age = gets.chomp + + print "Please enter the distance from its sun (in AU): " + @user_distance = gets.chomp + + print "Please enter the mass in relation to Earth: " + @user_mass = gets.chomp + + print "Does your planet have moons? " + @user_moons = gets.chomp + case @user_moons + when "yes","y","true" + @user_moons = true + else + @user_moons = false + end + + print "Does your planet have known life? " + @user_life = gets.chomp.downcase + case @user_life + when "yes","y","true" + @user_life = true + else + @user_life = false + end + + return Planet.new(@user_name, @user_days, @user_age, @user_distance, @user_mass, @user_moons, @user_life) + + end + + def rel_age + return (SolarSystem::AGE - @earth_age.to_i) * Planet::EARTH_YEAR / @earth_days.to_f + end + + def prettify + bullet = "\n - " + output = "\nPlanet: #{@name}#{bullet}#{@earth_days} Earth days per year#{bullet}#{earth_age} million Earth years old (#{rel_age.to_i} #{@name} years)#{bullet}#{@distance_from_the_sun}AU from its sun#{bullet}#{@earth_mass} times the weight of Earth" + if @has_moons + output += "#{bullet}has moons!" + else + output += "#{bullet}doesn't have any moons :(" + end + if @known_life + output += "#{bullet}has life!" + end + return output + end + +end # End of Class Planet + + +## SETUP METHODS +def user_prompt + print "(or \"add\" or \"exit\"): " + user_input = gets.chomp.downcase + return user_input +end + + +## DEFINE PLANETS & TEST +planet_1 = Planet.new("Mercury", 87.969, 3_000, 0.387, 0.0553, false, false) +planet_2 = Planet.new("Venus", 224.7, 1_000, 0.723, 0.815, false, false) +planet_3 = Planet.new("Earth", Planet::EARTH_YEAR, 500, 1, 1, true, true) +planet_4 = Planet.new("Mars", 687, 1.52, 1_200, 0.107, true, false) +planet_5 = Planet.new("Jupiter", 4_332.59, 3_900, 5.2, 317.8, true, false) +planet_6 = Planet.new("Saturn", 10_759, 400, 9.58, 95.2, true, false) + +this_system = SolarSystem.new([planet_1, planet_2, planet_3, planet_4, planet_5, planet_6]) + +## USER INTERACTION +puts "========================================\n\n" +puts "WELCOME TO OUR SOLAR SYSTEM\n\n" +puts "You can read about the following planets:\n" +puts this_system.planet_list +puts "\n========================================\n" +puts "(Enter \"add\" to add a planet yourself)" +puts "(Enter \"exit\" to quit this program)" +print "\nSelect a planet you would like to know more about: " + +selection = gets.chomp.downcase + +valid = false + +until selection == "exit" + valid = false + + this_system.planets.each_index do |i| + while (i + 1).to_s == selection || this_system.planets[i].name.downcase == selection + valid = true + puts this_system.planets[i].prettify + print "\nPick another planet " + selection = user_prompt + end + end + + if !valid && selection != "add" + print "Invalid selection, try again " + selection = user_prompt + end + + if selection == "add" + puts "OK, Let's add a planet!" + + valid = false + planet_pi = Planet.new(nil,nil,nil,nil,nil,nil,nil) + this_system.add_planet (planet_pi.user_input) + + puts "\nGreat, you have just added:" + puts this_system.planets[-1].prettify + puts "\n" + puts this_system.planet_list + + puts "\nPick another planet " + selection = user_prompt + + end +end + +puts "Thank you for visiting this Solar System!" From c57a5de1561db691cdf93ab2c2c19647f35b598b Mon Sep 17 00:00:00 2001 From: vertige Date: Wed, 16 Aug 2017 17:00:46 -0700 Subject: [PATCH 3/3] Changed user planet input from Class method to general method --- 02.1_Solar_System.rb | 86 ++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/02.1_Solar_System.rb b/02.1_Solar_System.rb index 74a6554f..2a68b5b3 100644 --- a/02.1_Solar_System.rb +++ b/02.1_Solar_System.rb @@ -1,4 +1,5 @@ ### Solar System +require "awesome_print" ## Setup Classes class SolarSystem @@ -41,46 +42,6 @@ def initialize(name, year, age, distance, mass, moons, life) @known_life = life end - def user_input #no input checks - puts "Please answer the following:" - - print "Planet name: " - @user_name = gets.chomp.capitalize - - print "Please enter its year length (in Earth days): " - @user_days = gets.chomp - - print "Please enter its age (in millions of Earth years): " - @user_age = gets.chomp - - print "Please enter the distance from its sun (in AU): " - @user_distance = gets.chomp - - print "Please enter the mass in relation to Earth: " - @user_mass = gets.chomp - - print "Does your planet have moons? " - @user_moons = gets.chomp - case @user_moons - when "yes","y","true" - @user_moons = true - else - @user_moons = false - end - - print "Does your planet have known life? " - @user_life = gets.chomp.downcase - case @user_life - when "yes","y","true" - @user_life = true - else - @user_life = false - end - - return Planet.new(@user_name, @user_days, @user_age, @user_distance, @user_mass, @user_moons, @user_life) - - end - def rel_age return (SolarSystem::AGE - @earth_age.to_i) * Planet::EARTH_YEAR / @earth_days.to_f end @@ -109,6 +70,45 @@ def user_prompt return user_input end +def user_planet_input #no input checks + puts "Please answer the following:" + + print "Planet name: " + user_name = gets.chomp.capitalize + + print "Please enter its year length (in Earth days): " + user_days = gets.chomp + + print "Please enter its age (in millions of Earth years): " + user_age = gets.chomp + + print "Please enter the distance from its sun (in AU): " + user_distance = gets.chomp + + print "Please enter the mass in relation to Earth: " + user_mass = gets.chomp + + print "Does your planet have moons? " + user_moons = gets.chomp + case user_moons + when "yes","y","true" + user_moons = true + else + user_moons = false + end + print "Does your planet have known life? " + user_life = gets.chomp.downcase + case user_life + when "yes","y","true" + user_life = true + else + user_life = false + end + + # return Planet.new(@user_name, @user_days, @user_age, @user_distance, @user_mass, @user_moons, @user_life) + return [user_name, user_days, user_age, user_distance, user_mass, user_moons, user_life] +end + ## DEFINE PLANETS & TEST planet_1 = Planet.new("Mercury", 87.969, 3_000, 0.387, 0.0553, false, false) @@ -153,10 +153,10 @@ def user_prompt if selection == "add" puts "OK, Let's add a planet!" - valid = false - planet_pi = Planet.new(nil,nil,nil,nil,nil,nil,nil) - this_system.add_planet (planet_pi.user_input) + + user_planet_array = user_planet_input + this_system.add_planet (Planet.new(user_planet_array[0],user_planet_array[1],user_planet_array[2],user_planet_array[3],user_planet_array[4],user_planet_array[5],user_planet_array[6])) puts "\nGreat, you have just added:" puts this_system.planets[-1].prettify