Need a program writen in java, needs to be able to read the read the credit card
ID: 3771960 • Letter: N
Question
Need a program writen in java, needs to be able to read the read the credit cards from a .txt, also need to have a object credit card class and a tester/ driver class.
Your task is to:
– Read the data listed below “Data to Evaluate” from a file.
– Evaluate each number to see if they are representing a possible credit card number.
– Validate each credit card number to see if they are a valid number.
– Store the valid numbers and invalid numbers in a separate array.
– Write the contents of validated credit card number array into a file called “valid_cards.txt”.
– Write the invalid credit card number array to a file called “invalid_numbers.txt”.
– Make sure to include the issuer for each group of credit card numbers identified.
– Your application should work with any number of credit card entries.
How to validate Credit Card Numbers?
Most credit card number can be validated using the Luhn algorithm, which is more or a less a glorified Modulo
10 formula!
The Luhn Formula:
• Drop the last digit from the number. The last digit is what we want to check against
• Reverse the numbers
• Multiply the digits in odd positions (1, 3, 5, etc.) by 2 and subtract 9 to all any result higher than 9
• Add all the numbers together
• The check digit (the last number of the card) is the amount that you would need to add to get a multiple
of 10 (Modulo 10)
Luhn Example:
Steps Total
Original Number: 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5 5
Drop the last digit: 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5
Reverse the digits: 5 8 9 9 8 6 8 5 7 3 7 6 5 5 4
Multiple odd digits by 2: 10 8 18 9 16 6 16 5 14 3 14 6 10 5 8
Add digits from line above 1 8 9 9 7 6 7 5 5 3 5 6 1 5 8
Add all numbers: 1 8 9 9 7 6 7 5 5 3 5 6 1 5 8 85
Mod 10: (85 + X) modulo 10 = 0 , thus X=5 (last digit of card)
List of credit card number formats
Credit Card Issuer Starts With ( IIN Range ) Length ( Number of digits )
American Express 34, 37 15
Diners Club - Carte Blanche 300, 301, 302, 303, 304, 305 14
Diners Club - International 36 14
Diners Club - USA & Canada 54 16
Discover 6011, 622126 to 622925, 644, 645, 646,
647, 648, 649, 65 16
InstaPayment 637, 638, 639 16
JCB 3528 to 3589 16
Laser 6304, 6706, 6771, 6709 16-19
Maestro 5018, 5020, 5038, 5893, 6304, 6759, 6761,
6762, 6763 16-19
MasterCard 51, 52, 53, 54, 55 16-19
Visa 4 13-16
Visa Electron 4026, 417500, 4508, 4844, 4913, 4917 16
Heres the credit card data from the .txt file
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
Explanation / Answer
Answer:
Program code to copy:
//CreditCard.java
public class CreditCard
{
String value;
char valArray[];
int valid[];
int invalid[];
int reverse[];
int multipleOdd[];
int addDigits[];
int dropLast;
// constructor
public CreditCard(String cardNum)
{
if (cardNum.length() >= 13 && cardNum.length() <= 19)
{
value = cardNum;
}
valArray = new char[cardNum.length()];
valid = new int[valArray.length];
reverse = new int[valArray.length];
multipleOdd = new int[valArray.length];
addDigits = new int[valArray.length];
dropLast = 0;
}
// validateCardNum method that validates the card number and returns
// boolean value
public boolean validateCardNum()
{
valArray = value.toCharArray();
for (int i = 0; i < valArray.length; i++)
{
valid[i] = (int) valArray[i];
}
for (int i = 0; i < valArray.length; i++)
{
valid[i] = Integer.parseInt(String.valueOf(valArray[valid.length - 1]));
}
dropLast = valid[valid.length - 1];
reverse = reverseArray(valid);
multipleOdd = multiplyAllOdd(reverse);
addDigits = sumDigits(multipleOdd);
int sum = 0;
for (int i = 0; i < addDigits.length; i++)
{
sum = sum + addDigits[i];
}
boolean isValid = isValidSum(sum, dropLast);
return isValid;
}
// reverseArray method that returns a integer array
public int[] reverseArray(int valid[])
{
for (int i = 0; i < valid.length; i++)
{
reverse[i] = valid[valid.length - 1 - i];
}
return reverse;
}
// multiplyAllOdd method that returns a integer array
public int[] multiplyAllOdd(int valid[])
{
for (int i = 0; i < valid.length; i++)
{
if (i % 2 == 0)
{
multipleOdd[i] = valid[i] * 2;
}
else
{
multipleOdd[i] = valid[i];
}
}
return multipleOdd;
}
// sumDigits method that returns a integer array
public int[] sumDigits(int valid[])
{
int sum = 0;
for (int i = 0; i < valid.length; i++)
{
sum = 0;
if (valid[i] >= 10)
{
sum = valid[i] / 10 + valid[i] % 10;
addDigits[i] = sum;
}
else
{
addDigits[i] = valid[i];
}
}
return addDigits;
}
// isValidSum method that returns a boolean value
public boolean isValidSum(int sum, int dropLast)
{
if ((sum + dropLast) % 10 == 0)
{
return true;
}
return false;
}
// getCardIssuer method that returns a String value
public String getCardIssuer()
{
if (value.substring(0, 2).equals("34") || value.substring(0, 2).equals("37"))
return "American Express";
else if (value.substring(0, 2).equals("30"))
return "Diners Club - Carte Blanche";
else if (value.substring(0, 2).equals("36"))
return "Diners Club - International";
else if (value.substring(0, 2).equals("54"))
return "Diners Club - USA & Canada";
else if (value.substring(0, 2).equals("60") || value.substring(0, 2).equals("62")
|| value.substring(0, 2).equals("64") || value.substring(0, 2).equals("65"))
return "Discover";
else if (value.substring(0, 2).equals("63"))
return "InstaPayment ";
else if (value.substring(0, 2).equals("35"))
return "JCB";
else if (value.substring(0, 2).equals("67"))
return "Laser";
else if (value.substring(0, 2).equals("50") || value.substring(0, 2).equals("58"))
return "Maestro";
else if (value.substring(0, 2).equals("51") || value.substring(0, 2).equals("52")
|| value.substring(0, 2).equals("53") || value.substring(0, 2).equals("54")
|| value.substring(0, 2).equals("51"))
return "MasterCard";
else if (value.substring(0, 1).equals("4"))
return "Visa";
else if (value.substring(0, 2).equals("40") || value.substring(0, 2).equals("41")
|| value.substring(0, 2).equals("45") || value.substring(0, 2).equals("48")
|| value.substring(0, 2).equals("49"))
return "Visa Electron";
else
return "No Issuer";
}
}
//CreditCardTester.java
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class CreditCardTester
{
// main method
public static void main(String args[]) throws Exception
{
// input file object
Scanner in = new Scanner(new File("CreditCardEvaluation.txt"));
// output file objects
PrintWriter pwInvalid = new PrintWriter(new File("invalid_numbers.txt"));
PrintWriter pwValid = new PrintWriter(new File("valid_cards.txt"));
String value;
CreditCard cc;
// loop till end of the file
while (in.hasNextLine())
{
value = in.nextLine();
cc = new CreditCard(value);
// write the valid card numbers into valid text file
if (cc.validateCardNum())
{
pwValid.println(cc.getCardIssuer() + " " + value + " " + value.length());
}
// write the invalid card numbers into invalid text file
else
pwInvalid.println(cc.getCardIssuer() + " " + value + " " + value.length());
}
// close all the files
pwInvalid.close();
pwValid.close();
in.close();
}
}
Output file : valid.txt
No Issuer 3158539628375348 16
No Issuer 3112804683572033 16
Diners Club - USA & Canada 5435528978467581 16
Laser 6706465468107999 16
InstaPayment 6304362971054667 16
Visa 4844104341377697 16
Visa 4913405490886727 16
Visa 4844885754351829 16
InstaPayment 6371473570875275 16
InstaPayment 6381475006869978 16
InstaPayment 6389057917814806 16
American Express 347100734345549 15
American Express 347100734345543 15
Discover 6011621663574413 16
Discover 6011824617460743 16
Discover 6011824617460745 16
Visa 4539318768050385 16
Diners Club - International 36880982786892 14
Diners Club - International 36845793345501 14
Diners Club - International 36661154562232 14
Diners Club - International 36661154562234 14
Laser 6761680165952016 16
Laser 6763100400984029 16
MasterCard 5127043299122389 16
Diners Club - USA & Canada 5429168755154025 16
Diners Club - USA & Canada 5429168755154023 16
American Express 375354034606481 15
American Express 379570632133224 15
Visa 4485521241443135 16
Visa 4532916206508075 16
Visa 4532916206508076 16
No Issuer 5540641137519895 16
Diners Club - Carte Blanche 30522070708059 14
Diners Club - Carte Blanche 30066552673241 14
Diners Club - Carte Blanche 30365487186802 14
Diners Club - Carte Blanche 30365487186801 14
Output Invalid file: invalid_numbers.txt:
No Issuer 3337465828248404 16
No Issuer 3112804683572030 16
Laser 6706169762827894 16
Laser 6706169762827892 16
Visa 4844885754351822 16
Discover 6011162533805000 16
InstaPayment 6389057917814802 16
Maestro 5893329089846994 16
Laser 6763100400984022 16
MasterCard 5330838017737340 16
No Issuer 5590976687287124 16
No Issuer 5540641137519892 16
Note:
The input used is already provided in the question. Just add the provided data in CreditCardEvaluation.txt file.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.