mjain

Elevator System

Aug 26th, 2019
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.39 KB | None | 0 0
  1. package testrun;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.LinkedList;
  5. import java.util.NoSuchElementException;
  6. import java.util.Queue;
  7. import java.util.Random;
  8.  
  9. public class ElevatorSystem {
  10.     ElevatorController myLift = new ElevatorController(Constants.ELEVATORS, Constants.FLOORS);
  11.  
  12.     public class Constants {
  13.         public static final int FLOORS     = 40;
  14.         public static final int ELEVATORS  = 16;
  15.         public static final int NUM_PEOPLE = 10;
  16.         public static final int MAX_PEOPLE = 5;
  17.         public static final int MIN_FLOOR  = FLOORS + 1;
  18.         public static final int MAX_FLOOR  = -1;
  19.  
  20.     }
  21.  
  22.     public static void main(String[] args) {
  23.         ElevatorSystem system = new ElevatorSystem();
  24.         system.myLift.status();
  25.         system.myLift.pickup(10, true);
  26.         system.myLift.status();
  27.     }
  28.  
  29.     public enum DIRECTION {
  30.         NONE, UP, DOWN
  31.     }
  32.     public interface IElevatorController {
  33.         public void status();
  34.  
  35.         public Elevator getElevator(int elevatorID);
  36.  
  37.         public void update(int elevatorId, int floor);
  38.  
  39.         public void pickup(int floor, boolean direction);
  40.  
  41.         public void reset(int elevatorId, int floor);
  42.  
  43.         public void step();
  44.     }
  45.     public interface ElevatorEventListener {
  46.         public void onStopped(Object sender);
  47.     }
  48.     public class Elevator {
  49.  
  50.         private DIRECTION             direction = DIRECTION.NONE;
  51.         private Boolean               move      = false;
  52.  
  53.         private boolean[]             floors;
  54.         private int                   countUp   = 0;
  55.         private int                   countDown = 0;
  56.         private int                   cf        = 0;
  57.         private int                   min       = Constants.MIN_FLOOR;
  58.         private int                   max       = Constants.MAX_FLOOR;
  59.         private int                   numFloors;
  60.  
  61.         private ElevatorEventListener elEventListener;
  62.  
  63.         public Elevator(int numFloors) {
  64.             if (numFloors < 0)
  65.                 throw new IllegalArgumentException();
  66.             this.numFloors = numFloors;
  67.             // this if for passenger destination
  68.             floors = new boolean[numFloors];
  69.         }
  70.  
  71.         public Integer getCurrentFloor() {
  72.             return cf;
  73.         }
  74.  
  75.         public int getGoalFloor() {
  76.             if (direction == DIRECTION.UP)
  77.                 return max;
  78.             if (direction == DIRECTION.DOWN)
  79.                 return min;
  80.             return -1;
  81.         }
  82.  
  83.         public void moveNext() {
  84.             if (!move) {
  85.                 move = (direction != DIRECTION.NONE);
  86.                 return;
  87.             }
  88.             if (direction == DIRECTION.UP) {
  89.                 if (floors[++cf]) {
  90.                     floors[cf] = false;
  91.                     if (--countUp == 0) {
  92.                         direction = (countDown == 0) ? (DIRECTION.NONE) : (DIRECTION.DOWN);
  93.                         max = Constants.MAX_FLOOR;
  94.                     }
  95.                     move = false;
  96.                     if (elEventListener != null)
  97.                         elEventListener.onStopped(this);
  98.                 }
  99.                 return;
  100.             }
  101.             if (direction == DIRECTION.DOWN) {
  102.                 if (floors[--cf]) {
  103.                     floors[cf] = false;
  104.                     if (++countDown == 0) {
  105.                         direction = (countUp == 0) ? (DIRECTION.NONE) : (DIRECTION.UP);
  106.                         min = Constants.MIN_FLOOR;
  107.                     }
  108.                     move = false;
  109.                     if (elEventListener != null)
  110.                         elEventListener.onStopped(this);
  111.                 }
  112.             }
  113.         }
  114.  
  115.         public void setGoalFloor(int gf) {
  116.             if ((gf < 0) || (gf >= numFloors))
  117.                 throw new IllegalArgumentException();
  118.             if (cf == gf)
  119.                 return;
  120.             if (floors[gf])
  121.                 return;
  122.             floors[gf] = true;
  123.             if (gf > cf) {
  124.                 countUp++;
  125.                 max = (gf > max) ? (gf) : (max);
  126.             }
  127.             if (gf < cf) {
  128.                 countDown--;
  129.                 min = (gf < min) ? (gf) : (min);
  130.             }
  131.             if (direction == DIRECTION.NONE)
  132.                 direction = (gf > cf) ? (DIRECTION.UP) : (DIRECTION.DOWN);
  133.         }
  134.  
  135.         public void reset() {
  136.             cf = countUp = countDown = 0;
  137.             move = false;
  138.             direction = DIRECTION.NONE;
  139.             floors = new boolean[numFloors];
  140.             max = Constants.MAX_FLOOR;
  141.             min = Constants.MIN_FLOOR;
  142.         }
  143.  
  144.         public void moveToFloor(int floor) {
  145.             if ((floor < 0) || (floor >= numFloors))
  146.                 throw new IllegalArgumentException();
  147.             reset();
  148.             cf = floor;
  149.         }
  150.  
  151.         public boolean getMove() {
  152.             return move;
  153.         }
  154.  
  155.         public DIRECTION getDirection() {
  156.             return direction;
  157.         }
  158.  
  159.         public void setElEventListener(ElevatorEventListener elEventListener) {
  160.             this.elEventListener = elEventListener;
  161.         }
  162.     }
  163.  
  164.     public class ElevatorController implements IElevatorController, ElevatorEventListener {
  165.         private int                       numElevators;
  166.         private int                       numFloors;
  167.         private Elevator[]                elevators  = null;
  168.         private ArrayList<Queue<Integer>> passengers = null;
  169.  
  170.         private void initElevators(int numElevators, int numFloors) {
  171.             if (numElevators < 0)
  172.                 throw new IllegalArgumentException();
  173.             elevators = new Elevator[numElevators];
  174.             for (int i = 0; i < numElevators; i++) {
  175.                 Elevator el = new Elevator(numFloors);
  176.                 el.setElEventListener(this);
  177.                 elevators[i] = el;
  178.             }
  179.         }
  180.  
  181.         private void initFloors(int numFloors) {
  182.             if (numFloors < 0)
  183.                 throw new IllegalArgumentException();
  184.             passengers = new ArrayList<Queue<Integer>>(numFloors);
  185.             for (int i = 0; i < numFloors; i++) {
  186.                 Queue<Integer> paxQ = new LinkedList<Integer>();
  187.                 passengers.add(i, paxQ);
  188.             }
  189.         }
  190.  
  191.         public ElevatorController(int numElevators, int numFloors) {
  192.             initFloors(numFloors);
  193.             initElevators(numElevators, numFloors);
  194.             this.numElevators = numElevators;
  195.             this.numFloors = numFloors;
  196.         }
  197.  
  198.         private int calculateRoute(int afloor, int bfloor) {
  199.             return Math.abs(afloor - bfloor);
  200.         }
  201.  
  202.         private int calculateRoute(int xfloor, int xefloor, int tfloor) {
  203.             return calculateRoute(xefloor, tfloor) + calculateRoute(xfloor, tfloor);
  204.         }
  205.  
  206.         public Queue<Integer> getPassengers(int floor) {
  207.             if ((floor < 0) || (floor >= numFloors))
  208.                 throw new IllegalArgumentException();
  209.             return passengers.get(floor);
  210.         }
  211.  
  212.         public void setPassengers(Queue<Integer> pssgrs, int floor) {
  213.             if (pssgrs == null)
  214.                 throw new NullPointerException();
  215.             if ((floor < 0) || (floor >= numFloors))
  216.                 throw new IllegalArgumentException();
  217.             this.passengers.add(floor, pssgrs);
  218.         }
  219.  
  220.         @Override
  221.         public void status() {
  222.             int i = 0;
  223.             for (Elevator el : elevators) {
  224.                 System.out.print(" elID =  " + i++ + " CurrentFloor = " + el.getCurrentFloor() + " Moving =  "
  225.                         + el.getMove() + " DIRECTION = " + el.getDirection() + "\n");
  226.             }
  227.         }
  228.  
  229.         @Override
  230.         public Elevator getElevator(int elevatorId) {
  231.             if ((elevatorId < 0) || (elevatorId >= numElevators))
  232.                 throw new NoSuchElementException();
  233.             return elevators[elevatorId];
  234.         }
  235.  
  236.         @Override
  237.         public void reset(int elevatorId, int floor) {
  238.             if ((elevatorId < 0) || (elevatorId >= numElevators))
  239.                 throw new NoSuchElementException();
  240.             Elevator elevator = elevators[elevatorId];
  241.             elevator.moveToFloor(floor);
  242.         }
  243.  
  244.         @Override
  245.         public void pickup(int floor, boolean direction) {
  246.             if ((floor < 0) || (floor >= numFloors))
  247.                 throw new IllegalArgumentException();
  248.             // shufling the order of elevators in the case where most of the
  249.             // elevators are on the same floors and they are picked up at the
  250.             // same time from different floors
  251.             // this is going to run several elevators instead of only the first
  252.             // !
  253.             int[] elevatorIDs = new int[numElevators];
  254.             for (int i = 0; i < numElevators; i++)
  255.                 elevatorIDs[i] = i;
  256.             // how to shuffle array content
  257.             Random rgen = new Random(); // Random number generator
  258.  
  259.             for (int i = 0; i < elevatorIDs.length; i++) {
  260.                 int randomPosition = rgen.nextInt(elevatorIDs.length);
  261.                 int temp = elevatorIDs[i];
  262.                 elevatorIDs[i] = elevatorIDs[randomPosition];
  263.                 elevatorIDs[randomPosition] = temp;
  264.             }
  265.  
  266.             DIRECTION userDirection = (direction) ? (DIRECTION.UP) : (DIRECTION.DOWN);
  267.             int minDistance = numFloors;
  268.             Elevator closestElevator = null;
  269.             int d;
  270.             for (int elID : elevatorIDs) {
  271.                 Elevator elevator = elevators[elID];
  272.                 if ((elevator.getMove() == false)
  273.                         || ((userDirection == DIRECTION.UP) && (elevator.getDirection() == DIRECTION.UP) && (floor >= elevator
  274.                                 .getCurrentFloor()))
  275.                         || ((userDirection == DIRECTION.DOWN) && (elevator.getDirection() == DIRECTION.DOWN) && (floor <= elevator
  276.                                 .getCurrentFloor())))
  277.                     d = calculateRoute(floor, elevator.getCurrentFloor());
  278.                 else
  279.                     d = calculateRoute(floor, elevator.getCurrentFloor(), elevator.getGoalFloor());
  280.  
  281.                 if (d < minDistance) {
  282.                     minDistance = d;
  283.                     closestElevator = elevator;
  284.                 }
  285.             }
  286.  
  287.             closestElevator.setGoalFloor(floor);
  288.         }
  289.  
  290.         @Override
  291.         public void update(int elevatorId, int floor) {
  292.             if ((floor < 0) || (floor >= numFloors))
  293.                 throw new IllegalArgumentException();
  294.             if ((elevatorId < 0) || (elevatorId >= numElevators))
  295.                 throw new NoSuchElementException();
  296.  
  297.             Elevator el = elevators[elevatorId];
  298.             el.setGoalFloor(floor);
  299.         }
  300.  
  301.         @Override
  302.         public void step() {
  303.             for (Elevator elevator : elevators)
  304.                 elevator.moveNext();
  305.         }
  306.  
  307.         @Override
  308.         public void onStopped(Object sender) {
  309.             // / onboarding waiting people
  310.             Queue<Integer> psQueue = getPassengers(((Elevator) sender).getCurrentFloor());
  311.             if (psQueue == null)
  312.                 return;
  313.             if (psQueue.isEmpty())
  314.                 return;
  315.             for (Integer goalFloor : psQueue) {
  316.                 ((Elevator) sender).setGoalFloor(goalFloor);
  317.             }
  318.         }
  319.     }
  320.  
  321. }
Add Comment
Please, Sign In to add comment