import java.util.Scanner;

/**
   This program checks if the social insurance
   number is valid. 
   * @author (Moyo)
   * @version (1.0 Oct 18, 2008)
*/

public class ValidateSIN
{
    public static void main(String[] args)
    {
        
        String  sinString;  //Get the Sin number as a string
        int  sinSize, sinSlots, sumodds, sumeven, multSlot, validation; // Variables to use for the validation 

        Scanner keyboard = new Scanner(System.in); 
        
        
        //Obtein the SIN number from the user
        System.out.println("This program will help you \n" +
                           "to know if your social insurance number is valid \n" +
                           "When your done type 0 to end the program \n" +
                           "Enter your Social Insurance Number: \n" );
        sinString = keyboard.nextLine();
        
        //while to end the program if sin = 0
        while ( !sinString.equals("0") )
        {
            
            //Check if the sin length are 9 digits
            sinSize = sinString.length();
            
            //Check if the sin contain only numbers
            if (!isNumeric(sinString))
            {
                System.out.println("Error...Invalid Input, SIN MUST HAVE 9 NUMBERS, AND NO CHARACTERS ");
            }
            else
            {
                //Chech if SIN contain 9 digits
                if ( sinSize != 9 )
                {
                    System.out.println("Error...Invalid Input, SIN MUST HAVE 9 DIGITS");    
                }    
                else
                {
                    //Sum of the odd digits from the string [1,3,5,7,9] 
                    sumodds = 0;
                    for(int i=0;i<sinSize;i +=2)
                    {
                        //Extract the string slots from sin to calculate the sum
                        sinSlots = Integer.parseInt(sinString.substring(i,i+1));
                        sumodds +=  sinSlots;
                    }
                                    
                    //Sum of the even digits from the string [2,4,6,8]
                    sumeven = 0;
                    multSlot = 0;
                    for(int i=1;i<sinSize;i +=2)
                    {
                        //Extract the string slots from sin to  multiply
                        sinSlots = Integer.parseInt(sinString.substring(i,i+1));
                        //sinSlots multiplied by 2
                        multSlot = sinSlots*2;
                        //if statement to add the numbers if they are greater then 10 if not sum the results from multSlot
                        if (multSlot >= 10)
                            sumeven += (multSlot - 9);
                        else 
                            sumeven += multSlot;
                    } 
                    //Validation of SIN.... 
                    validation = (sumeven + sumodds) % 10;
                    if (validation == 0)
                        System.out.println("Your Social Insurance Number is Valid... SIN: " + sinString);
                    else
                        System.out.println("Your Social Insurance Number is Invalid... SIN: " + sinString);
                     
                }
            }
            
           System.out.println("Enter your Social Insurance Number: ");
            sinString = keyboard.nextLine();
        } 
        
        //Program ENDS when a negative value is entered      
        System.out.println("Program ENDS");       
       
       }
    
       // Check if SIN contain only numbers
       // i just this part because i couldn't find a way to validate integers, just strings.
       private static boolean isNumeric(String sin)
       {
           try 
           {
               // if SIN contain numbers then the program will work 
               //convert the SIN string to integer to check for numbers
               Integer.parseInt(sin);
               return true;
           } catch (NumberFormatException nfe)
           {
               //return false if the SIN contain characters and numbers, the program gives an error message
               return false;
           }
    }
}