标签:
1 import java.util.regex.Matcher; 2 import java.util.regex.Pattern; 3 4 public class CheckBankNumber { 5 6 7 //Description: 银行卡号Luhm校验 8 9 //Luhm校验规则:16位银行卡号(19位通用): 10 11 // 1.将未带校验位的 15(或18)位卡号从右依次编号 1 到 15(18),位于奇数位号上的数字乘以 2。 12 // 2.将奇位乘积的个十位全部相加,再加上所有偶数位上的数字。 13 // 3.将加法和加上校验位能被 10 整除。 14 15 16 //开头6位 17 //private final static String strBin = "10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99"; 18 19 private final static String SUCCESS="true"; 20 21 private final static String BAD_LENGTH="银行卡号长度必须在16到19之间"; 22 23 private final static String NOT_NUMBER="银行卡必须全部为数字"; 24 25 private final static String ILLEGAL_NUMBER="银行卡不符合规则"; 26 27 public static String luhmCheck(String bankno){ 28 if (bankno.length() < 16 || bankno.length() > 19) { 29 return BAD_LENGTH; 30 } 31 32 Pattern pattern = Pattern.compile("[0-9]*"); 33 Matcher match = pattern.matcher(bankno); 34 if (match.matches() == false) { 35 return NOT_NUMBER; 36 } 37 38 // if (strBin.indexOf(bankno.substring(0, 2)) == -1) { 39 // return "银行卡号开头6位不符合规范"; 40 // } 41 42 int lastNum=Integer.parseInt(bankno.substring(bankno.length()-1,bankno.length()));//取出最后一位(与luhm进行比较) 43 44 String first15Num=bankno.substring(0,bankno.length()-1);//前15或18位 45 //System.out.println(first15Num); 46 char[] newArr = new char[first15Num.length()]; //倒叙装入newArr 47 char[] tempArr = first15Num.toCharArray(); 48 for(int i = 0; i < tempArr.length ; i++){ 49 newArr[tempArr.length-1-i] = tempArr[i]; 50 } 51 //System.out.println(newArr); 52 53 54 int[] arrSingleNum = new int[newArr.length]; //奇数位*2的积 <9 55 int[] arrSingleNum2 = new int[newArr.length];//奇数位*2的积 >9 56 int[] arrDoubleNum=new int[newArr.length]; //偶数位数组 57 58 for(int j = 0;j < newArr.length; j++){ 59 if((j+1)%2==1){//奇数位 60 if((int)(newArr[j]-48)*2<9) 61 arrSingleNum[j]=(int)(newArr[j]-48)*2; 62 else 63 arrSingleNum2[j]=(int)(newArr[j]-48)*2; 64 } 65 else //偶数位 66 arrDoubleNum[j]=(int)(newArr[j]-48); 67 } 68 69 int[] arrSingleNumChild = new int[newArr.length]; //奇数位*2 >9 的分割之后的数组个位数 70 int[] arrSingleNum2Child = new int[newArr.length];//奇数位*2 >9 的分割之后的数组十位数 71 72 for(int h=0;h<arrSingleNum2.length;h++){ 73 arrSingleNumChild[h] = (arrSingleNum2[h])%10; 74 arrSingleNum2Child[h] = (arrSingleNum2[h])/10; 75 } 76 77 int sumSingleNum=0; //奇数位*2 < 9 的数组之和 78 int sumDoubleNum=0; //偶数位数组之和 79 int sumSingleNumChild=0; //奇数位*2 >9 的分割之后的数组个位数之和 80 int sumSingleNum2Child=0; //奇数位*2 >9 的分割之后的数组十位数之和 81 int sumTotal=0; 82 for(int m=0;m<arrSingleNum.length;m++){ 83 sumSingleNum=sumSingleNum+arrSingleNum[m]; 84 } 85 86 for(int n=0;n<arrDoubleNum.length;n++){ 87 sumDoubleNum=sumDoubleNum+arrDoubleNum[n]; 88 } 89 90 for(int p=0;p<arrSingleNumChild.length;p++){ 91 sumSingleNumChild=sumSingleNumChild+arrSingleNumChild[p]; 92 sumSingleNum2Child=sumSingleNum2Child+arrSingleNum2Child[p]; 93 } 94 95 sumTotal=sumSingleNum+sumDoubleNum+sumSingleNumChild+sumSingleNum2Child; 96 97 //计算Luhm值 98 int k= sumTotal%10==0?10:sumTotal%10; 99 int luhm= 10-k; 100 101 if(lastNum == luhm){ 102 return SUCCESS;//验证通过 103 } 104 else{ 105 return ILLEGAL_NUMBER; 106 } 107 } 108 109 public static void main(String[] args) { 110 String bankno = "6214857110432079"; 111 System.out.println(bankno+":"+luhmCheck(bankno)); 112 String bankno1 = "6212164000046323291"; 113 System.out.println(bankno1+":"+luhmCheck(bankno1)); 114 String bankno2 = "6222023104004123546"; 115 System.out.println(bankno2+":"+luhmCheck(bankno2)); 116 } 117 }
标签:
原文地址:http://www.cnblogs.com/fubinhnust/p/4181217.html