Nov 16, 2010

Singapore License No Generator

I written a license number checksum generator, just input the chunk of the licence number as the first argument, then generate:

public class LicenceGenerator {
/**
* @Licence number
*/

public static void main(String[] args) {
   String in = args[0] +"";
   System.out.println("License Number: " + args[0] + generateChecksum(in));
}

private static String generateChecksum(String input){
   /*Logic:
   * The checksum letter is calculated by converting the letters into a number,
   * i.e. where A=1 and Z=26, giving 7 individual numbers from each registration plate.
   * SBA 1234 would therefore give 19, 2, 1, 1, 2, 3 and 4.
   * Each individual number is then multiplied by 7 fixed numbers.
   * These are added up, then divided by 19.
   * The remainder corresponds to one of the 19 letters used (A B C D E G H J K L M P R S T U X Y Z).
   * In the case of SBA 1234, the final letter should be a T.
   */


   char[] charvalarr = input.toUpperCase().toCharArray();
   int totalSum = 0;

   for(int i=1; i< charvalarr.length; i++){
      int charval = charvalarr[i]-64;

      if (charval < 0){
         charval = Integer.parseInt(charvalarr[i]+"");
      }

      charval = charval * constarr[i];
      totalSum += charval;
   }

   int finalchecksumVal = totalSum%19;
   return checksum[finalchecksumVal] +"";

}

   static final char[] checksum = {'A','Z','Y','X','U','T','S','R','P','M','L','K','J','H','G','E','D','C','B'};
   static final int[] constarr= {0,9,4,5,4,3,2};

}

No comments: