-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCovidDataLoader.java
More file actions
84 lines (75 loc) · 2.89 KB
/
CovidDataLoader.java
File metadata and controls
84 lines (75 loc) · 2.89 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import com.opencsv.CSVReader;
import java.net.URISyntaxException;
/**
* This class reads the provided csv, and creates
* an arraylist of CovidData objects.
*
* @author Rayan Popat, James Coward and Shicheng Li.
*/
public class CovidDataLoader {
/**
* Return an ArrayList containing the rows in the Covid London data set csv file.
*/
public ArrayList<CovidData> load() {
ArrayList<CovidData> records = new ArrayList<CovidData>();
try{
URL url = getClass().getResource("covid_london.csv");
CSVReader reader = new CSVReader(new FileReader(new File(url.toURI()).getAbsolutePath()));
String [] line;
//skip the first row (column headers)
reader.readNext();
while ((line = reader.readNext()) != null) {
String date = line[0];
String borough = line[1];
int retailRecreationGMR = convertInt(line[2]);
int groceryPharmacyGMR = convertInt(line[3]);
int parksGMR = convertInt(line[4]);
int transitGMR = convertInt(line[5]);
int workplacesGMR = convertInt(line[6]);
int residentialGMR = convertInt(line[7]);
int newCases = convertInt(line[8]);
int totalCases = convertInt(line[9]);
int newDeaths=convertInt(line[10]);
int totalDeaths=convertInt(line[11]);
CovidData record = new CovidData(date,borough,retailRecreationGMR,
groceryPharmacyGMR,parksGMR,transitGMR,workplacesGMR,
residentialGMR,newCases,totalCases,newDeaths,totalDeaths);
records.add(record);
}
} catch(IOException | URISyntaxException e){
e.printStackTrace();
}
return records;
}
/**
*
* @param doubleString the string to be converted to Double type
* @return the Double value of the string, or -1.0 if the string is
* either empty or just whitespace
*/
private Double convertDouble(String doubleString){
if(doubleString != null && !doubleString.trim().equals("")){
return Double.parseDouble(doubleString);
}
return -1.0;
}
/**
*
* @param intString the string to be converted to Integer type
* @return the Integer value of the string, or -1 if the string is
* either empty or just whitespace
*/
private Integer convertInt(String intString){
if(intString != null && !intString.trim().equals("")){
return Integer.parseInt(intString);
}
return -1;
}
}