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
109 lines (98 loc) · 3.48 KB
/
CodingCompCSVUtil.java
File metadata and controls
109 lines (98 loc) · 3.48 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
package codingcompetition2019;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CodingCompCSVUtil {
public List<List<String>> readCSVFileByCountry(String fileName, String countryName) throws IOException {
Scanner sc = new Scanner(new File(fileName));
List<List<String>> fin = new ArrayList<List<String>>();
while(sc.hasNextLine()) {
String line = sc.nextLine();
String[] ar = line.split(",");
if(ar[0].equals(countryName)) {
ArrayList<String> temp = new ArrayList<String>();
temp.add(line);
fin.add(temp);
}
}
sc.close();
return fin;
}
public List<List<String>> readCSVFileWithHeaders(String fileName) throws IOException {
Scanner sc = new Scanner(new File(fileName));
List<List<String>> fin = new ArrayList<List<String>>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[]ar = line.split(",");
ArrayList<String> temp = new ArrayList<String>();
temp.add(line);
fin.add(temp);
}
sc.close();
return fin;
}
public List<List<String>> readCSVFileWithoutHeaders(String fileName) throws IOException {
Scanner sc = new Scanner(new File(fileName));
sc.nextLine();
List<List<String>> fin = new ArrayList<List<String>>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[]ar = line.split(",");
ArrayList<String> temp = new ArrayList<String>();
temp.add(line);
fin.add(temp);
}
sc.close();
return fin;
}
public DisasterDescription getMostImpactfulYear(List<List<String>> records) {
DisasterDescription dd = new DisasterDescription(records);
return dd;
}
public DisasterDescription getMostImpactfulYearByCategory(String category, List<List<String>> records) {
DisasterDescription dd = new DisasterDescription(records);
dd.setYearByCategory(category);
return dd;
}
public DisasterDescription getMostImpactfulDisasterByYear(String year, List<List<String>> records) {
DisasterDescription dd = new DisasterDescription(records);
dd.setDisasterByYear(year);
return dd;
}
public DisasterDescription getTotalReportedIncidentsByCategory(String category, List<List<String>> records) {
DisasterDescription dd = new DisasterDescription(records);
dd.addUpIncedentsByCategory(category);
return dd;
}
/**
* 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) {
int inc = 0;
for(int i = 0; i < records.size();i++) {
String[] ar = records.get(i).get(0).split(",");
System.out.println(ar[0] + " "+ ar[3] +" " + records.size());
if(max == -1) {
if(Integer.parseInt(ar[3]) >= min){
inc += 1;
}
}else {
if(Integer.parseInt(ar[3]) >= min && Integer.parseInt(ar[3])<=max){
inc += 1;
}
}
}
return inc;
}
public boolean firstRecordsHaveMoreReportedIndicents(List<List<String>> records1, List<List<String>> records2) {
DisasterDescription dd1 = new DisasterDescription(records1);
DisasterDescription dd2 = new DisasterDescription(records2);
return dd1.getReportedIncidentsNum() > dd2.getReportedIncidentsNum();
}
}