/**
*
*/
package idt.algorithm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* @author 作者 Your-Name:
* @version 建立時間:2022年4月8日 下午2:13:31
* 類說明
*/
/**
* @author Dell
*2022年4月8日
*/
public class Util {
// 求Pn
private int getPn(int n,List<Object> arr1) {
if (n ==1) {
return 10;
}else {
return mod10(getSn(n - 1, arr1)) * 2;
}
}
//求特定的取餘10的結果
private int mod10(int num) {
if (num%10 == 0) {
return 10;
}else {
return num % 10;
}
}
//求Sn
private int getSn(int n,List<Object> arr1) {
// int a =0;
// try {
// a = ;
// } catch (Exception e) {
// System.out.println(e.getMessage());
// System.err.println(14-n+1);
System.out.println((String) arr1.get(14-n+1));
// }
return getPn(n, arr1) % 11 + Integer.parseInt(arr1.get(14-n+1)+"");
}
//求校驗碼
public Object getCheckCode(String code) {
String c = code+"x,";
List<Object> arr1 = new ArrayList<Object>();
String[] split = c.split("");
for (int i = split.length-1; i >=0; i--) {
arr1.add(split[i]);
}
for (int i = 0; i < 10; i++) {
arr1.set(1, i);
if (getSn(14, arr1)%10 == 1) {
arr1.remove(0);
Collections.reverse(arr1);
StringBuffer stringBuffer = new StringBuffer();
for (Object string : arr1) {
stringBuffer.append(string);
}
return stringBuffer.toString();
}
}
return null;
}
public static void main(String[] args) {
String code = "1021409230066";
Util util = new Util();
System.out.println(util.getCheckCode(code));
}
}