コンテナ番号検査
1449 ワード
/**
*
*
* @author yaoba
* @version 2010-6-21 12:16:16
*/
public class Container {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(check("AMFU8505137"));
System.out.println(check("TEXU2982987"));
}
/**
*
*
* @param no
* @return
*/
public static boolean check(String no) {
if (no == null && no.length() != 11)
return false;
no = no.toUpperCase();
int[] wi = new int[10];
for (int i = 0; i <= 4; i++) {
char ch = no.charAt(i);
if (ch == 'A') {
wi[i] = 10;
} else if (ch >= 'V' && ch <= 'Z') {
wi[i] = ch - 52;
} else if (ch >= 'L' && ch <= 'U') {
wi[i] = ch - 53;
} else {
wi[i] = ch - 54;
}
}
for (int i = 4; i < 10; i++) {
wi[i] = Integer.parseInt(no.substring(i, i + 1));
}
int sum = 0;
for (int i = 0; i < wi.length; i++) {
sum += wi[i] * Math.pow(2, i);
}
if (no.substring(0, 4) == "HLCU")
sum -= 2; // hapaq lloyd 2
return sum % 11 == Integer.parseInt(no.substring(10, 11));
}
}