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

Fire: Ayesha - Solar System project submission #48

Open
wants to merge 1 commit 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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/solar-system.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 125 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# frozen_string_literal: true
require 'awesome_print'
require_relative 'planet'
require_relative 'milky_way'

def create_solar_system
new_solar_system = SolarSystem.new('Sun')

earth = Planet.new('EARTH', 'bluish-green', 5.972e24, 1.496e8, 'Only planet known to support life')

pluto = Planet.new('PLUTO', 'blue', 1.309e22, 5.906e38, 'Known as a dwarf planet now!')

saturn = Planet.new('SATURN', 'yellow-ish brown', 5.683e26, 1.434, "This planet's atmosphere is made up mostly of hydrogen (H2) and helium (He).")

new_solar_system.add_planet(earth)
new_solar_system.add_planet(pluto)
new_solar_system.add_planet(saturn)

new_solar_system
end

def get_user_action_choice
puts 'What would you like to do?'

choices = ['List Planets', 'Planets Details', 'Add Planet', 'Distance', 'Exit']

choices.each do |choice|
puts choice.to_s
end

action_choice = gets.chomp.upcase

puts ''

ap action_choice
end

def get_user_planet_choice
puts 'Which planet would you like to learn about?'

planet_choice = gets.chomp.upcase

puts ''

planet_choice.upcase
end

def get_new_user_planet
puts "Let's add a new planet to our solar system! Please enter the information below:"
puts ''
puts 'Planet name:'
name = gets.chomp.upcase

puts 'Planet color:'
color = gets.chomp.upcase

puts "Planet's mass in kg:"
mass = gets.chomp.to_f
while mass <= 0
puts "Invalid mass value! Enter the mass value again"
mass = gets.chomp.to_f
end

puts "Planet's distance from the sun in km:"
distance = gets.chomp.to_f
while distance <= 0
puts "Invalid distance value! Enter the distance value again"
distance = gets.chomp.to_f
end

puts 'A fun fact about the planet:'
fun_fact = gets.chomp.upcase
puts ''

new_planet = Planet.new(name, color, mass, distance, fun_fact)

new_planet
end



def main
the_solar_system = create_solar_system

begin_program = true
# The planet show starts
while begin_program
# 1st - Get user's choice of the planet's show
user_choice = get_user_action_choice

case user_choice.upcase

when 'LIST PLANETS'
puts the_solar_system.list_planets

when 'PLANET DETAILS'
planet_name = get_user_planet_choice
planet_found = the_solar_system.find_planet_by_name(planet_name)

if planet_found
puts planet_found.summary
else
puts "Sorry - #{planet_name} does not exist yet!\n"
end

when 'ADD PLANET'
new_planet = get_new_user_planet
the_solar_system.add_planet(new_planet)

when "DISTANCE"
planet_name_1 = get_user_planet_choice
planet_name_2 = get_user_planet_choice

puts the_solar_system.distance_between(planet_name_1, planet_name_2)

when 'EXIT'
begin_program = false

else
puts "You entered an invalid option, let's try that again"
end
end
end

main
45 changes: 45 additions & 0 deletions milky_way.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require_relative 'planet'

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
list = "The Planets orbiting our #{star_name} in our milky_way galaxy are:\n"

planets.each do |planet|
list += "- #{planet.name}\n"
end
list += "\n"
list
end

def find_planet_by_name(query)

first_found_planet = planets.find do |planet|
planet.name.upcase == query.upcase
end
first_found_planet
end

def distance_between(planet_1, planet_2)
# planet_1 = "mars"
# planet_2 = "earth"
planet_1_object = find_planet_by_name(planet_1)
planet_2_object = find_planet_by_name(planet_2)

(planet_2_object.distance_from_sun_km - planet_1_object.distance_from_sun_km).abs
end

end


21 changes: 21 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

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
raise ArgumentError, 'Planet mass cannot be less than 0' if mass_kg <= 0

@distance_from_sun_km = distance_from_sun_km
raise ArgumentError, 'Planet distance from sun cannot be less than 0' if distance_from_sun_km <= 0

@fun_fact = fun_fact
end

def summary
"The planet #{name} is the color #{color}. It has a mass of #{mass_kg} kg. It is #{distance_from_sun_km} km away from the sun. #{fun_fact}.\n\n"
end
end