This repository was archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCodingCompCSVUtil.java
More file actions
263 lines (225 loc) · 10.3 KB
/
CodingCompCSVUtil.java
File metadata and controls
263 lines (225 loc) · 10.3 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
package codingcompetition2019;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class CodingCompCSVUtil {
/**
* This method reads the CSV file and returns the data about the specified country
* Each row has a data about the disaster for the a year separated by comma.
* Store the data separated by comma in a list if the name of the country matches.
* Store all the list with the matching country name in a list and return it.
*
* @param fileName filePath of the CSV file with data
* @param countryName name of the country to filter the records by
* @return Data about the disaster for that country
* @throws IOException error on reading the file
*/
public List<List<String>> readCSVFileByCountry(String fileName, String countryName) throws IOException {
List<List<String>> countries = readCSVFileWithoutHeaders(fileName);
// Filter the csv values by country
List<List<String>> countryEntries = new ArrayList<List<String>>();
for (List<String> entry : countries) {
// Check if the name of the country matches
if (entry.get(0).trim().toLowerCase().equals(countryName.trim().toLowerCase())) {
countryEntries.add(entry);
}
}
return countryEntries;
}
/**
* This method reads the CSV file including the header.
* Each line of the CSV has data about the disaster separated by comma. Store it in a list.
* Add that list to a master list and return the master list.
*
* @param fileName filePath of the CSV file with data
* @return list with a list of data related to the disaster
* @throws IOException error on reading the file
*/
public List<List<String>> readCSVFileWithHeaders(String fileName) throws IOException {
// Get all entries without the header
File inputFile = new File(fileName);
List<List<String>> csvEntriesWithHeader = new ArrayList<List<String>>();
if (inputFile.exists()) {
try {
Scanner input = new Scanner(inputFile);
// Read each line an add it to the list
while (input.hasNext()) {
String currentLine = input.nextLine();
// Split the line by comma and store it as array
List<String> values = Arrays.asList(currentLine.split(","));
csvEntriesWithHeader.add(values);
}
input.close();
} catch (IOException ex) {
throw ex;
} catch (Exception ex) {
// Other unexpected error occurred
ex.printStackTrace();
return null;
}
}
return csvEntriesWithHeader;
}
/**
* This method reads the CSV file without the header.
* Each line of the CSV has data about the disaster separated by comma. Store it in a list
* Add that list to a master list and return the master list.
*
* @param fileName filePath of the CSV file with data
* @return list with a list of data related to the disaster
* @throws IOException error on reading the file
*/
public List<List<String>> readCSVFileWithoutHeaders(String fileName) throws IOException {
// Get all records with the header
List<List<String>> allCSVEntries = readCSVFileWithHeaders(fileName);
//Remove the first entry of header
allCSVEntries.remove(0);
return allCSVEntries;
}
/**
* This method iterates through the records list and checks the number of reported incidents for each record
* It finds the year with the most number of reported incidents
*
* @param records list of data related to a disaster event
* @return info about the most impactful Disaster based on year
*/
public DisasterDescription getMostImpactfulYear(List<List<String>> records) {
// See if there is at least one record
if (records.size() > 0) {
//Assume the first entry is the most impactful
int year = Integer.parseInt(records.get(0).get(2));
int maxImpact = Integer.parseInt(records.get(0).get(3));
String entity = records.get(0).get(0);
//Iterate through each entry and get the find the most impact
for (List<String> entry : records) {
int currentImpact = Integer.parseInt(entry.get(3));
if (currentImpact > maxImpact) {
maxImpact = currentImpact;
year = Integer.parseInt(entry.get(2));
entity = entry.get(0);
}
}
DisasterDescription disaster = new DisasterDescription();
disaster.setYear(year);
disaster.setReportedIncidentsNum(maxImpact);
disaster.setCategory(entity);
return disaster;
}
return new DisasterDescription();
}
/**
* This method filters the records by the given category.
* It calls getMostImpactfulYear() method to find the most impactful year after filtering the list.
* Then returns the info about the most impactful disaster for that given category.
*
* @param category Category of disaster
* @param records List of data about the disaster
* @return Info about the most impactful disaster for the given category
*/
public DisasterDescription getMostImpactfulYearByCategory(String category, List<List<String>> records) {
//Filter the list by category
List<List<String>> recordByCategory = new LinkedList<List<String>>();
for (List<String> record : records) {
// Check if the category matches
if (record.get(0).toLowerCase().trim().equals(category.toLowerCase().trim())) {
recordByCategory.add(record);
}
}
return getMostImpactfulYear(recordByCategory);
}
/**
* This method filters the list by the given year.
* It calls getMostImpactfulYear() method to find the most impactful year after filtering the list.
* Then it returns info about the most impactful disaster for that given year.
*
* @param year Filter the record based on this year
* @param records List of data about the disaster
* @return Info about the most impactful disaster for the given year
*/
public DisasterDescription getMostImpactfulDisasterByYear(String year, List<List<String>> records) {
// Filter the record by year
List<List<String>> recordsByYear = new LinkedList<List<String>>();
for (List<String> record : records) {
if (record.get(2).trim().toLowerCase().equals(year.trim().toLowerCase())) {
// Ignore for all natural disasters
if (!record.get(0).trim().toLowerCase().equals("all natural disasters")) {
recordsByYear.add(record);
}
}
}
return getMostImpactfulYear(recordsByYear);
}
/**
* This method iterates through the list of records
* For each record, if the category matches, it retrieves the number of reported incidents
* Then it calculates the total number of reported incidents for that category.
*
* @param category Disaster category to filter the records
* @param records List of data about disaster
* @return Total number of reported incidents for the given category
*/
public DisasterDescription getTotalReportedIncidentsByCategory(String category, List<List<String>> records) {
int totalIncidents = 0;
for (List<String> record : records) {
// Check if the category matches
if (record.get(0).toLowerCase().trim().equals(category.toLowerCase().trim())) {
totalIncidents += Integer.parseInt(record.get(3));
}
}
DisasterDescription disaster = new DisasterDescription();
disaster.setCategory(category);
disaster.setReportedIncidentsNum(totalIncidents);
return disaster;
}
/**
* This method will return the count if the number of incident falls within the provided range.
* To simplify the problem, we assume:
* + A value of -1 is provided if the max range is NOT applicable.
* + A min value can be provided, without providing a max value (which then has to be -1 like indicated above).
* + If a max value is provided, then a max value is also needed.
*/
public int countImpactfulYearsWithReportedIncidentsWithinRange(List<List<String>> records, int min, int max) {
boolean hasMaximumRange = max != -1;
int count = 0;
for (List<String> record : records) {
int reportedIncident = Integer.parseInt(record.get(3));
if (hasMaximumRange) {
if (reportedIncident >= min && reportedIncident <= max) {
count++;
}
} else { // No maximum range
if (reportedIncident >= min) {
count++;
}
}
}
return count;
}
/**
* This method checks if the first list has more reported incidents than the second list.
* It calls getTotalReportedIncidents() to get the total number of reported incidents for both records
* It compares the total number of reported incidents for both records.
*
* @param records1 List of data about disaster
* @param records2 List of data about disaster
* @return Boolean value indicating record1 has more reported incidents than record2
*/
public boolean firstRecordsHaveMoreReportedIndicents(List<List<String>> records1, List<List<String>> records2) {
return getTotalReportedIncidents(records1) > getTotalReportedIncidents(records2);
}
/**
* This method iterates through the records
* For each record, it retrieves the number of reported incident.
* It calculates the total reported incidents for that given data.
*
* @param records List of data about the disaster
* @return int total reported incidents
*/
private int getTotalReportedIncidents(List<List<String>> records) {
int total = 0;
for (List<String> entry : records) {
total += Integer.parseInt(entry.get(3));
}
return total;
}
}