DUE : APRIL 10th
2000
Please
Visit Our Websites to view report and applet: www.cs.dal.ca/~jbassil or www.fortunecity.com/skyscraper/hacker/1308
Summary
:
In our program, it is
possible to readily change both the number of elevators which exist as well as
the floors. Each floor has a queue that is represented by blue bars that
increase and decrease depending on the flow of traffic that goes through each
floor. The greater the number of people
on a particular floor the larger the amount of space that will be occupied by
the color blue. Each elevator is represented by a small rectangle that travels
either up or down depending on the direction it’s headed. The color of the elevator is determined by
its occupancy. If the elevator is full,
the color of the rectangular box(i.e the elevator ) is blue, otherwise
the elevator is red. Furthermore, the
speed at which the elevator travels, and the appearance of persons on each
floor, can be altered by the features we have included into our program. Also, a complete copy of this report is
available on the websites given on the title page
Introduction :
The purpose of this project
is to simulate an elevator management system while utilizing our now decent
knowledge of java to make it enjoyable to the viewing public. With this purpose
in mind, it was our intent to design this program by expanding on two main
ideas. Firstly, to attempt to depict how an elevator management system may
handle small/large numbers of people and numerous/few elevators, while keeping
in mind our capabilities in java were still limited. Secondly, it was our intent to make this applet clear, well laid
out and most importantly interesting.
We accomplished our first goal by developing our own kind of management
system that was within our capabilities and also served as an efficient
system. We felt that this problem of
limited resources and skill is mirrored in industry today, when companies must
submit to the loss of some efficiency to save money. In our case, for example,
we decided our elevators would only let people off as the elevators are going
up in the building. Therefore there
could be no floor hopping by the passengers.
If the passenger wanted to go down he/she would have to go to the lobby
and then go up from there. A silly
situation would develop if a passenger wanted to go up to the last floor from the
second last floor. The passenger would
have to go all the way to the bottom and then come all the way back up. This problem was out of our reach to solve
due to time constraints. Our second
goal was reached by incorporating all of the knowledge we learned as first year
java programming students with our previous knowledge of html and a new program
called flash. By weaving these tools together, we felt that we were able to put
forth a presentation that was appealing to someone who knew nothing about computing
but enjoyed entertainment and had some appreciation for the applet, which
involved some detailed thought processing.
In our program, it is possible to stop and /or restart an elevator by
using the mouse. If you click within a
defined area for each specific elevator, it will stop or restart. There are two
types of elevators in our program. Two-thirds of the elevators service the
entire building, and one-third are specifically meant for the first half of the
floors. The speed of the elevators and
the rate of appearance of the people can be increased and decreased right on
the applet with a simple mouse click.
In the following sections of
our report we will discuss in detail, the difficulties we needed to overcome
while writing the program, as well as the features we added when we solved the
previous problems. Also, a clearly
written description of all the important methods and classes will be included,
followed by the approaches we took to solve any problems that arose while
coding and what “bugs” continue to infest our program(if there are anyJ).
The
purpose of the project was to produce a simulation of a building filled with
elevators. We went about this by
downloading the prepared code of Dr.Riordan and Dr.Moriarty and working from
there. We chose to follow the suggested
steps and worked on the necessary steps every week. The first few steps were to set up a thread to draw the elevator
going up and down. Once this was
accomplished it was an easy matter to draw more elevators that did the same
thing. The trick here was to create an
array of elevators, and be able to change the integer that determined how large
the array was. This enabled us to
change the number of elevators by changing an integer in the code and then running
the applet again. Similarly the same
thing could be done for the floors.
The
next step was to add a Mouse Listener in order so that the elevators could be
stopped and started by clicking on them.
This was achieved by creating a method called Mouse Adapter that would
listen for the mouse clicks and assign integers to them. When the mouse is
clicked it sends the integers to another method inside of the building
class. This method then stops the
appropriate elevator. The next step was
to draw the people waiting to get on the floors. The floor is drawn as a white rectangle when it is empty. The people fill up by adding PersonNodes to
the container class, in the floor class.
This is done every time the run method for the floor runs. A blue rectangle is drawn over the white
rectangle to represent that the floor is filling up. The number of people in the floor container determines the size
of the blue rectangle. The max number
of people waiting on a floor is 99.
The
step that was completed next was to get the elevators to pick people up on
their way down. Each floor has its own
button. If there are people waiting on
a floor to get picked up then the button is pressed, if the button is pressed
the elevator picks people up on its way down.
To pick people up the elevator calls a method. This method checks how many more people the elevator can carry
and how many people there are waiting on the floor. Then it transfers the appropriate number of people from the floor
to the elevator. The number of people
the elevator picks up is subtracted from the floor. When the elevator reaches the ground floor it calls a method that
empties the elevator.
After
that we had to get the elevator to pick people up from the lobby and drop them
off on the way up. We did this by
calling the same method that is used to pick people up from any other
floor. The only difference here is that
people that are picked up from the ground floor have a destination assigned to
them. There is an array of buttons on
each elevator. If there is a person on
the elevator that has a certain destination the appropriate button gets
pressed. When the elevator reaches a
floor and the button has been pressed it releases people. The number of people that get released is
determined by how many people had that destination. If three people got on in the lobby that had the destination
floor five then, three people would be let off the elevator when it reached
floor five. Once this step was
completed all the basics were done, everything from here on in is just extras
that we added to spice things up.
The
first special feature we added was two choice boxes. The first choice box is on the bottom of the screen. This choice box is used to increase and
decrease the speeds of the first fifteen elevators. When a certain elevator is picked to slow down its sleep time
increases, similarly when it is picked to speed up its sleep time
decreases. Another option here is to
increase the capacity of the first elevator.
The capacity can be increased up to ninety-five. The second choice box is on the right of the
screen. This choice box is used to
speed up and slow down the people appearing on the first fifteen floors. This is done the same way as with the speed
of the elevators. Another option here is
to change the background to black to represent nighttime and then back to gray.
The
second special option was that each elevator has a blue rectangle representing
the number of people in the elevator.
As the elevator picks people up, more blue is drawn than red. When it is empty it is completely red, when
it is full it is blue. Also the number
of people currently on each floor or elevator is represented by an integer drawn
on the floor or elevator respectively.
This way we can check that people are getting picked up and dropped off
as they are supposed to.
This is pretty much the
scope of our project. It is a
simulation of a building full of elevators, where the speed of the elevators
and the people appearing can be changed.
The number of people on each floor and elevator is shown in the
applet(pretty cool! We think). The only
bug that we did not have time to fix was that if we set the building up with
less then four floors then we got some null pointer exceptions. This is because there is a problem with the
counter that keeps track of what floor the elevator is currently on. If there are too few floors the elevators go
up and down too fast and the counter cannot get configured correctly. Other then that there were no bugs that we
noticed.
At this point, we had our
entire code written but we were not in agreement how to present our project to
you. We decided, since we had
approximately two weeks before the due date of the project we would bring a
twist to our presentation never before attempted by either of us. This twist comprised of using Flash 4.0, a
new type of software used to produce movies that had the capability to be
viewed through a webpage. The thought of
this appealed to us greatly. We knew
nothing about Flash or how to use it, so we had to attack this as
beginners. After hours of trial and
error , we put together a decent introduction for our project. To apply this movie we created to our
webpage was extremely difficult. We had
to embed this movie(extension .swf) in our html code, and this was done by
creating an object , the object being this movie. Some variables had to be
initialized when running the movie , such things as setting the loop variable
to false so it would not continuously run over and over again. After these small but new concepts were
successfully completed we then wanted to run our applet from a link on the same
page as the movie. We used the usual
applet tag necessary to access the code, however, the code= elevator.class was
not what we used because this class could not be found when the applet ran in
the web browser. Instead, after some
thought, we decided to use the elevators.tmp.html file that java creates when
we the program in java is written. This
file is compatible with browsers and solved our potentially devastating
problem. All in all, we applied our
problem solving techniques to solve as many problems as we could while
composing a complete presentation likely to be enjoyed by those who view it.
Please visit our webpage , which hosts the program in its entirety as well as
the added features of our web
designing. http://www.cs.dal.ca/~jbassil.
import
java.applet.Applet; import
java.awt.List; import
java.awt.*; import
java.awt.event.*; |
|
____
CODE__________________________ public
class elevators extends Applet implements ItemListener{ List firstList;. List secondList; Graphics Graphics h; building building; public void init() { this.setLayout(new BorderLayout()); firstList = new List(5); firstList.add("Elevator 1-slow
down"); firstList.add("Elevator 2-slow
down"); firstList.add("Elevator 3-slow
down"); firstList.add("Elevator 4-slow
down"); firstList.add("Elevator 5-slow
down"); firstList.add("Elevator 6-slow
down"); firstList.add("Elevator 7-slow
down"); firstList.add("Elevator 8-slow
down"); firstList.add("Elevator 9-slow
down"); firstList.add("Elevator 10-slow
down"); firstList.add("Elevator 11-slow
down"); firstList.add("Elevator 12-slow
down"); firstList.add("Elevator 13-slow
down"); firstList.add("Elevator 14-slow
down"); firstList.add("Elevator 15-slow
down"); firstList.add("Elevator 1-speed
up"); firstList.add("Elevator 2-speed
up"); firstList.add("Elevator 3-speed
up"); firstList.add("Elevator 4-speed
up"); firstList.add("Elevator 5-speed
up"); firstList.add("Elevator 6-speed
up"); firstList.add("Elevator 7-speed
up"); firstList.add("Elevator 8-speed
up"); firstList.add("Elevator 9-speed
up"); firstList.add("Elevator 10-speed
up"); firstList.add("Elevator 11-speed
up"); firstList.add("Elevator 12-speed
up"); firstList.add("Elevator 13-speed
up"); firstList.add("Elevator 14-speed
up"); firstList.add("Elevator 15-speed
up"); firstList.add("Elevator 1 -
Increase Capacity"); add(firstList,BorderLayout.SOUTH); firstList.addItemListener(this); secondList = new List(6); secondList.add("Floor 1-slow
down"); secondList.add("Floor 2-slow
down"); secondList.add("Floor 3-slow
down"); secondList.add("Floor 4-slow
down"); secondList.add("Floor 5-slow
down"); secondList.add("Floor 6-slow
down"); secondList.add("Floor 7-slow
down"); secondList.add("Floor 8-slow
down"); secondList.add("Floor 9-slow
down"); secondList.add("Floor 10-slow
down"); secondList.add("Floor 11-slow
down"); secondList.add("Floor 12-slow
down"); secondList.add("Floor 13-slow
down"); secondList.add("Floor 14-slow
down"); secondList.add("Floor 15-slow
down"); secondList.add("Floor 1-speed
up"); secondList.add("Floor 2-speed
up"); secondList.add("Floor 3-speed
up"); secondList.add("Floor 4-speed
up"); secondList.add("Floor 5-speed
up"); secondList.add("Floor 6-speed
up"); secondList.add("Floor 7-speed
up"); secondList.add("Floor 8-speed
up"); secondList.add("Floor 9-speed
up"); secondList.add("Floor 10-speed
up"); secondList.add("Floor 11-speed
up"); secondList.add("Floor 12-speed
up"); secondList.add("Floor 13-speed
up"); secondList.add("Floor 14-speed
up"); secondList.add("Floor 15-speed
up"); secondList.add("Night
Time"); secondList.add("Original"); add(secondList,BorderLayout.EAST); secondList.addItemListener(this); building = new building(getGraphics(),this); h = getGraphics(); mousePressHandler one = new mousePressHandler(); } public void itemStateChanged(ItemEvent e){ if(e.getSource() == firstList) I
if(firstList.getSelectedItem() ==
"Elevator 1-slow down") building.elevators[0].elevatorspeed
= building.elevators[0].elevatorspeed +
20; if(firstList.getSelectedItem() ==
"Elevator 2-slow down") building.elevators[1].elevatorspeed
= building.elevators[1].elevatorspeed +
20; if(firstList.getSelectedItem() ==
"Elevator 3-slow down") building.elevators[2].elevatorspeed
= building.elevators[2].elevatorspeed +
20; if(firstList.getSelectedItem() ==
"Elevator 4-slow down") building.elevators[3].elevatorspeed
= building.elevators[3].elevatorspeed + 20; if(firstList.getSelectedItem() ==
"Elevator 5-slow down") building.elevators[4].elevatorspeed
= building.elevators[4].elevatorspeed + 20; if(firstList.getSelectedItem() ==
"Elevator 6- slow down") building.elevators[5].elevatorspeed
= building.elevators[5].elevatorspeed + 20; if(firstList.getSelectedItem() ==
"Elevator 7- slow down") building.elevators[6].elevatorspeed = building.elevators[6].elevatorspeed + 20; if(firstList.getSelectedItem() ==
"Elevator 8-slow down") building.elevators[7].elevatorspeed = building.elevators[7].elevatorspeed + 20; if(firstList.getSelectedItem() ==
"Elevator 9-slow down") building.elevators[8].elevatorspeed = building.elevators[8].elevatorspeed + 20; if(firstList.getSelectedItem() ==
"Elevator 10-slow down") building.elevators[9].elevatorspeed = building.elevators[9].elevatorspeed + 20; if(firstList.getSelectedItem() ==
"Elevator 11-slow down") building.elevators[10].elevatorspeed
= building.elevators[10].elevatorspeed +
20; if(firstList.getSelectedItem() ==
"Elevator 12-slow down") building.elevators[11].elevatorspeed
= building.elevators[11].elevatorspeed + 20; if(firstList.getSelectedItem() ==
"Elevator 13-slow down") building.elevators[12].elevatorspeed = building.elevators[12].elevatorspeed + 20; if(firstList.getSelectedItem() == "Elevator 14-slow down") building.elevators[13].elevatorspeed = building.elevators[13].elevatorspeed + 20; if(firstList.getSelectedItem() == "Elevator 15-slow down") building.elevators[14].elevatorspeed
= building.elevators[14].elevatorspeed +
20; if(firstList.getSelectedItem() ==
"Elevator 1-speed up" && building.elevators[0].elevatorspeed >
10) building.elevators[0].elevatorspeed
= building.elevators[0].elevatorspeed -
20; if(firstList.getSelectedItem() ==
"Elevator 2-speed up" && building.elevators[1].elevatorspeed >
10) building.elevators[1].elevatorspeed
= building.elevators[1].elevatorspeed - 20; if(firstList.getSelectedItem() ==
"Elevator 3-speed up" && building.elevators[2].elevatorspeed >
10) building.elevators[2].elevatorspeed
= building.elevators[2].elevatorspeed - 20; if(firstList.getSelectedItem() == "Elevator 4-speed up"
&& building.elevators[3].elevatorspeed
> 10) building.elevators[3].elevatorspeed
= building.elevators[3].elevatorspeed -
20; if(firstList.getSelectedItem() ==
"Elevator 5-speed up" && building.elevators[4].elevatorspeed
> 10) building.elevators[4].elevatorspeed
= building.elevators[4].elevatorspeed -
20; if(firstList.getSelectedItem() == "Elevator 6-speed up"
&& building.elevators[5].elevatorspeed
> 10) building.elevators[5].elevatorspeed
= building.elevators[5].elevatorspeed -
20; if(firstList.getSelectedItem() == "Elevator 7-speed up"
&& building.elevators[6].elevatorspeed
> 10) building.elevators[6].elevatorspeed
= building.elevators[6].elevatorspeed -
20; if(firstList.getSelectedItem() == "Elevator 8-speed up"
&& building.elevators[7].elevatorspeed
> 10) building.elevators[7].elevatorspeed = building.elevators[7].elevatorspeed -
20; if(firstList.getSelectedItem() == "Elevator 9-speed up"
&& building.elevators[8].elevatorspeed
> 10) building.elevators[8].elevatorspeed = building.elevators[8].elevatorspeed -
20; if(firstList.getSelectedItem() == "Elevator 10-speed up"
&& building.elevators[9].elevatorspeed
> 10) building.elevators[9].elevatorspeed = building.elevators[9].elevatorspeed -
20; if(firstList.getSelectedItem() == "Elevator 11-speed up"
&& building.elevators[10].elevatorspeed
> 10) building.elevators[10].elevatorspeed =
building.elevators[10].elevatorspeed -
20; if(firstList.getSelectedItem() == "Elevator 12-speed up"
&& building.elevators[11].elevatorspeed
> 10) building.elevators[11].elevatorspeed =
building.elevators[11].elevatorspeed - 20; if(firstList.getSelectedItem() ==
"Elevator 13-speed up" && building.elevators[12].elevatorspeed
> 10) building.elevators[12].elevatorspeed
= building.elevators[12].elevatorspeed - 20; if(firstList.getSelectedItem()
== "Elevator 14-speed up" &&
building.elevators[13].elevatorspeed > 10) building.elevators[13].elevatorspeed
= building.elevators[13].elevatorspeed - 20; if(firstList.getSelectedItem()
== "Elevator 15-speed up" && building.elevators[14].elevatorspeed
> 10) building.elevators[14].elevatorspeed
= building.elevators[14].elevatorspeed - 20; if(firstList.getSelectedItem()
== "Elevator 1 - Increase Capacity" &&
building.elevators[0].CAPACITY < 95) building.elevators[0].CAPACITY
= building.elevators[0].CAPACITY + 5; if(e.getSource()
== secondList)
if(secondList.getSelectedItem() == "Floor 1- slow down") building.floors[0].FLOORCYCLE
= building.floors[0].FLOORCYCLE + 20;
if(secondList.getSelectedItem()
== "Floor 2- slow down") building.floors[1].FLOORCYCLE
= building.floors[1].FLOORCYCLE + 20; if(secondList.getSelectedItem()
== "Floor 3- slow down") building.floors[2].FLOORCYCLE
= building.floors[2].FLOORCYCLE + 20; if(secondList.getSelectedItem() == "Floor 4-slow down") building.floors[3].FLOORCYCLE
= building.floors[3].FLOORCYCLE + 20; if(secondList.getSelectedItem() == "Floor 5-slow down") building.floors[4].FLOORCYCLE
= building.floors[4].FLOORCYCLE + 20; if(secondList.getSelectedItem()
== "Floor 6-slow down") building.floors[5].FLOORCYCLE
= building.floors[5].FLOORCYCLE + 20; if(secondList.getSelectedItem() ==
"Floor 7-slow down") building.floors[6].FLOORCYCLE
= building.floors[6].FLOORCYCLE + 20; if(secondList.getSelectedItem()
== "Floor 8-slow down") building.floors[7].FLOORCYCLE
= building.floors[7].FLOORCYCLE + 20; if(secondList.getSelectedItem() == "Floor 9-slow down") building.floors[8].FLOORCYCLE
= building.floors[8].FLOORCYCLE + 20; if(secondList.getSelectedItem() == "Floor 10-slow down") building.floors[9].FLOORCYCLE
= building.floors[9].FLOORCYCLE + 20; if(secondList.getSelectedItem() == "Floor 11-slow down") building.floors[10].FLOORCYCLE
= building.floors[10].FLOORCYCLE + 20; if(secondList.getSelectedItem() == "Floor 12-slow down") building.floors[11].FLOORCYCLE
= building.floors[11].FLOORCYCLE + 20; if(secondList.getSelectedItem() == "Floor 13-slow down") building.floors[12].FLOORCYCLE
= building.floors[12].FLOORCYCLE + 20; if(secondList.getSelectedItem() == "Floor 14-slow down") building.floors[13].FLOORCYCLE
= building.floors[13].FLOORCYCLE + 20; if(secondList.getSelectedItem() == "Floor 15-slow down") building.floors[14].FLOORCYCLE
= building.floors[14].FLOORCYCLE + 20; if(secondList.getSelectedItem() == "Floor 1-speed up"
&& building.floors[0].FLOORCYCLE > 10) building.floors[0].FLOORCYCLE
= building.floors[0].FLOORCYCLE - 20; if(secondList.getSelectedItem() ==
"Floor 2-speed up" &&
building.floors[1].FLOORCYCLE > 10) building.floors[1].FLOORCYCLE
= building.floors[1].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Floor 3-speed up"
&& building.floors[2].FLOORCYCLE > 10) building.floors[2].FLOORCYCLE
= building.floors[2].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Floor 4-speed up"
&& building.floors[3].FLOORCYCLE > 10) building.floors[3].FLOORCYCLE
= building.floors[3].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Floor 5-speed up"
&& building.floors[4].FLOORCYCLE > 10) building.floors[4].FLOORCYCLE
= building.floors[4].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Floor 6-speed up"
&& building.floors[5].FLOORCYCLE > 10) building.floors[5].FLOORCYCLE
= building.floors[5].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Seventh Floor-speed up"
&& building.floors[6].FLOORCYCLE > 10) building.floors[6].FLOORCYCLE
= building.floors[6].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Eighth Floor-speed up"
&& building.floors[7].FLOORCYCLE > 10) building.floors[7].FLOORCYCLE
= building.floors[7].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Ninth Floor-speed up"
&& building.floors[8].FLOORCYCLE > 10) building.floors[8].FLOORCYCLE
= building.floors[8].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Tenth Floor-speed up"
&& building.floors[9].FLOORCYCLE > 10) building.floors[9].FLOORCYCLE
= building.floors[9].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Eleventh Floor-speed up"
&& building.floors[10].FLOORCYCLE > 10) building.floors[10].FLOORCYCLE
= building.floors[10].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Twelth Floor-speed up"
&& building.floors[11].FLOORCYCLE > 10) building.floors[11].FLOORCYCLE
= building.floors[11].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Thirtheenth Floor-speed up"
&& building.floors[12].FLOORCYCLE > 10) building.floors[12].FLOORCYCLE
= building.floors[12].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Fourteenth Floor-speed up"
&& building.floors[13].FLOORCYCLE > 10) building.floors[13].FLOORCYCLE
= building.floors[13].FLOORCYCLE - 20; if(secondList.getSelectedItem() == "Fifteenth Floor-speed up"
&& building.floors[14].FLOORCYCLE > 10) building.floors[14].FLOORCYCLE
= building.floors[14].FLOORCYCLE - 20;
if(secondList.getSelectedItem() == "Night Time" ) night(); if(secondList.getSelectedItem() == "Original" ) paint(getGraphics()); } public void paint(Graphics h){ h.setColor(Color.lightGray); h.fillRect(0,5,1000,1000); } public void night(){ Graphics k; k = getGraphics(); k.setColor(Color.black); k.fillRect(0,5,1000,1000); } class
mousePressHandler extends MouseAdapter implements MouseListener{ mousePressHandler(){addMouseListener(this);} public void mousePressed(MouseEvent
e){
building.mousePressed(e.getX(),e.getY());} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseReleased(MouseEvent
e){} } } class
building { private int NUMFLOORS = 25, FLOORXTOP = 70, FLOORYTOP = 0, BLDHEIGHT = NUMFLOORS*15, ELXTOP1 = 100, ELYTOP1 = 0, ELHEIGHT1 = NUMFLOORS*15, ELXTOP2 = 100, ELYTOP2 = 200, ELHEIGHT2 = 100, peoplespeed = 350, elevatorspeed = 50; protected int NUMEL = 18; protected Applet a; floor floors[] = new
floor[NUMFLOORS]; Elevator elevators[] = new Elevator[NUMEL]; building(Graphics g, Applet a){ floors[0] = new floor(g,0,NUMFLOORS,FLOORXTOP,FLOORYTOP,BLDHEIGHT-15*0,
peoplespeed-300,a); floors[0].start(); for (int i = 1; i < NUMFLOORS; i++) {
floors[i] = new floor(g,i,NUMFLOORS,FLOORXTOP,FLOORYTOP,BLDHEIGHT-15*i,
peoplespeed,a); floors[i].start(); } if (NUMEL == 1){ elevators[0] = new
Elevator(this,g,NUMFLOORS,ELXTOP1+20*0,ELYTOP1,ELHEIGHT1,
elevatorspeed,0,a); elevators[0].start(); } else{ for (int j = 0; j < 2*NUMEL/3;
j++){ elevators[j] = new Elevator(this,g,NUMFLOORS,ELXTOP1+20*j,ELYTOP1,ELHEIGHT1,
elevatorspeed,j,a); elevators[j].start(); } for (int k = 2*NUMEL/3; k < NUMEL;
k++){ elevators[k] = new
Elevator(this,g,NUMFLOORS/2,ELXTOP1+20*k,ELYTOP1,ELHEIGHT1, elevatorspeed,k,a); elevators[k].start(); } } } void mousePressed(int x, int y){ if (x>95 && x<105 && y<1000) elevators[0].OnOff(); if (x>115 && x<125
&& y<1000) elevators[1].OnOff(); if (x>135 && x<145
&& y<1000) elevators[2].OnOff(); if (x>155 && x<165
&& y<1000) elevators[3].OnOff(); if (x>175 && x<185
&& y<1000) elevators[4].OnOff(); if (x>195 && x<205
&& y<1000) elevators[5].OnOff(); if (x>215 && x<225
&& y<1000) elevators[6].OnOff(); if (x>235 && x<245 && y<1000)
elevators[7].OnOff(); if (x>255 && x<265
&& y<1000) elevators[8].OnOff(); if (x>275 && x<285
&& y<1000) elevators[9].OnOff(); if (x>295 && x<305
&& y<1000) elevators[10].OnOff(); if (x>315 && x<325
&& y<1000) elevators[11].OnOff(); if (x>335 && x<345
&& y<1000) elevators[12].OnOff(); if (x>355 && x<365
&& y<1000) elevators[13].OnOff(); if (x>375 && x<385
&& y<1000) elevators[14].OnOff(); if (x>395 && x<405
&& y<1000) elevators[15].OnOff(); if (x>415 && x<425 && y<1000)
elevators[16].OnOff(); if (x>435 && x<445
&& y<1000) elevators[17].OnOff(); if (x>455 && x<465
&& y<1000) elevators[18].OnOff(); if (x>475 && x<485
&& y<1000) elevators[19].OnOff(); if (x>495 && x<505
&& y<1000) elevators[20].OnOff(); if (x>515 && x<525
&& y<1000) elevators[21].OnOff(); if (x>535 && x<545
&& y<1000) elevators[22].OnOff(); if (x>555 && x<565
&& y<1000) elevators[23].OnOff(); if (x>575 && x<585
&& y<1000) elevators[24].OnOff();
if (x>595 && x<605 && y<1000)
elevators[25].OnOff(); if (x>615 && x<625
&& y<1000) elevators[26].OnOff(); if (x>635 && x<645
&& y<1000) elevators[27].OnOff(); if (x>655 && x<665
&& y<1000) elevators[28].OnOff(); if (x>675 && x<685
&& y<1000) elevators[29].OnOff(); if (x>695 && x<705
&& y<1000) elevators[30].OnOff(); if (x>715 && x<725
&& y<1000) elevators[31].OnOff(); if (x>735 && x<745
&& y<1000) elevators[32].OnOff(); if (x>755 && x<765
&& y<1000) elevators[33].OnOff(); if (x>775 && x<785
&& y<1000) elevators[34].OnOff(); if (x>795 && x<805
&& y<1000) elevators[35].OnOff(); if (x>815 && x<825
&& y<1000) elevators[36].OnOff(); if (x>835 && x<845
&& y<1000) elevators[37].OnOff(); if (x>855 && x<865
&& y<1000) elevators[38].OnOff(); if (x>875 && x<885
&& y<1000) elevators[39].OnOff(); if (x>895 && x<905
&& y<1000) elevators[40].OnOff(); if (x>915 && x<925
&& y<1000) elevators[41].OnOff(); if (x>935 && x<945
&& y<1000) elevators[42].OnOff(); if (x>955 && x<965
&& y<1000) elevators[43].OnOff(); if (x>975 && x<985
&& y<1000) elevators[44].OnOff(); } } class
floor extends Thread { private int floorNum, topX, topY, height,
MaxFloor, newArrivals = 2; protected int FLOORCYCLE; private Graphics g; protected Applet a; container container = new container(); personNode filler(int summon){ personNode people[] = new personNode[100]; for(int i=0; i<100; i++) people[i] = new personNode(); return people[summon]; } button button = new button(); floor(Graphics g, int floornum, int
MaxFloor,int topX, int topY, int height, int FLOORCYCLE, Applet a){ this.a = a; this.g = g; this.topX = topX; this.topY = topY; this.height = height; this.floorNum = floornum; this.MaxFloor = MaxFloor;
this.FLOORCYCLE = FLOORCYCLE; } private void draw(){ synchronized(g){ a.invalidate(); int EYPos = topY+height; g.setColor(Color.white); g.fillRect(topX-100, EYPos, 100,10); g.setColor(Color.blue); g.fillRect((topX-container.count()),
EYPos, container.count() ,10);
if(container.count() < 10) g.setColor(Color.black); else if (container.count() < 15) g.setColor(Color.orange); else g.setColor(Color.yellow); g.drawString(container.count()+
"", topX-15, EYPos+10); a.validate(); } } public void run(){ while(true){ draw(); try { Thread.sleep(FLOORCYCLE);} catch(InterruptedException
exception){} if (container.count() < 99)
container.insert(filler(container.count()+1)); if (container.count() > 0) button.Press(); if (container.count() == 0)
button.Release(); } } } class
Elevator extends Thread{ private
button button[] = new button[NUMFLOORS]; private int howmany[] = new
int[NUMFLOORS]; private int TopXPos, TopYPos, Height,
NUMFLOORS, number = 0; protected Applet a; protected int elevatorspeed=0, numPersons
= 0, CAPACITY = 15; private boolean GoingUp = true, GoingDown
= false, SwitchedOn = true; private building building; protected controller controller = new
controller(NUMFLOORS); private Graphics g; Elevator(building building, Graphics g,
int NUMFLOORS, int TopXPos, int
TopYPos, int Height, int elevatorspeed, int number,Applet a){
this.a = a; this.TopXPos = TopXPos; this.TopYPos = TopYPos;
this.Height = Height; this.number = number; this.building = building; this.g = g;
this.NUMFLOORS = NUMFLOORS; this.elevatorspeed = elevatorspeed; for (int i = 0; i < NUMFLOORS; i++)
button[i] = new button(); } int EYPos = TopYPos+Height; int FloorPos = TopYPos+Height; int EYPos(){ if(EYPos == Height+TopYPos){GoingUp = true; GoingDown =
false;} if(EYPos ==
(TopYPos+Height)-15*(NUMFLOORS-1)){GoingDown = true; GoingUp = false;} if(GoingDown){return EYPos+15;} if(GoingUp){return EYPos-15;} else return EYPos; } void OnOff(){SwitchedOn =
!SwitchedOn;} private
void draw(){ synchronized(g){
a.invalidate();
g.setColor(Color.yellow);
g.drawLine(TopXPos,(TopYPos+Height)-15*(NUMFLOORS-1),TopXPos,TopYPos+Height);
g.setColor(Color.white);
for ( int i = 0; i < NUMFLOORS; i++){ g.fillRect(TopXPos-5,FloorPos,12,10); FloorPos = FloorPos -15; }
FloorPos = TopYPos+Height;
g.setColor(Color.blue);
g.fillRect(TopXPos-5,EYPos,12,10);
g.setColor(Color.red);
g.fillRect(TopXPos-5,EYPos,12,10-(int)((numPersons*10)/CAPACITY));
g.setColor(Color.yellow);
g.drawString(numPersons + "", TopXPos-6, EYPos + 10);
a.validate();
} } public void run(){ int count = 0; while(true) {
if (SwitchedOn){ if(GoingUp) ++count; if(GoingDown) --count; if (GoingUp &&
button[count-1] != null){ if(button[count-1].pressed){ peopleOutup(number,howmany[coun t-1]); button[count-1].Release(); } } if(count-1 == 0){
peopleOut(number); peopleIn(0, number);
for ( int h = 0; h <
NUMFLOORS; h++)
howmany[h] = 0;
int people[] = new int[numPersons]; for ( int j = 0; j < numPersons; j++){ if (number
< 2*building.NUMEL/3) people[j] =
controller.getDestination(NUMFLOORS);
else
people[j] = controller.getDestination(NUMFLOORS/2); } for (int k = 0; k < numPersons; k++){ button[people[k]].Press(); howmany[people[k]] = howmany[people[k]] + 1; } } else if
((building.floors[count-1].button.Pressed()&&GoingDown) ||
((count)==NUMFLOORS&&building.floors[count-1].button.Pressed())) peopleIn(count-1, number); draw(); EYPos = EYPos(); try { Thread.sleep(elevatorspeed);}
catch(InterruptedException exception){} } else { draw(); try { Thread.sleep(elevatorspeed);} catch(InterruptedException exception){} } } } synchronized private void peopleOutup(int
i, int j){ building.elevators[i].numPersons = building.elevators[i].numPersons - j; } private void peopleOut(int i){ building.elevators[i].numPersons = 0;
} synchronized private void peopleIn(int i,
int j){ int temp = 0; if (building.elevators[j].numPersons
< building.elevators[j].CAPACITY) temp = building.elevators[j].CAPACITY -
building.elevators[j].numPersons; else
return; if ( temp
<=building.floors[i].container.count()){ building.elevators[j].numPersons
= building.elevators[j].numPersons + temp; while(temp
!= 0){ building.floors[i].container.removeHead(); temp--; } } else{ building.elevators[j].numPersons =
building.floors[i].container.count() + building.elevators[j].numPersons; building.floors[i].container.empty(); } } } interface
containerSpec { void insert (personNode pn); personNode removeHead(); void empty(); boolean isEmpty(); int count(); } class personNode { class person {int destination;} person
person; personNode next; personNode(){person
= new person(); next = null; } } class container implements containerSpec { private personNode header; container(){header = null; }
public void insert (personNode pn){ pn.next = header; header = pn; } public personNode removeHead(){ personNode temp = null;
if ( header != null) { temp = header;
header = header.next; } return temp; } public void empty(){ if ( header != null){ header.next = null; header = null; } } public boolean isEmpty(){ return ( header == null); } public
int count(){
int count = 0;
personNode ref = header;
while (ref != null){ count++; ref =
ref.next;
}
return count; } } interface controllerSpec {
int getfloorCycle();
int getelevatorCycle(); int getArrivals(int
floorNum); int getDestination(int floorNum);
} class
controller implements controllerSpec{
int NUMFLOORS;
controller(int NUMFLOORS){}
public int getfloorCycle(){ int a = 0; return a; } public
int getelevatorCycle(){int b = 0;return b;} public int getArrivals(int floorNum){int c
= 0; return c;} public int getDestination(int
floorNum){ int d = 0; d = (int)(Math.random()*(floorNum-1))+1; return d; } } interface buttonSpec{
boolean Pressed();
void Press();
void Release(); } class button implements buttonSpec{
boolean pressed = false; public boolean Pressed(){ return pressed;
}
public void Press(){ pressed = true;
}
public void Release(){ pressed = false;
} } |
PLAIN ENGLISH
EXPLANATION
This
is the elevators class which has within it , declarations of many variables.
For example , firstList and secondList are of type list and building is of
type building . On
the direct left of the page is what allows us to place the choicebox around
the border of the applet as opposed to the default, which extends horizontally
across the applet. Such commands to
place objects using the BorderLayout() method are SOUTH,EAST, NORTH, WEST ,
which are used to place objects in their respective positions. On
the left is the init method, in which a list called first list is constructed
and the parameter five represents how many objects are visible within the
choicebox. Such things as, “Elevator 1-slow down” and “Elevator 1-speed up”
are examples of the choices within the choicebox. Essentially, a choicebox is created. This
is an example of a command within the BorderLayout method that directs the
object to the SOUTH of the applet and places is there. With the options
within it being equal to those inserted into the first list above. This
adds the second list created in the above page to the eastern portion of the
applet. This
is a constructor for the building class as well as the mousePressedHandler
class. This
is the itemStateChanged() method that we used in our lights applet in an
assignment this year . If , for
example, “Elevator 1-slow down” is selected , the first elevator in our array
, defined by elevator[0] will sleep for 20 milliseconds longer than it’s
default length, which therefore makes the elevator appear to move slower in
the applet. The
same applies for each of the fifteen elevators we chose to allow the user to
vary the speed. This
code on the direct left hand side of the page, will decrease the amount of
time the set elevator sleeps by 20 milliseconds , to a minimum sleep time of
10 milliseconds. Anything less would simply make the passengers noxious and a
possible law suit may arise. This
choice is if the selected elevator is chosen, in this case, it would be
elevator one, if its capacity is less than 95 than the capacity will be
increased by 5 . The number 95 was chosen simply because of the problem with
choosing a three digit number causes. The problem was that it displays the
third digit directly on the applet and is outside a given floor/elevator. We
chose do apply this capacity capability only to the first elevator. The
FLOORCYCLE is similar to the elevator speed command in which the cycle of
people on and off a floor will take longer if 20 milliseconds is added. The
same applies for the remainder of the 15 floors that the user may
manipulate. The opposite applies when
the 20 milliseconds is subtracted from the sleep time of each floor. This
is the same logic that applied for the elevatorspeed in which the speed of
people cycling on and off of each floor will be increased by subtracting 20
milliseconds from the sleep time of each floor. If
the “Night Time” choice is chosen then the night() method is chosen and
repaint() repaints the applet with a black background to simulate a building
operating in the night. This
is the paint method which is called when the “Original” choice box is chosen And
redraws a light grey background rectangle with the given dimensions on the
left hand side. This
is the night() method which defines the same dimensions of a new rectangle to
be drawn and draws it black. This
is the mousePressHandler class which implements the mouse listener which
listens for whether or not the mouse is pressed . When the mouse is pressed , the mousePressed method in the
building class is given those x and y coordinates. As shown in the next line
on the next page. This
is the building class which defines all of the attributes of a building.
These attributes in simple English are: The number of floors in a building(25
in this case), the x and y coordinates of the top of each floor, and the x
and y coordinates of each elevator. The height of the building is a variable
in order for the applet to contain the number of floors if they are changed
within the code. The speed of the people appearing on each floor is defined
with peoplespeed which is equivalent to FLOORCYCLE , and this is initialized
at 350 milliseconds. The elevator speed is initialized to 50 milliseconds.
The number of elevators are defined to be of type int , for obvious reasons
and initialized to 18. Here
an array is created for all the floors.
The first floor is separate because the speed of the people appearing
at the lobby is faster than on all the other floors. Varying the NUMFLOORS variable can change
the size of the array and consequently the number of floors. The
same thing is done with the elevators.
The
mousePressed method. This is called from the mouseHandler method when the
mouse clicks. If the mouse is clicked
within the selected parameters ( ie on a certain elevator) that elevator then
stops or conversely starts up again. This
is the floor class. It is used for
the floors. The parameter topX is for
the top x coordinate, topY is the top y coordinate, height is the height of
the building, FLOORCYCLE is how long the thread sleeps for. A graphics object and an applet are also
declared. The floor class also has a
container which keeps track of the people on the floor. The
filler method is used to insert more people into the container. Each
floor has a button. The
constructor method for the floor. The
draw method for the floor. a.invalidate
is used to keep the applet from flickering when it is drawn. At the end of the draw method a.validate
is called. a is the name of the
applet which refers to the current applet.
It was passed into the building class and then into the floor and
elevator classes. The draw method
draws a white rectangle to represent the floor. Over this white rectangle is a blue rectangle which represents
the number of people on the floor.
Also the number of people on each floor is represented by an
integer. The color of the integer
changes depending on how many people there are on the floor. If there is a small number of people then
the color is black, because it is easier to see it on the white
background. If there is more then
fifteen people on the floor then the color of the integer is yellow because
it is easier to see on the blue background.
In between the color is orange.
The
run method for the floor. If
there is less than 99 people more people are added to the floor. If there are people on the floor then the
button is pressed. If there is no
people on the floor then the button is released. The
Elevator class. An
array of buttons for each elevator.
This is just like buttons on a real elevator. The
integer number represents the number of the elevator in the array of
elevators created up above. This
variable can later be used to refer to the specific elevator. The
Booleans GoingUp and GoingDown are used to determine whether the elvator is
going up or down. SwitchedOn is used
to determine if the elevator is on or off. The
constructor method for the Elevator class. A
method used to determine the position that the elevator is to be drawn
at. If it reaches the bottom it
switches GoingUp and GoingDown appropriately. Similarly when it reaches the top. If it is just going up or down then it increases or decreases its
position by 15. The
method to turn the elevator on and off. The
draw method. Draws
a line from the top to the bottom representing the elevator cable. Draws
the floors that the elevator goes through. Draws
a blue rectangle in the elevator representing how many people are currently
on the elevator. Draws
a red rectangle also. If the elevator
is empty then it is all red. Draws
the integer of how many people there is on the elevator. The
run method for the elevator. The
variable count is used to keep track of what floor the elevator is on. If it is going up then the variable is
increased, if it is going down then it is decreased. If
the elevator is going up and the button for the floor that it is on has been
pressed it lets the appropriate number of people out. If
it is on the bottom floor, it lets all the people out and takes on people
from the lobby that want to go up. Creates
an array which is used to represent the number of people who want to get off
at a specific floor. Creates
an array which calls the getDestination method and assigns a destination to
each person. If the elevator that is
doing this is one of the elevators that only services the bottom floors, the
destination is only for the bottom floors. This
for loop presses the buttons for the floors that people want to go to and
inserts into the array how many people want to go to each floor. If
the elevator is going down the floor and the button has been pressed or if
its at the top floor and the button has been pressed it lets people on. Gets
the new position. If
the elevator is not turned on it gets drawn in the same place over and over. The
method to let people out when the elevator is going up. One of the parameters is to determine
which elevator is being used and the other parameter is for how many people
get off. The
method for when the elevator reaches the bottom. The elevator is emptied. The
method to let people in to the elevator from the floors. If
the elevator is not full. It
calculates how many more people will fit on the elevator. If
there are more people on the floor than can fit on the elevator, then it adds
as many people as will fit on the elevator. Else
it adds all of the people from the floor. The
interface so we know what methods to put into the container class. The
class that is used to describe a person.
These personNodes are what is inserted and taken out of the floors. The
container class. This class is like a
List class. It is used to keep track
of the people on the floors. A
method to insert a person into the container. A
method to remove a person from the container. A
method to empty the container. A
method to check if the container is empty. A
method to count how many people are in the container (on the floor). An
interface to determine the methods for the controller. The
class for the controller. The
first three methods don’t do anything. This
last method determines a random destination when called. An
interface to determine the methods for the button class. The
button class. Used to make buttons. The
Pressed method returns the Boolean pressed.
This method is called to check if the button is pressed or not. This
method presses the button. This
method releases the button. |
Testing:
There is one bug
remaining. The bug is that if you set
the program so that it has less than four floors, then there is a number of
Null Pointer Exceptions. This problem
has something to do with the integer count in the elevator run method. This integer is used to keep track of what
floor the elevator is on; when there are a small number of floors it messes
up. This is the only bug that we couldn’t
fix. Other problems, which were
difficult to solve, were the people getting on the elevator on the lobby and
getting off on the way up. We don’t
have a container on each elevator; instead the people were represented by
integers. There was an array that kept
track of the destination of each person on the elevator. When the elevator reached a floor to which
people were heading, it let the number of people off that wanted to go
there. This was all done with arrays
and it may have been more sophisticated and cleaner if we had used lists and
put a container class in each elevator.
Nonetheless the way we did it does the exact same thing and therefore
serves the purpose of our simulation.
Another hard part was getting the applet to draw the elevator with a
blue bar representative of how full it was. This was tricky because the rectangle
starts in the upper right hand corner and draws downwards. To test the applet we first did the obvious
things such as compiling it and picking out any errors. There are some added features, which can
also be used to pick out bugs. For
instance, the blue bar representing the number of people in each elevator was
used to determine if the methods for letting the people on and off the
elevators were working correctly. At
one point in the evolution of our program the elevators were not letting people
off on the way up. Instead they would
take the people all the way to the top and then bring them back down to the
lobby and let them off there. This was
easily picked up with the blue bars, and the problem was fixed. There is another added feature where the
speed of the elevators and the speed of the appearance of the people can be
changed right on the applet while it is running. This can be a quick way to make sure that everything is working
correctly, because we don’t have to wait for the floors to fill up.
A
PERFECTLY FORMATTED COPY OF OUR CODE:
import
java.applet.Applet;
import
java.awt.List;
import
java.awt.*;
import
java.awt.event.*;
public
class levatahs extends Applet implements ItemListener{
List
firstList; // declares a choice list.
List secondList;
//declares
the h variable to be of type Graphics
Graphics
h;
//declares
building to be of type building
building
building;
//this method init() creates a constructor for the
building class and passes the getGraphics() method
//through
as a parameter
public
void init() { // initilizes the program.
this.setLayout(new
BorderLayout()); // changes the set layout.
firstList
= new List(5);// creates the choice list.
firstList.add("Elevator
1-slow down"); // adds elements to the list.
firstList.add("Elevator
2-slow down");
firstList.add("Elevator
3-slow down");
firstList.add("Elevator
4-slow down");
firstList.add("Elevator
5-slow down");
firstList.add("Elevator
6-slow down");
firstList.add("Elevator
7-slow down");
firstList.add("Elevator
8-slow down");
firstList.add("Elevator
9-slow down");
firstList.add("Elevator
10-slow down");
firstList.add("Elevator
11-slow down");
firstList.add("Elevator
12-slow down");
firstList.add("Elevator
13-slow down");
firstList.add("Elevator
14-slow down");
firstList.add("Elevator
15-slow down");
firstList.add("Elevator
1-speed up");
firstList.add("Elevator
2-speed up");
firstList.add("Elevator
3-speed up");
firstList.add("Elevator
4-speed up");
firstList.add("Elevator
5-speed up");
firstList.add("Elevator
6-speed up");
firstList.add("Elevator
7-speed up");
firstList.add("Elevator
8-speed up");
firstList.add("Elevator
9-speed up");
firstList.add("Elevator
10-speed up");
firstList.add("Elevator
11-speed up");
firstList.add("Elevator
12-speed up");
firstList.add("Elevator
13-speed up");
firstList.add("Elevator
14-speed up");
firstList.add("Elevator
15-speed up");
firstList.add("Elevator
1 - Increase Capacity");
add(firstList,BorderLayout.SOUTH);
firstList.addItemListener(this);//
this list listens for items.
secondList
= new List(6);
secondList.add("Floor
1-slow down");// adds elements to the second list.
secondList.add("Floor
2-slow down");
secondList.add("Floor
3-slow down");
secondList.add("Floor
4-slow down");
secondList.add("Floor
5-slow down");
secondList.add("Floor
6-slow down");
secondList.add("Floor
7-slow down");
secondList.add("Floor
8-slow down");
secondList.add("Floor
9-slow down");
secondList.add("Floor
10-slow down");
secondList.add("Floor
11-slow down");
secondList.add("Floor
12-slow down");
secondList.add("Floor
13-slow down");
secondList.add("Floor
14-slow down");
secondList.add("Floor
15-slow down");
secondList.add("Floor
1-speed up");
secondList.add("Floor
2-speed up");
secondList.add("Floor
3-speed up");
secondList.add("Floor
4-speed up");
secondList.add("Floor
5-speed up");
secondList.add("Floor
6-speed up");
secondList.add("Floor
7-speed up");
secondList.add("Floor
8-speed up");
secondList.add("Floor
9-speed up");
secondList.add("Floor
10-speed up");
secondList.add("Floor
11-speed up");
secondList.add("Floor
12-speed up");
secondList.add("Floor
13-speed up");
secondList.add("Floor
14-speed up");
secondList.add("Floor
15-speed up");
secondList.add("Night
Time");
secondList.add("Original");
add(secondList,BorderLayout.EAST);//
inserts the second list at the right.
secondList.addItemListener(this);//
the second list also listens for items.
building
= new building(getGraphics(),this); // creates a new building.
//assigns all of the attributes of the getGraphics()
method to the h variable previously definedto be of type Graphics
h
= getGraphics();
//creates a constructor for mousePressHandler
allowing it to be accessed from this class
mousePressHandler
one = new mousePressHandler();
}
public
void itemStateChanged(ItemEvent e){
if(e.getSource() == firstList) // if the
item state changed is on the first list.
if(firstList.getSelectedItem() ==
"Elevator 1-slow down") // if you press
building.elevators[0].elevatorspeed =
building.elevators[0].elevatorspeed + 20;// <--|
if(firstList.getSelectedItem() ==
"Elevator 2-slow down")
building.elevators[1].elevatorspeed =
building.elevators[1].elevatorspeed +
20;//slows a certain elevator down.
if(firstList.getSelectedItem() ==
"Elevator 3-slow down")
building.elevators[2].elevatorspeed =
building.elevators[2].elevatorspeed +
20;
if(firstList.getSelectedItem() == "Elevator 4-slow down")
building.elevators[3].elevatorspeed =
building.elevators[3].elevatorspeed +
20;
if(firstList.getSelectedItem() == "Elevator 5-slow down")
building.elevators[4].elevatorspeed =
building.elevators[4].elevatorspeed +
20;
if(firstList.getSelectedItem() == "Elevator 6-slow down")
building.elevators[5].elevatorspeed =
building.elevators[5].elevatorspeed +
20;
if(firstList.getSelectedItem() == "Elevator 7-slow down")
building.elevators[6].elevatorspeed = building.elevators[6].elevatorspeed
+
20;
if(firstList.getSelectedItem()
== "Elevator 8-slow down")
building.elevators[7].elevatorspeed =
building.elevators[7].elevatorspeed +
20;
if(firstList.getSelectedItem()
== "Elevator 9-slow down")
building.elevators[8].elevatorspeed =
building.elevators[8].elevatorspeed +
20;
if(firstList.getSelectedItem()
== "Elevator 10-slow down")
building.elevators[9].elevatorspeed =
building.elevators[9].elevatorspeed + 20;
if(firstList.getSelectedItem() ==
"Elevator 11-slow down")
building.elevators[10].elevatorspeed =
building.elevators[10].elevatorspeed +
20;
if(firstList.getSelectedItem() ==
"Elevator 12-slow down")
building.elevators[11].elevatorspeed =
building.elevators[11].elevatorspeed +
20;
if(firstList.getSelectedItem() ==
"Elevator 13-slow down")
building.elevators[12].elevatorspeed =
building.elevators[12].elevatorspeed +
20;
if(firstList.getSelectedItem() ==
"Elevator 14-slow down")
building.elevators[13].elevatorspeed =
building.elevators[13].elevatorspeed + 20;
if(firstList.getSelectedItem() == "Elevator 15-slow down")
building.elevators[14].elevatorspeed =
building.elevators[14].elevatorspeed + 20;
if(firstList.getSelectedItem() ==
"Elevator 1-speed up" &&
building.elevators[0].elevatorspeed
> 10)
building.elevators[0].elevatorspeed
= building.elevators[0].elevatorspeed - 20; //
speed a certain elevator up.
if(firstList.getSelectedItem() ==
"Elevator 2-speed up" &&
building.elevators[1].elevatorspeed
> 10)
building.elevators[1].elevatorspeed =
building.elevators[1].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 3-speed up" &&
building.elevators[2].elevatorspeed
> 10)
building.elevators[2].elevatorspeed =
building.elevators[2].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 4-speed up" &&
building.elevators[3].elevatorspeed
> 10)
building.elevators[3].elevatorspeed =
building.elevators[3].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 5-speed up" &&
building.elevators[4].elevatorspeed
> 10)
building.elevators[4].elevatorspeed =
building.elevators[4].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 6-speed up" &&
building.elevators[5].elevatorspeed
> 10)
building.elevators[5].elevatorspeed =
building.elevators[5].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 7-speed up" &&
building.elevators[6].elevatorspeed
> 10)
building.elevators[6].elevatorspeed =
building.elevators[6].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 8-speed up" &&
building.elevators[7].elevatorspeed
> 10)
building.elevators[7].elevatorspeed =
building.elevators[7].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 9-speed up" &&
building.elevators[8].elevatorspeed
> 10)
building.elevators[8].elevatorspeed =
building.elevators[8].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 10-speed up" &&
building.elevators[9].elevatorspeed
> 10)
building.elevators[9].elevatorspeed =
building.elevators[9].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 11-speed up" &&
building.elevators[10].elevatorspeed
> 10)
building.elevators[10].elevatorspeed =
building.elevators[10].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 12-speed up" &&
building.elevators[11].elevatorspeed
> 10)
building.elevators[11].elevatorspeed =
building.elevators[11].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 13-speed up" &&
building.elevators[12].elevatorspeed
> 10)
building.elevators[12].elevatorspeed =
building.elevators[12].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 14-speed up" &&
building.elevators[13].elevatorspeed > 10)
building.elevators[13].elevatorspeed
= building.elevators[13].elevatorspeed - 20;
if(firstList.getSelectedItem() == "Elevator
15-speed up" &&
building.elevators[14].elevatorspeed > 10)
building.elevators[14].elevatorspeed
= building.elevators[14].elevatorspeed - 20;
if(firstList.getSelectedItem() ==
"Elevator 1 - Increase Capacity" &&
building.elevators[0].CAPACITY <
95)
building.elevators[0].CAPACITY =
building.elevators[0].CAPACITY + 5;
if(e.getSource() == secondList)// if the
second list is pressed.
if(secondList.getSelectedItem() ==
"Floor 1-slow down") //slows the flow rate of
people on a certain floor.
building.floors[0].FLOORCYCLE =
building.floors[0].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 2-slow down")
building.floors[1].FLOORCYCLE =
building.floors[1].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 3-slow down")
building.floors[2].FLOORCYCLE =
building.floors[2].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 4-slow down")
building.floors[3].FLOORCYCLE =
building.floors[3].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 5-slow down")
building.floors[4].FLOORCYCLE =
building.floors[4].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 6-slow down")
building.floors[5].FLOORCYCLE =
building.floors[5].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 7-slow down")
building.floors[6].FLOORCYCLE =
building.floors[6].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 8-slow down")
building.floors[7].FLOORCYCLE =
building.floors[7].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 9-slow down")
building.floors[8].FLOORCYCLE =
building.floors[8].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 10-slow down")
building.floors[9].FLOORCYCLE =
building.floors[9].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 11-slow down")
building.floors[10].FLOORCYCLE =
building.floors[10].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 12-slow down")
building.floors[11].FLOORCYCLE =
building.floors[11].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 13-slow down")
building.floors[12].FLOORCYCLE =
building.floors[12].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 14-slow down")
building.floors[13].FLOORCYCLE =
building.floors[13].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 15-slow down")
building.floors[14].FLOORCYCLE =
building.floors[14].FLOORCYCLE + 20;
if(secondList.getSelectedItem() ==
"Floor 1-speed up" &&
building.floors[0].FLOORCYCLE > 10)
building.floors[0].FLOORCYCLE =
building.floors[0].FLOORCYCLE - 20;//
speeds the flow rate up on a certain
floor.
if(secondList.getSelectedItem() ==
"Floor 2-speed up" &&
building.floors[1].FLOORCYCLE > 10)
building.floors[1].FLOORCYCLE =
building.floors[1].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Floor 3-speed up" &&
building.floors[2].FLOORCYCLE > 10)
building.floors[2].FLOORCYCLE =
building.floors[2].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Floor 4-speed up" &&
building.floors[3].FLOORCYCLE > 10)
building.floors[3].FLOORCYCLE =
building.floors[3].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Floor 5-speed up" &&
building.floors[4].FLOORCYCLE > 10)
building.floors[4].FLOORCYCLE = building.floors[4].FLOORCYCLE
- 20;
if(secondList.getSelectedItem() ==
"Floor 6-speed up" &&
building.floors[5].FLOORCYCLE >
10)
building.floors[5].FLOORCYCLE =
building.floors[5].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Seventh Floor-speed up" &&
building.floors[6].FLOORCYCLE >
10)
building.floors[6].FLOORCYCLE =
building.floors[6].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Eighth Floor-speed up" &&
building.floors[7].FLOORCYCLE > 10)
building.floors[7].FLOORCYCLE =
building.floors[7].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Ninth Floor-speed up" &&
building.floors[8].FLOORCYCLE > 10)
building.floors[8].FLOORCYCLE =
building.floors[8].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Tenth Floor-speed up" &&
building.floors[9].FLOORCYCLE > 10)
building.floors[9].FLOORCYCLE =
building.floors[9].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Eleventh Floor-speed up" &&
building.floors[10].FLOORCYCLE >
10)
building.floors[10].FLOORCYCLE =
building.floors[10].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Twelth Floor-speed up" &&
building.floors[11].FLOORCYCLE >
10)
building.floors[11].FLOORCYCLE =
building.floors[11].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Thirtheenth Floor-speed up" &&
building.floors[12].FLOORCYCLE >
10)
building.floors[12].FLOORCYCLE =
building.floors[12].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Fourteenth Floor-speed up" &&
building.floors[13].FLOORCYCLE >
10)
building.floors[13].FLOORCYCLE =
building.floors[13].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Fifteenth Floor-speed up" &&
building.floors[14].FLOORCYCLE >
10)
building.floors[14].FLOORCYCLE =
building.floors[14].FLOORCYCLE - 20;
if(secondList.getSelectedItem() ==
"Night Time" )
night();
if(secondList.getSelectedItem() == "Original" )
paint(getGraphics());
}
//this method simply draws the entire
display and colors it lightgray
public void paint(Graphics h){
h.setColor(Color.lightGray);
h.fillRect(0,5,1000,1000);
}
public void night(){
Graphics k;
k = getGraphics();
k.setColor(Color.black);
k.fillRect(0,5,1000,1000);
}
class
mousePressHandler extends MouseAdapter implements MouseListener{
mousePressHandler(){addMouseListener(this);}
// to create a mousePressHandler
public void mousePressed(MouseEvent e){ //
in order so that something happens when
the mouse is pressed.
//the following if statements define the
x and y limits for where the mouse is capable
of stopping and\
//restarting each elevator. for example
the first elevator elevator zero is capable of
being stopped
//if pressed between 95 and 105 on the
x-scale and between 90 and 300 for the y-
scale. The same applies
//for the rest of the elevators
building.mousePressed(e.getX(),e.getY());}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}//
don't do anything
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}}
}
class
building { // the class which is for the building.
//the following variables are declared
privately, meaning they can only exist within
this class and are
//assigned initial values
private int NUMFLOORS = 25, FLOORXTOP =
70, FLOORYTOP = 0,
BLDHEIGHT = NUMFLOORS*15, ELXTOP1 = 100,
ELYTOP1 = 0, ELHEIGHT1 = NUMFLOORS*15,
ELXTOP2 = 100, ELYTOP2 =
200, ELHEIGHT2 = 100, peoplespeed = 350,
elevatorspeed = 50;
protected int NUMEL = 18;
protected Applet a;
// max number of floors is 44, min number
of floors is 4.(you don't need elevators if
you only have three floors)
// there is a bug here if you have three
or less floors, things mess up, explain later.
//
max number of elevators is 40, min number is 0 (pointless).
// speed of the people appearing and the
speed of the elevators can be varied.
floor floors[] = new floor[NUMFLOORS]; //
creates an array with the correct number
of floors.
Elevator elevators[] = new Elevator[NUMEL];// creates an array
with the correct
number of elevators
building(Graphics g, Applet a){//draws the
buliding.
floors[0] = new
floor(g,0,NUMFLOORS,FLOORXTOP,FLOORYTOP,BLDHEIGHT-15*0,
peoplespeed-300,a);
floors[0].start();// the bottom floor.
for (int i = 1; i < NUMFLOORS; i++)
{// a for loop to create the rest of the floors.
floors[i] = new f
floor(g,i,NUMFLOORS,FLOORXTOP,FLOORYTOP,BLDHEIGHT-15*i,
peoplespeed,a);
floors[i].start();
}
if (NUMEL == 1){
elevators[0] = new
Elevator(this,g,NUMFLOORS,ELXTOP1+20*0,ELYTOP1,ELHEIGHT1,
elevatorspeed,0,a); // creates a new
elevator.
elevators[0].start(); // starts the
elevator thread.
}
else{
for (int j = 0; j < 2*NUMEL/3; j++){
//a for loop to create the elevators
elevators[j] = new
Elevator(this,g,NUMFLOORS,ELXTOP1+20*j,ELYTOP1,ELHEIGHT1,
elevatorspeed,j,a); // creates a
new elevator.
elevators[j].start(); // starts the
elevator thread.
}
for (int k = 2*NUMEL/3; k < NUMEL;
k++){ // two thirds of the elevators go up all
the way, the rest only go up half
way.
elevators[k] = new
Elevator(this,g,NUMFLOORS/2,ELXTOP1+20*k,ELYTOP1,ELHEIGHT1,
elevatorspeed,k,a);
elevators[k].start();
}
}
}
void mousePressed(int x, int y){ // the
method to start and stop the elevators when the mouse clicks on them.
if
(x>95 && x<105 &&
y<1000) elevators[0].OnOff();
if
(x>115 && x<125 && y<1000) elevators[1].OnOff();
if
(x>135 && x<145 && y<1000) elevators[2].OnOff();
if
(x>155 && x<165 && y<1000) elevators[3].OnOff();
if
(x>175 && x<185 && y<1000) elevators[4].OnOff();
if
(x>195 && x<205 && y<1000) elevators[5].OnOff();
if
(x>215 && x<225 && y<1000) elevators[6].OnOff();
if
(x>235 && x<245 && y<1000) elevators[7].OnOff();
if
(x>255 && x<265 && y<1000) elevators[8].OnOff();
if
(x>275 && x<285 && y<1000) elevators[9].OnOff();
if
(x>295 && x<305 && y<1000) elevators[10].OnOff();
if
(x>315 && x<325 && y<1000) elevators[11].OnOff();
if
(x>335 && x<345 && y<1000) elevators[12].OnOff();
if
(x>355 && x<365 && y<1000) elevators[13].OnOff();
if
(x>375 && x<385 && y<1000) elevators[14].OnOff();
if
(x>395 && x<405 && y<1000) elevators[15].OnOff();
if
(x>415 && x<425 && y<1000) elevators[16].OnOff();
if
(x>435 && x<445 && y<1000) elevators[17].OnOff();
if
(x>455 && x<465 && y<1000) elevators[18].OnOff();
if
(x>475 && x<485 && y<1000) elevators[19].OnOff();
if
(x>495 && x<505 && y<1000) elevators[20].OnOff();
if
(x>515 && x<525 && y<1000) elevators[21].OnOff();
if
(x>535 && x<545 && y<1000) elevators[22].OnOff();
if
(x>555 && x<565 && y<1000) elevators[23].OnOff();
if
(x>575 && x<585 && y<1000) elevators[24].OnOff();
if
(x>595 && x<605 && y<1000) elevators[25].OnOff();
if
(x>615 && x<625 && y<1000) elevators[26].OnOff();
if
(x>635 && x<645 && y<1000) elevators[27].OnOff();
if
(x>655 && x<665 && y<1000) elevators[28].OnOff();
if
(x>675 && x<685 && y<1000) elevators[29].OnOff();
if
(x>695 && x<705 && y<1000) elevators[30].OnOff();
if
(x>715 && x<725 && y<1000) elevators[31].OnOff();
if
(x>735 && x<745 && y<1000) elevators[32].OnOff();
if
(x>755 && x<765 && y<1000) elevators[33].OnOff();
if
(x>775 && x<785 && y<1000) elevators[34].OnOff();
if
(x>795 && x<805 && y<1000) elevators[35].OnOff();
if
(x>815 && x<825 && y<1000) elevators[36].OnOff();
if
(x>835 && x<845 && y<1000) elevators[37].OnOff();
if
(x>855 && x<865 && y<1000) elevators[38].OnOff();
if
(x>875 && x<885 && y<1000) elevators[39].OnOff();
if
(x>895 && x<905 && y<1000) elevators[40].OnOff();
if
(x>915 && x<925 && y<1000) elevators[41].OnOff();
if
(x>935 && x<945 && y<1000) elevators[42].OnOff();
if
(x>955 && x<965 && y<1000) elevators[43].OnOff();
if
(x>975 && x<985 && y<1000) elevators[44].OnOff();
}
}
class
floor extends Thread { // a thread for
the floors.
//initiates
and declares the following variables
private
int floorNum, topX, topY, height, MaxFloor, newArrivals = 2;
protected int FLOORCYCLE;
private
Graphics g;
protected
Applet a;
container
container = new container();
personNode
filler(int summon){ // a method to add people to the floor
personNode
people[] = new personNode[100];
for(int i=0; i<100; i++)
people[i]
= new personNode();
return people[summon];
}
button
button = new button();
//this is the floor method within the floor class
that passes all of the following variables
//through
as parameters
floor(Graphics g, int floornum, int MaxFloor,int
topX, int topY, int height, int FLOORCYCLE, Applet a){
//this.x
allows for the usage of the same variable when assigning the new
variable
the value equal to
//that of the value passed
through as a parameter
this.a
= a;
this.g
= g; this.topX = topX; this.topY =
topY;
this.height
= height; this.floorNum = floornum;
this.MaxFloor
= MaxFloor; this.FLOORCYCLE = FLOORCYCLE;
}
private
void draw(){ // the method to draw the floor.
synchronized(g){
a.invalidate();
int
EYPos = topY+height;
g.setColor(Color.white);
g.fillRect(topX-100,
EYPos, 100,10);
g.setColor(Color.blue);
g.fillRect((topX-container.count()), EYPos, container.count()
,10); // draws the blue rectangle.
if(container.count()
< 10)
g.setColor(Color.black);
else
if (container.count() < 15)
g.setColor(Color.orange);
else
g.setColor(Color.yellow);
g.drawString(container.count()+
"", topX-15, EYPos+10);
a.validate();
}
}
public void run(){ // the run method for the
floor thread.
while(true){
draw();
// draws the floor.
try
{ Thread.sleep(FLOORCYCLE);}
catch(InterruptedException
exception){}
if
(container.count() < 99) container.insert(filler(container.count()+1));
//
adds more people waiting until it reaches 100.
if
(container.count() > 0) button.Press();
if
(container.count() == 0) button.Release();
}
}
}
class
Elevator extends Thread{ // a thread for the elevator.
private
button button[] = new button[NUMFLOORS];
private
int howmany[] = new int[NUMFLOORS];
private
int TopXPos, TopYPos, Height, NUMFLOORS, number = 0;
protected
Applet a;
protected
int elevatorspeed=0, numPersons = 0, CAPACITY = 15;
private
boolean GoingUp = true, GoingDown = false, SwitchedOn = true;
private
building building;
protected
controller controller = new controller(NUMFLOORS);
private
Graphics g;
Elevator(building building, Graphics g, int
NUMFLOORS, int TopXPos, int TopYPos,
int Height, int elevatorspeed, int number,Applet a){
this.a = a; this.TopXPos = TopXPos; this.TopYPos =
TopYPos; this.Height = Height; this.number = number;
this.building
= building; this.g = g; this.NUMFLOORS = NUMFLOORS; t his.elevatorspeed = elevatorspeed;
for
(int i = 0; i < NUMFLOORS; i++)// creates button on the elevator.
button[i] = new button();
}
int EYPos = TopYPos+Height;
int FloorPos = TopYPos+Height;
int EYPos(){//determines the position of the
elevator.
if(EYPos == Height+TopYPos){GoingUp =
true; GoingDown = false;}
if(EYPos ==
(TopYPos+Height)-15*(NUMFLOORS-1)){GoingDown = true;
GoingUp = false;}
if(GoingDown){return EYPos+15;}
if(GoingUp){return EYPos-15;}
else return EYPos;
}
void OnOff(){SwitchedOn = !SwitchedOn;} //
turns the elevator on and off.
private void draw(){// a method which draws
the elevator.
synchronized(g){
a.invalidate();
g.setColor(Color.yellow);
g.drawLine(TopXPos,(TopYPos+Height)-15*(NUMFLOORS-
1),TopXPos,TopYPos+Height);
g.setColor(Color.white);
//this draws each of the 15 floors ,
spacing them by the designated
increments , equally each time
for ( int i = 0; i <
NUMFLOORS; i++){
g.fillRect(TopXPos-5,FloorPos,12,10);
FloorPos = FloorPos
-15;
}
FloorPos = TopYPos+Height;
g.setColor(Color.blue);
g.fillRect(TopXPos-5,EYPos,12,10);
g.setColor(Color.red);//
draws the elevator red if it is empty, draws it blue
if it is full
g.fillRect(TopXPos-5,EYPos,12,10-(int)((numPersons*10)/CAPACITY));//if
it is half full it draws it half red and half blue
g.setColor(Color.yellow);
g.drawString(numPersons +
"", TopXPos-6, EYPos + 10);
a.validate();
}
}
public void run(){ // the run method for the
elevator thread.
int count = 0;
while(true) {
if (SwitchedOn){ // if SwitchedOn then the elevator is drawn as moving.
if(GoingUp) ++count; // these
are to keep track of what floor the elevator is on.
if(GoingDown) --count;
if (GoingUp &&
button[count-1] != null){// drops people off as it is going up.
if(button[count-1].pressed){
peopleOutup(number,howmany[count-1]);
button[count-1].Release();
}
}
if(count-1 == 0){
peopleOut(number); // the method to let people out of the
elevator when
it
reaches the bottom.
peopleIn(0, number); // pick people up in the lobby.
for ( int h = 0; h < NUMFLOORS; h++)
howmany[h] = 0;
int people[] = new int[numPersons];
for ( int j = 0; j < numPersons; j++){// gets peoples
destination.
if (number < 2*building.NUMEL/3) people[j]
=
controller.getDestination(NUMFLOORS);
else people[j] =
controller.getDestination(NUMFLOORS/2);
}
for (int k = 0; k < numPersons; k++){
button[people[k]].Press();
howmany[people[k]]
= howmany[people[k]] + 1;
}
}
else
if
((building.floors[count-1].button.Pressed()&&GoingDown) ||
((count)==NUMFLOORS&&building.floors[count-1].button.Pressed()))
peopleIn(count-1, number); //
lets people in at every floor.
draw(); // calls the draw
method.
EYPos = EYPos();
try {
Thread.sleep(elevatorspeed);}
catch(InterruptedException
exception){}
}
else { // if the elevator is stopped it draws this.
draw();
try { Thread.sleep(elevatorspeed);}
catch(InterruptedException exception){}
}
}
}
synchronized private void peopleOutup(int i, int
j){// people get out as it is going
up.
building.elevators[i].numPersons =
building.elevators[i].numPersons - j;
}
private void peopleOut(int i){ // people getting out of the elevators.
building.elevators[i].numPersons = 0; // all the people get out
of the
elevator.
}
synchronized private void peopleIn(int i, int j){ // people getting into
the
elevator.
int temp = 0;
if (building.elevators[j].numPersons <
building.elevators[j].CAPACITY) // if the elevator is not full.
temp = building.elevators[j].CAPACITY –
building.elevators[j].numPersons;
else return; // if the elevator is full then return.
if ( temp <=
building.floors[i].container.count()){// if more people are waiting
then can fit on the elevator.
building.elevators[j].numPersons =
building.elevators[j].numPersons + temp;
while(temp != 0){
building.floors[i].container.removeHead();
temp--;
}
}
else{ // if everyone who is waiting can
fit on the elevator.
building.elevators[j].numPersons = building.floors[i].container.count()
+
building.elevators[j].numPersons;
building.floors[i].container.empty();
}
}
}
interface containerSpec {
void insert (personNode pn); // Inserts a new person at head.
personNode removeHead(); //Removes the head.
void empty(); // Empties the container
boolean isEmpty(); // Check if container is empty.
int count(); // Persons in container.
}
class personNode {
class person {int destination;}
person person; personNode
next;
personNode(){person = new
person(); next = null;
}
}
class container implements containerSpec {
private personNode header;
container(){header = null;
}
public void insert (personNode pn){ //
Inserts a new person at head.
pn.next = header;
header = pn;
}
public personNode removeHead(){ //Removes
the head.
personNode temp = null;
if ( header != null) {
temp = header;
header = header.next;
}
return temp;
}
public void empty(){ // Empties the
container
if ( header != null){
header.next = null;
header = null;
}
}
public boolean isEmpty(){ // Check if
container is empty.
return ( header == null);
}
public int count(){ // Persons in
container.
int count = 0;
personNode ref = header;
while (ref != null){
count++;
ref =
ref.next;
}
return count;
}
}
interface controllerSpec {
int
getfloorCycle(); //
Generates the floor sleep cycle.
int getelevatorCycle();
// The elevator sleep cycle.
int getArrivals(int floorNum); // The arrival of persons on each floor.
int getDestination(int floorNum); // The destination of persons
going up
from lobby.
}
class
controller implements controllerSpec{
int NUMFLOORS;
controller(int NUMFLOORS){}
public int getfloorCycle(){int a = 0; return a;}// useless
public int getelevatorCycle(){int b = 0;return b;}// useless
public int getArrivals(int floorNum){int c = 0; return c;}//
useless
public int getDestination(int floorNum){ // generates a random
destination.
int d = 0;
d = (int)(Math.random()*(floorNum-1))+1;
return d
}
}
interface
buttonSpec{
boolean Pressed();
void Press();
void Release();
}
class button implements buttonSpec{ // class
for the buttons.
boolean pressed = false;
public boolean
Pressed(){
return pressed;
}
public void Press(){
pressed = true;
}
public void Release(){
pressed = false;
}
}
Conclusion:
After all of the
hair-pulling problems and endless hours of confusion, we finally prevailed with
a project that made all of our troubles well worth it. We learned a great deal about debugging in
this project and problem solving techniques.
When we would get an error with the code, initially, we would spend much
of our time trying to figure out what kind of error it was. After some time, we learned what steps to
take not to make the same mistakes again.
This, we believe was one of the best parts of our individual group
project. Furthermore, we learned how to
communicate our ideas effectively to each other given a time frame and were
able to combine our ideas to bring to you our finished product, The Machineã. In
our opinion this project was at a very good level for the material learned in
the course. The first version and many
of the beginning steps were not too complicated and were enjoyable to figure
out. The final steps and the extras
were a bit harder but they provided a solid challenge to finish the project off. It was good how there were steps outlined to
say what to do and in what order. This
made the project easier. For future
years this would still be a good project to continue with but you may want to
consider something else in order to change things up. But even if you do change it you should keep the same type of
format that you used for this project (version 1, version 2 etc.). What we learned was how to apply all that we
have learned throughout the year. We
had the skills but by doing the project we were really able to see how
everything connects to everything else.