Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Background: most credit card companies use the Luhn algorithm to generate the la

ID: 666849 • Letter: B

Question

Background: most credit card companies use the Luhn algorithm to generate the last digit of the card number. In conjunction with the leading numbers (specific to the card company) and the number of digits in the number, this algorithm guards against inadvertent mistyping of card numbers. (It is not a strong encryption algorithm that would guard against hacking, however.)

Write a Java application that validates a credit card number. Assume 4 types of credit card:

American Express (AX)

Master Card   (MC)

Visa (V)

Discover (D)

Every credit card has a number, Cardholder’s name and type.

The following table shows the valid lengths and leading numbers for the 4 types of credit cards:

Type

Length

Prefix

AX

15

34,35

MC

16

51-55

V

13 or 16

4

D

16

6011, 622126-622925, 644-649, or 65

The algorithm for validating a credit card number is based on the following:

Luhn Algorithm (used for almost all credit cards and other id numbers)

Starting with a given credit card number 5466 1602 3048   9914

Reverse the digits:

4 1 9 9 8 4 0 3 2 0 6 1 6 6 4 5

Starting with second digit, double every other number:

            4 2 9 18 8 8 0 6 2 0 6 2 6 12 4 10

Add up all the digits:

4+2+9+1+8+8+8+0+6+2+0+6+2+6+1+2+4+1+0=70

            ** Note that when adding 18 – add the 1 and then add the 8

If the number is a multiple of ten the card checksum is valid.

Data Element Class – CreditCard

The class CreditCard will contain:

Constructor. At least two constructors, one that is the no-arg constructor.

Instance variables

Constants. Use “named constants” for any literal values that will not change during program execution.

getter and setter methods

toString method

Data Manager Class - CardValidator

Contains an arraylist of CreditCard objects

A method validateCard that takes in the Cardholder’s name, card type and card number and returns a boolean, based on the Luhn algorithm. If the card number is valid, a new CreditCard object is created and added to the arraylist and true is returned. If the card number is invalid, nothing is added to the arraylist and false is returned.

A method getValidCards that returns a String of all the objects in the arraylist. Use the toString method of the CreditCard class in a for each loop.

At least two private utility methods that are called from the validateCard method, ideas:

A method to determine if the card type is valid for the card number

A method to check the if the card length is valid based on the card type

A method to reverse the digits of the card number

A method to double every other digit in the card number

You may add any additional methods you find necessary for your design

Driver Class/Classes

Print a title for the application

Provide buttons/event handling for the following events:Validate a Credit Card:

Use radio buttons for the user to select the type of credit card

Extract the cardholder’s name and credit card number

Display whether the card is valid or invalid

Report of Valid Cards:

Use a JOptionPane dialog box to display the valid credit cards

Clear Fields

Clear the Cardholders name and card number fields, as well as the valid or invalid label/field

Exit

Exit the application

Type

Length

Prefix

AX

15

34,35

MC

16

51-55

V

13 or 16

4

D

16

6011, 622126-622925, 644-649, or 65

Explanation / Answer

**********************another approach***credit card validatator*************

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CreditCardValidator {

public CreditCardValidator() {
}

private String message = null;

public String getMessage() {
return message;
}

private void setMessage(String message) {
this.message = message;
}

public static final int MASTERCARD = 0, VISA = 1;
public static final int AMEX = 2, DISCOVER = 3, DINERS = 4;

private static final String[] messages = {
“Not a valid number for MasterCard.”,
“Not a valid number for Visa.”,
“Not a valid number for American Express.”,
“Not a valid number for Discover.”,
“Not a valid number for Diner’s Club” };

public boolean isCreditCardValid(String number, int type) {

if (number.equals(“”)) {
setMessage(“Field cannnot be blank.”);
return false;
}

Matcher m = Pattern.compile(“[^\d\s.-]“).matcher(number);

if (m.find()) {
setMessage(“Credit card number can only contain numbers, spaces, ”-”, and ”.”");
return false;
}

setMessage(messages[type]);
Matcher matcher = Pattern.compile(“[\s.-]“).matcher(number);
number = matcher.replaceAll(“”);

return validate(number, type);

}

// Check that cards start with proper digits for
// selected card type and are also the right length.

private boolean validate(String number, int type) {

if (null == number || number.length() < 12)
return false;

switch (type) {

case MASTERCARD:
if (number.length() != 16
|| Integer.parseInt(number.substring(0, 2)) < 51
|| Integer.parseInt(number.substring(0, 2)) > 55) {
return false;
}
break;

case VISA:
if ((number.length() != 13 && number.length() != 16)
|| Integer.parseInt(number.substring(0, 1)) != 4) {
return false;
}
break;

case AMEX:
if (number.length() != 15
|| (Integer.parseInt(number.substring(0, 2)) != 34 && Integer
.parseInt(number.substring(0, 2)) != 37)) {
return false;
}
break;

case DISCOVER:
if (number.length() != 16
|| Integer.parseInt(number.substring(0, 4)) != 6011) {
return false;
}
break;

case DINERS:
if (number.length() != 14
|| ((Integer.parseInt(number.substring(0, 2)) != 36 && Integer
.parseInt(number.substring(0, 2)) != 38)
&& Integer.parseInt(number.substring(0, 3)) < 300 || Integer
.parseInt(number.substring(0, 3)) > 305)) {
return false;
}
break;
}

if (type == DISCOVER) { // no luhn validate for DISCOVER
return true;
}

return luhnValidate(number);
}

// The Luhn algorithm is basically a CRC type
// system for checking the validity of an entry.
// All major credit cards use numbers that will
// pass the Luhn check. Also, all of them are based
// on MOD 10.

private boolean luhnValidate(String numberString) {

char[] charArray = numberString.toCharArray();
int[] number = new int[charArray.length];
int total = 0;

for (int i = 0; i < charArray.length; i++) {
number[i] = Character.getNumericValue(charArray[i]);
}

for (int i = number.length – 2; i > -1; i -= 2) {
number[i] *= 2;

if (number[i] > 9)
number[i] -= 9;
}

for (int i = 0; i < number.length; i++)
total += number[i];

if (total % 10 != 0)
return false;

return true;
}

}

************credit card**************

*********credit card validator*********

Hope it helps