汽车VIN码合法性校验工具类-Java语言
伸手党麻烦点个赞
package vsoc.utils;
import java.util.HashMap;
public class VinUtil {
private static final Boolean LEGAL = true;
private static final Boolean ILLEGAL = false;
private static final int RIGHT_LENGTH = 17;
private static final HashMap<Character, Integer> ALPHABET_MAP = new HashMap<Character, Integer>() {{
put('A', 1);put('B', 2);put('C', 3);put('D', 4);put('E', 5);put('F', 6);put('G', 7);put('H', 8);
put('J', 1);put('K', 2);put('L', 3);put('M', 4);put('N', 5);put('P', 7);put('Q', 8);put('R', 9);
put('S', 2);put('T', 3);put('U', 4);put('V', 5);put('W', 6);put('X', 7);put('Y', 8);put('Z', 9);
}};
private static final int[] CHAR_WEIGHTING = new int[]{8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2};
public static Boolean vinVerify(String vinCode) {
vinCode = vinCode.toUpperCase();
char[] vinArray = vinCode.toCharArray();
if (vinArray.length != RIGHT_LENGTH) {
System.out.println("VIN长度不符合");
return ILLEGAL;
}
int totalValue = 0;
int checkBitValue = Integer.parseInt(String.valueOf(vinArray[8]));
System.out.println("校验位的值:" + checkBitValue);
for (int i = 0; i < RIGHT_LENGTH; i++) {
char currentChar;
int currentCharValue;
if (vinArray[i] >= '0' && vinArray[i] <= '9') {
currentChar = vinArray[i];
currentCharValue = Integer.parseInt(String.valueOf(vinArray[i]));
} else if (vinArray[i] >= 'A' && vinArray[i] <= 'Z') {
currentChar = vinArray[i];
currentCharValue = ALPHABET_MAP.get(currentChar);
} else {
System.out.println("VIN中出现非法字符\n");
return ILLEGAL;
}
totalValue += currentCharValue * CHAR_WEIGHTING[i];
}
System.out.println("加权计算结束");
int result = totalValue % 11;
System.out.printf("校验位的值:%d,加权取余结果:%d\n", checkBitValue, result);
if (result != checkBitValue) {
System.out.println("未通过校验\n");
return ILLEGAL;
}
System.out.println("校验通过\n");
return LEGAL;
}
public static void main(String[] args) {
String Vin = "JUYMH11929730MEXX";
String realVin = "LSGKE54H3GW217917";
Boolean aBoolean = vinVerify(realVin);
System.out.println(aBoolean);
}
}