This is my submission for the Individual Project. This application validate credit card numbers from different input files and output the result to a file.
The problem is to read list of credit cards from a file (CSV, JSON or XML) and verify if they are valid or not. Also need to identify what type of card it is (Visa, MasterCard, AmEx or Discover).
I implement this using Java and used 3 design patterns to make the code better and extensible.
-
Chain of Responsibility: I used this for the validation part. The validator pass the card number to next validator if it cannot handle it.
- Classes:
CardValidator,VisaValidator,MasterCardValidator, etc.
- Classes:
-
Factory Pattern: This is used to create the Credit Card objects and also to get the correct File Parser.
- Classes:
CreditCardFactory,ParserFactory
- Classes:
-
Strategy Pattern: This allow the program to handle different file formats easily. If we need to add new format, we just add new strategy.
- Classes:
CSVParser,JSONParser,XMLParser
- Classes:
src/main/java: Contains all the source codesrc/test/java: Contains all the JUnit testssample_input.csv: Sample file for testinginput_file.json: Sample JSON inputsample_input.xml: Sample XML input
You need to have Maven and Java installed.
Run this command in terminal:
mvn clean compileYou can run the processor with input file and output file arguments:
For CSV:
java -cp target/classes com.creditcard.CreditCardProcessor sample_input.csv output.csvFor JSON:
java -cp target/classes com.creditcard.CreditCardProcessor input_file.json output.jsonFor XML:
java -cp target/classes com.creditcard.CreditCardProcessor sample_input.xml output.xmlI have added JUnit tests for all the classes. You can run them using:
mvn test- Card numbers are treated as String because of leading zeros.
- Expiration date is just passed through, logic only check the card number format.
- For invalid cards, I create a InvalidCC object with error message.