vin校验


 * VIN格式验证
 */
public class VinVerificationUtil{
    private static final Logger logger = Logger.getLogger(VinVerificationUtil.class);
    private static int vinIndexWeights[] = {8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2};
    /**
     * 
     * @Title: vinValidate
     * @param  vin
     * @return Integer
     * 0 验证通过
     * 2 VIN码格式错误
     */
    public  static int vinValidate(String vin){
        logger.info("Info->CommonServiceImpl-->vinValidate|param:vin="+vin);

        String vinCode=vin.toUpperCase();
        
        //长度验证
        if (vinCode.length() == 17){
            //Vin不会存在IOQ三个字母 
            if (vinCode.indexOf("I") < 0 && vinCode.indexOf("O") < 0 && vinCode.indexOf("Q")<0){
                int vinValidate = 0;//验证结果
                int validateTotal = 0;//所有位数乘以权重后的和
                //最后四位必须是数字
                if (StringUtils.isNumeric(vinCode.substring(13, 17))){
                    //将VIN拆分成17个字母的数组
                    char[] vinCharArray = vinCode.toCharArray();
                    for (int i = 0; i < vinCharArray.length; i++){
                        //校验码计算
                        if (i == 8){
                            if ('X' == vinCharArray[i]){
                                vinValidate = 10;
                            }else{
                                try {
                                    vinValidate = Integer.parseInt(vinCharArray[i]+"");
                                } catch (Exception e) {
                                    logger.error("Exception->CommonServiceImpl-->vinValidate|Exception="+e.getMessage()+"|param:vinCharArray["+i+"]="+vinCharArray[i]+"|vin="+vin);
                                    return CommonConstantConfig.VIN_VALIDDATE_VIN_ERROR;
                                }
                            }
                        }else{
                            try {
                                validateTotal += getCharValue(vinCharArray[i]+"") * vinIndexWeights[i];
                            } catch (Exception e) {
                                e.printStackTrace();
                                logger.error("Exception->CommonServiceImpl-->vinValidate|Exception="+e.getMessage()+"|param:vinCharArray["+i+"]="+vinCharArray[i]+"|vin="+vin);
                                return CommonConstantConfig.VIN_VALIDDATE_VIN_ERROR;
                            }
                        }
                    }
                    int vinValidateTmp = 0;
                    try {
                        vinValidateTmp = validateTotal % 11;
                    } catch (Exception e) {
                        e.printStackTrace();
                        logger.error("Exception->CommonServiceImpl-->vinValidate|Exception="+e.getMessage()+"|param:validateTotal="+validateTotal+"|vin="+vin);
                        return CommonConstantConfig.VIN_VALIDDATE_VIN_ERROR;
                    }
                    //计算规则 第九位的值等于其他16位乘以权重的和对11取余
                    if ( vinValidateTmp == vinValidate){
                        return CommonConstantConfig.VIN_VALIDDATE_SUCCESS;
                    }else{
                        return CommonConstantConfig.VIN_VALIDDATE_VIN_ERROR;
                    }    
                }else {
                    return CommonConstantConfig.VIN_VALIDDATE_VIN_ERROR;
                }
            }else{
                return CommonConstantConfig.VIN_VALIDDATE_VIN_ERROR;
            }
        }else{
            return CommonConstantConfig.VIN_VALIDDATE_VIN_ERROR;
        }
    }
    
    private static int getCharValue(String code){
        if(code.equals("0")){
            return 0;
        }
        if(code.equals("1") || code.equals("A") || code.equals("J")){
            return 1;
        }
        if(code.equals("2") || code.equals("B") || code.equals("K") || code.equals("S")){
            return 2;
        }
        if(code.equals("3") || code.equals("C") || code.equals("L") || code.equals("T")){
            return 3;
        }
        if(code.equals("4") || code.equals("D") || code.equals("M") || code.equals("U")){
            return 4;
        }
        if(code.equals("5") || code.equals("E") || code.equals("N") || code.equals("V")){
            return 5;
        }
        if(code.equals("6") || code.equals("F") || code.equals("W")){
            return 6;
        }
        if(code.equals("7") || code.equals("G") || code.equals("P") || code.equals("X")){
            return 7;
        }
        if(code.equals("8") || code.equals("H") || code.equals("Y")){
            return 8;
        }
        if(code.equals("9") || code.equals("R") || code.equals("Z")){
            return 9;
        }
        return 0;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值