-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
277 lines (244 loc) · 7.92 KB
/
Copy pathmain.cpp
File metadata and controls
277 lines (244 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <sys/types.h>
#include <dirent.h>
#include <ctime>
#include <vector>
using namespace std;
class Transaction {
public:
string file_id, ARN, SettlementType;
int jdate; // data juliana
void print(ostream &cout) const{
cout << "\"" << this->file_id << "\",";
cout << "\"" << this->ARN << "\",";
cout << "\"2" << this->jdate << "\","; //Adding year digit
cout << "\"" << this->SettlementType << "\"";
cout << endl;
}
Transaction(){
file_id = "";
ARN = "";
jdate = 0;
SettlementType = "";
}
void clean(){
ARN = SettlementType = "";
jdate = 0;
}
};
enum states {
initial_state = 0,
read_line_state,
after_read_line_state,
get_file_id_state,
find_chargeback_state,
get_arn_state,
discard_01_state,
get_date_state,
error_state,
close_state,
new_file_state
};
vector <Transaction> allTransactions;
int count_different_than_27 = 0;
void parseReport(string &line, enum states ¤t_state, enum states &previous_state, Transaction &new_transaction){
string data = "", file_id = "", ARN="", date="", intern_line="", key="", ddays="";
int i_date = 0;
stringstream ss(line);
ss >> key; ss >> key;
switch (current_state)
{
case initial_state:
previous_state = initial_state;
current_state = get_file_id_state;
break;
case get_file_id_state:
data = line.substr(12,2);
if(data != "90"){
cout << "File ID line is invalid" << endl;
previous_state = get_file_id_state;
current_state = error_state;
break;
}
file_id = line.substr(90,23);
new_transaction.file_id = file_id;
current_state = read_line_state;
previous_state = get_file_id_state;
break;
case find_chargeback_state:
data = line.substr(11+key.size(),2);
if(data != "15") {
current_state = read_line_state;
previous_state = find_chargeback_state;
break;
}
intern_line = line.substr(15+key.size(),2);
if(intern_line != "00") {
current_state = read_line_state;
previous_state = find_chargeback_state;
break;
}
previous_state = find_chargeback_state;
current_state = get_arn_state;
break;
case get_arn_state:
data = line.substr(11+key.size(),2);
if(data != "15") {
cout << "ARN line is invalid" << endl;
current_state = error_state;
previous_state = get_arn_state;
break;
}
intern_line = line.substr(15+key.size(),2);
if(intern_line != "00") {
cout << "Interline isn't invalid" << endl;
current_state = error_state;
previous_state = get_arn_state;
break;
}
ARN = line.substr(39+key.size(),23);
new_transaction.ARN = ARN;
previous_state = get_arn_state;
current_state = read_line_state;
break;
case discard_01_state:
data = line.substr(11+key.size(),2);
if(data != "15") {
cout << "Transaction type isn't 15" << endl;
current_state = error_state;
previous_state = get_date_state;
break;
}
intern_line = line.substr(15+key.size(),2);
if(intern_line != "01") {
cout << "[01] - Interline isn't valid" << endl;
current_state = error_state;
previous_state = discard_01_state;
break;
}
current_state = read_line_state;
previous_state = discard_01_state;
break;
case get_date_state:
data = line.substr(11+key.size(),2);
if(data != "15") {
cout << "Transaction type isn't 15" << endl;
current_state = error_state;
previous_state = get_date_state;
break;
}
intern_line = line.substr(15+key.size(),2);
if(intern_line != "02") {
cout << "[02] - Interline isn't valid" << endl;
current_state = error_state;
previous_state = get_date_state;
break;
}
ddays = line.substr(36+key.size(),2);
new_transaction.SettlementType = ddays;
if(ddays != "27"){
count_different_than_27++;
cout << "Warning dday different than 27" << endl;
}
date = line.substr(48+key.size(),4);
i_date = stoi(date);
new_transaction.jdate = i_date;
previous_state = get_date_state;
current_state = close_state;
break;
case read_line_state:
if(previous_state == initial_state) current_state = get_file_id_state;
if(previous_state == get_file_id_state) current_state = find_chargeback_state;
if(previous_state == find_chargeback_state) current_state = find_chargeback_state;
if(previous_state == get_arn_state) current_state = discard_01_state;
if(previous_state == discard_01_state) current_state = get_date_state;
if(previous_state == get_date_state) current_state = close_state;
previous_state = read_line_state;
break;
case close_state:
allTransactions.push_back(new_transaction);
new_transaction.clean();
current_state = read_line_state;
previous_state = find_chargeback_state;
break;
case error_state:
if(previous_state == get_file_id_state){
cout << "file without file id state, going to the next file." << endl;
current_state = new_file_state;
previous_state = close_state;
break;
}
current_state = find_chargeback_state;
previous_state = close_state;
break;
default:
break;
}
}
void getInput(string path){
DIR *dr;
struct dirent *en;
dr = opendir(path.c_str());
if (dr) {
while ((en = readdir(dr)) != NULL) {
string filename(en->d_name);
cout << "Reading File: " << filename << endl;
ifstream fin;
fin.open(path+'/'+filename);
// Initialize parse
enum states current_state = initial_state;
enum states previous_state = initial_state;
// Building Transaction
Transaction new_transaction;
string line;
while(getline(fin, line)){
while(not line.empty() && current_state != read_line_state ){
if(current_state == after_read_line_state){
current_state = read_line_state;
}
parseReport(line, current_state, previous_state, new_transaction);
if(current_state == new_file_state)
goto endfile;
}
if(current_state == read_line_state)
current_state = after_read_line_state;
}
endfile:
fin.close();
}
closedir(dr);
} else {
cout << "No reports founded." << endl;
}
}
bool sortbyarn(Transaction &A, Transaction &B){
return A.ARN < B.ARN;
}
void outputSolutions(vector<Transaction> transactions, string outputFile){
sort(transactions.begin(), transactions.end(), sortbyarn);
ofstream myfile;
myfile.open(outputFile);
if(transactions.empty()){
myfile << "There is no solution." << endl;
return ;
}
if(count_different_than_27){
cout << "Warning" << endl;
cout << count_different_than_27 << " different than BR 27." <<endl;
}
myfile << "\"FILE_ID\", \"ARN\", \"Julian Date Time\", \"SettlementType\"" << endl;
for(int i=0; i < transactions.size(); i++){
transactions[i].print(myfile);
}
myfile.close();
}
int main(int argc, char *argv[]){
string path = "./reports";
string outputFile = "output.csv";
getInput(path);
outputSolutions(allTransactions, outputFile);
return 0;
}