com.acmetech.cc
Class CreditCardInfo

java.lang.Object
  extended bycom.acmetech.cc.CreditCardInfo

public final class CreditCardInfo
extends java.lang.Object

Validate credit card checksum and determine type of credit card.
The following credit card numbers can be used for testing:

Author:
Wayne K. Walrath, Acme Technologies

Version History
0.1 - 2005-03-23 - First release


Copyright 2005 © Acme Technologies. All rights reserved. Permission granted to incorporate these classes into non-commercial (shareware applications ARE allowed) derivative works. You may not post these classes on the internet or otherwise distribute them EXCEPT as part of a derivative work which includes the Acme Technologies copyright and the address of our website (www.acmetech.com).

For an easy flat-fee redistribution license for commercial use, please contact Acme Technologies through one of the means listed at www.acmetech.com.

Method Summary
static java.lang.String cardType(java.lang.String cardAccountNumber)
          Get the card type for a given credit card account number.
static void main(java.lang.String[] args)
          Test all the credit card utilities in this class.
static java.lang.String stripNonDigits(java.lang.String s)
          Strip out non-digit characters returning the raw credit card account number.
static boolean validateChecksum(java.lang.String cardAccountNumber)
          Determine if card account number passes checksum verification.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

validateChecksum

public static boolean validateChecksum(java.lang.String cardAccountNumber)
                                throws java.lang.NullPointerException
Determine if card account number passes checksum verification. (NOTE: this does not mean it is a valid account number, only that the checksum matches the check digit.)

Calculated using the Luhn formula for computing modulus 10 "double-add-double" check digit: Double the value of alternate (odd) digits starting by the least significant (first right hand) digit. Then add the individual digits of doubled (odd) numbers and even digits of the original number. If the value ends in 0 then the check digit is 0. Otherwise subtract the value from the next higher number ending in 0 (tens complement of the unit digit). The result is the check digit. Example: If the account number without check digit is 1234 5678 9012 344 then (8) + 4 + (6) + 2 + (2) + 0 + (1 + 8) + 8 + (1 + 4) + 6 + (1 + 0) + 4 + (6) + 2 + (2) = 65, therefore the check digit is 70 - 65 = 5 and so the complete account number is 1234 5678 9012 3445.

Parameters:
cardAccountNumber - the credit card account number (non-digit characters are ignored)
Returns:
true if the checksum matches the check digit.
Throws:
java.lang.NullPointerException

cardType

public static java.lang.String cardType(java.lang.String cardAccountNumber)
                                 throws java.lang.NumberFormatException
Get the card type for a given credit card account number.

Parameters:
cardAccountNumber - credit card number
Returns:
type of credit card or "Unknown&qout; if type can't be determined.
Throws:
java.lang.NumberFormatException

stripNonDigits

public static java.lang.String stripNonDigits(java.lang.String s)
Strip out non-digit characters returning the raw credit card account number.

Parameters:
s - credit card account number
Returns:
cleaned account number (with nothing but digits)

main

public static void main(java.lang.String[] args)
Test all the credit card utilities in this class.

Parameters:
args - one or more credit card account numbers.
Returns:
writes information about the card to stdout.