Groovy web console

subscribe to the feed Subscribe
to this
site

Generar Clabe

Published 4 months ago by Anonymous
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def generarClabe(String strBankCode, String strPlazaCode, String strAccountNumber) {
    
    // Input: Account without check digit (17 length)
    def strAccountClabe = strBankCode + "" + strPlazaCode + "" + strAccountNumber;
    def arrAccountClabe = strAccountClabe.replaceAll(/\D++/, '').collect { it as int }
    println(arrAccountClabe);

    // 1. Multiply each digit of the account number by the respective weighting factor
    int[] arrWeighing = [3,7,1,3,7,1,3,7,1,3,7,1,3,7,1,3,7];
    println(arrWeighing);
    def arrResultStepOne = [];
    for (int i = 0; i < arrWeighing.length; i++) {
        int num1 = arrAccountClabe[i];
        int num2 = arrWeighing[i];
        arrResultStepOne.add(num1 * num2);
    }
    println(arrResultStepOne);
    // 2. Take module 10 of each result obtaining in step 1
    def arrResultStepTwo = [];
    for (int i = 0; i < arrResultStepOne.size; i++) {
        int num1 = arrResultStepOne[i];
        arrResultStepTwo.add(num1 % 10);
    }
    println(arrResultStepTwo);
    // 3. Add the results of each of the module operations performed in step 2
    def arrAuxResultStepThree = [];
    for (int i = 0; i < arrResultStepTwo.size; i++) {
        int num1 = arrResultStepTwo[i];
        arrAuxResultStepThree.add(num1 % 10);
    }
    def intResultStepThree = arrAuxResultStepThree.value.sum();
    println(intResultStepThree)
    // 4. Take modulo 10 of the sum calculated in step 3
    def intResultStepFour = intResultStepThree % 10;
    println(intResultStepFour);
    // 5. Take the value A obtained in step 4 and subtract it from 10
    def intResultStepFive = 10 - intResultStepFour;
    println(intResultStepFour);
    // 6. The Verifying Digit is the result of obtaining the module 10 of the number B calculated in step 5
    def intCheckDigit = intResultStepFive % 10;
    
  return strAccountClabe + "" + intCheckDigit;
}

generarClabe("002", "180", "03224094670");