We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 17
PropertyViewer 2021-Oct-38 21:14 Page 1
import java.awt.Desktop;
import java.net.URL;
Anport java.net URTSyntaxException;
import java.util.ArrayList; // Importing ArrayList
Ie
% This project implements a simple application. Properties from a fixed
% file can be displayed.
* This class creates the viewer and displays it as a GUI on the screen
% It is used to update the details stored in the GUI
* @author Michael Kélling, Josh Murphy and Krishna Prasanna Kumar (k21004839)
* @version 1.8
*/
public class PropertyViewer
4
al User Int
private PropertyViewerGUI gui; he Graphic
private Portfolio portfolio;
private Property currentProperty; // the property currently being
accessed by the GUI
private int index;
private ArrayList list0fPropertie:
objects of data type Property
ap t that can hold
[om
* The constructor creates a PropertyViewer and display its GUI on screen
* It also displays the details of the first property of index @ on the GUL
*/
public PropertyViewer()
4
index = 0;
ListOfProperties = new ArrayList<>();
gui = new PropertyViewerGUI(this) ;
portfolio = new Portfolio("airbnb-london.csv");
propertyGUI(); // Calls the GUI method which fi
first property on the GUT
,
in the details of the
[ae
% This method completes actions that are required by the constructor,
nextProperty and the previousProperty method
* It updates the property viewer GUI with the details of the current
property
*/
public void propertyGUI()
{
currentProperty = portfolio.getProperty(index) ;
‘gui. showProperty(currentProperty) ;
gui. showID(currentProperty) ;
gui. showFavourite(currentProperty) ;
List0fProperties.add(currentProperty); //Adds
current properPropertyViewer 2021-Oct-38 21:14 Page 2
+
ye
% This method increments the index by 1 and the GUI is updated accordingly
* It is called when the Next button is pressed.
*
public void nextProperty()
{
index ++
index %= portfolio.numberOfProperties(); // Uses the modulus function to
loop rounnd to 8 when all properties have been viewed
propertyGUI();
}
[ss
* This method decrements the index by 1 and the GUI is updated accordingly
* It is called when the Previous button is pressed
*/
public void previousProperty()
{
index = (indextportfolio.numberOfProperties()-1) %
portfolio.numberOfProperties(); // Decreases the index by 1 using the modulus
function
propertyGUI();
}
In
%* This method negates the current favourite boolean value of the property
* It then displays at the bottom of the screen if the current property is
favourited or not
*/
public void toggleFavourite()
4
currentProperty.toggleFavourite()
gui. showFavourite(currentProperty) ;
[om
* This method opens the system's default internet browser
* The Google maps page should show the current properties location on the map
using the methods declared in the Property class.
*
public void viewMap() throws Exception
{
double latitude = currentProperty.getLatitude();
double longitude = currentProperty.getLongitude() ;
URI uri = new URI("https://mw.google.com/maps/place/* + latitude + "," +
longitude) ;
java. awt Desktop .getDesktop() .browse(uri) ;PropertyViewer 2021-Oct-38 21:14 Page 3
+
Ins
* This method returns the number of properties viewed by using the ArrayLists
size method on the ArrayList that stores all properties viewed.
*/
public int getNumberOfPropertiesViewed()
{
return ListofProperties.size();
y
[oe
* This method returns the average price of the properties viewed so far
*
public int averagePropertyPrice()
{
int total = 0;
for (Property property : listofProperties){ // For loop
the ArrayList adding the price of each property
total += property.getPrice();
that goes t
+
return total/listOfProperties.size(); //Average calculated by divid
rice by the number of properties in the ArrayListPortfolio 2021-Oct-38 21:14 Page 1
import java.util.List;
import java.util.ArrayList;
‘import java.io.File;
import java.io. BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io. 10Exception;
import java.net .URL;
import java.util.ArrayList;
import java.util.Arrays;
import com.opencsv.CSVReader ;
import java.net.URISyntaxException;
I
% A portfolio is a collection of properties. It reads properties from a file on
disk,
% and it can be used to retrieve single properties.
% The file name to read from is passed in at construction.
* @author Michael Kélling and Josh Murphy
* @version 1.8
*/
public class Portfolio
4
private List properties;
[ae
* Constructor for objects of class Portfolio
*/
public Portfolio(String directoryNane)
4
properties = loadProperties();
}
[ss
* Return a property from this Portfolio.
*/
public Property getProperty(int propertyNumber)
4
return properties.get(propertyNumber) ;
}
[ss
* Return the number of Properties in this Portfolio.
*/
public int numberOfProperties()
4
+
return properties.size();
Io
* Return an ArrayList containing the rows in the Air8nB London data set csv
file
*Portfolio 2021-Oct-38 21:14 Page 2
System.out.print("Begin loading Airbnb london dataset...°);
ArrayList listings = new ArrayList();
try
URL url = getClass() .getResource(“airbnb-london.csv");
CSVReader reader = new CSVReader(new FileReader(new
File(url.toURT()).getAbsolutePath())) ;
string [] line;
kip the first row (
reader. readNext() ;
while ((1ine = reader.readNext()) !
String id = line[@);
String name = line[1];
String host_id = line[21;
String host_name = line[3
String neighbourhood = line[4];
double latitude = convertDouble(1ine[5]) ;
double longitude = convertDouble(1ine[6]) ;
String room-type = line[7];
Ant price = convertInt(1ine[8]
int minimunNights = convertInt(1ine[9]) ;
int availability365 = convertint(1ine[10]) ;
null) ¢
Property currentProperty = new Property(id, name, host_id,
host_nane,
neighbourhood, latitude, longitude, room_type, price,
minimumNights, availability365) ;
Listings.add(currentProperty) ;
}
} catch(IOException | URISyntaxException e) {
System.out .printIn("Failure! Something went wrong when loading the
property file”
e.printStackTrace() ;
»
System.out.print1n( "Success! Number of loaded records: * +
listings.size());
return listings;
}
In
+ @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 enpty or just whitespace
*/
private Double convertDouble(String doubleString) {
if(doubleString != null && !doubleString.trim() .equals(”")){
return Double.parseDouble(doubleSt ring) ;
}
return -1.0;
>
[se
* @param intString the string to be converted to Integer type
* @return the Integer value of the string, or -1 if the string isPortfolio 2021-Oct-38 21:14 Page 3
*/
private Integer convertInt(String intString)
if(antString = null && !intString. trim() .equals(“")){
| return Integer.parseInt(intString) ;
+
| }
,
returnPropertyViewerGUI
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax. swing.border.*
import java.io.File;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
ye
* PropertyViewerGUI provides the GUI for the project
+ and strings, and it listens to button clicks
2021-Oct-38 21:14 Page 1
It displays the property
% This class stores the methods required to create the GUI window for both the
Properties and when the statistics button is pressed
* @author Michael KOlling, David J Barnes, Josh Murphy and Krishna Prasanna
Kumar (21004839)
% Bversion 3.0
a
public class PropertyViewerGUI
{
// fields
private JFrame frame;
private JFrame statsFrame;
private JPanel propertyPanel;
private JPanel statsPanel;
private JLabel idLabel;
private JLabel favouriteLabel;
private JLabel noPropertiesViewed;
private JLabel averagePriceLabel;
private JTextField hostIDLabel;
private JTextField hostNameLabel;
Private JTextField neighbourhoodLabel ;
private JTextField roomTypeLabel;
private JTextField priceLabel;
private JTextField minNightsLabel;
private JTextArea descriptionLabel;
private Property currentProperty:
private PropertyViewer viewer;
private boolean fixedSize;
[ae
* Create a PropertyViewer and display its GUI on screen
+)
public PropertyViewerGUI(PropertyViewer viewer)
<
currentProperty = null;
this.viewer = viewe
fixedSize = false;
makeFrame() ;
this.setPropertyViewSize(490, 250);PropertyViewerGUI 2021-Oct-38 21:14 Page 2
for
y
[se
* Display a given property on the Property viewer GUI
*/
public void showProperty(Property property)
4
hostIDLabel.setText(property.getHostID());
hostNameLabel .setText (property .getHostName());
neaghbourhoodl abel. setText (property .getNesghbourhood()) ;
roonTypeLabel..setText (property .getRoonType());
pricelabel.setText("€” + property.getPrice());
minNightsLabel.setText(property.getMinNights());
descriptionLabel .setText (property getDescrip
,
fax
+ Set a fixed size for the property display. If set, this size will be used
all properties
* If not set, the GUI will resize for each property
+
public void setPropertyViewSize(int width, int height)
{
propertyPanel.setPreferredSize(new Dimension(width, height));
frame.pack();
fixedSize = true;
,
[xe
* Show a message in the status bar at the bottom of the screen.
+
public void showFavourite(Property property)
{
String favouriteText =" ";
if (property.isFavourite()){
favouriteText += "This is one of your favourite properties!
}
favouriteLabel .setText(favouriteText) ;
}
In
* Show the ID in the top of the screen.
+
public void showID(Property property) (
idLabel.setText("Current Property ID:” + property.getI0());
)
f button functions ----
implenentat:
[ax
* Called when the ‘Next’ button was clickedPropertyViewerGUI 2021-Oct-38 21:14 Page 3
*/
private void nextButton()
{
,
viewer .nextProperty();
[xe
* Called when the ‘Previous’ button was clicked.
+
private void previousButton()
{
,
viewer. previousProperty() ;
foe
* Called when the ‘View on Map’ button was clicked.
+
private void view0nMapsButton()
{
try{
viewer. viewMap() ;
>
catch(Exception e){
System.out.print1n("URL INVALID");
»
}
[ss
* Called when the ‘Toggle Favourite’ button was clicked
*/
private void toggleFavouriteButton(){
viewer . toggleFavourite() ;
)
Jn
* Called when the ‘Statistics’ button is pressed.
*/
private void statButton()
{
statsGUI(); // Calls the internal method
)
/ swing stuff to build the frame and all its conponents
Jn
+ Create the Swing frame and its content. Added the Statistics Button
*/
private void makeFrame()
{
frame = new JFrame("Portfolio Viewer Application");
JPanel contentPane = (JPanel)frame .getContentPane() ;
contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));PropertyViewerGUI 2021-Oct-38 21:14 Page 4
fy the layout manager with nice spacing
contentPane. setLayout(new BorderLayout(6, 6));
Create the pr
propertyPanel = new JPanel();
propertyPanel.setLayout(new GridLayout (6,2);
y pane in the center
propertyPanel.add(new JLabel("HostID: "));
hostIDLabel = new JTextField("default”);
host IDLabel. setEditable( false) ;
propertyPanel.add(hostIDLabel) ;
propertyPanel.add(new JLabel("Host Name:
hostNameLabel = new JTextField(“default");
hostNameLabel.setEditable( false);
propertyPanel.add(hostNameLabel) ;
Di
propertyPanel.add(new JLabel( "Neighbourhood: "));
neighbourhoodLabel = new JTextField("default");
neighbourhoodLabel. setEditable(false) ;
propertyPanel .add(neighbourhoodLabel) ;
PropertyPanel.add(new JLabel("Room typi
roomTypeLabel = new JTextField("default");
roomlypeLabel .setEditable( false) ;
propertyPanel .add(roomTypel.abel) ;
propertyPanel.add(new JLabel("Price: "));
priceLabel = new JTextField("default");
priceLabel.setEditable( false) ;
propertyPanel..add(priceLabel) ;
propertyPanel.add(new JLabel("Minimum nights: "));
minNightsLabel = new JTextField("default");
minNightsLabel.setéditable(false) ;
PropertyPanel.add(minNightsLabel) ;
propertyPanel.setBorder(new EtchedBorder());
contentPane.add(propertyPanel, BorderLayout .CENTER) ;
idLabel = new JLabel("default");
contentPane.add(idLabel, BorderLayout NORTH
favouriteLabel = new JLabel(" °);
contentPane.add(favouriteLabel, BorderLayout.SOUTH) ;
Create bar with the butt
JPanel toolbar = new JPanel() ;
toolbar .setLayout(new GridLayout(8, 1));
JButton nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() {PropertyViewerGUI
[se
previousButton()
2021-Oct-38 21:14
public void actionPerformed(ActionEvent
nextButton(); }
:
toolbar .add(nextButton) ;
JButton previousButton = new JButton("Previous");
PreviousButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent
)
:
toolbar .add(previousButton) ;
Button mapButton = new JButton("View Property on Map");
mapButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent
‘viewOnMepsButton(); }
Yn:
toolbar .add(mapButton) ;
JButton favouriteButton = new JButton( "Toggle Favourite");
favouriteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent
toggleFavouriteButton(); }
:
toolbar .add( favouriteButton) ;
Button statButton = new JButton("Statistics"); // Adds the
statButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent
statButton(); }
Wi
toolbar .add(statButton) ;
panel with flow layout for spacing
JPanel flow = new JPanel();
flow.add( toolbar) ;
contentPane.add(flow, BorderLayout .WEST) ;
building is done - arrange the component
frame.pack()
place the frame at ti show
Dimension d = Toolkit.getDefaultToolkit() .getScreenSize();
center of the scr
frame.setLocation(d.width/2 - frame.getWidth()/2, d-height/2 -
frame.getHeight()/2);
frame.setVisible(true) ;
Page 5
e)¢
tisites
e){PropertyViewerGUI 2021-Oct-38 21:14 Page 6
% This method creates the window that is displayed when the ‘Statistics’
button is pressed.
% It will display the number of properties viewed and the average property
price of the properties that have been viewed.
+
public void statsGUI()
{
statsFrame = new JFrame(“Statistics");
JPanel statsPane = (JPanel)statsFrame.getContentPane(
statsPane.setBorder(new EmptyBorder(6, 6, 6, 6)):
noPropertiesViewed = new JLabel("Number of properties viewed: * +
viewer .getNumber0fPropertiesViewed()); ates a JLabel which will display the
lumber of properties viewed
averagePriceLabel = new JLabel("Average Property Price: £" +
viewer .averagePropertyPrice()); //Creates a JLabel which will display the
average prope
ty price of the properties that have been viewed
+ Two Lir
statsPane.add(noPropertiesViewed, BorderLayout NORTH) ;
statsPane.add(averagePriceLabel, BorderLayout SOUTH) ;
s below positions the Labels on the new window
statsFrame.pack();
Dimension d = Toolkit.getDefaultToolkit() .getScreensize() ;
statsFrame.setLocation(d.width/2 - statsFrame.getWidth()/2, d.height/2 -
statsFrame.getHeight()/2); // Positions the stats window at the centre of the
statsFrame.setVisible( true) ;
}Property 2021-Oct-38 21:14
import java.awt.*;
import java.awt.image.«;
import javax.swing.;
import javax.imageio.+;
import java.io.*;
ye
* Property is a class that defines a property for display.
% @author Michael Kélling and Josh Murphy
* aversion 2.8
a
public class Property
{
private String id;
private String description;
private String hostID;
private String hostNane;
private String neighbourhood;
private double latitude;
private double longitude;
private String roomType;
private int price;
private int minimumNights
private int availability36:
private boolean isFavourit
[ss
* Create a new property with specified initial values.
*/
roomType,
int price, int minimumNights, int availability365){
this.id = id;
this.description = name
this.hostID = hostID;
this.hostName = hostName;
this.neighbourhood = neighbourhood;
this.latitude = latitude;
this.longitude = longitude;
this.roomType = roonType;
this.price = price;
this.minimunNights = minimumNights;
this.availability365 = availability365;
isFavourite = false;
Iss
* Return the Id of this property
*/
public String getz0(){
| return ids
Page 1
public Property(String id, String name, String hostID, String hostName,
String neighbourhood, double latitude, double longitude, StringProperty 2021-Oct-38 21:14 Page 2
+
[oe
+ Return the hostId of this property
*/
public String getHost1D(){
return hostID;
y
[ox
* Return the latitude of this property
*
public double getLatitude(){
return latitude;
,
In
% Return the longitude of this property.
+
public double getLongitude(){
return longitude;
,
[se
* Return the price of this property
*/
public int getPrice(){
return price;
d
[ae
* Returns true if this property is currently marked as a favourite, false
otherwise.
+
public boolean isFavourite(){
return isFavourite;
+
[ns
* Return the host name of this property.
+!
public String getHostName(){
return hostName;
,
[oe
* Return the neighbourhood of this property
*/
public String getNeighbourhood(){
return neighbourhood;
y
[ox
* Return the room type of this propertyProperty 2021-Oct-38 21:14 Page 3
*/
public String getRoonType(){
return roomtype;
}
[se
* Return the minimum number of nights this property can be booked for.
*/
public String getMinNights(){
return "" + minimunNights;
d
[ae
* Return the description of this property
+)
public String getDescription(){
return description;
,
[xe
* Toggles whether this property is marked as a favourite or not
*
public void toggleFavourite()
{
isFavourite = !isFavourite;
y