public static boolean isValid ( long creditCardNumber ) { int i = findMatchingRange ( creditCardNumber ) ; if ( i < 0 ) { return false ; } // else { // we have a match if ( ranges [ i ] . hasCheckDigit ) { // there is a checkdigit to be validated /* * Manual method MOD 10 checkdigit 706-511-227 7 0 6 5 1 1 2 2 7 2 * 2 * 2 * 2 * --------------------------------- 7 + 0 + 6 +1+0+ 1 + 2 + 2 + 4 = 23 23 MOD 10 = 3 10 - 3 = 7 -- * the check digit Note digits of multiplication results must be added before sum. Computer Method * MOD 10 checkdigit 706-511-227 7 0 6 5 1 1 2 2 7 Z Z Z Z --------------------------------- 7 + 0 + * 6 + 1 + 1 + 2 + 2 + 4 + 7 = 30 30 MOD 10 had better = 0 */ long number = creditCardNumber ; int checksum = 0 ; // work right to left for ( int place = 0 ; place < 16 ; place ++ ) { int digit = ( int ) ( number % 10 ) ; number /= 10 ; if ( ( place & 1 ) == 0 ) { // even position (0-based from right), just add digit checksum += digit ; } else { // odd position (0-based from right), must double // and add checksum += z ( digit ) ; } if ( number == 0 ) { break ; } } // end for // good checksum should be 0 mod 10 return ( checksum % 10 ) == 0 ; } return true ; // no checksum needed // } // end if have match }
Determine if the credit card number is valid i . e . has good prefix and checkdigit . Does _not_ ask the credit card company if this card has been issued or is in good standing .