This application is developed using Java implementing various Design Patterns. This Application lets the user provide the Credit Card records, and it determines the type of the credit card. The application accepts the records in various files including CSV, JSON, XML. The application reads a record from the file, verifies that the credit card number is a possible account number, and creates an instance of the appropriate credit card class. The input files should contain credit card information such as credit card number, expiration date, and the name of the cardholder. The design patterns implemented are:
- Strategy Pattern
- Factory Pattern
- Clone the Repository.
- Open the project in any IDE (Ex: IntelliJ)
- This is a maven project, so all the dependencies which are defined in the pom.xml file will be automatically downloaded.
- Run the class CreditCardClient as given below
- Open the class CreditCardClient in IDE.
- The input files are present in the folder src/main/resources/input/ folder.
- The output files will be created in the folder src/main/resources/output/ folder.
- The input files and input files can be modified by using the appropriate variables present in the main() function.
- Run the main() function to see the application in action.
The primary problem that I have tried to solve here is to read the file, that contains records of the credit cards,and for each record based on credit card number, expiration date, and the name of the card holder verify if the credit card number is a possible account number and create an instance of the appropriate credit card class among the given credit card types. - Master, Discover, Visa and American Express.
The secondary problems that l've come across are as below -
- how to parse the files
- how to choose and efficiently implement appropriate design patterns.
The design patterns that I've implemented are:
- Strategy Design pattern allows changing the behaviour of an application based on the selected strategy.
- Since there are different file types, objects for each file type are created and appropriate strategies are used based on the input file.
- I used a strategy design pattern to support different file formats.
- I have created 3 different interfaces CsvFileHandler, JsonHandler and XmlHandler. All these are extended to a file parser. Based on the input, the behaviour changes.
- Factory pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
- Since there are various types of cards, With an interface called cardFactory, implements it to return a created object of the card type.
Ans: Strategy pattern: With usage of strategy pattern, the context becomes independent of concrete strategies, so you can add new implementations or modify existing ones without changing the code of the context or other strategies. Hence, now the code supports sv, xml and json file formats and could be extended further for any file types by just writing another class and adding it to strategy pattern. Factory pattern: The Factory Method separates construction code from the code that actually uses. Therefore, it's easier to extend the construction code independently from the rest of the code. Currently code supports VisaCC, MasterCC, American express ,Discover, to add a new card type to the app, you'll only need to create a new creator subclass and override the factory method in it.
Here the given question is to extend the code to read input from (ison, xml, csv) different types, hence we use Strategy pattern to scan the file extension, based on the file extension type, that particular file parser class is executed.
(Kindly click on the images and zoom for clarity - Thank you)
• UML Class Diagram for Factory Pattern Implementation
• UML Class Diagram for Strategy Pattern Implementation!
- CSV Input
- CSV Output
- XML Input
- XML Output
- JSON Input
- JSON Output
- JUnit Test cases