Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php:2) in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 1570

Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php:2) in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pub/skins/simple/simple.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php:2) in /hermes/bosweb/web234/b2344/sl.harringtonweb/public_html/pmwiki2.php on line 866
Harrington Web : Brian - Credit Card Enumeration

Home > Projects > Credit Card Enumeration


This class makes it easy to determine the type of a credit card given the card number. Information about card numbering can easily be found on the web. This class uses the Luhn algorithm, prefixes, and number of digits to determine the type of the number.

Example of Use

The code shown below is a sample program that uses the CreditCard enum to determine the types of randomly generated card numbers.

public class CreditCheck
{
    public static void printTitles()
    {
        int width=80;
        System.out.printf("%-19s | %-16s | %-26s | %-8s%n", 
            "Number", "Type", "Name", "Accepted");
        for(int i=0; i<width; i++)
            System.out.print("=");
        System.out.println();
    }

    public static boolean isAccepted(CreditCard cc)
    {
        boolean accepted=false;
        switch(cc)
        {
            case AMERICAN_EXPRESS:
            case DINERS_CLUB_US:
            case DISCOVER_CARD:
            case MASTER_CARD:
            case VISA:
                accepted=true;
                break;
            default: 
                accepted=false;
                break;
        }
        return accepted;
    }

    public static void main(String[] args)
    {
        // Process some random credit card numbers
        System.out.printf("Random Card Numbers:%n%n");
        printTitles();
        for(int i=0; i<5; i++)
        {
            String number=CreditCard.getRandomCreditCardNumber();
            CreditCard cc=CreditCard.getType(number);
            System.out.printf("%-19s | %-16s | %-26s | %-8s%n", 
                number, cc, cc.getName(), isAccepted(cc));
        }

        // Process a random credit card number of each type
        System.out.printf("%nRandom Card Numbers of Each Type:%n%n");
        printTitles();
        for(CreditCard card : CreditCard.values())
        {
            String number=CreditCard.getRandomCreditCardNumber(card);
            CreditCard cc=CreditCard.getType(number);
            System.out.printf("%-19s | %-16s | %-26s | %-8s%n", 
                number, cc, cc.getName(), isAccepted(cc));
        }
    }
}

The output from this program should look something like:

Random Card Numbers:

Number              | Type             | Name                       | Accepted
================================================================================
4975674785174157    | VISA             | Visa                       | true    
4175008678009477    | VISA_ELECTRON    | Visa Electron              | false   
4175008373103484    | VISA_ELECTRON    | Visa Electron              | false   
4984268392868       | VISA             | Visa                       | true    
3054356455766043    | JCB              | Japan Credit Bureau        | false   

Random Card Numbers of Each Type:

Number              | Type             | Name                       | Accepted
================================================================================
349044863495246     | AMERICAN_EXPRESS | American Express           | true    
36291491461557      | DINERS_CLUB_INTL | Diner's Club International | false   
5574748251400635    | DINERS_CLUB_US   | Diner's Club US & Canada   | true    
6011203045169871    | DISCOVER_CARD    | Discover Card              | true    
3039861376051631    | JCB              | Japan Credit Bureau        | false   
5020841059741449    | MAESTRO          | Maestro Debit Card         | false   
5115809889869090    | MASTER_CARD      | Master Card                | true    
633903633851814958  | SOLO             | Solo Debit Card            | false   
493604921870598293  | SWITCH           | Switch Debit Card          | false   
4184465535467182    | VISA             | Visa                       | true    
4175000582230844    | VISA_ELECTRON    | Visa Electron              | false   
0                   | INVALID          | Invalid Card Number        | false   

Download

 
Page last modified on August 08, 2006, at 01:54 PM.