-
Notifications
You must be signed in to change notification settings - Fork 0
/
AirbnbDataLoader.java
executable file
·83 lines (77 loc) · 3.12 KB
/
AirbnbDataLoader.java
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
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;
public class AirbnbDataLoader {
/**
* Return an ArrayList containing the rows in the AirBnB London data set csv file.
*/
public ArrayList<AirbnbListing> load() {
System.out.print("Begin loading Airbnb london dataset...");
ArrayList<AirbnbListing> listings = new ArrayList<AirbnbListing>();
try{
URL url = getClass().getResource("airbnb-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 id = line[0];
String name = line[1];
String host_id = line[2];
String host_name = line[3];
String neighbourhood = line[4];
double latitude = convertDouble(line[5]);
double longitude = convertDouble(line[6]);
String room_type = line[7];
int price = convertInt(line[8]);
int minimumNights = convertInt(line[9]);
int numberOfReviews = convertInt(line[10]);
String lastReview = line[11];
double reviewsPerMonth = convertDouble(line[12]);
int calculatedHostListingsCount = convertInt(line[13]);
int availability365 = convertInt(line[14]);
AirbnbListing listing = new AirbnbListing(id, name, host_id,
host_name, neighbourhood, latitude, longitude, room_type,
price, minimumNights, numberOfReviews, lastReview,
reviewsPerMonth, calculatedHostListingsCount, availability365
);
listings.add(listing);
}
} catch(IOException | URISyntaxException e){
System.out.println("Failure! Something went wrong");
e.printStackTrace();
}
System.out.println("Success! Number of loaded records: " + listings.size());
return listings;
}
/**
*
* @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;
}
}