Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.net.URL;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import com.opencsv.CSVReader;
  9. import java.net.URISyntaxException;
  10. import java.util.*;
  11. import javax.swing.SpinnerNumberModel;
  12.  
  13. public class AirbnbDataLoader {
  14.  
  15. private HashMap<String, Integer> houseNumbers = new HashMap<String, Integer>();
  16. private ArrayList<AirbnbListing> listings;
  17. private int from;
  18. private int to;
  19.  
  20. public AirbnbDataLoader(int fromValue, int toValue)
  21. {
  22. fillMap();
  23. listings = load();
  24. from = fromValue;
  25. to = toValue;
  26.  
  27. }
  28.  
  29. /**
  30. * Return an ArrayList containing the rows in the AirBnB London data set csv file.
  31. */
  32. public ArrayList<AirbnbListing> load() {
  33. System.out.print("Begin loading Airbnb london dataset...");
  34. listings = new ArrayList<AirbnbListing>();
  35. try{
  36. URL url = getClass().getResource("airbnb-london.csv");
  37. CSVReader reader = new CSVReader(new FileReader(new File(url.toURI()).getAbsolutePath()));
  38. String [] line;
  39. //skip the first row (column headers)
  40. reader.readNext();
  41. while ((line = reader.readNext()) != null) {
  42. String id = line[0];
  43. String name = line[1];
  44. String host_id = line[2];
  45. String host_name = line[3];
  46. String neighbourhood = line[4];
  47. double latitude = convertDouble(line[5]);
  48. double longitude = convertDouble(line[6]);
  49. String room_type = line[7];
  50. int price = convertInt(line[8]);
  51. int minimumNights = convertInt(line[9]);
  52. int numberOfReviews = convertInt(line[10]);
  53. String lastReview = line[11];
  54. double reviewsPerMonth = convertDouble(line[12]);
  55. int calculatedHostListingsCount = convertInt(line[13]);
  56. int availability365 = convertInt(line[14]);
  57.  
  58. AirbnbListing listing = new AirbnbListing(id, name, host_id,
  59. host_name, neighbourhood, latitude, longitude, room_type,
  60. price, minimumNights, numberOfReviews, lastReview,
  61. reviewsPerMonth, calculatedHostListingsCount, availability365
  62. );
  63. listings.add(listing);
  64. }
  65. } catch(IOException | URISyntaxException e){
  66. System.out.println("Failure! Something went wrong");
  67. e.printStackTrace();
  68. }
  69. System.out.println("Success! Number of loaded records: " + listings.size());
  70. return listings;
  71. }
  72.  
  73. /**
  74. *
  75. * @param doubleString the string to be converted to Double type
  76. * @return the Double value of the string, or -1.0 if the string is
  77. * either empty or just whitespace
  78. */
  79. private Double convertDouble(String doubleString){
  80. if(doubleString != null && !doubleString.trim().equals("")){
  81. return Double.parseDouble(doubleString);
  82. }
  83. return -1.0;
  84. }
  85.  
  86. /**
  87. *
  88. * @param intString the string to be converted to Integer type
  89. * @return the Integer value of the string, or -1 if the string is
  90. * either empty or just whitespace
  91. */
  92. private Integer convertInt(String intString){
  93. if(intString != null && !intString.trim().equals("")){
  94. return Integer.parseInt(intString);
  95. }
  96. return -1;
  97. }
  98.  
  99. private void fillMap()
  100. {
  101. houseNumbers.put("Kingston upon Thames",0);
  102. houseNumbers.put("Croydon",0);
  103. houseNumbers.put("Bromley",0);
  104. houseNumbers.put("Hounslow",0);
  105. houseNumbers.put("Ealing",0);
  106. houseNumbers.put("Havering",0);
  107. houseNumbers.put("Hillingdon",0);
  108. houseNumbers.put("Harrow",0);
  109. houseNumbers.put("Brent",0);
  110. houseNumbers.put("Barnet",0);
  111. houseNumbers.put("Enfield",0);
  112. houseNumbers.put("Waltham Forest",0);
  113. houseNumbers.put("Redbridge",0);
  114. houseNumbers.put("Sutton",0);
  115. houseNumbers.put("Lambeth",0);
  116. houseNumbers.put("Southwark",0);
  117. houseNumbers.put("Redbridge",0);
  118. houseNumbers.put("Lewisham",0);
  119. houseNumbers.put("Bexley",0);
  120. houseNumbers.put("Richmond upon Thames",0);
  121. houseNumbers.put("Merton",0);
  122. houseNumbers.put("Wandsworth",0);
  123. houseNumbers.put("Hammersmith and Fulham",0);
  124. houseNumbers.put("Kensington and Chelsea",0);
  125. houseNumbers.put("City of London",0);
  126. houseNumbers.put("Westminster",0);
  127. houseNumbers.put("Tower Hamlets",0);
  128. houseNumbers.put("Greenwich",0);
  129. houseNumbers.put("Barking and Dagenham",0);
  130. houseNumbers.put("Newham",0);
  131. houseNumbers.put("Haringey",0);
  132. houseNumbers.put("Hackney",0);
  133. houseNumbers.put("Islington",0);
  134. houseNumbers.put("Camden",0);
  135. }
  136.  
  137. public HashMap<String, Integer> getHouseNumbersHashMap()
  138. {
  139.  
  140. for(AirbnbListing listing: listings)
  141. {
  142. if(listing.getPrice()>=from && listing.getPrice()<=to)
  143. {
  144. String neighbourhood= listing.getNeighbourhood();
  145. int value= houseNumbers.get(neighbourhood);
  146. houseNumbers.put(listing.getNeighbourhood(), value+1);
  147. }
  148.  
  149. }
  150.  
  151. return houseNumbers;
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement