VinUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import java.util.*;

/**
* @ClassName VinUtil
* @Description TODO
* @Author will
* @Date 2020/11/19 10:25
* @Company 天津易和信息科技有限公司
*/
public class VinUtil {
/**车架号地区代码数组*/
public static final String areaArray[] = new String[]{"1", "2", "3", "6", "9", "J", "K", "L", "R", "S", "T", "V", "W", "Y", "Z", "G"};

/**车架号中可能出现的字符数组*/
public static final String charArray[] = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "V", "W", "X", "Y"};

/**车架号校验位计算数组*/
public static final Object[][] KVMACTHUP = new Object[][]{{'A', 1}, {'B', 2}, {'C', 3}, {'D', 4}, {'E', 5}, {'F', 6},{'G', 7}, {'H', 8}, {'I', 0}, {'J', 1}, {'K', 2}, {'L', 3},{'M', 4}, {'N', 5}, {'O', 0}, {'P', 7}, {'Q', 8}, {'R', 9}, {'S', 2}, {'T', 3}, {'U', 4}, {'V', 5}, {'W', 6}, {'X', 7},{'Y', 8}, {'Z', 9}};
/**车架号数据加权数组*/
public static final int[] WEIGHTVALUE = new int[]{8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2};

/**
* 计算车架号的校验位
* @return
*/
public String getIsuredCode(String vin) {
char[] Vin = vin.toCharArray();
int sum = 0,tempValue = 0;
char temp;
for (int i = 0; i < 17; i++) {
if (Vin[i] >= 'a' && Vin[i] <= 'z') {
temp = (char) (Vin[i] - 32);
} else if ((Vin[i] >= 'A') && (Vin[i] <= 'Z')) {
temp = Vin[i];
} else if ((Vin[i] >= '0') && (Vin[i] <= '9')) {
tempValue = Integer.parseInt(String.valueOf(Vin[i]));
temp = Vin[i];
} else {
return "ERROR";
}
if ((temp >= 'A') && (temp <= 'Z')) {
for (int j = 0; j < 26; j++) {
if (temp == (char)KVMACTHUP[j][0]) {
tempValue = (int) KVMACTHUP[j][1];
}
}
}
sum += tempValue * WEIGHTVALUE[i];
}
int reslt = sum % 11;
if (reslt != 10) {
return String.valueOf(reslt);
} else {
return "X";
}
}

/**
* 判断vin是否正确
* @param vin
* @return
*/
public boolean isVin(String vin) {
String isuredCode = getIsuredCode(vin);
if (vin.substring(8, 9).equals(isuredCode)) {
return true;
} else {
return false;
}
}

/**
* 拼接车架号
*
* @param beforeStr
* @param afterStr
* @return
*/
public String spellVin(String beforeStr, String afterStr) {
StringBuffer vinBuffer = new StringBuffer();
String preVin = vinBuffer.append(beforeStr).append("X").append(afterStr).toString();
String isuredCode = getIsuredCode(preVin);
String vin = new StringBuffer(beforeStr).append(isuredCode).append(afterStr).toString();
if (isVin(vin)) {
return vin;
} else {
return spellVin(beforeStr, afterStr);
}
}

/**
* 生成随机前缀
*
* @return
*/
public String prepareBeforeStr() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("VNN");
for (int i = 0; i < 5; i++) {
stringBuffer.append(getRandomChar(areaArray));
}
return stringBuffer.toString();
}

/**
* 生成随机后缀
*
* @return
*/
public String prepareAfterStr() {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < 3; i++) {
stringBuffer.append(getRandomChar(charArray));
}
stringBuffer.append(prepareNo());
return stringBuffer.toString();
}

/**
* 生成随机的生产序号
* @return
*/
public String prepareNo(){
Random random = new Random();
StringBuffer numStrBuff = new StringBuffer();
for(int i=0;i<5;i++){
numStrBuff.append(Integer.toHexString(random.nextInt(16)).toUpperCase());
}
return numStrBuff.toString();
}
/**
* 返回随机字符
* @return
*/
public String getRandomChar(Object array[]) {
return charArray[(int) (Math.random() * 100 % array.length)];
}
/**
* 获取随机的车架号
* @return
*/
public String getRandomVin() {
String beforeStr = prepareBeforeStr();
String afterStr = prepareAfterStr();
String vin = spellVin(beforeStr, afterStr);
return vin;
}

/**
* 获取对应数量的随机车架号
* @param num
* @return
*/
public List<String> getVinList(int num){
List<String> vinList = new ArrayList<>();
for(int i =0;i<num;i++){
vinList.add(getRandomVin());
}
return vinList;
}
}

VinUtil
https://github.com/yangxiangnanwill/yangxiangnanwill.github.io/2024/01/03/好好码代码吖/JAVA/工具类/VinUtil/
作者
will
发布于
2024年1月3日
许可协议