Created
April 12, 2013 07:53
-
-
Save turbanoff/5370315 to your computer and use it in GitHub Desktop.
Проверка ИНН на Java
This file contains 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 main; | |
import java.util.regex.Pattern; | |
public class Main { | |
public static void main(String[] args) { | |
assert isValidINN("500100732259"); | |
assert isValidINN("7830002293"); | |
assert isValidINN(" 7830002293 "); | |
assert !isValidINN("7830002294"); | |
assert !isValidINN("500100332251"); | |
assert !isValidINN("7830002294s"); | |
assert !isValidINN("78300022942"); | |
assert !isValidINN(" 783000229"); | |
} | |
private static final Pattern innPatter = Pattern.compile("\\d{10}|\\d{12}"); | |
public static boolean isValidINN(String inn) { | |
inn = inn.trim(); | |
if (!innPatter.matcher(inn).matches()) { | |
return false; | |
} | |
int length = inn.length(); | |
if (length == 12) { | |
return INNStep(inn, 2, 1) && INNStep(inn, 1, 0); | |
} else { | |
return INNStep(inn, 1, 2); | |
} | |
} | |
private static final int[] checkArr = new int[] {3,7,2,4,10,3,5,9,4,6,8}; | |
private static boolean INNStep(String inn, int offset, int arrOffset) { | |
int sum = 0; | |
int length = inn.length(); | |
for (int i = 0; i < length - offset; i++) { | |
sum += (inn.charAt(i) - '0') * checkArr[i + arrOffset]; | |
} | |
return (sum % 11) % 10 == inn.charAt(length - offset) - '0'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment