Credit Card Validator

     

This little program validates credit card numbers according to their structure. That is, it does not tell you anything regarding the account associated with the credit card number passed to it. All it does is tell you that the argument passed could be a real credit card number. It does this using the Luhn formula. This app is easily incorporated into any e-commerce application you might have in place, and works well as a front-line validation before incurring the overhead required to check the account and process the transaction.

Demonstrates

Converting Strings, ternary operator, validating input, designing the methods of a class, JavaDoc, using the Character class, for loops , arrays.

 

 package net.javagarage.misc; /** <p>  * Shows how to validate a credit card number.  * Credit card numbers should check out against  * the LUHN formula (MOD 10 algorithm).  * <p>  * Will check out fine against the numbers commonly  * used for testing, such as 4111 1111 1111 1111  * <p>  * On a credit card, the first number identifies  * the type of card (MC, Visa, Amex, etc), and the  * middle digits belong to the bank, and the last  * digits to the customer.  * <p>  * Gives you code that you might one day really need,  * and demos how to use for loops, Character,  * separate work, and use the ternary operator.  *  * @author eben hewitt  * @see Character,  **/ public class CreditCardValidator { /**  *Use private constructor to disallow  *creation of objects. it isn't necessary  *since we're just doing an operation, not  *storing data  */ private CreditCardValidator() { } /**  * Determines if the number checks out against the  * algorithmobviously it does not perform any  * operation with respect to the user's account.  * <p>  * @param String the number to be validated  * @return boolean true if the card is valid,  * false otherwise.  **/ public static boolean validate(String input) {       //first off, make sure we're even       //in the ballpark of a real number       //(not NULL and between 13 and 16 digits)       if (input == null              (input.length() < 13)              (input.length() > 16)){       reject(); } //remove spaces, dashes, etc String number = cleanup(input); //this will be our total int total = 0; //multiply all digits but the first one by 2 int position = 1; //Starting with the second-to-last-digit... for(int i = number.length() - 1; i >= 0; i) { // make each digit in base 10 int digit = Character.digit(number.charAt(i), 10); //multiply every other digit by 2 //starting with the second one digit *= (position == 1) ? position++ : position; //if the digit is greater than 10, //try mod 10 + 1 total += (digit >= 10) ? (digit % 10) + 1 : digit; } //a valid number MOD 10 will be 0 //the result of this expression is the boolean return value return (total % 10) == 0; } /**  * Clean the passed string so that it contains  * only numbers, not spaces or dashes or anything  * extraneous.  * @param String the number as entered by user  * @return String the cleaned-up number  **/ public static String cleanup(String input) { char[] result = new char[input.length()]; //index of the char array int idx = 0; for (int i = 0; i < input.length(); i++) { char thisChar = input.charAt(i); //use static Character wrapper class //method to see if it's a digit if (Character.isDigit(thisChar)) { //it is, so keep it result[idx++] = thisChar; } } /*  * String has a constructor that builds a  * String from a subset of a char[], using  * the char[], the starting position (here, 0),  * and the ending position (here, idx).  */ return new String(result,0,idx); } /**  * Used when the user passes bad information to  * stop the show and help them out.  */ private static void reject(){ System.out.println("usage->java CreditCardValidator number"); System.exit(-1); } //start the app public static void main(String[] args) { if (args.length == 1) { System.out.println("Is valid number? " + validate(args[0])); } else { //user didn't enter a card number reject(); } } }//eof 

Result

The result of running the validator with different arguments is shown here.

Passing 12345 yields this result:

 

 usage->java CreditCardValidator number 

That's because the program rejects the argument if it is obviously not anything like a credit card number (between 13 and 16 characters ).

Passing 456789098765432 yields this result:

 

 Is valid number? false 

Passing 4111111111111111 yields this result:

 

 Is valid number? true 

In the next few sections, we'll look at putting together a text editor, an RSS newsreader, and a drawing pad. It will be very thrilling and educational.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net