Skip to content

Commit

Permalink
Clean-up of code, javadocs, and minor bug fixes and usability enhance…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
sualeh committed Apr 16, 2015
1 parent c81f64b commit c0612e4
Show file tree
Hide file tree
Showing 27 changed files with 320 additions and 160 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

*Credit Card Number* is a Java library that can provide details of a bank issued credit card number.

All classes are immutable and thread-safe. The standard `toString()` function formats data in a readable form. Validity is enforced by JUnit tests. Java 6 or newer is required. In order to be Java 8 ready, Credit Card Number depends on Stephen Colebourne's [ThreeTen backport project](https://github.com/ThreeTen/threetenbp), a port of [JSR 310](https://jcp.org/en/jsr/detail?id=310) to Java 6.

> The goal of this project is to use publicly and freely available documentation to create a reliable Java library to provide information about credit card numbers.
All classes are immutable and thread-safe. All getter methods return non-null values. The standard `toString()` function formats data in a readable form. Validity is enforced by JUnit tests.

Java 6 or newer is required. This library deliberately supports Java 6, to make it usable in Android apps. In order to be Java 8 ready, Credit Card Number depends on Stephen Colebourne's [ThreeTen backport project](https://github.com/ThreeTen/threetenbp), a port of [JSR 310](https://jcp.org/en/jsr/detail?id=310) to Java 6.

Some resources consulted are:
* [Bank card number](http://en.wikipedia.org/wiki/Bank_card_number)
* [How do you detect Credit card type based on number?](http://stackoverflow.com/questions/72768/how-do-you-detect-credit-card-type-based-on-number)
Expand All @@ -24,7 +26,7 @@ To use *Credit Card Number* in your Maven build, include the following dependenc
<dependency>
<groupId>us.fatehi</groupId>
<artifactId>credit_card_number</artifactId>
<version>1.3</version>
<version>1.6</version>
</dependency>
```

Expand All @@ -34,13 +36,16 @@ To use *Credit Card Number* in your Maven build, include the following dependenc

To get bank card information, use code like:
```java
final PrimaryAccountNumber pan = new AccountNumber("5266-0922-0141-6174");
final BankCard card = new BankCard(pan);
PrimaryAccountNumber pan = new AccountNumber("5266-0922-0141-6174");
ExpirationDate expirationDate = new ExpirationDate(2015, 4);
Name name = new Name("Sualeh", "Fatehi");
BankCard card = new BankCard(pan, expirationDate, name);
System.out.println(card);
```
and you will get this output:
```
Bank Card Information:
Raw Account Number: 5266-0922-0141-6174
Primary Account Number: 5266092201416174
Primary Account Number (Secure): MasterCard-6174
Major Industry Identifier: 5 - Banking and financial
Expand All @@ -49,6 +54,9 @@ Bank Card Information:
Last Four Digits: 6174
Passes Luhn Check? Yes
Is Primary Account Number Valid? Yes
Expiration Date: 1504
Is Expired: No
Name: Sualeh Fatehi
```

### How to Secure the Credit Card Number
Expand Down
47 changes: 37 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<prerequisites>
<maven>3.3.1</maven>
</prerequisites>
<groupId>us.fatehi</groupId>
<artifactId>credit_card_number</artifactId>
<version>1.3</version>
<version>1.6</version>
<packaging>jar</packaging>
<name>Credit Card Number</name>
<description>Credit Card Number is a library that can provide details of a bank issued credit card number. All classes are immutable and thread-safe. The standard `toString()` function formats data in a readable form.</description>
Expand Down Expand Up @@ -50,18 +55,18 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12-beta-3</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>1.1</version>
<version>1.2</version>
</dependency>
</dependencies>
<properties>
Expand All @@ -73,7 +78,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.3</version>
<configuration>
<source>6</source>
<target>6</target>
Expand All @@ -87,7 +92,7 @@
<archive>
<index>true</index>
<manifest>
<mainClass>us.fatehi.magnetictrack.Main</mainClass>
<mainClass>us.fatehi.creditcardnumber.Main</mainClass>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
Expand All @@ -104,7 +109,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -115,6 +120,27 @@
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.12.4</version>
</dependency>
</dependencies>
<configuration>
<runOrder>random</runOrder>
<workingDirectory>${java.io.tmpdir}</workingDirectory>
<systemPropertyVariables>
<buildDirectory>${project.build.directory}</buildDirectory>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
Expand All @@ -128,10 +154,11 @@
<quiet>true</quiet>
<show>public</show>
<detectOfflineLinks>true</detectOfflineLinks>
<additionalparam>-Xdoclint:none</additionalparam>
<links>
<link>http://docs.oracle.com/javase/6/docs/api/</link>
</links>
<bottom>Copyright &#169; 2014 {organizationName}. All rights
<bottom>Copyright &#169; 2014-2015 {organizationName}. All rights
reserved.</bottom>
<doctitle>${project.name} ${project.version}</doctitle>
</configuration>
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/us/fatehi/creditcardnumber/AccountNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Credit Card Number
* https://github.com/sualeh/credit_card_number
* Copyright (c) 2014, Sualeh Fatehi.
* Copyright (c) 2014-2015, Sualeh Fatehi.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
Expand All @@ -22,8 +22,9 @@

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.left;
import static org.apache.commons.lang3.StringUtils.length;
import static org.apache.commons.lang3.StringUtils.leftPad;
import static org.apache.commons.lang3.StringUtils.right;
import static org.apache.commons.lang3.StringUtils.rightPad;
import static org.apache.commons.lang3.StringUtils.trimToEmpty;
import static us.fatehi.creditcardnumber.Utility.non_digit;

Expand Down Expand Up @@ -109,7 +110,7 @@ else if (!accountNumber.equals(other.accountNumber))
@Override
public boolean exceedsMaximumLength()
{
return trimToEmpty(getRawData()).length() > 19;
return getAccountNumberLength() > 19;
}

/**
Expand All @@ -124,7 +125,7 @@ public String getAccountNumber()
@Override
public int getAccountNumberLength()
{
return length(accountNumber);
return accountNumber.length();
}

/**
Expand All @@ -142,7 +143,7 @@ public CardBrand getCardBrand()
@Override
public String getIssuerIdentificationNumber()
{
return left(accountNumber, 6);
return rightPad(left(accountNumber, 6), 6, "0");
}

/**
Expand All @@ -151,7 +152,7 @@ public String getIssuerIdentificationNumber()
@Override
public String getLastFourDigits()
{
return right(accountNumber, 4);
return leftPad(right(accountNumber, 4), 4, "0");
}

/**
Expand Down Expand Up @@ -205,7 +206,7 @@ public boolean isPrimaryAccountNumberValid()
}

/**
* @see us.fatehi.creditcardnumber.PrimaryAccountNumber#isPassesLuhnCheck()
* @see us.fatehi.creditcardnumber.PrimaryAccountNumber#passesLuhnCheck()
*/
@Override
public boolean passesLuhnCheck()
Expand Down
45 changes: 24 additions & 21 deletions src/main/java/us/fatehi/creditcardnumber/AccountNumberInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* Credit Card Number
* https://github.com/sualeh/credit_card_number
* Copyright (c) 2014, Sualeh Fatehi.
* Copyright (c) 2014-2015, Sualeh Fatehi.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
Expand All @@ -21,6 +21,7 @@


public final class AccountNumberInfo
extends BaseRawData
implements PrimaryAccountNumber
{

Expand All @@ -35,31 +36,33 @@ public final class AccountNumberInfo
private final boolean isLengthValid;
private final boolean isPrimaryAccountNumberValid;

public AccountNumberInfo(final PrimaryAccountNumber accountNumber)
public AccountNumberInfo(final PrimaryAccountNumber pan)
{
if (accountNumber != null)
super("");
final PrimaryAccountNumber accountNumber;
if (pan == null)
{
majorIndustryIdentifier = accountNumber.getMajorIndustryIdentifier();
issuerIdentificationNumber = accountNumber
.getIssuerIdentificationNumber();
lastFourDigits = accountNumber.getLastFourDigits();
cardBrand = accountNumber.getCardBrand();
passesLuhnCheck = accountNumber.passesLuhnCheck();
accountNumberLength = accountNumber.getAccountNumberLength();
isLengthValid = accountNumber.isLengthValid();
isPrimaryAccountNumberValid = accountNumber.isPrimaryAccountNumberValid();
accountNumber = new AccountNumber();
}
else
{
majorIndustryIdentifier = MajorIndustryIdentifier.unknown;
issuerIdentificationNumber = "";
lastFourDigits = "";
cardBrand = CardBrand.Unknown;
passesLuhnCheck = false;
accountNumberLength = -1;
isLengthValid = false;
isPrimaryAccountNumberValid = false;
accountNumber = pan;
}

majorIndustryIdentifier = accountNumber.getMajorIndustryIdentifier();
issuerIdentificationNumber = accountNumber.getIssuerIdentificationNumber();
lastFourDigits = accountNumber.getLastFourDigits();
cardBrand = accountNumber.getCardBrand();
passesLuhnCheck = accountNumber.passesLuhnCheck();
accountNumberLength = accountNumber.getAccountNumberLength();
isLengthValid = accountNumber.isLengthValid();
isPrimaryAccountNumberValid = accountNumber.isPrimaryAccountNumberValid();
}

@Override
public boolean exceedsMaximumLength()
{
return false;
}

/**
Expand Down Expand Up @@ -126,7 +129,7 @@ public boolean hasPrimaryAccountNumber()
}

/**
* @see us.fatehi.creditcardnumber.PrimaryAccountNumber#isValidLength()
* @see us.fatehi.creditcardnumber.PrimaryAccountNumber#isLengthValid()
*/
@Override
public boolean isLengthValid()
Expand Down
Loading

0 comments on commit c0612e4

Please sign in to comment.