Advertisement
giancarloparma

CINUtil

Sep 11th, 2018
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1.  
  2. public class CINUtil {
  3.  
  4.     public static String computeCin(String abi, String cab, String conto) {
  5.         String result = null;
  6.        
  7.         String NUMERI = "0123456789";
  8.         String LETTERE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  9.         int DIVISORE  = 26;
  10.         int somma = 0;
  11.                
  12.         int[] aListaPari = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
  13.         int[] aListaDispari = new int[]{1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23};
  14.        
  15.         String strAbiCabConto = abi + cab + conto;
  16.         String strChar = "";
  17.         int i = 0;
  18.        
  19.         for (int k = 1; k <= 22; k++) {
  20.             strChar = strAbiCabConto.substring(k - 1, k);
  21.             i = NUMERI.indexOf(strChar);
  22.             if (i < 0) {
  23.                 i = LETTERE.indexOf(strChar);
  24.             }
  25.            
  26.             i++;
  27.            
  28.             somma = somma + (k % 2 == 0 ? aListaPari[i - 1] : aListaDispari[i - 1]);
  29.         }
  30.        
  31.         int y = 1 + (somma % DIVISORE);
  32.         result = LETTERE.substring(y - 1, y);
  33.        
  34.         return result;
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement