-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertiesModel.java
More file actions
428 lines (387 loc) · 15.2 KB
/
PropertiesModel.java
File metadata and controls
428 lines (387 loc) · 15.2 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import java.util.ArrayList;
import java.util.HashMap;
import java.text.DecimalFormat;
import java.util.Set;
import java.util.stream.Collectors;
/**
* This class involves storing all the properties listing of our data
*
* @author Kwan Yui Chiu (K21003778)
* @version 14-03-2022
*/
public class PropertiesModel
{
// constants for the model class
private static final String DEFAULT_FILE = "property-london.csv";
private static final DecimalFormat df = new DecimalFormat("0.00");
// the name of file loaded to this model currently
private String fileName;
private ArrayList<PropertyListing> properties;
// Various statistics
private Integer totalNumberOfHomesOrApart;
private Integer totalAvailable;
private Integer totalReviews;
private String mostExpensiveBoroughName;
private String boroughWithHighestListingCount;
private Integer maxMinNight;
private String hostWithMostListing;
private String listingIDWithLongestAvailablePeriod;
/**
* Constructor for the model class
*
* Will add parameter to allow future model choice
*/
public PropertiesModel()
{
setProperties(DEFAULT_FILE);
}
/**
* Constructor for the model class with option to start with a different file
*
* @param String fileName file intended to load
*/
public PropertiesModel(String fileName)
{
setProperties(fileName);
}
/**
* Returns the borough with the most listings
* @return total number of properties
*/
public String getBoroughWithHighestListingCount(){
if (boroughWithHighestListingCount == null){
HashMap<String,Integer> boroughCounters = new HashMap<>();
for (PropertyListing property: this.properties){
if (boroughCounters.get(property.getNeighbourhood()) == null){
boroughCounters.put(property.getNeighbourhood(),
property.getCalculatedHostListingsCount());
}
else{
int currentValue = boroughCounters.get(property.getNeighbourhood());
int newValue = currentValue + property.getCalculatedHostListingsCount();
boroughCounters.put(property.getNeighbourhood(),newValue);
}
}
int maximumCalculatedHostListingsCount = 0;
for (String borough:boroughCounters.keySet()){
if (boroughCounters.get(borough) > maximumCalculatedHostListingsCount){
boroughWithHighestListingCount = borough;
maximumCalculatedHostListingsCount = boroughCounters.get(borough);
}
}
}
return boroughWithHighestListingCount;
}
/**
* Returns the total number of properties in dataset
* @return total number of properties
*/
public int getTotalNumberOfProperties(){
return this.properties.size();
}
/**
* Returns the name of the file loaded
* @return the name of the file loaded
*/
public String getFileName(){
return this.fileName;
}
/**
* Returns the actual list of properties
* @return listings of properties from file loaded
*/
public ArrayList<PropertyListing> getProperties(){
return this.properties;
}
/**
* Load properties using fileName
*
* @param String fileName
*/
private void setProperties(String fileName){
this.fileName = fileName;
DataLoader dataloader = new PropertyDataLoader();
this.properties = dataloader.load(fileName);
}
/**
* Calculates the average number of reviews excluding errorneus data
*
* @return double for average number of reviews
*/
public String getAverageNumberReviews(){
if (totalReviews == null){
totalReviews = this.properties.stream()
.filter(p -> p.getReviewsPerMonth() > 0)
.map(p -> p.getNumberOfReviews())
.reduce(0, (total,review) -> total + review);
}
return df.format((double) totalReviews/this.properties.size());
}
/**
* Calculates the number of available properties in the coming year
*
* @return integer for number of available properties
*/
public int getNumberOfAvailableProperties(){
if (totalAvailable == null){
totalAvailable = this.properties.stream()
.filter(p -> p.getAvailability365() > 0)
.map(p -> 1)
.reduce(0,(total,ONE) -> total + ONE);
}
return totalAvailable;
}
/**
* Calculates the number of apartment/entire home in the coming year
*
* @return integer for number of homes or apartments
*/
public int getNumberOfHomesOrApart(){
if (totalNumberOfHomesOrApart == null){
totalNumberOfHomesOrApart = this.properties.stream()
.filter(p -> "Entire home/apt".equals(p.getRoom_type()))
.map(p -> 1)
.reduce(0,(accumulated,ONE) -> accumulated + ONE);
}
return totalNumberOfHomesOrApart;
}
/**
* Returns the maximum minimum night from all listing
*
* @return integer of nights
*/
private int getMaximumMinimumNight(){
if (maxMinNight == null){
maxMinNight = 0;
for (PropertyListing property:this.properties){
if (maxMinNight < property.getMinimumNights())
maxMinNight = property.getMinimumNights();
}
}
return maxMinNight;
}
/**
* Returns a list of properties within specific price range
* @param int lower bound of price
* @param int upper bound of price
* @return the list of property listings in range
*/
public ArrayList<PropertyListing> getPropertiesWithinSpecificRange(String Borough,int lowerBound,int upperBound){
ArrayList<PropertyListing> propertiesInRange = new ArrayList<>();
for (PropertyListing property: this.properties){
int priceOfProperty = property.getPrice() * property.getMinimumNights();
if (priceIsInRange(priceOfProperty, lowerBound, upperBound) && property.getNeighbourhood().equals(Borough)){
propertiesInRange.add(property);
}
}
return propertiesInRange;
}
/**
* Returns the name of the most expensive borough
*
* @return name of the most expensive borough
*/
public String getMostExpensiveBorough(){
if (mostExpensiveBoroughName == null){
int Max_minNight = getMaximumMinimumNight();
HashMap<String,Long> boroughCounters = new HashMap<>();
for (PropertyListing property: this.properties){
long priceOfProperty = (long) property.getPrice() * Max_minNight;
if (boroughCounters.get(property.getNeighbourhood()) == null){
boroughCounters.put(property.getNeighbourhood(),priceOfProperty);
}
else{
long currentValue = boroughCounters.get(property.getNeighbourhood());
long newValue = currentValue + priceOfProperty;
boroughCounters.put(property.getNeighbourhood(),newValue);
}
}
long maximumTotalCost = 0;
for (String borough:boroughCounters.keySet()){
if (boroughCounters.get(borough) > maximumTotalCost){
mostExpensiveBoroughName = borough;
maximumTotalCost = boroughCounters.get(borough);
}
}
}
return mostExpensiveBoroughName;
}
/**
* @return boolean whether the price inputted is within range
*/
private boolean priceIsInRange(int price, int lowerBound, int upperBound){
return lowerBound <= price && price <= upperBound;
}
/**
* @return the list of borough names
*/
private Set<String> boroughNames(){
return this.properties.stream()
.map(property -> property.getNeighbourhood())
.collect(Collectors.toSet());
}
/**
* Return number of properties per borough with price per night filter with minimum and maximum price per night
*
* @param int lowerBound for price per night
* @param int upperBound for price per night
* @return a dictionary with borough name and their relative number of properties
*/
public HashMap<String,Integer> noPropertiesPerBorough(int lowerBoundPrice,int upperBoundPrice){
HashMap<String,Integer> boroughCounters = new HashMap<>();
boroughNames().forEach(borough -> boroughCounters.put(borough,0));
for (PropertyListing property: this.properties){
int propertyPrice = property.getPrice() * property.getMinimumNights();
if (priceIsInRange(propertyPrice,lowerBoundPrice,upperBoundPrice)){
int currentValue = boroughCounters.get(property.getNeighbourhood());
int newValue = currentValue + 1;
boroughCounters.put(property.getNeighbourhood(),newValue);
}
}
return boroughCounters;
}
/**
* Return number of properties per borough with price per night filter with minimum price per night
*
* @param int lowerBound for price per night
* @return a dictionary with borough name and their relative number of properties
*/
public HashMap<String,Integer> noPropertiesPerBorough(int lowerBoundPrice){
return noPropertiesPerBorough(lowerBoundPrice,Integer.MAX_VALUE);
}
/**
* Return number of properties per borough with price per night filter
*
* @return a dictionary with borough name and their relative number of properties
*/
public HashMap<String,Integer> noPropertiesPerBorough(){
return noPropertiesPerBorough(0,Integer.MAX_VALUE);
}
/**
* Price for the cheapest property from ALL properties with minimum and maximum price per night
*
* @param int lowerBound for price per night
* @param int upperBound for price per night
* @return the cheapest property pirce base on criteria
*/
public int minPricePerNight(int lowerBound,int upperBound){
int minPricePerNight = Integer.MAX_VALUE;
for (PropertyListing property: this.properties){
int priceOfProperty = property.getPrice() * property.getMinimumNights();
if (priceOfProperty < minPricePerNight && priceIsInRange(priceOfProperty,lowerBound,upperBound)){
minPricePerNight = priceOfProperty;
}
}
return minPricePerNight;
}
/**
* Price for the cheapest property from ALL properties with a minimum price per night
*
* @param int lowerBound for price per night
* @return the cheapest property price base on criteria
*/
public int minPricePerNight(int lowerBound){
return minPricePerNight(lowerBound,Integer.MAX_VALUE);
}
/**
* Price for the cheapest property from ALL properties with a price filter
*
* @return the cheapest property price
*/
public int minPricePerNight(){
return minPricePerNight(0,Integer.MAX_VALUE);
}
/**
* Price for the most expensive property from ALL properties with minimum and maximum price per night
*
* @param int lowerBound for price per night
* @param int upperBound for price per night
* @return most expensive property price from criteria
*/
public int maxPricePerNight(int lowerBound, int upperBound){
int maxPricePerNight = 0;
for (PropertyListing property: this.properties){
int priceOfProperty = property.getPrice() * property.getMinimumNights();
if (priceOfProperty > maxPricePerNight && priceIsInRange(priceOfProperty,lowerBound,upperBound)){
maxPricePerNight = priceOfProperty;
}
}
return maxPricePerNight;
}
/**
* Price for the most expensive property from ALL properties with a minimum price per night
* @param int lowerBound for price per night
* @return most expensive property price from criteria
*/
public int maxPricePerNight(int lowerBound){
return maxPricePerNight(lowerBound,Integer.MAX_VALUE);
}
/**
* Price for the most expensive property from ALL properties
* @return most expensive property price
*/
public int maxPricePerNight(){
return maxPricePerNight(0,Integer.MAX_VALUE);
}
/**
* Returns the name of host with specific hostID
*
*@param String corresponding hostID
*@return name of host with the input hostID
*/
private String findHostName(String hostID){
for (PropertyListing property:this.properties){
if (hostID.equals(property.getHost_id())){
return property.getHost_name();
}
}
return "Not Found";
}
/**
* Find the host with the most listings in the dataset
*
* @return Name of the host with most listings
*/
public String getHostWithMostListing(){
if(hostWithMostListing == null){
HashMap<String,Integer> hostCounters = new HashMap<>();
for(PropertyListing property: this.properties){
if (hostCounters.get(property.getHost_id()) == null){
hostCounters.put(property.getHost_id(),1);
}
else{
int currentValue = hostCounters.get(property.getHost_id());
currentValue++;
hostCounters.put(property.getHost_id(),currentValue);
}
}
int maximum = 0;
String maxHostID = "";
for (String hostID:hostCounters.keySet()){
if (hostCounters.get(hostID) > maximum){
maxHostID = hostID;
maximum = hostCounters.get(hostID);
}
}
hostWithMostListing = findHostName(maxHostID);
}
return hostWithMostListing;
}
/**
* Method for finding the listing ID with the most available period
* @return the listing ID With longest available period
*/
public String getListingIDOfPropertyWithLongestAvailablePeriod(){
if (listingIDWithLongestAvailablePeriod == null){
int longestAvailable365 = 0;
listingIDWithLongestAvailablePeriod = "";
for (PropertyListing property: this.properties){
if (property.getAvailability365() > longestAvailable365){
longestAvailable365 = property.getAvailability365();
listingIDWithLongestAvailablePeriod = property.getId();
}
}
}
return listingIDWithLongestAvailablePeriod;
}
}