Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 18th, 2012  |  syntax: None  |  size: 1.86 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. public class ControlUnit {
  5.  
  6.     private int numOfFloors;
  7.     private int numOfElevators;
  8.     private ArrayList<Elevator> listOfElevators = new ArrayList();
  9.     private ElevatorControlPanel ECP = new ElevatorControlPanel();
  10.  
  11.     public ControlUnit () {
  12.     }
  13.  
  14.     public ControlUnit (int floors, int elevators)
  15.     {
  16.         numOfFloors = floors;
  17.         numOfElevators = elevators;
  18.     }
  19.    
  20.     public ArrayList<Elevator> getListOfElevators () {
  21.         return listOfElevators;
  22.     }
  23.  
  24.     public int getNumOfElevators () {
  25.         return numOfElevators;
  26.     }
  27.  
  28.     public void setNumOfElevators (int val) {
  29.         this.numOfElevators = val;
  30.     }
  31.  
  32.     public int getNumOfFloors () {
  33.         return numOfFloors;
  34.     }
  35.    
  36.     public void setNumOfFloors (int val) {
  37.         this.numOfFloors = val;
  38.     }
  39.    
  40.     public void addElevator () {
  41.         listOfElevators.add(new Elevator());
  42.     }
  43.    
  44.     public Elevator getClosestElevator (int floor) {
  45.         //if elevator is not idle but going towards the floor, use that elevator
  46.         //if elevator is idle, get closest elevator (if more than one to choose from, get random
  47.         return null;
  48.     }
  49.    
  50.     public Elevator randomElevator (int floor) {
  51.         int counter = 0;
  52.         int current = 0;
  53.        
  54.         for (int i=0; i < getNumOfElevators(); i++)
  55.         {
  56.             if (listOfElevators.get(i).getCurrentFloor() == floor)
  57.             {
  58.                 counter++;
  59.                 current = i;
  60.             }
  61.  
  62.         }
  63.         if (counter > 1)
  64.         {
  65.             Random generator = new Random();
  66.             int rand = generator.nextInt(counter);
  67.             return listOfElevators.get(rand);
  68.         }
  69.         else
  70.         {
  71.             return listOfElevators.get(current);
  72.         }
  73.     }
  74.  
  75. }