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

Leaves_Ga-Young #35

Open
wants to merge 7 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
44 changes: 44 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Ga-Young Jin, Cohort 12
# Monday, August 19th, 2019
# Week 3, Solar System Project

require_relative 'planet'
require_relative 'solar_system'

def main
# initiating new solar system and planets
solar_system = SolarSystem.new('Sol')
earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life')
mars = Planet.new('Mars', 'red', 6.39e23, 2.28e8, 'The planet is named after Mars, the Roman god of war')

solar_system.add_planet(earth)
solar_system.add_planet(mars)

# user interaction to explore and search through planets
do_next = nil
until do_next == "EXIT" || do_next == "4"
puts "\nWhat would you like to do?
1. List planets
2. Get planet details
3. Add planet
4. Exit\n"

do_next = gets.chomp

case do_next
when "LIST PLANETS", "1"
puts solar_system.list_planets
when "GET PLANET DETAILS", "2"
puts solar_system.planet_details
when "ADD PLANET", "3"
solar_system.add_new_planet
when "EXIT", "4"
else
puts "\nYou have entered an invalid response. Please try again."
end
end
end

main


21 changes: 21 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Ga-Young Jin, Cohort 12
# Monday, August 19th, 2019
# Week 3, Solar System Project

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)
@name = name
@color = color
@mass_kg = mass_kg
@distance_from_sun_km = distance_from_sun_km
@fun_fact = fun_fact
end

# method that returns a nice summary of a planet
def summary
return "\nThis planet is called #{@name}. It is #{@color} in color, #{@mass_kg} kilograms in weight,
and #{@distance_from_sun_km} kilometers from the sun. A fun fact about this planet: #{@fun_fact}."
end
end
78 changes: 78 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Ga-Young Jin, Cohort 12
# Monday, August 19th, 2019
# Week 3, Solar System Project

class SolarSystem
def initialize(star_name)
@star_name = star_name
@planets = []
end

attr_reader :star_name, :planets

# method to add a new planet to the planet array
def add_planet(planet)
@planets << planet
end

# method to list all planets in the planet array
def list_planets
planet_count = 1
list = "\nPlanets orbiting #{star_name} \n"
@planets.each do |planet|
list = list + "#{planet_count}. #{planet.name} \n"
planet_count += 1
end
return list
end

# method that asks user for a planet to search and passes to find_planet_by_name method
def planet_details
puts "\nFor which planet would you like details?"
chosen_planet = gets.chomp
find_planet_by_name(chosen_planet)
end

# method that searches for planet and returns that planet's summary
def find_planet_by_name(search_planet_name)
@planets.each do |planet|
if search_planet_name.upcase == planet.name.upcase
return planet.summary
end
end
return "\nThere is no planet by that name."
end

# method to add a new planet
def add_new_planet
puts "\nWhat is the name of this new planet?"

Choose a reason for hiding this comment

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

In the interest of keeping things to a single responsibility, I would move the methods that do any sort of puts or chomp into main.rb

new_name = gets.chomp

puts "\nWhat is the color of this new planet?"
new_color = gets.chomp

puts "\nHow much does this new planet weigh in kilograms?"
new_weight = gets.chomp.to_i
while new_weight < 0
puts "\nThe weight cannot be less than 0. Please enter another value."
new_weight = gets.chomp.to_i
end

puts "\nHow far is this new planet from the sun in kilometers?"
new_distance = gets.chomp.to_i
while new_distance < 0
puts "\nThe distance cannot be less than 0. Please enter another value."
new_distance = gets.chomp.to_i
end

puts "\nWhat is a fun fact about this new planet?"
new_fact = gets.chomp

new_planet = Planet.new(new_name, new_color, new_weight, new_distance, new_fact)
add_planet(new_planet)

puts "\nThe new planet, #{new_name}, was added to the list."
end
end