|
| 1 | +# Individual-project-cmpe202-Komal |
| 2 | + |
| 3 | +# Student ID: 016860532 |
| 4 | + |
| 5 | +#### 1. Describe what is the primary problem you try to solve? |
| 6 | + |
| 7 | +Ans: The primary problem I'm trying to solve is how to determine whether a credit card is valid or not by comparing it |
| 8 | +to the various credit card types, such as Mastercard, Visa, American Express, and Discover, and using the credit card |
| 9 | +number to find the card issuer and the appropriate objects based on the type of card. |
| 10 | + |
| 11 | +#### 2. Describe what are the secondary problems you try to solve (if there are any)? |
| 12 | + |
| 13 | +Ans: Determining the appropriate design patterns to take into account is the secondary issue. Future additions of new |
| 14 | +credit classes for various credit card types. |
| 15 | + |
| 16 | +#### 3. Describe what design pattern(s) you use (use plain text and diagrams)? |
| 17 | + |
| 18 | +Ans: Design Patterns: Chain of Responsibility, Strategy |
| 19 | + |
| 20 | +###### a) Strategy Design Pattern: |
| 21 | + |
| 22 | +The Strategy Design Pattern enables dynamic behavior changes in an application based on a selected strategy at runtime. |
| 23 | +It allows for the creation of objects and strategies specific to different file types, adapting the application's |
| 24 | +behavior accordingly. |
| 25 | + |
| 26 | +I implemented Strategy Design Pattern to support multiple file formats in my application. I designed three interfaces - |
| 27 | +Reader, Writer, and CreditCardHandler - to handle different aspects of file processing and credit card handling. |
| 28 | + |
| 29 | +The Reader interface provides a common contract for classes that can read input files, and it includes a readFile method |
| 30 | +to handle the reading process based on the input file type. |
| 31 | + |
| 32 | +The CreditCardHandler interface defines a common structure for classes responsible for checking the credit card type of |
| 33 | +parsed input files. It includes a checkCreditType method that takes the parsed input file and determines if it belongs |
| 34 | +to any specific credit card type category. |
| 35 | + |
| 36 | +The Writer interface provides a common structure for classes responsible for writing data to different file formats. It |
| 37 | +includes a writeToFile method that takes an input of type OutputEntry (which represents the final output format) and |
| 38 | +writes the data to the expected file format. |
| 39 | + |
| 40 | +This design pattern enables runtime behavior swapping and follows the Open/Closed principle. It allows for the |
| 41 | +introduction of new strategies without affecting the client code, making it easy to accommodate new file formats without |
| 42 | +disrupting the existing code or design. |
| 43 | + |
| 44 | +###### b) Chain of Responsibility Design Pattern: |
| 45 | + |
| 46 | +When a request is issued by the client, it is received by the first handler in the chain. Each handler checks the |
| 47 | +request and decides whether to process it or pass it along the chain. If a handler decides not to process the request, |
| 48 | +it passes the request to the next handler in the chain. This process continues until the request is handled or the chain |
| 49 | +is exhausted. |
| 50 | + |
| 51 | +After parsing an XML, JSON, or CSV file, we need to validate its content to determine the credit card type among the |
| 52 | +four available types. |
| 53 | + |
| 54 | +We can utilize the Chain of Responsibility design pattern to validate the file against different credit card handlers. |
| 55 | +The validation process starts with the MasterCard handler. If the file does not match the criteria for MasterCard, it is |
| 56 | +passed on to the next credit card handler in the chain. This allows for flexible and extensible validation of the file |
| 57 | +against multiple credit card types. |
| 58 | + |
| 59 | +To facilitate the overall process, we introduce a main credit card handler that acts as a mediator. This handler |
| 60 | +receives the file and passes it to each individual credit card handler for validation. Each credit card handler |
| 61 | +implements the main handler and performs the necessary checks specific to its credit card type. This approach enables a |
| 62 | +centralized and organized validation process, allowing each handler to focus on its designated credit card type. |
| 63 | + |
| 64 | +#### 4. Describe the consequences of using this/these pattern(s)? |
| 65 | + |
| 66 | +###### a) Strategy Design Pattern: |
| 67 | + |
| 68 | +###### Advantages: |
| 69 | + |
| 70 | +-> The Strategy design pattern provides flexibility in adding new strategies as needed without impacting existing code. |
| 71 | + |
| 72 | +-> It promotes code reusability. By encapsulating specific algorithms or behaviors into separate strategies, these |
| 73 | +strategies can be reused across different parts of the application. This eliminates the need for duplicating code or |
| 74 | +implementing similar logic multiple times, leading to cleaner and more maintainable code. |
| 75 | + |
| 76 | +###### Disadvantages: |
| 77 | + |
| 78 | +-> Users of the Strategy design pattern should be aware of multiple strategies and understand the distinctions between |
| 79 | +them. |
| 80 | + |
| 81 | +-> Having a large number of objects at the same time can become cumbersome and redundant in the Strategy design pattern. |
| 82 | + |
| 83 | +##### b) Chain of Responsibility |
| 84 | + |
| 85 | +###### Advantages: |
| 86 | + |
| 87 | +-> Decouples the sender of a request from its recipients. |
| 88 | + |
| 89 | +-> The client is unaware of the chain structure and interacts with the chain through direct method calls. |
| 90 | + |
| 91 | +-> The order of the chain can be easily modified or adjusted, and new handlers can be added or existing ones can be |
| 92 | +removed as needed. |
| 93 | + |
| 94 | +###### Disadvantages: |
| 95 | + |
| 96 | +-> Debugging and observing the runtime characteristics of the chain can be challenging due to the dynamic nature of the |
| 97 | +pattern. |
| 98 | +-> If the chain is not properly configured or there are gaps in the chain, it may lead to requests being ignored or not |
| 99 | +processed correctly. |
| 100 | + |
| 101 | +#### 5. Class Diagram: |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +#### 6. How to Run the project: |
| 106 | + |
| 107 | +Run the App.java |
| 108 | + |
| 109 | +Enter the input file path: Input file path with extension |
| 110 | + |
| 111 | +Enter the output file path: Ouput file path with extension |
| 112 | + |
| 113 | +#### 7. How to test Junit test? |
| 114 | + |
| 115 | +Click on run all tests to check JUnit testcases. |
0 commit comments