Last active
February 18, 2016 17:05
-
-
Save xlbruce/a5ef576b61c0174c321f to your computer and use it in GitHub Desktop.
Faz a validação do TIA (Terminal Informativo do Aluno) do Mackenzie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.validator; | |
/** | |
* Faz a validação do TIA (Terminal Informativo do Aluno) do Mackenzie | |
* @author gilson | |
*/ | |
public class TIAValidator { | |
private static final byte[] nums = {8, 7, 6, 5, 4, 3, 2}; | |
private TIAValidator() {} | |
public static boolean validate(int tia) { | |
int lastDigit = tia % 10; | |
tia /= 10; | |
int remainder; | |
int sum = 0; | |
int i = 6; //Iterate from right to left | |
while (tia > 0) { | |
remainder = tia % 10; | |
sum += (remainder * nums[i--]); | |
tia /= 10; | |
} | |
remainder = sum % 11; | |
int calculatedDigit = (remainder <= 1) ? remainder : (11 - remainder); | |
return calculatedDigit == lastDigit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment