-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertiesWindow.java
140 lines (116 loc) · 5.49 KB
/
PropertiesWindow.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
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
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import java.util.*;
/*
This class stands for the window displaying the list of properties in a specific borough matching the price range selected by the customer.
Whenever a house icon is clicked , an instance of that class is created. The user can choose to sort the list by host name, review or price.
*/
public class PropertiesWindow extends Windows{
private ArrayList<AirbnbListing> matchingProperties; // the list of properties displayed in the window.
private JComboBox sort; // contains the buttons to sort the list.
private String borough; // the borough in which the displayed properties are located.
private JFrame propertiesFrame; // the window holding the propertiesPanel.
private JPanel propertiesPanel; // the panel holding the list of properties.
private JScrollPane sp; // a scroll pane that allows the user to easily scroll through the properties.
public PropertiesWindow(String borough)
{
this.borough = borough;
listOfProperties(WelcomeWindow.lowerPrice,WelcomeWindow.upperPrice);
buildInitialWindow();
}
// Stores ,in the matchingProperties list, all the properties located in the borough and matching the price range selected by the customer.
private void listOfProperties( int fromPrice, int toPrice) {
matchingProperties = new ArrayList<>();
for (AirbnbListing p : database) {
int price = p.getPrice();
String neighbourhood= p.getNeighbourhood();
if ((neighbourhood.equals(borough)) && (price <= toPrice) && (price >= fromPrice)) {
matchingProperties.add(p);
}
}
}
// Sorts the list called matching properties by host name.
private void sortByHostName()
{
if(sort.getSelectedItem().equals("Host name")) {
matchingProperties.sort(Comparator.comparing(AirbnbListing::getHost_name));
buildSortedWindow();
}
}
// Sorts the list called matching properties by number of reviews.
private void sortByNumberOfReviews()
{
if(sort.getSelectedItem().equals("Number of reviews")) {
matchingProperties.sort(Comparator.comparing(AirbnbListing::getNumberOfReviews));
buildSortedWindow();
}
}
// Sorts the list called matching properties by price.
private void sortByPrice()
{
if(sort.getSelectedItem().equals("Price")) {
matchingProperties.sort(Comparator.comparing(AirbnbListing::getPrice));
buildSortedWindow();
}
}
// Builds the window that appears when an house icon is clicked.
private void buildInitialWindow()
{
propertiesFrame = new JFrame(borough);
String[] sortingWays = {"Sort by" ,"Host name", "Number of reviews", "Price"};
sort = new JComboBox<>(sortingWays);
sort.addActionListener(e-> {sortByHostName(); sortByNumberOfReviews(); sortByPrice(); });
propertiesFrame.setPreferredSize(new Dimension(1200,800));
buildPanel();
sp = new JScrollPane(propertiesPanel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.getVerticalScrollBar().setUnitIncrement(20);
propertiesFrame.getContentPane().add(sp);
propertiesFrame.setVisible(true);
propertiesFrame.pack();
}
// Builds the window displaying the sorted list of properties.
private void buildSortedWindow()
{
propertiesFrame.remove(sp);
buildPanel();
sp = new JScrollPane(propertiesPanel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.getVerticalScrollBar().setUnitIncrement(20);
propertiesFrame.getContentPane().add(sp);
propertiesFrame.revalidate();
propertiesFrame.repaint();
}
// Builds the panel showing the matching properties list. This panel is contained within the main window.
private void buildPanel()
{
propertiesPanel = new JPanel(new GridLayout(matchingProperties.size()+1, 1, 0, 10));
propertiesPanel.setBackground(Color.WHITE);
propertiesPanel.add(sort);
int propertyNumber = 1;
for (AirbnbListing p : matchingProperties) {
int price = p.getPrice();
int reviews = p.getNumberOfReviews();
int nights = p.getMinimumNights();
String host = p.getHost_name();
JLabel label = new JLabel("Property " + propertyNumber + ": " + "Host Name : " + host + " " + "Price : " + price + " " + "Number of reviews : " + reviews + " " + "Minimhum number of nights : " + nights);
/*
Each element of the list is a property and is stored into the variable label.
Each time one of the properties is clicked the description of the clicked property is displayed in another window.
*/
label.addMouseListener(
new MouseAdapter()
{
public void mousePressed(MouseEvent mouseEvent)
{
JFrame descriptionFrame = new JFrame();
descriptionFrame.getContentPane().add(new JLabel(p.getName()));
descriptionFrame.setVisible(true);
descriptionFrame.pack();
}
});
propertiesPanel.add(label);
propertyNumber++;
}
}
}