-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVFile.java
More file actions
230 lines (203 loc) · 5.31 KB
/
CSVFile.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
package FileManagement;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class CSVFile {
String fileName;
boolean IsAsc = true;
public CSVFile()
{}
/**
* Reads the CSV file and returns array of records.
*
* @param file String file path to import the CSV file.
*
* @return ArrayList<Record>
*
*/
public ArrayList<Record> CSVReadFile(String file) throws IOException{
ArrayList<Record> Records = new ArrayList<Record>();
try(BufferedReader in = new BufferedReader(new FileReader(file)))
{
String ln;
boolean skip_first_line = true;
while( (ln = in.readLine()) !=null) {
//skip first line - column name.
if(skip_first_line)
{
skip_first_line = false;
continue;
}
String row[] = ln.split(",");
if(row.length == 9) {
Records.add(new Record(
Integer.parseInt(row[0]), row[1], row[2], row[3], row[4],
row[5], row[6], row[7], row[8]));
}
else {
Records.add(new Record(
Integer.parseInt(row[0]), row[1], row[2], row[3], row[4],
row[5], row[6], row[7], row[8], row[9]));
}
}
return Records;
}
}
/**
* Sorts the array of records read.
*
* @param Array of records.
*
* @return N/A
*/
public void sort(ArrayList<Record> Records)
{
Collections.sort(Records, new Compare(this.IsAsc));
}
/**
* Prints array of record read and Sorted.
*
* @param Array of record.
*
* @return N/A
*
*/
public void print(ArrayList<Record> Records)
{//iterate through record
for(Record rec : 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
*
*/
public void setSortDirection(boolean b)
{
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
*
*/
public void save(ArrayList<Record> Records, String filePath) throws IOException{
try(BufferedWriter out = new BufferedWriter(new FileWriter(filePath)))
{
out.write("IncidntNum,Category,Descript,DayOfWeek,Date,Time,PdDistrict,Resolution,Address");
out.write("\n");
for(Record rec : 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>
*
*/
public ArrayList<Record> search(ArrayList<Record> Records, String search_catagory)
{
ArrayList<Record> searchRecords = new ArrayList<Record>();
boolean recordFound = false;
for(Record rec : 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.");
}
return searchRecords;
}
/**
* 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>
*
*/
public ArrayList<Record> run_transaction(ArrayList<Record> Records_trans, ArrayList<Record> Records )
{
for(Record rec : 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()){
Record rec_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()) {
Record r = i.next();
if(rec.id == r.id){
i.remove();
}
}
}
}
return Records;
}
}