Author Topic: [Java]Credit Card Number Validator  (Read 5978 times)

0 Members and 1 Guest are viewing this topic.

Offline Lykos

  • Serf
  • *
  • Posts: 26
  • Cookies: 0
    • View Profile
[Java]Credit Card Number Validator
« on: December 03, 2012, 04:35:24 am »
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:
Code: [Select]

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/fWSKnjHP


Any constructive criticisms or questions are welcome.


« Last Edit: December 03, 2012, 04:57:44 am by Lykos »

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [Java]Credit Card Number Validator
« Reply #1 on: December 03, 2012, 04:39:08 am »
Any papers or lecture notes on the subject?
>>>import this
-----------------------------

Offline Lykos

  • Serf
  • *
  • Posts: 26
  • Cookies: 0
    • View Profile
Re: [Java]Credit Card Number Validator
« Reply #2 on: December 03, 2012, 04:49:56 am »
No lectures on CC validation, but plenty on everything in the code.  Like I said, I've looked everywhere on the fileserver for the document on validation, but it seems to have been taken down.  I'll do a write-up when I get time, or you can look at the code.  The comments should explain a lot.

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: [Java]Credit Card Number Validator
« Reply #3 on: December 03, 2012, 04:52:06 am »
Why does the link take me to an outlook web app instead of pastebin? Please fix it
« Last Edit: December 03, 2012, 04:52:58 am by Daemon »
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline Lykos

  • Serf
  • *
  • Posts: 26
  • Cookies: 0
    • View Profile
Re: [Java]Credit Card Number Validator
« Reply #4 on: December 03, 2012, 04:58:32 am »
No idea how the hell that happened.  Fixed.