From 08c084971491ab69c9379902d30e27b77f460872 Mon Sep 17 00:00:00 2001 From: Ayesha Asif Date: Thu, 24 Sep 2020 22:37:26 -0700 Subject: [PATCH] Solar System project submission --- .idea/.gitignore | 8 +++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ .idea/solar-system.iml | 16 ++++++ .idea/vcs.xml | 6 ++ main.rb | 125 +++++++++++++++++++++++++++++++++++++++++ milky_way.rb | 45 +++++++++++++++ planet.rb | 21 +++++++ 8 files changed, 233 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/solar-system.iml create mode 100644 .idea/vcs.xml create mode 100644 main.rb create mode 100644 milky_way.rb create mode 100644 planet.rb diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..73f69e09 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..510e7fcc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..8489f57f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/solar-system.iml b/.idea/solar-system.iml new file mode 100644 index 00000000..3ea978c9 --- /dev/null +++ b/.idea/solar-system.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..7a048c74 --- /dev/null +++ b/main.rb @@ -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 diff --git a/milky_way.rb b/milky_way.rb new file mode 100644 index 00000000..9841aaed --- /dev/null +++ b/milky_way.rb @@ -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 + + diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..991d5bb4 --- /dev/null +++ b/planet.rb @@ -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