-
Notifications
You must be signed in to change notification settings - Fork 21
add solar system file #6
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
base: erg/master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| class Planet | ||
|
|
||
| attr_accessor :name, :diameter, :mass, :number_of_moons, :color, :distance_from_the_sun | ||
|
|
||
| def initialize(planet_hash) | ||
| @name = planet_hash[:name] | ||
| @diameter = planet_hash[:diameter] | ||
| @mass = planet_hash[:mass] | ||
| @color = planet_hash[:color] | ||
| @number_of_moons = planet_hash[:number_of_moons] | ||
| @distance_from_the_sun = planet_hash[:distance_from_the_sun] | ||
| end | ||
|
|
||
| def number_of_moons(number_of_moons = 0) | ||
| @number_of_moons = number_of_moons | ||
| end | ||
|
|
||
| def about | ||
| puts "I am a planet named #{@name}! I have a diameter of #{@diameter} meters and a mass of #{@mass} kilograms." | ||
| puts "I have #{@number_of_moons} moons and I am #{@color}." | ||
| puts "I am #{@distance_from_the_sun} meters from the sun" | ||
| end | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| class SolarSystem | ||
|
|
||
| attr_accessor :name, :planets | ||
|
|
||
| def initialize (planet_hash) | ||
|
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. It seems like this is good - but without providing an example for how you're instantiating your |
||
| @name = planet_hash[:name] | ||
| @planets = [] | ||
| end | ||
|
|
||
| def add_planet(planet) | ||
| @planets.push(planet) | ||
| end | ||
|
|
||
| def add_multiple_planets(planet_array) | ||
|
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. This is very good! You can also use a method called |
||
| planet_array.each do |plan| | ||
| @planets.push(plan) | ||
| end | ||
| end | ||
|
|
||
| def about | ||
|
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. Cool! I think the way you make this interactive to learn about each of the planets. I like the way that you use the count to dynamically display all of the different planets, and a while loop to continue throughout. |
||
| puts "Here are the planets in the #{@name} solar system:" | ||
| count = 1 | ||
|
|
||
| @planets.each do |planet| | ||
| puts "#{count}. #{planet.name}" | ||
| count += 1 | ||
| end | ||
| puts "#{count}. Exit" | ||
| puts "Which planet do you want to learn about?" | ||
| learn = gets.chomp.to_i | ||
| continue = true | ||
|
|
||
| while continue == true | ||
|
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. Since |
||
| if learn < count | ||
| planets[learn-1].about | ||
| puts "What planet do you want to learn about next?" | ||
| learn = gets.chomp.to_i | ||
| elsif learn == count | ||
| puts "Goodbye!" | ||
| continue = false | ||
| else | ||
| puts "I am sorry. I don't know what you mean. Please enter in a number from 1 to #{count-1} to learn about a planet, or #{count} to exit" | ||
| learn = gets.chomp.to_i | ||
| end | ||
| end | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| require "./planets2" | ||
| require "./solar_system" | ||
|
|
||
| apple_hash = { | ||
| name:"Apple", | ||
| diameter: 60, | ||
| mass:4_500, | ||
| number_of_moons:0, | ||
| color:"red", | ||
| distance_from_the_sun:4000, | ||
| } | ||
|
|
||
| apple = Planet.new(apple_hash) | ||
|
|
||
| pear_hash = { | ||
| name: "Pear", | ||
| diameter: 20, | ||
| mass: 3000, | ||
| number_of_moons: 4, | ||
| color: "green", | ||
| distance_from_the_sun: 5000, | ||
| } | ||
|
|
||
| pear = Planet.new(pear_hash) | ||
|
|
||
| orange_hash = { | ||
| name: "Orange", | ||
| diameter: 10, | ||
| mass: 500, | ||
| color: "orange", | ||
| distance_from_the_sun:10000, | ||
| } | ||
|
|
||
| orange = Planet.new(orange_hash) | ||
|
|
||
| orange.number_of_moons | ||
|
|
||
| planets_array = [] | ||
| planets_array.push(apple) | ||
| planets_array.push(pear) | ||
|
|
||
| tree_hash = { | ||
| name: "Tree", | ||
| } | ||
|
|
||
| tree = SolarSystem.new(tree_hash) | ||
|
|
||
|
|
||
| tree.add_planet(orange) | ||
|
|
||
| tree.add_multiple_planets(planets_array) | ||
|
|
||
| tree.about | ||
|
|
||
| # Empty Hash template to create new planets | ||
| #empty_planet = { | ||
| # name:, | ||
| # diameter:, | ||
| # mass:, | ||
| # number_of_moons:, | ||
| # color:, | ||
| # distance_from_the_sun:, | ||
| # } |
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.
Watch your indentation here - you want to tab this in once
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.
I just added in my Planet class and solar system program. Sorry they were missing the first time around!