So this reads in credit card numbers from a specified text file. This was done in BlueJ for an IDE (school required) and the file was in the same folder as the code, so it searched there. Edit it as needed. Might revise it so that instead of the filename being hardcoded in, it'll take the name as input from the user.
The algorithms for determining whether the numbers are valid were taken from the paper we were given in class about validation for CCs. I'll post that as well if I can find it, seems to have been taken off the student fileserver. Bit of a problem with the error catching that I've been trying to work out, but I already submitted this and have been working on finals so no time right now.
Main class:
import java.io.*;
import java.util.ArrayList;
/**
* Class ProcessCardNumbers reads a text file of
* credit card numbers and determines of the
* numbers are valid, invalid, or if the number
* is an unknown credit card type.
*
* @author Lykos
* @version 11.28.2012
*/
public class ProcessCardNumbers
{
public static void main(String[] args)
{
String line = "";
ArrayList<CreditCard> cards = new ArrayList<CreditCard>();
try
{
BufferedReader inFile = new BufferedReader(new FileReader("cardNumbers.txt"));
line = inFile.readLine();
while(line != null)
{
cards.add(new CreditCard(line));
line = inFile.readLine();
}
inFile.close();
}
catch(Exception e)
{
System.out.println("Problem opening or reading file");
}
for(CreditCard card : cards)
{
String cardType = card.creditCardType();
String cardNum = card.getCardNumber();
if(cardType.equals("Unknown"))
{
System.out.println("Card number " + cardNum + " is an unknown credit card type.");
}
else
{
if(card.isValid() == true)
{
System.out.println(cardType + " number " + cardNum + " is valid.");
}
else
{
System.out.println(cardType + " number " + cardNum + " is not valid.");
}
}
}
}
}
Here's the pastebin for the other class, which actually does the work of processing the numbers. Had a problem with the formatting here so just posting the link:
http://pastebin.com/fWSKnjHPAny constructive criticisms or questions are welcome.