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
89 changes: 89 additions & 0 deletions lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require "awesome_print"
require_relative "planet"
require_relative "solar_system"

def main
option = ""
solar_system_name = "Puppy Kingdom"
solar_system = SolarSystem.new(solar_system_name)
cybertron = Planet.new("Cybertron", "Silver", 10234, 890890, "Autobots don't live there anymore - see Transformers")
arrakis = Planet.new("Arrakis", "Orange", 52345234, 12, "it is not a planet of arrays - see Dune")
pa = Planet.new("P-apes", "Purple", 8999333, 5, "Apes RUlE!! - see... you know this one, don't you?")
planet_express = Planet.new("Planet-express", "Lot of them", 234, 12, "it is mailing company from the future.")
solar_system.add_planet(cybertron)
solar_system.add_planet(arrakis)
solar_system.add_planet(pa)
solar_system.add_planet(planet_express)
puts "\n-------- Welcome to Planets --------"
while option != "5"
puts "\nWhat would you like to do next?
1. List Planets
2. Planet Details
3. Add a Planet
4. Calculate Distance
5. Exit "
option = gets.chomp.capitalize

case option
when "1"
list = solar_system.list_planets
puts list
puts "\nEnter to continue..."
gets.chomp
when "2"
puts "\nPlease, tell me the name of the planet..."
planet_name = gets.chomp.capitalize
match = false
found_planet = solar_system.find_planet_by_name(planet_name, match)
if found_planet == false
puts "Planet not found :("
else
puts found_planet.summary
end
puts "\nEnter to continue..."
gets.chomp
when "3"
puts "\nPlease input the following information to create a planet..."
puts "- Name:"
name = gets.chomp.capitalize
puts "- Color:"
color = gets.chomp.downcase
puts "- Mass (Kg):"
mass = gets.chomp.to_f
while mass <= 0
puts "Please enter a number higher than 0...
- Mass (Kg):"
mass = gets.chomp.to_f
end
puts "- Distance from #{solar_system_name} (Kms):"
distance_from_sun_km = gets.chomp.to_f
while distance_from_sun_km <= 0
puts "Please enter a number higher than 0...
- Distance from #{solar_system_name} (Kms):"
distance_from_sun_km = gets.chomp.to_f
end
puts "- Fun Fact"
fun_fact = gets.chomp.downcase
planet = Planet.new(name, color, mass, distance_from_sun_km, fun_fact)
solar_system.add_planet(planet)
puts "\nEnter to continue..."
gets.chomp
when "4"
puts "\nCalculate Planets Distance"
temporary_distance = 0
2.times do |i|
puts "What is the planet N #{i + 1}?"
planet_q = gets.chomp.capitalize
temporary_distance = (temporary_distance - solar_system.distance_between(planet_q)).abs
end
puts " The distance between the two planets is: #{temporary_distance}Kms"
puts "\nEnter to continue..."
gets.chomp
when "5"
puts "Bye..."
break
end
end
end

main
26 changes: 26 additions & 0 deletions lib/planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

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 "\nIn a galaxy far away (not start wars)
there is a planet called #{name}. It appears
to be #{color} and its mass is #{mass_kg}.
The distance between #{name} and the
sun is #{distance_from_sun_km} kms.
Something that you might not know is that #{fun_fact}."
end
end

# Question: Why do we puts in main but not in Planet#summary?
# Because in Planet#summary we are modeling the behavior and state
# for the new instances of the class. In main we actually
# use the state and behavior
47 changes: 47 additions & 0 deletions lib/solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@


class SolarSystem
attr_reader :star_name, :planets

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

def add_planet(planet)
@planets << planet
end

def list_planets
string = "\nPlanets orbitting #{star_name}\n"
@planets.each_with_index do |planet, index|
string += "#{index + 1}. #{planet.name}\n"
end
return string
end

def find_planet_by_name(planet_name, match)
@planets.each do |planet|

Choose a reason for hiding this comment

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

You could simply:

return planets.find do |planet|
  planet.name.upcase == planet_name.upcase
end

# return planet.name == planet_name ? planet : false
# return planet if planet.name == planet_name
if planet.name == planet_name
match = true
return planet

# else
# return
end
end
if match == false
return false
end
end

def distance_between(planet_q)

Choose a reason for hiding this comment

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

This isn't calculating the distance between two planets. Instead it's calculating the distance between a planet and the sun, which is already served by a method in the Planet class.

@planets.each do |planet|
if planet.name == planet_q
return planet.distance_from_sun_km
end
end
end
end