package bankersalgorithm;

import java.io.IOException;
import java.util.Scanner;

public class BankersAlgorithm {
    //np = no. of processes \ nr = no. of resources
    private int need[][],allocate[][],max[][],avail[][],np,nr;
    
    private void input(){
     Scanner sc = new Scanner(System.in);
     System.out.print("Enter no. of processes and resources: ");
     np=sc.nextInt();  //no. of processes
     nr=sc.nextInt();  //no. of resources
     need=new int[np][nr];  //initializing arrays
     max=new int[np][nr];
     allocate=new int[np][nr];
     avail=new int[1][nr];
     
     System.out.println("Enter allocation matrix: ");
     for(int i=0;i<np;i++)
          for(int j=0;j<nr;j++)
         allocate[i][j]=sc.nextInt();  //allocation matrix
      
     System.out.println("Enter max. matrix: ");
     for(int i=0;i<np;i++)
          for(int j=0;j<nr;j++)
         max[i][j]=sc.nextInt();  //max matrix
      
        System.out.println("Enter available matrix: ");
        for(int j=0;j<nr;j++)
            avail[0][j]=sc.nextInt();  //available matrix
        
        sc.close();
    }
    
    //calculate the need matrix
    private int[][] calc_need(){
        for(int i=0;i<np;i++){
            for(int j=0;j<nr;j++){  //calculating need matrix
                need[i][j]=max[i][j]-allocate[i][j];
            }
        }
        return need;
    }
 
    //Check if the requested resource is available or not
    private boolean check(int i){
       //checking if all resources for it^(h) process can be allocated
      for(int j=0;j<nr;j++) {
        if(avail[0][j]<need[i][j]) {
          return false;
        }
      }
      return true;
    }

    
    // Check if by fulfilling the resource request the system remains in safe state
    public void isSafe(){
       input(); //collecting data from the user
       calc_need(); //mathmagics here :DD
       boolean done[]=new boolean[np];
       int j=0;

       while(j<np){  //loop until all process allocated
       boolean allocated=false;
       for(int i=0;i<np;i++)
        if(!done[i] && check(i)){  //trying to allocate
            for(int k=0;k<nr;k++)
            avail[0][k]=avail[0][k]-need[i][k]+max[i][k];
         System.out.println("Allocated process : "+i);
         allocated=done[i]=true;
               j++;
             }
          if(!allocated) break;  //if no allocation
       }
       if(j==np)  //if all processes are allocated
        System.out.println("\nSafely allocated!");
       else
        System.err.println("\nAll proceess can't be allocated safely!");
    }

    public static void main(String[] args) throws IOException {
        new BankersAlgorithm().isSafe();
    }
}

@@ -258,16 +258,20 @@
 )%7B%0A     
+    
 Scanner 
@@ -299,16 +299,20 @@
 em.in);%0A
+    
      Sys
@@ -373,16 +373,20 @@
 );%0A     
+   
+ 
 np=sc.ne
@@ -414,16 +414,20 @@
 ocesses%0A
+    
      nr=
@@ -461,24 +461,28 @@
 ources%0A     
+    
 need=new int
@@ -519,16 +519,20 @@
 ys%0A     
+ 
+   
 max=new 
@@ -540,24 +540,28 @@
 nt%5Bnp%5D%5Bnr%5D;%0A
+    
      allocat
@@ -580,24 +580,28 @@
 %5D%5Bnr%5D;%0A     
+   
+ 
 avail=new in
@@ -610,22 +610,21 @@
 1%5D%5Bnr%5D;%0A
- 
+%0A
     
-%0A
      Sys
@@ -666,32 +666,36 @@
 atrix: %22);%0A     
+    
 for(int i=0;i%3Cnp
@@ -702,32 +702,36 @@
 ;i++)%0A          
+  
+  
 for(int j=0;j%3Cnr
@@ -728,32 +728,36 @@
 t j=0;j%3Cnr;j++)%0A
+    
          allocat
@@ -795,31 +795,29 @@
 tion matrix%0A
-  
+%0A
     
-%0A
      System.
@@ -853,24 +853,28 @@
 x: %22);%0A     
+    
 for(int i=0;
@@ -875,32 +875,36 @@
 t i=0;i%3Cnp;i++)%0A
+    
           for(in
@@ -928,16 +928,20 @@
         
+  
+  
 max%5Bi%5D%5Bj
@@ -970,23 +970,21 @@
  matrix%0A
-  
+%0A
     
-%0A
         
@@ -1023,32 +1023,36 @@
 ble matrix: %22);%0A
+    
         for(int 
@@ -1069,32 +1069,36 @@
 ++)%0A            
+    
 avail%5B0%5D%5Bj%5D=sc.n
@@ -1131,17 +1131,13 @@
 rix%0A
-    
+%0A
     
-%0A
     

Update Java version