Skip to content

kunal768/credit-card-processor

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Review Assignment Due Date

Credit Card Processor

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.

Prerequisites

  • Java JDK 11 or higher
  • Maven 3.6+ (for building and running tests)

Verify Installation

java -version    # Should show Java 11 or higher
mvn -version     # Should show Maven 3.6 or higher

Setup Instructions

  1. Clone the repository (if not already done):

    git clone <repository-url>
    cd cmpe202-02-individual-project-kunal768
  2. Compile the project:

    mvn clean compile

    This will compile all source files and place them in the target/classes directory.

Running the Application

Basic Usage

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>

Examples

Process a CSV file:

java -cp target/classes Application input_file.csv output_file.csv

Process a JSON file:

java -cp target/classes Application input_file.json output_file.json

Process an XML file:

java -cp target/classes Application input_file.xml output_file.xml

Output

The application will:

  1. Parse the input file based on its extension (.csv, .json, or .xml)
  2. Validate each credit card number
  3. Identify the card type (Visa, MasterCard, AmEx, Discover)
  4. 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 to output_file.csv in CSV format

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

Running Tests

To run all unit tests:

mvn test

To compile and run tests in one command:

mvn clean test

Project Structure

.
├── 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)

Supported Card Types

  • 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

Input File Formats

CSV Format

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.

JSON Format

[
  {
    "cardNumber": "4111111111111111",
    "expirationDate": "12/25",
    "cardHolderName": "John Doe"
  }
]

XML Format

<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.

Output File Formats

The output files follow the same format as the input files but contain validation results:

CSV Output Format

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.

JSON Output Format

[
  {
    "cardNumber": "4111111111111111",
    "cardType": "Visa"
  },
  {
    "cardNumber": "123",
    "cardType": "Invalid: not a possible card number"
  }
]

XML Output Format

<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.

Troubleshooting

Error: "Unsupported file format"

  • Ensure your input file has a .csv, .json, or .xml extension

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

Design Patterns

Chain of Responsibility Pattern

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:

  1. AmExHandler - Checks for cards starting with "34" or "37" (most specific, checked first)
  2. VisaHandler - Checks for cards starting with "4"
  3. MasterHandler - Checks for cards starting with "5"
  4. 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.

Design Patterns & Diagrams

This project implements several design patterns to achieve extensibility, maintainability, and separation of concerns.

Design Patterns Used

  1. Factory Pattern - Centralizes credit card object creation
  2. Chain of Responsibility Pattern - Handles card type identification through a chain of handlers
  3. Template Method Pattern - Defines common structure for credit card classes
  4. Strategy Pattern - Encapsulates different file parsing algorithms

Class Diagrams

Factory Pattern

Factory Pattern Diagram

Chain of Responsibility Pattern

Chain of Responsibility Pattern Diagram

Template Method Pattern

Template Method Pattern Diagram

Part 1 Complete Class Diagram

Part 1 Complete Class Diagram

Strategy Pattern (Parser)

Strategy Pattern Diagram

Activity Diagram

Activity Diagram

About

A credit-card processor application written in Java

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%