-
Notifications
You must be signed in to change notification settings - Fork 44
Ampers Solar system Aruna #22
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: 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,33 @@ | ||
|
|
||
|
|
||
| #Create Solarsystem class | ||
| class SolarSystem | ||
| attr_accessor :planet,:size,:orbit,:diameter,:rotate,:distance | ||
|
|
||
| def initialize(planet_name) | ||
| @planet = planet_name | ||
| end | ||
|
|
||
| def addPlanetToList(planet_name) | ||
| @planet << planet_name | ||
| end | ||
| def returnPlanetList | ||
| @planet = planet | ||
| planetList = "" | ||
| @planet.each_with_index do |planet, index| | ||
| planetList += "#{index+1}. #{planet} \n" | ||
| end | ||
| return planetList | ||
| end | ||
| end | ||
|
|
||
|
|
||
| planet_array= %w[Earth Mercury Venus Neptune Saturn Mars] | ||
| list_of_planet = SolarSystem.new(planet_array) | ||
| print list_of_planet.returnPlanetList | ||
|
|
||
|
|
||
| planet_1 = {name: "Mercury",size:"Smallest planet",orbital_speed:"47.8km/sec",diameter:"4878km",orbits_the_sun:"87.97days",distance_from_the_sun:"70millionkm"} | ||
| planet_2 = {name: "Uranus",size:"Sixth largest planet",orbital_speed:"35km/sec",diameter:"12,100km",orbits_the_sun:"243days",distance_from_the_sun:"108million km"} | ||
| my_solar_system = SolarSystem.new( [planet_1, planet_2]) | ||
| print my_solar_system.returnPlanetList |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #Create Plant class | ||
| class Planet | ||
| attr_reader :planet,:desc,:orbit,:diameter,:rotate,:distance | ||
| def initialize(planet,desc,orbit,diameter,rotate,distance) | ||
| @planet = planet | ||
| @desc = desc | ||
| @orbit =orbit | ||
| @diameter=diameter | ||
| @rotate = rotate | ||
| @distance = distance | ||
| end | ||
| def planet_attributes | ||
| attributes = "Planet: #{@planet}\nDesc: #{@desc}\nOrbit: #{@orbit}\nDiameter: #{@diameter}\nYear Length: #{@rotate}\nDistance from Sun: #{@distance}\n" | ||
| return attributes | ||
| end | ||
| end | ||
|
|
||
| #Create Solar System Class | ||
| class SolarSystem | ||
| attr_accessor :planet | ||
| def initialize(planet) | ||
| @planet = planet | ||
|
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 |
||
| end | ||
| def add_planet(new_planet) | ||
| @planet<<new_planet | ||
| end | ||
| def return_planets | ||
| list_of_planets = [] | ||
| @planet.each do |planet| | ||
| list_of_planets << "#{@planet.index(planet)+1}. #{planet}" | ||
| end | ||
| return list_of_planets | ||
| end | ||
| #Calling Planet Class to print attributes from SolarSystem class | ||
| def check | ||
| choice=gets.chomp.to_i | ||
| if choice == 1 | ||
| element = Planet.new("Mercury","Smallest planet","47.8km/sec","4878km","87.97days","70millionkm") | ||
|
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. Why create new planets here? Why not simply return the Look at this method closely? Think about how you could create a method that takes a number and returns the indicated planet from |
||
| b =element.planet_attributes | ||
| return b | ||
| elsif choice == 2 | ||
| element = Planet.new("Mars","Fourth planet in Solar System","35km/sec","12,100km","243days","108million km") | ||
| b = element.planet_attributes | ||
| return b | ||
| elsif choice == 3 | ||
| element = Planet.new("Venus","Second planet in Solar system","70km/sec","11000km","345days","234million km") | ||
| b = element.planet_attributes | ||
| return b | ||
| elsif choice == 4 | ||
| element = Planet.new("Earth","Third planet in Solar System","88km/sec","234334km","365days","12million km") | ||
| b = element.planet_attributes | ||
| return b | ||
| end | ||
| end | ||
| end | ||
| #Method to add new planet from user input | ||
| def create_planet_userinput(new_planet,new_planet_desc,new_planet_orbit,new_planet_diameter, | ||
|
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 have this and following code indented, it shouldn't be. |
||
| new_planet_rotate,new_planet_distance) | ||
| new_element = Planet.new(new_planet,new_planet_desc,new_planet_orbit,new_planet_diameter, | ||
| new_planet_rotate,new_planet_distance) | ||
| return new_element | ||
| end | ||
|
|
||
| #Array of planets | ||
| planet_array = %w[Mercury Mars Venus Earth] | ||
| planet_list = SolarSystem.new(planet_array) | ||
| #Start of user interface | ||
| print"Welcome to the Solar System\n" | ||
| answer = "y" | ||
| while answer.include?("y") | ||
|
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. The main loop never lets you add a |
||
| print"Select a planet to know the details:\n" | ||
| puts planet_list.return_planets | ||
| puts planet_list.check | ||
| print "Do you know about other planets (y/n)?" | ||
| answer = gets.chomp | ||
| end | ||
| #New planet routine | ||
| print "Do you want to add a new planet (y/n)" | ||
| answer1 = gets.chomp | ||
|
|
||
| if answer1 == "y" | ||
| print "Please enter the planet name" | ||
| new_planet = gets.chomp | ||
| print "Enter the desc" | ||
|
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 should give some space between the prompt and the input. like: Same for all of the following. |
||
| new_planet_orbit = gets.chomp | ||
| print"orbit" | ||
| new_planet_desc = gets.chomp | ||
| print "Enter the diameter " | ||
| new_planet_diameter = gets.chomp | ||
| print " orbital speed " | ||
| new_planet_rotate = gets.chomp | ||
| print "enter the distance" | ||
| new_planet_distance = gets.chomp | ||
|
|
||
| new_planet1 = create_planet_userinput(new_planet, new_planet_desc,new_planet_orbit,new_planet_diameter,new_planet_rotate,new_planet_distance) | ||
| print new_planet1.planet_attributes | ||
|
|
||
| planet_array << new_planet | ||
| puts planet_list.return_planets | ||
| #print planet_list | ||
| elsif answer1 == "n" | ||
| print "Thanks for looking" | ||
|
|
||
| end | ||
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 make
planetan accessor? Why not a reader? Do you have a reason users would be able to changeplanet?