// NewCheckNRIC.java
// Aaron Tan
// To determine the check code of a Singapore NRIC number.
// This is a 'modularized' version of CheckNRIC.java

import java.util.*;

public class NewCheckNRIC {
 
   public static void main(String[] args) {

      Scanner stdIn = new Scanner(System.in);

      System.out.print("Enter 7-digit NRIC number: ");
      int number = stdIn.nextInt();

      char checkCode = generateCheckCode(number);

      System.out.println("Check code for " + number 
                          + " is " + checkCode);

   } // end main

   /*********************************************************
      This method takes a 7-digit value num representing the 
      NRIC number and returns the check code.
   **********************************************************/
   public static char generateCheckCode(int num) {

      // Extract the digits
      int digit7 = num%10; num /= 10;
      int digit6 = num%10; num /= 10;
      int digit5 = num%10; num /= 10;
      int digit4 = num%10; num /= 10;
      int digit3 = num%10; num /= 10;
      int digit2 = num%10; num /= 10;
      int digit1 = num%10;

      int step1 = digit1*2 + digit2*7 + digit3*6 + digit4*5 +
                  digit5*4 + digit6*3 + digit7*2;
      int step2 = step1 % 11;
      int step3 = 11 - step2;

      char code = ' ';
      switch (step3) {
         case 1: code = 'A'; break;
         case 2: code = 'B'; break;
         case 3: code = 'C'; break;
         case 4: code = 'D'; break;
         case 5: code = 'E'; break;
         case 6: code = 'F'; break;
         case 7: code = 'G'; break;
         case 8: code = 'H'; break;
         case 9: code = 'I'; break;
         case 10: code = 'Z'; break;
         case 11: code = 'J';
      }

      return code;

   }// end generateCheckCode

}// end class NewCheckNRIC


