Advertisement
Guest User

Untitled

a guest
Dec 24th, 2013
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.concurrent.BlockingQueue;
  4.  
  5. public class Elevator implements Runnable{
  6.  
  7. int currentPosition;
  8.  
  9. BlockingQueue<ElevatorCall> queue;
  10. List<Integer> destinationFloors;
  11. List<ElevatorCall> peopleOnElevator;
  12.  
  13. public Elevator(BlockingQueue<ElevatorCall> queue)
  14. {
  15. this.currentPosition = 0;
  16. this.queue = queue;
  17. }
  18.  
  19. @Override
  20. public synchronized void run()
  21. {
  22. while(true)
  23. {
  24.  
  25.  
  26. List<ElevatorCall> callsWaiting = new ArrayList<ElevatorCall>();
  27. queue.drainTo(callsWaiting);
  28.  
  29. for(int i = 0; i < callsWaiting.size(); i++)
  30. {
  31. boolean directionToGo = getDirection(callsWaiting.get(i).getDestinationFloor(), currentPosition);
  32. int floorsToGoForCollection = getFloorsToGo(callsWaiting.get(i).getCollectionFloor());
  33. int floorsToGoForDestination = getFloorsToGo(callsWaiting.get(i).getDestinationFloor());
  34.  
  35. System.out.println("Elevator is moving to floor #" + callsWaiting.get(i).getCollectionFloor());
  36. System.out.println("Elevator is on floor " + currentPosition);
  37.  
  38. for(int j = 0; j < floorsToGoForCollection; j++)
  39. {
  40. boolean wasPersonOnFloor = checkIfPersonIsOnFloor(currentPosition, callsWaiting);
  41. boolean isDestinationFloor = checkIfDestination(currentPosition, callsWaiting);
  42.  
  43. if(wasPersonOnFloor)
  44. System.out.println("A person on this floor called the elevator");
  45.  
  46. if(isDestinationFloor)
  47. callsWaiting = removeDestinationFromQueue(currentPosition, callsWaiting);
  48.  
  49.  
  50. wasPersonOnFloor = false;
  51. moveFloor(directionToGo);
  52.  
  53. }
  54. }
  55. return;
  56. }
  57. }
  58.  
  59. //Checks and returns if the current floor is the destination for any elements of the queue
  60. public boolean checkIfDestination(int currentPosition, List<ElevatorCall> callsWaiting)
  61. {
  62. boolean isDestination = false;
  63.  
  64. for(int i = 0; i < callsWaiting.size(); i++)
  65. {
  66. if(callsWaiting.get(i).getDestinationFloor() == currentPosition)
  67. {
  68. isDestination = true;
  69. break;
  70. }
  71. }
  72.  
  73. return isDestination;
  74. }
  75.  
  76. //Removes a call from the queue if it is a destination floor
  77. private List<ElevatorCall> removeDestinationFromQueue(int currentPosition, List<ElevatorCall> callsWaiting)
  78. {
  79. List<ElevatorCall> newCallsWaiting = callsWaiting;
  80.  
  81. for(int i = 0; i < callsWaiting.size(); i++)
  82. {
  83. if(callsWaiting.get(i).getDestinationFloor() == currentPosition)
  84. {
  85. callsWaiting.remove(i);
  86. System.out.println("This floor was a destination, queue shortened");
  87. }
  88. }
  89.  
  90. return newCallsWaiting;
  91. }
  92.  
  93. //Gets how many floors until elevator reaches its destination
  94. public int getFloorsToGo(int destinationFloor)
  95. {
  96. int floorsToGo = destinationFloor - currentPosition;
  97. return floorsToGo;
  98. }
  99.  
  100. //Moves up or down dependent on direction
  101. public synchronized void moveFloor(boolean direction)
  102. {
  103. if(direction = false)
  104. currentPosition--;
  105. else
  106. currentPosition++;
  107.  
  108. System.out.println("Elevator is on floor " + currentPosition);
  109.  
  110. try {
  111. wait(100);
  112. } catch (InterruptedException e) {
  113. // TODO Auto-generated catch block
  114. e.printStackTrace();
  115. }
  116. }
  117.  
  118. //Direction to move, true is up, false is down
  119. public boolean getDirection(int destinationFloor, int currentPosition)
  120. {
  121. boolean directionToGo = true;
  122.  
  123. if(destinationFloor < currentPosition)
  124. directionToGo = false;
  125.  
  126. return directionToGo;
  127. }
  128.  
  129. //Checks and returns if a person is on the current floor
  130. public boolean checkIfPersonIsOnFloor(int currentPosition, List<ElevatorCall> callsWaiting)
  131. {
  132. boolean wasPersonOnFloor = false;
  133.  
  134. for(int i = 0; i < callsWaiting.size(); i++)
  135. {
  136. if(callsWaiting.get(i).getCollectionFloor() == currentPosition)
  137. {
  138. wasPersonOnFloor = true;
  139. break;
  140. }
  141. }
  142.  
  143. return wasPersonOnFloor;
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement