0% found this document useful (0 votes)
108 views17 pages

Assignment 1 PDF (K21004839)

Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
108 views17 pages

Assignment 1 PDF (K21004839)

Copyright
© © All Rights Reserved
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
PropertyViewer 2021-Oct-38 21:14 Page 1 import [Link]; import [Link]; Anport [Link] URTSyntaxException; import [Link]; // 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("[Link]"); 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 = [Link](index) ; ‘gui. showProperty(currentProperty) ; gui. showID(currentProperty) ; gui. showFavourite(currentProperty) ; [Link](currentProperty); //Adds current proper PropertyViewer 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 %= [Link](); // 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 = ([Link]()-1) % [Link](); // 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 [Link]() 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 = [Link](); double longitude = [Link]() ; URI uri = new URI("[Link] + 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 [Link](); 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 += [Link](); that goes t + return total/[Link](); //Average calculated by divid rice by the number of properties in the ArrayList Portfolio 2021-Oct-38 21:14 Page 1 import [Link]; import [Link]; ‘import [Link]; import [Link]. BufferedReader; import [Link]; import [Link]; import [Link]. 10Exception; import [Link] .URL; import [Link]; import [Link]; import [Link] ; import [Link]; 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 [Link](propertyNumber) ; } [ss * Return the number of Properties in this Portfolio. */ public int numberOfProperties() 4 + return [Link](); Io * Return an ArrayList containing the rows in the Air8nB London data set csv file * Portfolio 2021-Oct-38 21:14 Page 2 [Link]("Begin loading Airbnb london dataset...°); ArrayList listings = new ArrayList(); try URL url = getClass() .getResource(“[Link]"); CSVReader reader = new CSVReader(new FileReader(new File([Link]()).getAbsolutePath())) ; string [] line; kip the first row ( reader. readNext() ; while ((1ine = [Link]()) ! 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) ; [Link](currentProperty) ; } } catch(IOException | URISyntaxException e) { [Link] .printIn("Failure! Something went wrong when loading the property file” [Link]() ; » [Link].print1n( "Success! Number of loaded records: * + [Link]()); 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 && ![Link]() .equals(”")){ return [Link](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 is Portfolio 2021-Oct-38 21:14 Page 3 */ private Integer convertInt(String intString) if(antString = null && !intString. trim() .equals(“")){ | return [Link](intString) ; + | } , return PropertyViewerGUI import [Link].*; import [Link].*; import [Link].*; import javax. [Link].* import [Link]; import [Link]; import [Link]; import [Link]; 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; [Link] = viewe fixedSize = false; makeFrame() ; [Link](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 [Link]([Link]()); hostNameLabel .setText (property .getHostName()); neaghbourhoodl abel. setText (property .getNesghbourhood()) ; roonTypeLabel..setText (property .getRoonType()); [Link]("€” + [Link]()); [Link]([Link]()); 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) { [Link](new Dimension(width, height)); [Link](); fixedSize = true; , [xe * Show a message in the status bar at the bottom of the screen. + public void showFavourite(Property property) { String favouriteText =" "; if ([Link]()){ 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) ( [Link]("Current Property ID:” + property.getI0()); ) f button functions ---- implenentat: [ax * Called when the ‘Next’ button was clicked PropertyViewerGUI 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){ [Link].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() ; [Link](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(); [Link](new GridLayout (6,2); y pane in the center [Link](new JLabel("HostID: ")); hostIDLabel = new JTextField("default”); host IDLabel. setEditable( false) ; [Link](hostIDLabel) ; [Link](new JLabel("Host Name: hostNameLabel = new JTextField(“default"); [Link]( false); [Link](hostNameLabel) ; Di [Link](new JLabel( "Neighbourhood: ")); neighbourhoodLabel = new JTextField("default"); neighbourhoodLabel. setEditable(false) ; propertyPanel .add(neighbourhoodLabel) ; [Link](new JLabel("Room typi roomTypeLabel = new JTextField("default"); roomlypeLabel .setEditable( false) ; propertyPanel .add([Link]) ; [Link](new JLabel("Price: ")); priceLabel = new JTextField("default"); [Link]( false) ; propertyPanel..add(priceLabel) ; [Link](new JLabel("Minimum nights: ")); minNightsLabel = new JTextField("default"); [Link]éditable(false) ; [Link](minNightsLabel) ; [Link](new EtchedBorder()); [Link](propertyPanel, BorderLayout .CENTER) ; idLabel = new JLabel("default"); [Link](idLabel, BorderLayout NORTH favouriteLabel = new JLabel(" °); [Link](favouriteLabel, [Link]) ; Create bar with the butt JPanel toolbar = new JPanel() ; toolbar .setLayout(new GridLayout(8, 1)); JButton nextButton = new JButton("Next"); [Link](new ActionListener() { PropertyViewerGUI [se previousButton() 2021-Oct-38 21:14 public void actionPerformed(ActionEvent nextButton(); } : toolbar .add(nextButton) ; JButton previousButton = new JButton("Previous"); [Link](new ActionListener() { public void actionPerformed(ActionEvent ) : toolbar .add(previousButton) ; Button mapButton = new JButton("View Property on Map"); [Link](new ActionListener() { public void actionPerformed(ActionEvent ‘viewOnMepsButton(); } Yn: toolbar .add(mapButton) ; JButton favouriteButton = new JButton( "Toggle Favourite"); [Link](new ActionListener() { public void actionPerformed(ActionEvent toggleFavouriteButton(); } : toolbar .add( favouriteButton) ; Button statButton = new JButton("Statistics"); // Adds the [Link](new ActionListener() { public void actionPerformed(ActionEvent statButton(); } Wi toolbar .add(statButton) ; panel with flow layout for spacing JPanel flow = new JPanel(); [Link]( toolbar) ; [Link](flow, BorderLayout .WEST) ; building is done - arrange the component [Link]() place the frame at ti show Dimension d = [Link]() .getScreenSize(); center of the scr [Link]([Link]/2 - [Link]()/2, d-height/2 - [Link]()/2); [Link](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)[Link]( [Link](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 [Link](noPropertiesViewed, BorderLayout NORTH) ; [Link](averagePriceLabel, BorderLayout SOUTH) ; s below positions the Labels on the new window [Link](); Dimension d = [Link]() .getScreensize() ; [Link]([Link]/2 - [Link]()/2, [Link]/2 - [Link]()/2); // Positions the stats window at the centre of the [Link]( true) ; } Property 2021-Oct-38 21:14 import [Link].*; import [Link].«; import [Link].; import [Link].+; import [Link].*; 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){ [Link] = id; [Link] = name [Link] = hostID; [Link] = hostName; [Link] = neighbourhood; [Link] = latitude; [Link] = longitude; [Link] = roonType; [Link] = price; [Link] = 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, String Property 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 property Property 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

You might also like