Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ports - Kim #31

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
59 changes: 59 additions & 0 deletions lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require_relative "planet"
require_relative "solar_system"

# method that creates an instance of a solar system and adds instances of planets to it
# also, creates a control panel for the user
def main
# creating an instance of a solar system
big_ol_pupper = SolarSystem.new("Big Ol Pupper")

# creating instances of planets
waffles = Planet.new("Waffles", "\'maple syrup\' brown", 3.555e23, 1.273e12, "It is the only known " \
"planet to be smothered in fried chicken")
cuddles = Planet.new("Cuddles", "soft velvet grey", 9.973e30, 0.000002, "This planet loves the Big Ol " \
"Pupper so much that it can\'t get close enough to it")
whiskey = Planet.new("Whiskey", "rich amber", 4.342e27, 3.473e13, "This planet has been aged for" \
"billions of years in a giant cosmic cedar barrel")
bear = Planet.new("Bear", "jet black", 5.342e20, 5.342e14, "Contains more hugs per capita than any other" \
"known planet")
pancake = Planet.new("Pancake", "light brown", 7.888e24, 3.342e5, "There is no planet fluffier or " \
"flatter than Pancake")

# adding instances of planets to an array
big_ol_pupper.add_planet(waffles)
big_ol_pupper.add_planet(cuddles)
big_ol_pupper.add_planet(whiskey)
big_ol_pupper.add_planet(bear)
big_ol_pupper.add_planet(pancake)

# control panel that allows users to list planets, look at planet detials, add planets, or exit program
selection = "list planets"
while ["list planets", "planet details", "add planet", "exit"].include?(selection)
print "\nWhat would you like to do next? (list planets, planet details, add planet, or exit): "
selection = gets.chomp.downcase

until ["list planets", "planet details", "add planet", "exit"].include?(selection)
print "Whoopsie Daisy! Please select an option from the list (list planets, planet details, add planet, or exit): "
selection = gets.chomp.downcase
end

case selection
when "list planets"
puts "\n#{big_ol_pupper.list_planets}"
when "planet details"
print "\nWhat planet would you like to see details for?: "
planet_details = gets.chomp.downcase
found_planet = big_ol_pupper.find_planet_by_name(planet_details)
puts "\n====== #{found_planet.name} ======"
puts "#{found_planet.summary}"
when "add planet"
big_ol_pupper.add_new_planet
puts "\n========= New planet has been added to the Big Ol Pupper solar system! <3 =========="
when "exit"
break
end
end
end

# runs main method
main
20 changes: 20 additions & 0 deletions lib/planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# class that creates instances of planets
class Planet
attr_accessor :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact

# constructor that initializes a name, color, mass (kg), distance from the sun (km), and fun fact
# about each instance of a planet
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

# method that prints out a summary of a planet instance
def summary
return "#{@name} is a #{@color} planet with a mass of #{@mass_kg} kg and is #{@distance_from_sun_km} " \
"km from the sun! DID YOU KNOW: #{@fun_fact}?!?!"
end
end
49 changes: 49 additions & 0 deletions lib/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# class that is responsible for keeping track of a collection of instances of Planet
class SolarSystem
# creating methods to read/return star_name & planets
attr_reader :star_name, :planets

# constructor that accepts and stores star_name in an instance variable
# and assigns an empty array to the instance variable planets
def initialize(star_name)
@star_name = star_name
@planets = []
end

# method that adds a planet to the @planets array
def add_planet(planet)
@planets << planet
end

# method that returns a list of all planets in a given solar system
def list_planets
planet_list = "Doggos orbiting #{@star_name}: "
@planets.each_with_index { |planet, i| planet_list += "\n#{i + 1}. #{planet.name}" }
return planet_list
end

# method that returns the instance of a planet when given a string of its name
def find_planet_by_name(planet_string)
@planets.each do |planet|
if planet.name == planet_string.capitalize
return planet
end
end
end

# method that adds a new planet to the existing solar system
def add_new_planet

Choose a reason for hiding this comment

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

I suggest moving this method to main.rb to separate the user interface role from the role tracking and organizing planets.

print "\nWhat is the name of the planet you would like to add?: "
new_planet = gets.chomp.capitalize
print "What is the color of the planet your are adding?: "
new_planet_color = gets.chomp.downcase
print "What is the mass of the planet you are adding (in kg)?: "
new_planet_mass = gets.chomp
print "What is the distance of this planet from Big Ol Pupper (in km)?: "
new_planet_distance = gets.chomp
print "What is a fun fact about this planet?: "
new_planet_fun_fact = gets.chomp.capitalize
new_planet_final = Planet.new(new_planet, new_planet_color, new_planet_mass, new_planet_distance, new_planet_fun_fact)
add_planet(new_planet_final)
end
end