forked from AdaGold/solar-system
-
Notifications
You must be signed in to change notification settings - Fork 44
Angelica Ceja - Solar System - Octos #44
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
Open
amcejamorales
wants to merge
1
commit into
Ada-C9:master
Choose a base branch
from
amcejamorales:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| # solar system program contains a SolarSystem class which can be composed of an array of instances of the Planet class | ||
| # user can create new planets and make them a part of any solar system | ||
| # user can interact with planets, requesting information about any of them | ||
| # user can also interact with a solar system as a whole, requesting the distance between two planets as well as their local year based on their year length and the solar system's age | ||
|
|
||
| # ================== | ||
| # class and method definitions | ||
| # ================== | ||
|
|
||
| # class Planet lets you create a planet with various attributes and access that information individually and all-together | ||
| # part of wave 2 | ||
| class Planet | ||
| attr_accessor :name, :order, :color, :temp, :water, :diameter, :moons, :year_length, :dist_from_sun | ||
|
|
||
| # order refers to order from the sun | ||
| # temperature is in degrees Celsius | ||
| # diameter is in km | ||
| # year length is in Earth days | ||
| # distance from sun is in million km | ||
| def initialize(name, order, color, temp, water, diameter, moons, year_length, dist_from_sun) | ||
| @name = name | ||
| @order = order | ||
| @color = color | ||
| @temp = temp | ||
| @water = water | ||
| @diameter = diameter | ||
| @moons = moons | ||
| @year_length = year_length | ||
| @dist_from_sun = dist_from_sun | ||
| end | ||
|
|
||
| def planet_info | ||
| return planet_information = "Name: #{@name} \nOrder: #{@order} \nColor: #{@color} \nAverage Temperature (C): #{@temp} \nContains Water: #{@water} \nDiameter (km): #{@diameter} \nNumber of Moons: #{@moons} \nYear length (Earth days): #{@year_length} \nDistance from the sun (million km): #{@dist_from_sun}" | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # class SolarSystem lets you create a solar system with two attributes, age and an array of planets of class Planet | ||
| # user can request a list of all planets in a system, add a new planet, get the local year of any given planet, and find the distance between any two planets | ||
| # part of wave 1 | ||
| class SolarSystem | ||
| attr_accessor :age, :planets | ||
|
|
||
| def initialize(age, planets) | ||
| # age in million Earth years | ||
| @age = age | ||
| @planets = planets | ||
| end | ||
|
|
||
| def add_planet=(new_planet) | ||
| @planets << new_planet | ||
| end | ||
|
|
||
| def list_planets | ||
| list_of_planets = "" | ||
| @planets.each_with_index do |planet, index| | ||
| list_of_planets += "#{index + 1}. #{planet.name}\n" | ||
| end | ||
| return list_of_planets | ||
| end | ||
|
|
||
| # part of wave 3 optional | ||
| def get_local_year(planet) | ||
| year_length = planet.year_length #planet.year_length in Earth days | ||
| local_year = @age * 1_000_000 * 365 / year_length | ||
| return local_year.to_i | ||
| end | ||
|
|
||
| # part of wave 3 optional | ||
| def get_distance_between_planets(planet_a, planet_b) | ||
| distance_between = (planet_a.dist_from_sun - planet_b.dist_from_sun).abs | ||
| return "The distance between #{planet_a.name} and #{planet_b.name} is #{distance_between} million km." | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # create_planet method asks for user input to create a new planet of class Planet | ||
| # part of wave 2 optional & wave 3 | ||
| def create_planet | ||
| puts "Let's create a new planet. What do you want to call it?" | ||
| name = gets.chomp | ||
| puts "What order does #{name} come in?" | ||
| order = gets.chomp.to_i | ||
| puts "What color is #{name}?" | ||
| color = gets.chomp | ||
| puts "What is the average temperature (in degrees Celsius) on #{name}?" | ||
| temp = gets.chomp.to_f | ||
| puts "Dose #{name} contain water? Type yes or no." | ||
| water = gets.chomp | ||
| if water == "yes" | ||
| water = true | ||
| else | ||
| water = false | ||
| end | ||
| puts "What is the diameter of #{name} in km?" | ||
| diameter = gets.chomp.to_i | ||
| puts "How many moons does #{name} have?" | ||
| moons = gets.chomp.to_i | ||
| puts "How long is one year on #{name} in Earth days?" | ||
| year_length = gets.chomp.to_i | ||
| puts "How far is #{name} from the sun in million km?" | ||
| dist_from_sun = gets.chomp.to_f | ||
|
|
||
| return Planet.new(name, order, color, temp, water, diameter, moons, year_length, dist_from_sun) | ||
|
|
||
| end | ||
|
|
||
| # ================== | ||
| # tests | ||
| # ================== | ||
|
|
||
| # objects of class Planet created and saved in array | ||
| mercury = Planet.new("Mercury", 1, "yellow", 427.0, true, 4_879, 0, 88, 57.91) | ||
| venus = Planet.new("Venus", 2, "brown", 462.0, false, 12_104, 0, 225, 108.2) | ||
| earth = Planet.new("Earth", 3, "blue", 58.3, true, 12_742, 1, 365 ,149.6) | ||
| mars = Planet.new("Mars", 4, "red", -60.0, true, 6_779, 2, 687, 227.9) | ||
| jupiter = Planet.new("Jupiter", 5, "brown-red", 145, false, 139_822, 69, 4380, 778.5) | ||
| hoth = Planet.new("Hoth", 6, "ice-blue", -61.0, true, 116_464, 62, 10_759, 1_429.0) | ||
| # modification from wave 1 to wave 2 | ||
| # declaring the planets array remains the same; only difference is that each element in the array goes from a hash in wave 1 to an object of class Planet in wave 2 | ||
| planets = [mercury, venus, earth, mars, jupiter, hoth] | ||
|
|
||
| # test class Planet planet_info method | ||
| # part of wave 2 | ||
| puts hoth.planet_info | ||
|
|
||
| # solar system of class SolarSystem created and given an age | ||
| # age part of wave 3 optional | ||
| solar_system_a = SolarSystem.new(28.7, planets) | ||
|
|
||
| # test list_planets method | ||
| puts solar_system_a.list_planets | ||
|
|
||
| uranus = Planet.new("Uranus", 7, "blue", -216, false, 50_724, 27, 30_770, 2_871) | ||
|
|
||
| # test add_planet method | ||
| solar_system_a.add_planet = uranus | ||
|
|
||
| # test that list of planets in solar system is updated after adding new planet | ||
| puts solar_system_a.list_planets | ||
|
|
||
| # test get_local_year method | ||
| # part of wave 3 optional | ||
| puts solar_system_a.get_local_year(mars) | ||
|
|
||
| # test get_distance_between_planets method | ||
| # both outputs below should be the same | ||
| # part of wave 3 optional | ||
| puts solar_system_a.get_distance_between_planets(earth, hoth) | ||
| puts solar_system_a.get_distance_between_planets(hoth, earth) | ||
|
|
||
| # ================== | ||
| # user interaction | ||
| # ================== | ||
|
|
||
| # display information about any planet | ||
| def display_info(planet_selection) | ||
| info_about_planet = "#{planet_selection.name} is a(n) #{planet_selection.color} planet. It comes in at \##{planet_selection.order} in the order of planets from the sun. #{planet_selection.name} has a diameter of #{planet_selection.diameter} km and has #{planet_selection.moons} moon(s). It has pleasant weather, averaging about #{planet_selection.temp} degrees Celsius throughout the year. Oh, by the way, one year on #{planet_selection.name} takes about #{planet_selection.year_length} Earth days. That's because it's #{planet_selection.dist_from_sun} million kilometers from the sun." | ||
| if planet_selection.water | ||
| info_about_planet += " Luckily, this planet does have water." | ||
| else | ||
| info_about_planet += " Unfortunately, this planet lacks water." | ||
| end | ||
| return info_about_planet | ||
| end | ||
|
|
||
| # ask user to select a planet to explore and allow user to select another | ||
| def planets_info(solar_system) | ||
| puts "What planet would you like to learn about? Here are your options: " | ||
|
|
||
| solar_system.planets.each_with_index do |planet, planet_order| | ||
| puts "#{planet_order + 1}. #{planet.name}" | ||
| end | ||
|
|
||
| # enter the proper name of an existing planet within the solar system you are exploring, e.g., "Mercury", "Jupiter", not "mercury", "jupiter" | ||
| planet_selection = gets.chomp | ||
| planet_information = "" | ||
|
|
||
| solar_system.planets.each do |planet| | ||
| if planet_selection == planet.name | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're comparing the entry to the Planet's name, but you print out the planet names along with a number. A user is usually expecting to enter the number to select the planet. It's just a little counterintuitive. |
||
| planet_information = display_info(planet) | ||
| end | ||
| end | ||
|
|
||
| if planet_information != "" | ||
| puts planet_information | ||
| else | ||
| puts "That planet doesn't exist in our system." | ||
| end | ||
|
|
||
| choose_another(solar_system) | ||
| end | ||
|
|
||
| # asks user if they want to learn about another planet, if not, leaves the program | ||
| def choose_another(solar_system) | ||
| puts "Would you like to learn about another planet? Enter 'yes' or 'no'." | ||
| play_again = gets.chomp | ||
| if play_again == "yes" | ||
| planets_info(solar_system) | ||
| else | ||
| puts "Ok, goodbye!" | ||
| end | ||
| end | ||
|
|
||
| # test solar system exploration user interface | ||
| planets_info(solar_system_a) | ||
|
|
||
| # test create_planet method and verify with Planet class method planet_info | ||
| neptune = create_planet | ||
| puts neptune.planet_info | ||
| # you can input the following information for testing purposes | ||
| # name : Neptune | ||
| # order: 8 | ||
| # color: navy blue | ||
| # avg temp: -200 | ||
| # water: yes | ||
| # diameter: 42_244 | ||
| # moons: 14 | ||
| # year length: 60_182 | ||
| # distance from sun: 4_498 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use
attr_accessor. In general useattr_readerunless you specifically want to give the user the ability to change an attribute directly.