A Java application that processes credit card records from CSV, JSON, or XML files, validates them, identifies card types (Visa, MasterCard, AmEx, Discover), and outputs the results.
- Java JDK 11 or higher
- Maven 3.6+ (for building and running tests)
java -version # Should show Java 11 or higher
mvn -version # Should show Maven 3.6 or higher-
Clone the repository (if not already done):
git clone <repository-url> cd cmpe202-02-individual-project-kunal768
-
Compile the project:
mvn clean compile
This will compile all source files and place them in the
target/classesdirectory.
The application takes two command-line arguments: an input file and an output file name. The output file will be written in the same format as the input file (CSV, JSON, or XML):
java -cp target/classes Application <input_file> <output_file>Process a CSV file:
java -cp target/classes Application input_file.csv output_file.csvProcess a JSON file:
java -cp target/classes Application input_file.json output_file.jsonProcess an XML file:
java -cp target/classes Application input_file.xml output_file.xmlThe application will:
- Parse the input file based on its extension (.csv, .json, or .xml)
- Validate each credit card number
- Identify the card type (Visa, MasterCard, AmEx, Discover)
- Generate an output file with the name you specify in the same format as the input file
- Example:
input_file.csv output_file.csv→ writes results tooutput_file.csvin CSV format
- Example:
Important: The output file format will match the input file format. If you provide a CSV input file, the output will be written in CSV format regardless of the output file extension. However, it's recommended to use matching extensions for clarity.
The output file will contain validation results for each card, including:
- Card number
- Card type (if valid) or error message
- Validation status
To run all unit tests:
mvn testTo compile and run tests in one command:
mvn clean test.
├── pom.xml # Maven configuration
├── src/
│ ├── main/
│ │ ├── Application.java # Main entry point
│ │ ├── CreditCard/ # Credit card classes
│ │ │ ├── CreditCard.java
│ │ │ ├── VisaCC.java
│ │ │ ├── MasterCC.java
│ │ │ ├── AmExCC.java
│ │ │ ├── DiscoverCC.java
│ │ │ ├── CreditCardFactory.java
│ │ │ ├── DefaultCreditCardFactory.java
│ │ │ ├── CardHandler.java # Chain of Responsibility handler base class
│ │ │ ├── AmExHandler.java # AmEx card type handler
│ │ │ ├── VisaHandler.java # Visa card type handler
│ │ │ ├── MasterHandler.java # MasterCard type handler
│ │ │ ├── DiscoverHandler.java # Discover card type handler
│ │ │ └── ...
│ │ └── Parser/ # File parser classes
│ │ ├── RecordParser.java
│ │ ├── CsvParser.java
│ │ ├── JsonParser.java
│ │ └── XmlParser.java
│ └── test/ # Unit tests
└── target/ # Compiled classes (generated)
- Visa: Starts with 4, 13 or 16 digits
- MasterCard: Starts with 51-55, 16 digits
- American Express: Starts with 34 or 37, 15 digits
- Discover: Starts with 6011, 16 digits
cardNumber,expirationDate,cardHolderName
4111111111111111,12/25,John Doe
5123456789012345,01/24,Jane Smith
Note: The CSV file can optionally include a header row. If present, the header should contain "cardNumber" (case-insensitive) to be recognized as a header.
[
{
"cardNumber": "4111111111111111",
"expirationDate": "12/25",
"cardHolderName": "John Doe"
}
]<CARD>
<CARD_NUMBER>4111111111111111</CARD_NUMBER>
<EXPIRATION_DATE>12/25</EXPIRATION_DATE>
<CARD_HOLDER_NAME>John Doe</CARD_HOLDER_NAME>
</CARD>Note: XML format uses uppercase tag names with underscores. Multiple cards can be included by repeating the <CARD> element structure.
The output files follow the same format as the input files but contain validation results:
cardNumber,cardType
4111111111111111,Visa
5123456789012345,MasterCard
123,Invalid: not a possible card number
Note: The cardType column contains either the card type (Visa, MasterCard, AmEx, Discover) or an error message if the card is invalid.
[
{
"cardNumber": "4111111111111111",
"cardType": "Visa"
},
{
"cardNumber": "123",
"cardType": "Invalid: not a possible card number"
}
]<CARDS>
<CARD>
<CARD_NUMBER>4111111111111111</CARD_NUMBER>
<CARD_TYPE>Visa</CARD_TYPE>
</CARD>
<CARD>
<CARD_NUMBER>123</CARD_NUMBER>
<CARD_TYPE>Invalid: not a possible card number</CARD_TYPE>
</CARD>
</CARDS>Note: The <CARD_TYPE> tag contains either the card type or an error message.
Error: "Unsupported file format"
- Ensure your input file has a
.csv,.json, or.xmlextension
Error: "Could not find or load main class Application"
- Make sure you've compiled the project:
mvn clean compile - Verify you're using the correct classpath:
-cp target/classes
Error: "Usage: java Application <input_file> <output_file>"
- Provide both the input file path and output file path as command-line arguments
The credit card type identification system uses the Chain of Responsibility design pattern to identify card types based on their number prefixes. This design provides several benefits:
- Open/Closed Principle: New card types can be added by creating new handler classes without modifying existing code
- Single Responsibility: Each handler is responsible for identifying and creating one specific card type
- Extensibility: Easy to add new card types or modify identification rules for individual card types
- Maintainability: Clear separation of concerns makes the code easier to understand and test
The DefaultCreditCardFactory assembles a chain of handlers in a specific order:
AmExHandler- Checks for cards starting with "34" or "37" (most specific, checked first)VisaHandler- Checks for cards starting with "4"MasterHandler- Checks for cards starting with "5"DiscoverHandler- Checks for cards starting with "6"
The order is critical: AmEx handlers must come before Visa/Master handlers to ensure cards starting with "34" or "37" are correctly identified as AmEx before being matched by the single-digit prefixes.
When a card number is processed, the factory delegates to the first handler in the chain. Each handler checks if it can process the request. If it can, it creates the appropriate card instance. If not, it passes the request to the next handler in the chain.
This project implements several design patterns to achieve extensibility, maintainability, and separation of concerns.
- Factory Pattern - Centralizes credit card object creation
- Chain of Responsibility Pattern - Handles card type identification through a chain of handlers
- Template Method Pattern - Defines common structure for credit card classes
- Strategy Pattern - Encapsulates different file parsing algorithms





