32 lines
846 B
Java
32 lines
846 B
Java
|
package company;
|
||
|
|
||
|
public class PeselValidator {
|
||
|
|
||
|
public static boolean validate(String pesel) {
|
||
|
if (pesel == null || pesel.length() != 11)
|
||
|
return false;
|
||
|
|
||
|
return validateControlSum(pesel);
|
||
|
}
|
||
|
|
||
|
private static boolean validateControlSum(String pesel) {
|
||
|
int controlSum = 0;
|
||
|
var weightings = new int[] { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };
|
||
|
var peselArray = pesel.toCharArray();
|
||
|
|
||
|
for (int i = 0; i < 10; i++)
|
||
|
{
|
||
|
controlSum += Character.getNumericValue(peselArray[i]) * weightings[i];
|
||
|
}
|
||
|
|
||
|
controlSum = controlSum % 10;
|
||
|
controlSum = 10 - controlSum;
|
||
|
controlSum = controlSum % 10;
|
||
|
|
||
|
if (controlSum == Character.getNumericValue(peselArray[10]))
|
||
|
return true;
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|