Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- import java.util.*;
- import javax.swing.SpinnerNumberModel;
- public class AirbnbDataLoader {
- private HashMap<String, Integer> houseNumbers = new HashMap<String, Integer>();
- private ArrayList<AirbnbListing> listings;
- private int from;
- private int to;
- public AirbnbDataLoader(int fromValue, int toValue)
- {
- fillMap();
- listings = load();
- from = fromValue;
- to = toValue;
- }
- /**
- * 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...");
- 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;
- }
- private void fillMap()
- {
- houseNumbers.put("Kingston upon Thames",0);
- houseNumbers.put("Croydon",0);
- houseNumbers.put("Bromley",0);
- houseNumbers.put("Hounslow",0);
- houseNumbers.put("Ealing",0);
- houseNumbers.put("Havering",0);
- houseNumbers.put("Hillingdon",0);
- houseNumbers.put("Harrow",0);
- houseNumbers.put("Brent",0);
- houseNumbers.put("Barnet",0);
- houseNumbers.put("Enfield",0);
- houseNumbers.put("Waltham Forest",0);
- houseNumbers.put("Redbridge",0);
- houseNumbers.put("Sutton",0);
- houseNumbers.put("Lambeth",0);
- houseNumbers.put("Southwark",0);
- houseNumbers.put("Redbridge",0);
- houseNumbers.put("Lewisham",0);
- houseNumbers.put("Bexley",0);
- houseNumbers.put("Richmond upon Thames",0);
- houseNumbers.put("Merton",0);
- houseNumbers.put("Wandsworth",0);
- houseNumbers.put("Hammersmith and Fulham",0);
- houseNumbers.put("Kensington and Chelsea",0);
- houseNumbers.put("City of London",0);
- houseNumbers.put("Westminster",0);
- houseNumbers.put("Tower Hamlets",0);
- houseNumbers.put("Greenwich",0);
- houseNumbers.put("Barking and Dagenham",0);
- houseNumbers.put("Newham",0);
- houseNumbers.put("Haringey",0);
- houseNumbers.put("Hackney",0);
- houseNumbers.put("Islington",0);
- houseNumbers.put("Camden",0);
- }
- public HashMap<String, Integer> getHouseNumbersHashMap()
- {
- for(AirbnbListing listing: listings)
- {
- if(listing.getPrice()>=from && listing.getPrice()<=to)
- {
- String neighbourhood= listing.getNeighbourhood();
- int value= houseNumbers.get(neighbourhood);
- houseNumbers.put(listing.getNeighbourhood(), value+1);
- }
- }
- return houseNumbers;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement