Skip to content
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
70 changes: 70 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# main.rb

require_relative 'planet'
require_relative 'solar-system'

def add_planet_info()
print "What is the name of the planet you'd like to add? "
planet_name = gets.chomp.downcase.capitalize

print "What is the planet's color? "
planet_color = gets.chomp.downcase.capitalize
print "How much does this planet weigh, in kg? "
planet_weight = gets.chomp.downcase
print "How far is this planet from the sun, in km? "
planet_distance = gets.chomp.downcase
print "What is a fun fact about this planet? "
planet_fun_fact = gets.chomp.downcase
new_planet = Planet.new(planet_name, planet_color, planet_weight, planet_distance, planet_fun_fact)
return new_planet
end

def main

# Create Instance of Solar System
solar_system = SolarSystem.new('Sun')

# Create Instances of Planets
venus = Planet.new('Venus', 'yellowish', 108212290.56, 4.867e+24, 'Lady Gaga has an absolute banger on her Artpop album also titled "Venus".')
earth = Planet.new('Earth', 'blue-green', 5.972e24, 1.496e8, 'Only planet known to support life.')
neptune = Planet.new('Neptune', 'blue', 2781900000, 1.024e+26, 'its cold and scary.')

# Add planets to solar system
solar_system.add_planet(venus)
solar_system.add_planet(earth)
solar_system.add_planet(neptune)

puts
puts "Welcome to the Solar System Program! You have four options: "
puts " 1. If you would like to know which planets are currently in this system, please type 'list planets'"
puts " 2. If you would like to learn about a specific planet in this system, please type 'planet info'"
puts " 3. If you want to add a planet to this system, please type 'add planet'"
puts " 4. Or if you would like to quit this program, please type 'exit'"
print "Which option would you like? "
option = gets.chomp.downcase


case (option)

when "list planets"
puts solar_system.list_planets

when "planet info"
print "What is the name of the planet you'd like to know more about? "
planet_choice = gets.chomp.downcase
found_planet = solar_system.find_planet_by_name(planet_choice)
puts found_planet.summary

when "add planet"
new_planet = add_planet_info()
solar_system.add_planet(new_planet)
puts "Cool! thanks for adding #{new_planet.name}. The updated planet list now looks like this: " + "\n" + "#{solar_system.list_planets}"

when "exit"
puts "Okay, bye!"

end

end

main # invoke main
23 changes: 23 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# planet.rb

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

def summary
return "Here are some basics on #{@name}:" +
"\n" + " 1. It is the color #{@color}." +
"\n" + " 2. It weighs #{@mass_kg} kg." +
"\n" + " 3. It is #{@distance_from_sun_km } kilometers from the sun." +
"\n" + " 4. And fun fact: #{@fun_fact}"
end

end
35 changes: 35 additions & 0 deletions solar-system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# solar-system.rb

class SolarSystem

attr_reader :star_name, :planets

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

def add_planet(new_planet)
@planets << new_planet
end

def list_planets
str = "Planets orbiting #{@star_name}"
@planets.each_with_index do |planet, i|
str.concat("\n #{i+1}. #{planet.name}")
end
return str
end

def find_planet_by_name(name)
formatted_name = name.downcase.capitalize # doing this means that every newly instantiated planet name must be recorded in this case format: "Name"
match = ""
@planets.each do |planet|
if formatted_name == planet.name
match = planet
end
end
return match
end

end