Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 219 additions & 0 deletions ss_wave_2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@

class SolarSystem
attr_accessor :ss

def initialize(planet_1, planet_2 )
@ss = [planet_1, planet_2]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this certainly works, we were expecting you to pass a list of planets directly into SolarSystem.new, rather than passing planets one by one. So something like:

class SolarSystem
  attr_accessor :ss
  def initialize(planets)
    @ss = planets
  end
  # ... rest of the class ...
end

planet_a = Planet.new("Jupiter","1882","details")
planet_b = Planet.new("Uranus", "1932","about")
planet_list = [planet_a, planet_b]

my_ss = SolarSystem.new(planet_list)

That way whoever creates the instance of SolarSystem can create it with as many planets as needed.

end

# adds another planet
def add_planet(new_planet)
@ss << new_planet

end
# prints the planets out in a list
def planet_list
list = []
count = 0
@ss.each do |planets|
count += 1
list << "#{count}.#{planets.planet_name}\n"

end
return list
end

def pick_planet(user_pick)
@ss.each do |planets|
if planets.planet_name == user_pick
puts "#{planets.details}\n\n\n#{planets.planet_attributes}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the idea of this method! One way you might make it more flexible is to return the Planet itself, rather than putsing a string. That way, the caller could do whatever they like with it.


end
end
end
end

class Planet
attr_accessor :planet_name, :discovery, :attributes


def initialize(planet_name, discovery, attributes)
@planet_name = planet_name
@discovery = "#{planet_name} was discovered in the year #{discovery}."
# to define the values of the attributes maybe create a method that sets
# sets user input to a variable and iterates through the the keys reassining their
# values based on user response to prompts
@attributes = {mass: "", gravity: "", orbit: "", day_length: "", yr_length: ""}

end

def planet_name
return @planet_name

end

def details
return @discovery

end

def change_value(user_key, user_value)
@attributes = {}
count = 0
@attributes[:user_key] = user_value
while count < 3
puts "what attribute would you like to add?(i.e. gravity, mass, length of a day.)"
user_key = gets.chomp
puts "what is the value of #{user_key}? (i.e. mass is 317.8)"
user_value = gets.chomp
@attributes[:user_key] = user_value
end

return @attributes

end

def planet_attributes
about_planet = []
@attributes.each do |key, value|
about_planet << "#{@planet_name}'s #{key} is #{value}.\n"

end
puts about_planet
end
end


# Create a method that pre-populates the solar system with 8 planets
planet_a = Planet.new("Jupiter","1882","details")
planet_b = Planet.new("Uranus", "1932","about")


# Prints out current planets in the solar system
my_ss = SolarSystem.new(planet_a, planet_b)

def user_choice(solar_system,planet_choice)

if solar_system.planet_list.include?(planet_choice)
return solar_system.pick_planet(planet_choice)
else
while solar_system.planet_list.include?(planet_choice) == false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditions for the if and the while here are a little redundant. Maybe this code could be refactored to something like:

planet_choice = nil
until solar_system.planet_list.include?(planet_choice)
  puts "Please choose a planet"
  planet_choice = gets.chomp.downcase.capitalize
end
solar_system.pick_planet(planet_choice)

print "Please enter a planet on the list.\s"
planet_choice = gets.chomp.downcase.capitalize
return solar_system.pick_planet(planet_choice)

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you have a return in your while loop here, the loop body will only ever execute once!


end
end


def know_another_question
puts "Would you like to know more about another planet? Enter (Y/N)\s"

end

def which_planet_question
puts "which planet would you like to know more about?"

end

def create_planet_question
puts "Would you like to add a planet?"

end
def user_planet_name
puts "what would you like to name your planet?"

end


# defines and adds a new planet to the SolarSystem
def add_planet(name, date_discovered,input)
planet = Planet.new(name, date_discovered, input)
return planet

end

# User interface
puts "Welcome to the Solar System App!"
puts "Click around and see what you can find!"

puts my_ss.planet_list

# Checks to see if user wants to know more about the planets in the solar system
print "Would you like to know more about a particular planet? Enter (Y/N)\s"
know_more = gets.chomp.downcase
# If the user wants to see more about the solar system check to see which planet
if know_more == "y" || know_more == "yes"
which_planet_question()
which_planet = gets.chomp
user_choice(my_ss,which_planet)
know_another_question()
know_another = gets.chomp.downcase
while know_another == "y" || know_another == "yes"
which_planet_question()
which_planet = gets.chomp
user_choice(my_ss, know_another)
know_another_question
know_another = gets.chomp.downcase
end
case know_another
when "n","no"
create_planet_question
user_create = gets.chomp.downcase
if user_create == "y" || user_create == "yes"
user_planet_name
user_planet = gets.chomp
puts "when was your planet discovered?"
user_planet_details = gets.chomp
puts "how many attributes does your planet have?"
attributes = gets.chomp
add_planet(user_planet, user_planet_details, attributes)
# puts here to test
puts "what attribute would you like to add?(i.e. gravity, mass, length of a day.)"
user_key = gets.chomp
puts "what is the value of #{user_key}? (i.e. mass is 317.8)"
user_value = gets.chomp
change_value(user_key, user_value)

planet.planet_attributes

my_ss.add_planet(planet)


puts "You have added a new planet to the solar system!"
else
puts "Thank you for using the Solar System App!"
end

end
elsif know_more == "n" || know_more == "no"
create_planet_question
user_create = gets.chomp.downcase
if user_create == "y" || user_create == "yes"
user_planet_name
user_planet = gets.chomp
puts "when was your planet discovered?"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is almost exactly the same as the planet creation code you've got starting on line 166. Could you consolidate the two somehow?

user_planet_details = gets.chomp
puts "how many attributes does your planet have?"
attributes = gets.chomp
add_planet(user_planet, user_planet_details, attributes)
# puts here to test
puts "what attribute would you like to add?(i.e. gravity, mass, length of a day.)"
user_key = gets.chomp
puts "what is the value of #{user_key}? (i.e. mass is 317.8)"
user_value = gets.chomp
change_value(user_key, user_value)

planet.planet_attributes

my_ss.add_planet(planet)


puts "You have added a new planet to the solar system!"
else
puts "Thank you for using the Solar System App!"
end
end