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 - Ngoc #32

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
51 changes: 51 additions & 0 deletions lib/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require_relative "planet.rb"
require_relative "solar_system.rb"

def main

#Added planets' info to each planet instance
solar_system = SolarSystem.new("Sun")

earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life")
solar_system.add_planet(earth)

mercury = Planet.new("Mercury", "dark grey", 4.867e24, 5.791e7, "Mercury is the smallest plannet in our solar system")
solar_system.add_planet(mercury)

neptune = Planet.new("Neptune", "purple", 1.024e26, 4.495e9, "It is four times wider than Earth")
solar_system.add_planet(neptune)

saturn = Planet.new("Saturn", "yellowish-brown", 5.683e26, 1.434e9, "Saturn is the second largest planet in our solar system")
solar_system.add_planet(saturn)

venus = Planet.new("Venus", "opal", 4.867e24, 1.082e8, "It has intense heat and volcanic activity")
solar_system.add_planet(venus)

######## Prompt User's inputs########

puts "Welcome to Solar System program!"
puts "What would you like to do?"
puts "[list planets] [planet details] [add planet] [exit]"
user_input = gets.chomp

until user_input.downcase == "exit"
case user_input.downcase
when "list planets"
puts solar_system.list_planets
when "planet details"
puts "Which planet would you like to learn about?"
planet_details_input = gets.chomp
found_planet = solar_system.find_planet_by_name(planet_details_input.downcase)
puts found_planet.summary
when "add planet"
solar_system.user_added_planet
puts solar_system.list_planets
end

puts "What would you like to do next?"
puts "[list planets] [planet details] [add planet] [exit]"
user_input = gets.chomp
end
end

main
15 changes: 15 additions & 0 deletions lib/planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 "Planet #{name} has the color of #{color} with the mass of #{mass_kg} kgs. This planet is #{distance_from_sun_km} km from the sun. A fun-fact about this planet is: #{fun_fact}"
end
end
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, :planet

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

def add_planet(planet_instance)
@planets << planet_instance
end

def list_planets
conclusion_statement = "Planets orbiting #{star_name}:\n"

i = 1
planet_list = ""
@planets.each do |planet|
planet_list += "#{i}.#{planet.name} \n"
i += 1
end

return conclusion_statement + planet_list
end

def find_planet_by_name(planet_name)
@planets.find { |planet| planet.name.downcase == planet_name.downcase }
end

def user_added_planet

Choose a reason for hiding this comment

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

I would suggest moving this method to the main.rb file so to separate the user interface role from the role of storing and organizing the planets.

puts "Please enter new planet's name"
planet_name = gets.chomp
puts "Please enter new planet's color"
planet_color = gets.chomp
puts "Please enter new planet's mass in kilograms"
planet_mass = gets.chomp.to_i
puts "Please enter the distance between this new planet and the sun"
planet_distance = gets.chomp.to_i
puts "Please enter new planet's fun_fact"
fun_fact = gets.chomp

user_planet = Planet.new(planet_name.capitalize, planet_color, planet_mass, planet_distance, fun_fact)

add_planet(user_planet)
end
end
11 changes: 11 additions & 0 deletions specs/planet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "minitest/autorun"
require "minitest/reporters"
require_relative "../lib/planet.rb"
Minitest::Reporters.use!

describe "planet" do
it "Check if a new instance is an instance of class Planet " do
earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life")
expect(earth).must_be_instance_of Planet
end
end