- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVFile.java
More file actions
Latest commit
230 lines (203 loc) · 5.31 KB
/
Copy pathCSVFile.java
File metadata and controls
230 lines (203 loc) · 5.31 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
packageFileManagement;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Iterator;
publicclassCSVFile {
StringfileName;
booleanIsAsc = true;
publicCSVFile()
{}
/**
* Reads the CSV file and returns array of records.
*
* @param file String file path to import the CSV file.
*
* @return ArrayList<Record>
*
*/
publicArrayList<Record> CSVReadFile(Stringfile) throwsIOException{
ArrayList<Record> Records = newArrayList<Record>();
try(BufferedReaderin = newBufferedReader(newFileReader(file)))
{
Stringln;
booleanskip_first_line = true;
while( (ln = in.readLine()) !=null) {
//skip first line - column name.
if(skip_first_line)
{
skip_first_line = false;
continue;
}
Stringrow[] = ln.split(",");
if(row.length == 9) {
Records.add(newRecord(
Integer.parseInt(row[0]), row[1], row[2], row[3], row[4],
row[5], row[6], row[7], row[8]));
}
else {
Records.add(newRecord(
Integer.parseInt(row[0]), row[1], row[2], row[3], row[4],
row[5], row[6], row[7], row[8], row[9]));
}
}
returnRecords;
}
}
/**
* Sorts the array of records read.
*
* @param Array of records.
*
* @return N/A
*/
publicvoidsort(ArrayList<Record> Records)
{
Collections.sort(Records, newCompare(this.IsAsc));
}
/**
* Prints array of record read and Sorted.
*
* @param Array of record.
*
* @return N/A
*
*/
publicvoidprint(ArrayList<Record> Records)
{//iterate through record
for(Recordrec : Records){
System.out.println(rec.id +"\t" +
rec.category + "\t" +
rec.descript +"\t" +
rec.dayOfTheWeek +"\t" +
rec.date +"\t" +
rec.time +"\t" +
rec.pdDistrict +"\t" +
rec.resolution +"\t" +
rec.address
);
}
System.out.println();
}
/**
* Sets the sort ascending order.
*
* @param Boolean value.
*
* @return N/A
*
*/
publicvoidsetSortDirection(booleanb)
{
this.IsAsc = b;
}
/**
* Writes the array of records to the CSV file.
*
* @param Array of record and string path to export the results.
*
* @return N/A
*
*/
publicvoidsave(ArrayList<Record> Records, StringfilePath) throwsIOException{
try(BufferedWriterout = newBufferedWriter(newFileWriter(filePath)))
{
out.write("IncidntNum,Category,Descript,DayOfWeek,Date,Time,PdDistrict,Resolution,Address");
out.write("\n");
for(Recordrec : Records){
out.write(rec.id +"," +
rec.category + "," +
rec.descript +"," +
rec.dayOfTheWeek +"," +
rec.date +"," +
rec.time +"," +
rec.pdDistrict +"," +
rec.resolution +"," +
rec.address
);
out.write("\n");
}
out.close();
}
}
/**
* Searches for string value specified in array of record.
*
* @param Array of record and string value to search.
*
* @return ArrayList<Record>
*
*/
publicArrayList<Record> search(ArrayList<Record> Records, Stringsearch_catagory)
{
ArrayList<Record> searchRecords = newArrayList<Record>();
booleanrecordFound = false;
for(Recordrec : Records){
if(rec.category.equals(search_catagory))
{
// Create records to be written to the file.
recordFound = true;
searchRecords.add(rec);
}
}
if(recordFound)
{
System.out.println("Records found, file generated.");
System.out.println();
}
else
{
System.out.println("Record not found.");
}
returnsearchRecords;
}
/**
* Reads the transaction CSV file and appends the array of records to master file.
*
* @param Master file Array of records and transaction Array of records.
*
* @return ArrayList<Record>
*
*/
publicArrayList<Record> run_transaction(ArrayList<Record> Records_trans, ArrayList<Record> Records )
{
for(Recordrec : Records_trans){
if(rec.transaction.equals("insert")) {
//append the record
Records.add(rec);
}
if(rec.transaction.equals("update")) {
//update the record
Iterator<Record> i = Records.iterator();
while (i.hasNext()){
Recordrec_temp = i.next();
if(rec.id == rec_temp.id){
// this is the record update
rec_temp.category = rec.category;
rec_temp.descript = rec.descript;
rec_temp.dayOfTheWeek = rec.dayOfTheWeek;
rec_temp.date = rec.date;
rec_temp.time = rec.time;
rec_temp.pdDistrict = rec.pdDistrict;
rec_temp.resolution = rec.resolution;
rec_temp.address = rec.address;
}
}
}
if(rec.transaction.equals("delete")) {
Iterator<Record> i = Records.iterator();
while (i.hasNext()) {
Recordr = i.next();
if(rec.id == r.id){
i.remove();
}
}
}
}
returnRecords;
}
}