Advertisement
Tal_Rofe

Algorithm_Project

Jan 6th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.63 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. class Algorithm{
  5.     private int spotsInRow; // as an input, can be changed in range of 4,6,7,8
  6.     private Spot[][][] park; // the park itself..
  7.    
  8.     Algorithm(){ // CONSTRUCTOR
  9.         int spotsInRow = 4; // initial value
  10.         int i, j, t;
  11.         park = new Spot[3][3][spotsInRow];
  12.         for (i = 0; i < 3; i++){
  13.             for (j = 0; j < 3; j++){
  14.                 for(t = 0; t < 4; t++){
  15.                     park[i][j][t] = Spot();
  16.                 }
  17.             }
  18.         }
  19.     }
  20.    
  21.     Algorithm(int numSpotsInRow){ // 2nd CONSTRUCTOR
  22.         int i, j, t;
  23.         park = new Spot[3][3][numSpotsInRow];
  24.         for (i = 0; i < 3; i++){
  25.             for (j = 0; j < 3; j++){
  26.                 for(t = 0; t < numSpotsInRow; t++){
  27.                     park[i][j][t] = Spot();
  28.                 }
  29.             }
  30.         }
  31.     }
  32.    
  33.     Algorithm(int numSpotsInRow, Spot[][][] parkArray){ // 3rd CONSTRUCTOR
  34.         spotsInRow = numSpotsInRow;
  35.         park = parkArray;
  36.     }
  37.    
  38.     private boolean checkAvailable(int exitTime){ // checking whether insert command is executable
  39.         int i, j, k;
  40.         for (i = 0; i < 3; i++){
  41.             for (j = 0; j < 3; j++){
  42.                 for (k = 0; k < spotsInRow; k++){
  43.                     // check if there is any spot in the park which is empty or ordered with the following condition:
  44.                     // if there is an ordered spot with entry time bigger than the current exit time, it is fine
  45.                     if(park[i][j][k].getStatus() == 'e'
  46.                             || (park[i][j][k].getStatus() == 'o' && exitTime <= park[i][j][k].getEntryTime()))
  47.                         return true;
  48.                 }
  49.             }
  50.         }
  51.         return false;  
  52.     }
  53.    
  54.     private int[] locateCarSpot(String carID){
  55.         int i, j, k;
  56.         int[] a = {-1, -1, -1}; // if failed to locate this car with the given 'carID'
  57.         for (i = 0; i < 3; i++){
  58.             for (j = 0; j < 3; j++){
  59.                 for (k = 0; k < this.spotsInRow; k++){
  60.                     if(park[i][j][k].getcarID().equals(carID)){ // if we locate this car then update a and finish
  61.                         a[0] = i;
  62.                         a[1] = j;
  63.                         a[2] = k;
  64.                         return a;
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.         return a;
  70.     }
  71.    
  72.     // get the cars out of the park from a specific depth
  73.     // al is a list which keeps the ejected cars
  74.     private void ejectInDepth(ArrayList<Spot> al, int i, int j, int k){
  75.         int t;
  76.         for (t = 0; t < i; t++){
  77.             if (park[t][j][k].getStatus() == 'f'){ // if the spot is full - then there is a car to eject
  78.                 al.add(park[t][j][k]);
  79.                 park[t][j][k].setStatus('e'); // update the spot as empty..
  80.             }
  81.         }  
  82.     }
  83.    
  84.     // get the cars out of the park from a specific floor
  85.     // al is a list which keeps the ejected cars
  86.     private void ejectInFloor(ArrayList al, int i, int j, int k){
  87.         int t;
  88.         for (t = 0; t < j; t++){
  89.             if (park[i][t][k].getStatus() == 'f'){ // if the spot is full - then there is a car to eject
  90.                 al.add(park[i][t][k]);
  91.                 park[i][t][k].setStatus('e'); // update the spot as empty
  92.             }
  93.         }
  94.     }
  95.    
  96.     // find the optimal position for an entry command of a car
  97.     private int[] locateOptimalPosition(int exitTime){
  98.         int i, j, k, moves, minMoves = 6;
  99.         int[] optimal = new int[3];
  100.         for (k = 0; k < spotsInRow; k++){ // first loop on width
  101.             for (j = 0; j < 3; j++){ // then loop on height
  102.                 moves = 0; // will be calculated with each iteration
  103.                 for (i = 0; i < 3; i++){
  104.                     if (park[i][j][k].getStatus() == 'e' || // if we found an empty spot
  105.                             // or we found an ordered spot which we can enter into the car because the
  106.                             // current exit time is less than the spot's entry time
  107.                             (park[i][j][k].getStatus() == 'o' && exitTime <= park[i][j][k].getEntryTime()))
  108.                         break;
  109.                     moves++;
  110.                 }
  111.                 if (moves < minMoves){ // when we find lower number of moves we update
  112.                     minMoves = moves;
  113.                     optimal[0] = i; optimal[1] = j; optimal[2] = k;
  114.                 }
  115.             }
  116.         }
  117.         return optimal;
  118.     }
  119.    
  120.     // FORMAT: <Calculation of converted convenient indexes> with <status in the spot>
  121.     public String generateStatusString(){
  122.         String statusString = "";
  123.         for(int i = 0 ; i < 3; i++){
  124.             for(int j = 0; j < 3; j++){
  125.                 for(int k = 0; k < this.spotsInRow; k++){
  126.                     int value = i * 3 * this.spotsInRow + j * this.spotsInRow + k;
  127.                     statusString += Integer.toString(value);
  128.                     statusString += this.park[i][j][k].getStatus();
  129.                 }
  130.             }
  131.         }
  132.         return statusString;
  133.     }
  134.    
  135.     // FORMAT: <depth index,> <height index,> <width index,> <status in spot> <entry time in spot>
  136.     // <exit time in spot> <\n to end a line> ((EACH LINE DESCRIBES ONE SPOT))
  137.     public String generateDBString(){
  138.         int i, j, k;
  139.         String DBString = "";
  140.         for (i = 0; i < 3; i++){
  141.             for(j = 0; j < 3; j++){
  142.                 for(k = 0; k < this.spotsInRow; k++){
  143.                     DBString += Integer.toString(i) + "," + Integer.toString(j) + "," + Integer.toString(k);
  144.                     DBString += ",";
  145.                     DBString += this.park[i][j][k].getStatus() + ",";
  146.                     DBString += Integer.toString(this.park[i][j][k].getEntryTime()) + ",";
  147.                     DBString += Integer.toString(this.park[i][j][k].getExitTime()) + ",";
  148.                     if (i != 2 && j != 2 && k != this.spotsInRow - 1)
  149.                         DBString += this.park[i][j][k].getcarID() + "\n";
  150.                 }
  151.             }
  152.         }
  153.         return DBString;
  154.     }
  155.    
  156.     public void insertCar(String carID, int exitTime){
  157.         boolean fullPark = !this.checkAvailable(exitTime);
  158.         if(fullPark){
  159.             try{
  160.                 // send to server: ROBOT can't place the car because there is no place for it
  161.             }catch(Exception e){ // TO MODIFY
  162.                 // Handle the exception of failed send command
  163.             }
  164.         }
  165.        
  166.         else{
  167.             int[] a = locateOptimalPosition(exitTime);
  168.             ArrayList<Spot> list = new ArrayList<Spot>();
  169.             ejectInDepth(list, a[0], a[1], a[2]);
  170.             ejectInFloor(list, a[0], a[1], a[2]);
  171.             Collections.sort(list);
  172.            
  173.             for (int counter = 0; counter < list.size(); counter++){
  174.                 int t = 0;
  175.                 while(t != a[1]){
  176.                     if(park[a[0]][t][a[2]].getStatus() == 'e'){
  177.                         park[a[0]][t][a[2]] = list.get(counter);
  178.                         park[a[0]][t][a[2]].setStatus('f');
  179.                         break;
  180.                     }
  181.                    
  182.                     if(park[a[0]][t][a[2]].getStatus() == 'o' && list.get(counter).getExitTime() <= park[a[0]][t][a[2]].getEntryTime()){
  183.                         park[a[0]][t][a[2]] = list.get(counter);
  184.                         park[a[0]][t][a[2]].setStatus('f');
  185.                         break;
  186.                     }
  187.                     t++;
  188.                 }
  189.                
  190.                 t = 0;
  191.                 while(t != a[0]){
  192.                     if(park[t][a[1]][a[2]].getStatus() == 'e'){
  193.                         park[t][a[1]][a[2]] = list.get(counter);
  194.                         park[t][a[1]][a[2]].setStatus('f');
  195.                         break;
  196.                     }
  197.                    
  198.                     if(park[t][a[1]][a[2]].getStatus() == 'o' && list.get(counter).getExitTime() <= park[t][a[1]][a[2]].getEntryTime()){
  199.                         park[t][a[1]][a[2]] = list.get(counter);
  200.                         park[t][a[1]][a[2]].setStatus('f');
  201.                         break;
  202.                     }
  203.                     t++;
  204.                 }
  205.             }
  206.            
  207.         }
  208.     }
  209.    
  210.     public void ejectCar(String carID){
  211.         int[] a = locateCarSpot(carID);
  212.         ArrayList<Spot> list = new ArrayList<Spot>();
  213.         ejectInDepth(list, a[0], a[1], a[2]);
  214.         ejectInFloor(list, a[0], a[1], a[2]);
  215.         Collections.sort(list); // change it to comp sort... by leaving time..
  216.        
  217.         for (int counter = 0; counter < list.size(); counter++){
  218.             int t = 0;
  219.             while(t != a[1]){
  220.                 if(park[a[0]][t][a[2]].getStatus() == 'e'){
  221.                     park[a[0]][t][a[2]] = list.get(counter);
  222.                     park[a[0]][t][a[2]].setStatus('f');
  223.                     break;
  224.                 }
  225.                
  226.                 if(park[a[0]][t][a[2]].getStatus() == 'o' && list.get(counter).getExitTime() <= park[a[0]][t][a[2]].getEntryTime()){
  227.                     park[a[0]][t][a[2]] = list.get(counter);
  228.                     park[a[0]][t][a[2]].setStatus('f');
  229.                     break;
  230.                 }
  231.                 t++;
  232.             }
  233.            
  234.             t = 0;
  235.             while(t != a[0]){
  236.                 if(park[t][a[1]][a[2]].getStatus() == 'e'){
  237.                     park[t][a[1]][a[2]] = list.get(counter);
  238.                     park[t][a[1]][a[2]].setStatus('f');
  239.                     break;
  240.                 }
  241.                
  242.                 if(park[t][a[1]][a[2]].getStatus() == 'o' && list.get(counter).getExitTime() <= park[t][a[1]][a[2]].getEntryTime()){
  243.                     park[t][a[1]][a[2]] = list.get(counter);
  244.                     park[t][a[1]][a[2]].setStatus('f');
  245.                     break;
  246.                 }
  247.                 t++;
  248.             }
  249.         }
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement