CreditCardPack

com.keyoti.creditCard.logic
Class CardType

java.lang.Object
  |
  +--com.keyoti.creditCard.logic.CardType

public class CardType
extends Object
implements Serializable

Represents a credit card type (eg; Visa, AMEX, etc).

Overview
CardType objects can check that a card number agrees with a card type as defined by ISO/IEC 7812-1:1993 & ANSI X4.13. This is possible because each industry card type has two properties - card number length and card number pattern. For example, Visa card numbers can be either 13 or 16 digits long and the first 6 digits (the IIN) must be 4xxxxx where "x" is a wildcard.

Preset card types
This Class comes with several preset card types (see the static fields). These can be accessed using a static field identifier in the constructor,
eg; new CardType(CardType.AMERICAN_EXPRESS);

Non-preset card types
To create card types that haven't been included in this Class, or to amend ones that have with changes to the card issuers IIN (Industry Identifier Number) information, simply instantiate a new CardType object with the relevant data;
eg; new CardType("New Card Name", idPatterns, numberLengths);

Where idPatterns is an array of Strings containing the valid patterns for the card type. Eg; MasterCard cards have card numbers that follow any of these patterns (from left to right) "51xxxx", "52xxxx", "53xxxx", "54xxxx", "55xxxx" where "x" signifies a wildcard. Therefore idPatterns would be created as
String [] idPatterns = {"51xxxx", "52xxxx", "53xxxx", "54xxxx", "55xxxx"};

numberLengths is an array of ints containing the valid card number lengths for this card type. eg;
int[] numberLengths = {16};

The card name argument is a String that is presented to the user to identify this card type.

Carte Blanche and Diner's Club share pattern and length specifiers.

Note: currently Delta and Switch cards have undisclosed identifiers and lengths, therefore checkNumberMatchesIdentifier will always return true for these card types.

Author:
Keyoti
See Also:
Serialized Form

Field Summary
static int AMERICAN_EXPRESS
          Preset card type, suitable for type constructor argument.
static int CARTE_BLANCHE
          Preset card type, suitable for type constructor argument.
static int DELTA
          Preset card type, suitable for type constructor argument.
static int DINERS_CLUB
          Preset card type, suitable for type constructor argument.
static int DISCOVER
          Preset card type, suitable for type constructor argument.
static int EN_ROUTE
          Preset card type, suitable for type constructor argument.
protected  String[] identifierPatterns
          Card number patterns for this card type, eg "51xxxx" for MasterCard.
static int JCB
          Preset card type, suitable for type constructor argument.
static int MASTERCARD
          Preset card type, suitable for type constructor argument.
protected  String name
          A string name for this type, eg "MasterCard".
static int SWITCH
          Preset card type, suitable for type constructor argument.
protected  int[] validNumberLengths
          Valid card number lengths for this type of card, this may be empty.
static int VISA
          Preset card type, suitable for type constructor argument.
 
Constructor Summary
CardType(int type)
          Construct a preset card type, where type is one of the static fields (CardType.AMERICAN_EXPRESS, CardType.VISA etc).
CardType(String name, String[] identifierPatterns, int[] validNumberLengths)
          Construct a new card type.
 
Method Summary
 boolean checkNumberLength(String cardNumber)
          Checks that the card number is one of the valid lengths specified by this card type.
 boolean checkNumberMatchesIdentifier(String cardNumber)
          Checks that the card number conforms to one of the identifier patterns for this type.
 boolean equals(CardType type)
          Evaluates if this card type is 'equal' to type, based on equality checks on name, identifierPatterns and validNumberLengths.
static CardType findTypeFromCardNumber(String cardNumber, CardType[] types)
          Returns the first card type (in types) whose identifier pattern matches the cardNumber, or null if none match.
static CardType[] getAllCardTypes()
          Returns all the preset card types in this class in an array.
 String[] getIdentifierPatterns()
          Gets the String[] array of identifier patterns for this type of card.
 String getName()
          Gets the name for this type of card, never returns null.
 int[] getValidNumberLengths()
          Gets the int[] array of valid card number lengths for this type of card, never returns null.
 boolean isCardNumberValid(String cardNumber)
          Checks if the card number conforms to one of this card type's patterns.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

name

protected String name
A string name for this type, eg "MasterCard". This is presented to the user and is never null.

identifierPatterns

protected String[] identifierPatterns
Card number patterns for this card type, eg "51xxxx" for MasterCard.

Identifier patterns are an array of Strings containing the valid patterns for the card type.
Eg; MasterCard cards have card numbers that follow any of these patterns "51xxxx", "52xxxx", "53xxxx", "54xxxx", "55xxxx" where "x" signifies a wildcard. This is never null although it may be an empty array.


validNumberLengths

protected int[] validNumberLengths
Valid card number lengths for this type of card, this may be empty.

AMERICAN_EXPRESS

public static transient int AMERICAN_EXPRESS
Preset card type, suitable for type constructor argument.

CARTE_BLANCHE

public static transient int CARTE_BLANCHE
Preset card type, suitable for type constructor argument.

DELTA

public static transient int DELTA
Preset card type, suitable for type constructor argument.

DINERS_CLUB

public static transient int DINERS_CLUB
Preset card type, suitable for type constructor argument.

DISCOVER

public static transient int DISCOVER
Preset card type, suitable for type constructor argument.

EN_ROUTE

public static transient int EN_ROUTE
Preset card type, suitable for type constructor argument.

JCB

public static transient int JCB
Preset card type, suitable for type constructor argument.

MASTERCARD

public static transient int MASTERCARD
Preset card type, suitable for type constructor argument.

SWITCH

public static transient int SWITCH
Preset card type, suitable for type constructor argument.

VISA

public static transient int VISA
Preset card type, suitable for type constructor argument.
Constructor Detail

CardType

public CardType(String name,
                String[] identifierPatterns,
                int[] validNumberLengths)
Construct a new card type.
Parameters:
name - the String shown to the user as a representation of this card type.
identifierPatterns - String[] array of patterns that card numbers under this card type should have, eg; AMEX 34xxxx or 37xxxx
validNumberLengths - int[] array of valid card number lengths
Throws:
NullPointerException - if name, identifierPatterns or validNumberLengths is null.
Preconditions:
name != null && identifierPatterns != null && validNumberLengths != null
Postconditions:
getName() != null && getIdentifierPatterns() != null && getValidNumberLengths() != null

CardType

public CardType(int type)
         throws IllegalArgumentException
Construct a preset card type, where type is one of the static fields (CardType.AMERICAN_EXPRESS, CardType.VISA etc).
Parameters:
type - a static field type, such as CardType.AMERICAN_EXPRESS
Throws:
IllegalArgumentException - if type is not a valid CardType field.
Preconditions:
type == AMERICAN_EXPRESS || CARTE_BLANCHE || DELTA || DINERS_CLUB || DISCOVER || EN_ROUTE || JCB || MASTERCARD || SWITCH || VISA
Postconditions:
getName() != null && getIdentifierPatterns() != null && getValidNumberLengths() != null
Method Detail

getName

public String getName()
Gets the name for this type of card, never returns null.
Returns:
a String name that means something to the user.
Postconditions:
return != null

getIdentifierPatterns

public String[] getIdentifierPatterns()
Gets the String[] array of identifier patterns for this type of card.
Returns:
Array of String objects representing the identifierPatterns set, this may be null.
Postconditions:
return != null
See Also:
identifierPatterns

getValidNumberLengths

public int[] getValidNumberLengths()
Gets the int[] array of valid card number lengths for this type of card, never returns null.
Postconditions:
return != null

getAllCardTypes

public static CardType[] getAllCardTypes()
Returns all the preset card types in this class in an array.
Returns:
all preset card types in a CardType[] array.

isCardNumberValid

public boolean isCardNumberValid(String cardNumber)
Checks if the card number conforms to one of this card type's patterns. Does this by matching the card number with the identifier patterns for this card type and checks there are the right amount of digits.
Parameters:
cardNumber - the card number to check with this card type.
Returns:
true if the card number matches this card type, otherwise (including if cardNumber is null) this returns false.

checkNumberMatchesIdentifier

public boolean checkNumberMatchesIdentifier(String cardNumber)
Checks that the card number conforms to one of the identifier patterns for this type.
Parameters:
cardNumber - the card number to check.
Returns:
true if the card number matches an identifier, otherwise (including if cardNumber is null) this returns false.
Postconditions:
return == (true AND cardNumber matches patterns specified by self) OR (false)
See Also:
identifierPatterns

checkNumberLength

public boolean checkNumberLength(String cardNumber)
Checks that the card number is one of the valid lengths specified by this card type.
Parameters:
cardNumber - the card number to check, non-digit chars are ignored.
Returns:
true if the card number length is correct, otherwise (including if cardNumber is null) this returns false.
Postconditions:
return == (true AND number of digits (not including whitespace) conforms to number length specified by self ) OR (false)

findTypeFromCardNumber

public static CardType findTypeFromCardNumber(String cardNumber,
                                              CardType[] types)
Returns the first card type (in types) whose identifier pattern matches the cardNumber, or null if none match.
Eg;
CardType usersCard = CardType.findTypeFromCardNumber( usersCardNumber, CardType.getAllCardTypes() );
Parameters:
cardNumber - String representation of the cardNumber, non-digit chars are ignored.
types - CardType[] array of card types to look for match in.
Returns:
CardType that matches the card number, or null if no match was found
Throws:
NullPointerException - if cardNumber or types were null..

equals

public boolean equals(CardType type)
Evaluates if this card type is 'equal' to type, based on equality checks on name, identifierPatterns and validNumberLengths. The elements in identifierPatterns and validNumberLengths are tested for equality with elements in the corresponding arrays in type, the order of these arrays is relevant in determining equality.
Parameters:
type - CardType to test for equality with.
Throws:
NullPointerException - if type is null
Preconditions:
type != null
Postconditions:
result == (true if name == type.getName() AND identifierPatterns == type.identifierPatters AND validNumberLengths == validNumberLengths) OR (false)

CreditCardPack

Copyright © 2002 Keyoti All Rights Reserved.