package Elevator; class Wachtrij { protected int wachtrijLengte; protected int wachtrijBreedte; protected int[][] wachtrij; /////////////////// // CONSTRUCTOR /////////////////// // Name: wachtrij // Description: Constructs a clean wachtrij // Input: None // Result: A wachtrij filled with -1 values /////////////////// public Wachtrij(){ //voor overerving is een default constructor verpicht. } public Wachtrij(int l, int b){ wachtrijLengte = l; wachtrijBreedte = b; wachtrij = new int[wachtrijLengte][wachtrijBreedte]; // Initialiseer de wachtrij, zet alles op -1 for (int i=0;i < wachtrijLengte;i++){ for (int j=0;j < wachtrijBreedte;j++){ wachtrij[i][j] = -1; } } } /////////////////// // Name: cleanupWachtrij // Description: cleans up the wachtrij, removes empty spaces // Input: None // Result: A wachtrij with the non empty entires at the beginning of the array /////////////////// public void cleanupWachtrij() { int[][] wachtrijCopy = new int[wachtrijLengte][wachtrijBreedte]; int k=0; // de index counter van de wachtrijCopy // Loop de wachtrij na, copieer in wachtrijCopy behalve die waar Etage = -1 for (int i=0;i < wachtrijLengte;i++){ if ((wachtrij[i][0] != -1)){ wachtrijCopy[k][0] = wachtrij[i][0]; wachtrijCopy[k][1] = wachtrij[i][1]; k++; } } // Vul de rest van de wachtrij copy met -1 (de etage en de richting) for (;k < wachtrijLengte;k++){ for (int j=0;j < wachtrijBreedte;j++){ wachtrijCopy[k][j] = -1; } } // de huidige wachtrij is opgeschoond, verwijs nu naar wachtrijCopy wachtrij = wachtrijCopy; } /////////////////// // Name: bepaalEindeWachtrij // Description: Searches for the end of the wachtrij in the wachtrij array // Input: None // Result: The index of the wachtrij where a new request could be stored /////////////////// public int bepaalEindeWachtrij(){ // ga van achteraf in de wachtrij controleren welke plaats nog vrij // is om toe te voegen int i; int j = wachtrijLengte-1; for (i=wachtrijLengte-1;(i>=0);i--){ if (wachtrij[wachtrijLengte-1][0] != -1){ System.out.println("De wachtrij is vol, deze etage en richting zitten al in de wachtrij"); System.out.println("maar dat moet nog gecontroleerd worden!"); } try { if (i==0){ if (wachtrij[i][0] == -1){ // de hele wachtrij is blijkbaar leeg } } else if(i>0){ if ((wachtrij[i][0] == -1) && (wachtrij[i-1][0] == -1)){ // doe niks en zoek verder j--; } } } catch (Exception e) { System.out.println("er ging iets fout.."); } } return j; } /////////////////// // Name: isWachtrijEmpty // Description: Looks to see if the wachtrij is empty // Input: None // Result: True if empty, false if not empty /////////////////// public boolean isWachtrijEmpty(){ cleanupWachtrij(); // make sure the wachtrij is nicely ordered (extra insurance..) if (wachtrij[0][0] == -1){ return true; // the wachtrij is empty } else{ return false; } } /////////////////// // Name: printWachtrij // Description: Prints the wachtrij // Input: None // Result: The wachtrij is printed on screen, in text version /////////////////// public void printWachtrij() { System.out.println("############ Wachtrij ###########"); // Loop de hele wachtrij af en print hem op het scherm for (int i=0;i < wachtrijLengte;i++){ // i geeft de positie in de wachtrij aan if(wachtrij[i][0] < 0) { // De wachtrij is op deze positie leeg, of is verkeerd ingevuld!! (-1 = leeg) System.out.print("Leeg"); } else{ for (int j=0;j < wachtrijBreedte;j++){ // j==0 geeft de etage aan if(j==0){ System.out.print("Iemand op etage " + wachtrij[i][j] + " "); } // j==1 geeft de richting aan else if(j==1){ if(wachtrij[i][j] == 0){ System.out.print("wil omlaag"); } else if(wachtrij[i][j] == 1){ System.out.print("wil omhoog"); } else{ System.out.print("Niet bestaande richting!"); } } } } System.out.println(""); } System.out.println(""); } }