1
+ package com .cmpe202 .individualproject .main ;
2
+
3
+ import com .cmpe202 .individualproject .handlers .CreditCardHandler ;
4
+ import com .cmpe202 .individualproject .handlers .MasterCard ;
5
+ import com .cmpe202 .individualproject .reader .CSVReader ;
6
+ import com .cmpe202 .individualproject .reader .JSONReader ;
7
+ import com .cmpe202 .individualproject .reader .Reader ;
8
+ import com .cmpe202 .individualproject .reader .XMLReader ;
9
+ import com .cmpe202 .individualproject .writer .CSVWriter ;
10
+ import com .cmpe202 .individualproject .writer .JSONWriter ;
11
+ import com .cmpe202 .individualproject .writer .Writer ;
12
+ import com .cmpe202 .individualproject .writer .XMLWriter ;
13
+
14
+ import java .io .File ;
15
+ import java .util .ArrayList ;
16
+ import java .util .List ;
17
+ import java .util .Scanner ;
18
+
19
+
20
+ public class App {
21
+ public static String getFileType (String inputFile ) {
22
+ int lastDotIndex = inputFile .lastIndexOf ('.' ); // Find the index of the last dot in the file name
23
+ if (lastDotIndex > 0 && lastDotIndex < inputFile .length () - 1 ) { // Ensure the dot is not the first or last character
24
+ return inputFile .substring (lastDotIndex + 1 ); // Extract the file extension after the dot
25
+ }
26
+ return "" ; // Return an empty string if no valid file extension is found
27
+ }
28
+
29
+ public static void main (String [] args ) {
30
+ String outputFile , inputFile ;
31
+ String inputFileType = "" ;
32
+ String outputFileType = "" ;
33
+ Reader fileReader ;
34
+ Writer fileWriter = null ;
35
+ List <CreditCardEntry > creditCardEntries = new ArrayList <>();
36
+ List <OutputEntry > finalEntries = new ArrayList <>();
37
+
38
+ Scanner scanner = new Scanner (System .in );
39
+
40
+ // Get input file path from the user
41
+ System .out .print ("Enter the input file path: " );
42
+ inputFile = scanner .nextLine ();
43
+
44
+ // Get output file path from the user
45
+ System .out .print ("Enter the output file path: " );
46
+ outputFile = scanner .nextLine ();
47
+
48
+ // Extract the input and output file extensions
49
+ inputFileType = inputFile .substring (inputFile .indexOf ("." ));
50
+ outputFileType = outputFile .substring (outputFile .indexOf ("." ));
51
+
52
+ if (!inputFileType .equals (outputFileType )) {
53
+ System .out .println ("Input and Output extensions are not the same" );
54
+ System .exit (0 );
55
+ } else {
56
+ System .out .println ("Input and output extensions " + inputFileType + " and " + outputFileType + " are the same" );
57
+ }
58
+
59
+ // Get the file type (extension) of the input file
60
+ String fileType = getFileType (inputFile );
61
+ System .out .println ("File type: " + fileType );
62
+
63
+ // Initialize the appropriate reader and writer based on the file type
64
+ switch (fileType .toLowerCase ()) {
65
+ case "csv" :
66
+ fileReader = new CSVReader (new File (inputFile ));
67
+ fileWriter = new CSVWriter ();
68
+ break ;
69
+ case "json" :
70
+ fileReader = new JSONReader (new File (inputFile ));
71
+ fileWriter = new JSONWriter ();
72
+ break ;
73
+ case "xml" :
74
+ System .out .println ("Processing XML file" );
75
+ fileReader = new XMLReader (new File (inputFile ));
76
+ fileWriter = new XMLWriter ();
77
+ break ;
78
+ default :
79
+ System .out .println ("Unsupported file type: " + fileType );
80
+ return ;
81
+ }
82
+
83
+ // Read the credit card entries from the input file
84
+ creditCardEntries = fileReader .readFile (inputFile );
85
+
86
+ // Process each credit card entry and write the results to the output file
87
+ for (CreditCardEntry eachCreditCardEntry : creditCardEntries ) {
88
+ CreditCardHandler ccHandler = new MasterCard ();
89
+ String creditCardType = ccHandler .checkCreditCardType (eachCreditCardEntry );
90
+ String ccNumber = eachCreditCardEntry .getCardNumber ();
91
+ finalEntries .add (new OutputEntry (ccNumber , creditCardType ));
92
+ }
93
+ fileWriter .writeToFile (finalEntries , outputFile );
94
+ }
95
+
96
+
97
+ }
0 commit comments