Skip to content
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
33 changes: 33 additions & 0 deletions New_wave1.rb
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
104 changes: 104 additions & 0 deletions New_wave3.rb
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make planet an accessor? Why not a reader? Do you have a reason users would be able to change planet?

def initialize(planet)
@planet = planet

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since @planet is going to hold a list of planets, it should probably be plural (@planets as opposed to @planet).

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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why create new planets here? Why not simply return the Planet from @planet?

Look at this method closely? Think about how you could create a method that takes a number and returns the indicated planet from @planet. Get help from me or a tutor/TA if you need.

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,

Choose a reason for hiding this comment

The 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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main loop never lets you add a Planet until it's done. Then you can't view the Planet you added.

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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should give some space between the prompt and the input.

like: print "Enter the description ==> "

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