Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.ArrayList;
- import javax.swing.table.*;
- /**
- * Write a description of class EstateAgent here.
- *
- * @author Richard Schwabe
- * @version 1.0.0
- * @Student Id: 10048345
- */
- public class EstateAgent
- {
- // instance variables - replace the example below with your own
- private JFrame frame;
- private final int SCREEN_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
- private final int SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
- private ArrayList<Property> properties;
- private static JTable table;
- private static DefaultTableModel model = new DefaultTableModel();
- /**
- * Constructor for objects of class EstateAgent
- */
- public EstateAgent()
- {
- properties = new ArrayList<Property>();
- this.makeFrame();
- this.makeMenuBar();
- //for testing only:
- //this.createPropertyFrame(0);
- }
- /**
- * Make the GUI window
- */
- private void makeFrame()
- {
- frame = new JFrame( "Eastate Agent V1.0.0" );
- frame.setPreferredSize( new Dimension( 800,600 ) );
- frame.setLocation( SCREEN_WIDTH / 2 - 400, SCREEN_HEIGHT / 2 - 300 ); //center the window
- frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
- frame.setJMenuBar(this.makeMenuBar());
- Container cp = frame.getContentPane();
- cp.setLayout(new BorderLayout());
- createTable(cp);
- frame.pack();
- frame.setVisible( true );
- }
- /**
- * insert rows into the table
- */
- private void insertValuesIntoTable()
- {
- Object[] data = new Object[]{};
- for(int i=0; i <= properties.size() - 1; i++)
- {
- if( properties.get(i).getClass().toString().equals("class PropertyToSell") )
- {
- PropertyToSell pts = (PropertyToSell) properties.get(i);
- data = new Object[]{i, pts.getAddress(), pts.getLocation(), pts.getNumberOfBedrooms(), pts.getPrice(), new String("----"), new String("----"), pts.getPurchaser()};
- }
- else if ( properties.get(i).getClass().toString().equals("class PropertyToLet") )
- {
- PropertyToLet ptl = (PropertyToLet) properties.get(i);
- data = new Object[]{i, ptl.getAddress(), ptl.getLocation(), ptl.getNumberOfBedrooms(),new String("----"), ptl.getMonthlyRent(), ptl.getTenantName(), new String("----")};
- }
- }
- model.addRow(data);
- }
- /**
- * Create the table and show it in the main window
- */
- private void createTable(Container cp)
- {
- //on the default table model add now the columns!
- model.addColumn("#");
- model.addColumn("Address");
- model.addColumn("Location");
- model.addColumn("Number of Bedrooms");
- model.addColumn("Price");
- model.addColumn("Rent");
- model.addColumn("Tenant");
- model.addColumn("Purchaser");
- table = new JTable(model);
- //no make some columns thinner and wider
- TableColumn col;
- //set # col to 25
- col = table.getColumnModel().getColumn(0);
- col.setPreferredWidth(25);
- //set address to 150
- col = table.getColumnModel().getColumn(1);
- col.setPreferredWidth(150);
- //make sure that the header is shown - scrollpane takes care about that automatically
- JScrollPane scrollPane = new JScrollPane(table);
- table.setFillsViewportHeight(true);
- cp.add(scrollPane, BorderLayout.CENTER);
- }
- /**
- * create the property creation frame depending on what the user chose in the dialog box.
- */
- private void createPropertyFrame(final int what)
- {
- //what:
- // 0 = sell
- // 1 = let
- //*********************************************
- final JFrame propWindow = new JFrame();
- //set the price according to the purpose of the window
- String title = "Create new property to ";
- if ( what == 0 )
- {
- title += "sell";
- }
- else
- {
- title += "let";
- }
- propWindow.setTitle(title);
- //just close the window not the programm by clicking on the x
- propWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- propWindow.setPreferredSize(new Dimension(400,300));
- propWindow.setResizable(false);
- propWindow.setLocation( SCREEN_WIDTH / 2 - 200, SCREEN_HEIGHT / 2 - 150 ); //center the window
- Container swCP = propWindow.getContentPane();
- swCP.setLayout( new GridBagLayout() );
- GridBagConstraints c = new GridBagConstraints();
- c.insets = new Insets(10,0,0,0); //top padding
- //Create the labels and textfields
- //and put the into the GridBag layout
- //*******************************************************************************
- //address string
- //*******************************************************************************
- JLabel addressLabel = new JLabel("Address:*");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 0;
- swCP.add(addressLabel,c);
- final JTextField addressField = new JTextField();
- addressField.setPreferredSize(new Dimension(200,20));
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 1;
- c.gridy = 0;
- swCP.add(addressField,c);
- //*******************************************************************************
- //location char
- //*******************************************************************************
- JLabel locationLabel = new JLabel("Location:*");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 1;
- swCP.add(locationLabel,c);
- String[] locations = {"North London", "South London", "West London", "North London", "Not available."};
- final JComboBox locationField = new JComboBox(locations);
- locationField.setPreferredSize(new Dimension(200,20));
- locationField.setSelectedItem("Not available.");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 1;
- c.gridy = 1;
- swCP.add(locationField,c);
- //*******************************************************************************
- //number of bedrooms(nob) int
- //*******************************************************************************
- JLabel nobLabel = new JLabel("Number of Bedrooms:*");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 2;
- swCP.add(nobLabel,c);
- final JTextField nobField = new JTextField();
- nobField.setPreferredSize(new Dimension(200,20));
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 1;
- c.gridy = 2;
- swCP.add(nobField,c);
- //the specific things regarding to the choise of the user
- String priceText, nameText;
- if ( what == 0 )
- {
- priceText = "Price:*";
- nameText = "Purchaser Name:";
- }
- else
- {
- priceText = "Rent:*";
- nameText = "Tenant Name:";
- }
- //*******************************************************************************
- //price/rent int
- //*******************************************************************************
- JLabel priceLabel = new JLabel(priceText);
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 3;
- swCP.add(priceLabel,c);
- final JTextField priceField = new JTextField();
- priceField.setPreferredSize(new Dimension(200,20));
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 1;
- c.gridy = 3;
- swCP.add(priceField,c);
- //*******************************************************************************
- //purchaser/tenant name string
- //*******************************************************************************
- JLabel nameLabel = new JLabel(nameText);
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 4;
- swCP.add(nameLabel,c);
- final JTextField nameField = new JTextField();
- nameField.setPreferredSize(new Dimension(200,20));
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 1;
- c.gridy = 4;
- swCP.add(nameField,c);
- //*******************************************************************************
- //create button
- //*******************************************************************************
- JButton createBtn = new JButton("Insert new property");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 5;
- swCP.add(createBtn,c);
- //*******************************************************************************
- //notification
- //*******************************************************************************
- JLabel notificationLabel = new JLabel("All field with * are required.");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 1;
- c.gridy = 5;
- swCP.add(notificationLabel,c);
- //validate the form and insert the property
- createBtn.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- //set some standard values and introduce new vars
- int error = 0;
- String address, name;
- int price, nob;
- char location;
- nob = 0;
- price = 0;
- //check if the values are correct as they are expected
- address = addressField.getText();
- if ( address.isEmpty() )
- {
- JOptionPane.showMessageDialog(frame,
- "Address cannot be empty!",
- "Address Error",
- JOptionPane.ERROR_MESSAGE);
- error++;
- }
- else
- {
- address = addressField.getText().trim();
- }
- //location is fixed therefore no validation
- //just adjusting the value
- String l = locationField.getSelectedItem().toString();
- location = l.charAt(0);
- //check the price
- try
- {
- price = Integer.parseInt( priceField.getText() );
- }
- catch (NumberFormatException priceE)
- {
- JOptionPane.showMessageDialog(frame,
- "Price must be a number - only digits.",
- "Price Error",
- JOptionPane.ERROR_MESSAGE);
- error++; //set error
- }
- //check the bedroom
- try
- {
- nob = Integer.parseInt( nobField.getText() );
- }
- catch (NumberFormatException nobE)
- {
- JOptionPane.showMessageDialog(frame,
- "Number of Bedrooms must be a number - only digits.",
- "Number of Bedrooms Error",
- JOptionPane.ERROR_MESSAGE);
- error++; //set error
- }
- name = nameField.getText();
- if ( name.isEmpty() )
- {
- name = "";
- }
- else
- {
- name = nameField.getText().trim();
- }
- if ( error == 0 )
- {
- if ( what == 0 )
- {
- PropertyToSell prop = new PropertyToSell(address, location, price, nob );
- //if the name is not empty add purchaser
- if ( name.isEmpty() != true )
- {
- prop.setPurchaser(name);
- }
- //add the property to the list
- properties.add(prop);
- }
- else
- {
- PropertyToLet prop = new PropertyToLet(address, location, nob, price );
- //if the name is not empty add tenant
- if ( name.isEmpty() != true )
- {
- prop.addTenant(name);
- }
- //add the property to the list
- properties.add(prop);
- }
- //propWindow.dispose();
- //now clean all the values from the textfields
- addressField.setText("");
- locationField.setSelectedItem("Not available.");
- priceField.setText("");
- nobField.setText("");
- propWindow.dispose();
- //for testing only
- insertValuesIntoTable();
- }
- }
- }
- );
- //show the window
- propWindow.pack();
- propWindow.setVisible(true);
- }
- /**
- * create the JMenuBar
- * @return JMenuBar
- */
- private JMenuBar makeMenuBar()
- {
- //creat the menubar
- JMenuBar menuBar = new JMenuBar();
- //create the file menu for exit && new
- JMenu fileMenu = new JMenu("File");
- //*********************************************
- //EXIT BUTTON
- //*********************************************
- JMenuItem exitItem = new JMenuItem("Exit");
- exitItem.addActionListener( new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- System.exit(0);
- }
- });
- //*********************************************
- //New BUTTON
- //*********************************************
- JMenuItem newItem = new JMenuItem("New Property");
- newItem.addActionListener( new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- Object[] options = {"Property to Sell", "Property to Let"};
- int a = JOptionPane.showOptionDialog(frame, "Choose the type of property you would like to create", "New Property", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
- //if a == 0 -> property to sell
- //if a == 1 -> property to let
- createPropertyFrame(a);
- }
- });
- //Add the file menu items;
- fileMenu.add(newItem);
- fileMenu.add(exitItem);
- //Add the menus to the menubar
- menuBar.add(fileMenu);
- //return the menuBar
- return menuBar;
- }
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- new EstateAgent();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement