credit card validation in java using Luhn Formula for validation here is the cre
ID: 3770699 • Letter: C
Question
credit card validation in java using Luhn Formula for validation here is the credit card numbers to evaluate Data to Evaluate
Data to Evaluate
3158539628375348
3337465828248404
3112804683572030
3112804683572033
5435528978467581
6706465468107999
6304362971054667
6706169762827894
6706169762827892
4844104341377697
4913405490886727
4844885754351829
4844885754351822
6371473570875275
6381475006869978
6389057917814806
347100734345549
347100734345543
6011162533805000
6011621663574413
6011824617460743
6011824617460745
6389057917814802
4539318768050385
36880982786892
36845793345501
36661154562232
36661154562234
5893329089846994
6761680165952016
6763100400984029
6763100400984022
5127043299122389
5330838017737340
5429168755154025
5429168755154023
375354034606481
379570632133224
4485521241443135
4532916206508075
4532916206508076
5590976687287124
5540641137519895
5540641137519892
30522070708059
30066552673241
30365487186802
30365487186801
organize the credit card number output
VISA:
45
44
MasterCard:
51
53
American Express (AMEX):
37
34
Discover:
60
JCB:
31
33
Diners Club - North America:
54
55
Diners Club - Carte Blanche:
30
Diners Club - International:
36
Maestro:
58
LASER:
67
Visa Electron:
48
49
InstaPayment:
63
Explanation / Answer
Program:
// CreditCard.java
//import the required classes
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
//CreditCard that checks the list of cards numbers
//validates them and separates the validated and invalidated
//cards numbers into separate files
public class CreditCard
{
// main method
public static void main(String args[])
{
// initialize three string arrays
String valid[] = new String[50];
String invalid[] = new String[50];
String cards[] = new String[50];
// define the counter variables for each array
int vcount = 0, invcount = 0, count = 0;
// logic to read values from the text file put it
// in try..catch block
try
{
// Scanner class object that points to the
// input file
Scanner input = new Scanner(new File("DataEvaluation.txt"));
// read each line and store the values into
// cards array until end of the file
while (input.hasNextLine())
{
cards[count] = input.nextLine();
count++;
}
// close the file
input.close();
}
// catch block if any
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(" Card number Card Issuer");
// logic to validate each card then storing each valid cards into
// valid array and invalid cards into invalid array
for (int i = 0; i < count; i++)
{
// condition to check whether the length of the cards are with
// in the range
if (cards[i].length() >= 13 && cards[i].length() <= 19)
{
System.out.printf("%20s %s ", cards[i],
cardIssuer(cards[i]));
// call isCardValid() method that returns boolean value
if (isCardValid(cards[i]))
{
// if the method returns true
// store the cards[i] into valid array
valid[vcount] = cards[i];
vcount++;
}
else
{
// if the method returns false
// store the cards[i] into invalid array
invalid[invcount] = cards[i];
invcount++;
}
}
else
{
System.out
.println("Can not validate the card because of the length of the card number.");
}
}
// logic to write values into the text file put the logic
// in try..catch block
try
{
// create PrintWriter object to write data into valid_cards.txt
PrintWriter validV = new PrintWriter(new File("valid_cards.txt"));
// print the values into text file
for (int i = 0; i < vcount; i++)
validV.println(valid[i]);
validV.close();
// create PrintWriter object to write data into
// invalid_numbers.txt
PrintWriter invalidV = new PrintWriter(new File(
"invalid_numbers.txt"));
// print the values into text file
for (int i = 0; i < invcount; i++)
invalidV.println(invalid[i]);
invalidV.close();
}
// catch the exceptions if any
catch (Exception e)
{
e.printStackTrace();
}
}
// isCardValid() that takes a card number of type string
// and returns a boolean value
public static boolean isCardValid(String cards)
{
// convert the string into integer array
int values[] = new int[cards.length()];
for (int i = 0; i < cards.length(); i++)
{
values[i] = Integer.parseInt(cards.charAt(i) + "");
}
// get the last digit number by calling the
// dropLastDigit() by passing values array
int x = dropLastDigit(values);
// reverse the values in values array by calling the
// reverseDigits() by passing values array
int reverse[] = reverseDigits(values);
// multiply the odd position values of reverse array by calling the
// doubleOddDigits() by passing reverse array
int multiplyOdd[] = doubleOddDigits(reverse);
// add the digits of each integer values of multiplyOdd array by
// calling the
// addDigits() by passing multiplyOdd array
int addDigits[] = addDigits(multiplyOdd);
// logic to add all the integer values of the addDigits array
int sum = 0;
for (int i = 0; i < addDigits.length; i++)
sum += addDigits[i];
// validate the sum and last digit by modulo function
// and depending on the boolean value return the value
boolean validate = validation(sum, x);
if (validate)
return true;
else
return false;
}
// dropLastDigit() that takes integer array as parameter
// and returns an integer value containing the last digit
public static int dropLastDigit(int[] cardNum)
{
return cardNum[cardNum.length - 1];
}
// reverseDigits() that takes an integer array as parameter and
// returns an integer array by reversing the original array
public static int[] reverseDigits(int[] cardNum)
{
int reverse[] = new int[cardNum.length];
for (int i = 0; i < cardNum.length; i++)
{
reverse[i] = cardNum[cardNum.length - 1 - i];
}
return reverse;
}
// doubleOddDigits() that takes an integer array as parameter and
// returns an integer array by doubling the odd position values of
// original array. Here it takes the even positions because array starts
// from position 0.
public static int[] doubleOddDigits(int[] reverse)
{
int doubleOdds[] = new int[reverse.length];
for (int i = 0; i < reverse.length; i++)
{
if (i % 2 == 0)
{
doubleOdds[i] = reverse[i] * 2;
}
else
{
doubleOdds[i] = reverse[i];
}
}
return doubleOdds;
}
// addDigits() that takes an integer array as parameter and
// returns an integer array by adding the digits at each index of
// original array
public static int[] addDigits(int[] multipliedOdd)
{
int sumDigits[] = new int[multipliedOdd.length];
int first, second;
for (int i = 0; i < multipliedOdd.length; i++)
{
if (multipliedOdd[i] >= 10)
{
first = multipliedOdd[i] / 10;
second = multipliedOdd[i] % 10;
sumDigits[i] = first + second;
}
else if (multipliedOdd[i] < 10)
{
sumDigits[i] = multipliedOdd[i];
}
}
return sumDigits;
}
// validation() that takes two integers as parameter and
// returns a boolean value depending on the modulo function
public static boolean validation(int sum, int x)
{
if ((sum + x) % 10 == 0)
return true;
else
return false;
}
// cardIssuer() that takes in a string and returns a string
// indicating the card issuer name
public static String cardIssuer(String cardNum)
{
String startingTwoChars = cardNum.substring(0, 2);
int value = Integer.parseInt(startingTwoChars);
if (value == 34 || value == 37)
return "American Express";
else if (value == 45 || value == 44)
return "VISA";
else if (value == 51 || value == 53)
return "MasterCard";
else if (value == 60)
return "Discover";
else if (value == 31 || value == 33)
return "JCB";
else if (value == 54 || value == 55)
return "Diners Club - North America";
else if (value == 30)
return "Diners Club - Carte Blanche";
else if (value == 36)
return "Diners Club - International";
else if (value == 58)
return "Maestro";
else if (value == 67)
return "LASER";
else if (value == 48 || value == 49)
return "Visa Electron";
else if (value == 63)
return "InstaPayment";
else
return "Not issued by any";
}
}
Sample Output text files:
valid_cards.txt:
invalid_numbers.txt:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.