Advertisement
Guest User

Untitled

a guest
May 2nd, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.22 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.ArrayList;
  5. import javax.swing.table.*;
  6. /**
  7.  * Write a description of class EstateAgent here.
  8.  *
  9.  * @author Richard Schwabe
  10.  * @version 1.0.0
  11.  * @Student Id: 10048345
  12.  */
  13. public class EstateAgent
  14. {
  15.     // instance variables - replace the example below with your own
  16.     private JFrame frame;
  17.     private final int SCREEN_WIDTH  =   Toolkit.getDefaultToolkit().getScreenSize().width;
  18.     private final int SCREEN_HEIGHT =   Toolkit.getDefaultToolkit().getScreenSize().height;
  19.     private ArrayList<Property> properties;
  20.     private static JTable table;
  21.     private static DefaultTableModel model = new DefaultTableModel();
  22.  
  23.     /**
  24.      * Constructor for objects of class EstateAgent
  25.      */
  26.     public EstateAgent()
  27.     {
  28.         properties  =   new ArrayList<Property>();
  29.        
  30.         this.makeFrame();
  31.         this.makeMenuBar();
  32.         //for testing only:
  33.         //this.createPropertyFrame(0);
  34.     }
  35.    
  36.    
  37.     /**
  38.      * Make the GUI window
  39.      */
  40.     private void makeFrame()
  41.     {
  42.         frame   =   new JFrame( "Eastate Agent V1.0.0" );
  43.         frame.setPreferredSize( new Dimension( 800,600 ) );
  44.         frame.setLocation( SCREEN_WIDTH / 2 - 400, SCREEN_HEIGHT / 2 - 300 ); //center the window
  45.         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  46.         frame.setJMenuBar(this.makeMenuBar());
  47.         Container cp    =   frame.getContentPane();
  48.         cp.setLayout(new BorderLayout());
  49.         createTable(cp);
  50.        
  51.         frame.pack();
  52.         frame.setVisible( true );
  53.     }
  54.    
  55.     /**
  56.      * insert rows into the table
  57.      */
  58.     private void insertValuesIntoTable()
  59.     {
  60.         Object[] data = new Object[]{};
  61.         for(int i=0; i <= properties.size() - 1; i++)
  62.         {
  63.            
  64.            if( properties.get(i).getClass().toString().equals("class PropertyToSell") )
  65.            {
  66.                PropertyToSell pts   =   (PropertyToSell) properties.get(i);  
  67.                data                 =   new Object[]{i, pts.getAddress(), pts.getLocation(), pts.getNumberOfBedrooms(), pts.getPrice(), new String("----"), new String("----"),  pts.getPurchaser()};                
  68.            }
  69.            else if ( properties.get(i).getClass().toString().equals("class PropertyToLet") )
  70.            {
  71.                 PropertyToLet ptl   =   (PropertyToLet) properties.get(i);  
  72.                 data                =   new Object[]{i, ptl.getAddress(), ptl.getLocation(), ptl.getNumberOfBedrooms(),new String("----"), ptl.getMonthlyRent(), ptl.getTenantName(), new String("----")};
  73.            }
  74.            
  75.         }
  76.         model.addRow(data);
  77.     }
  78.    
  79.     /**
  80.      * Create the table and show it in the main window
  81.      */
  82.     private void createTable(Container cp)
  83.     {                        
  84.         //on the default table model add now the columns!
  85.         model.addColumn("#");
  86.         model.addColumn("Address");
  87.         model.addColumn("Location");
  88.         model.addColumn("Number of Bedrooms");
  89.         model.addColumn("Price");
  90.         model.addColumn("Rent");
  91.         model.addColumn("Tenant");
  92.         model.addColumn("Purchaser");
  93.        
  94.        table = new JTable(model);
  95.        
  96.        //no make some columns thinner and wider
  97.        TableColumn col;
  98.        //set # col to 25
  99.        col =   table.getColumnModel().getColumn(0);
  100.        col.setPreferredWidth(25);
  101.        
  102.        //set address to 150
  103.        col =   table.getColumnModel().getColumn(1);
  104.        col.setPreferredWidth(150);
  105.        
  106.        
  107.        
  108.        //make sure that the header is shown - scrollpane takes care about that automatically
  109.        JScrollPane scrollPane = new JScrollPane(table);
  110.        table.setFillsViewportHeight(true);
  111.        
  112.        cp.add(scrollPane, BorderLayout.CENTER);
  113.     }
  114.    
  115.     /**
  116.      * create the property creation frame depending on what the user chose in the dialog box.
  117.      */
  118.     private void createPropertyFrame(final int what)
  119.     {
  120.        //what:
  121.        // 0 = sell
  122.        // 1 = let
  123.        //*********************************************
  124.        
  125.        final JFrame propWindow   =   new JFrame();    
  126.        //set the price according to the purpose of the window
  127.        String title =   "Create new property to ";
  128.        
  129.        if ( what == 0 )
  130.        {
  131.            title += "sell";
  132.        }
  133.        else
  134.        {
  135.         title += "let";
  136.        }
  137.        
  138.        propWindow.setTitle(title);
  139.        
  140.        
  141.        //just close the window not the programm by clicking on the x
  142.        propWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);        
  143.        propWindow.setPreferredSize(new Dimension(400,300));
  144.        propWindow.setResizable(false);
  145.        propWindow.setLocation( SCREEN_WIDTH / 2 - 200, SCREEN_HEIGHT / 2 - 150 ); //center the window
  146.        
  147.        Container swCP  =   propWindow.getContentPane();
  148.        swCP.setLayout( new GridBagLayout() );
  149.        GridBagConstraints c = new GridBagConstraints();
  150.        c.insets = new Insets(10,0,0,0);  //top padding
  151.        
  152.        //Create the labels and textfields
  153.        //and put the into the GridBag layout
  154.        
  155.        
  156.        //*******************************************************************************
  157.        //address string
  158.        //*******************************************************************************
  159.        JLabel addressLabel    =   new JLabel("Address:*");      
  160.        c.fill = GridBagConstraints.HORIZONTAL;
  161.        c.gridx = 0;
  162.        c.gridy = 0;
  163.        swCP.add(addressLabel,c);
  164.        
  165.        final JTextField addressField =   new JTextField();
  166.            addressField.setPreferredSize(new Dimension(200,20));      
  167.        c.fill = GridBagConstraints.HORIZONTAL;
  168.        c.gridx = 1;
  169.        c.gridy = 0;        
  170.        swCP.add(addressField,c);
  171.        
  172.        
  173.        //*******************************************************************************
  174.        //location char
  175.        //*******************************************************************************
  176.        JLabel locationLabel        =   new JLabel("Location:*");  
  177.        c.fill = GridBagConstraints.HORIZONTAL;
  178.        c.gridx = 0;
  179.        c.gridy = 1;
  180.        swCP.add(locationLabel,c);
  181.        
  182.        String[] locations       =   {"North London", "South London", "West London", "North London", "Not available."};
  183.        final JComboBox locationField    =   new JComboBox(locations);
  184.            locationField.setPreferredSize(new Dimension(200,20));  
  185.            locationField.setSelectedItem("Not available.");
  186.        c.fill = GridBagConstraints.HORIZONTAL;
  187.        c.gridx = 1;
  188.        c.gridy = 1;
  189.        swCP.add(locationField,c);
  190.        
  191.        
  192.        //*******************************************************************************
  193.        //number of bedrooms(nob) int
  194.        //*******************************************************************************
  195.        JLabel nobLabel        =   new JLabel("Number of Bedrooms:*");  
  196.        c.fill = GridBagConstraints.HORIZONTAL;
  197.        c.gridx = 0;
  198.        c.gridy = 2;
  199.        swCP.add(nobLabel,c);
  200.        
  201.        final JTextField nobField    =   new JTextField();
  202.            nobField.setPreferredSize(new Dimension(200,20));  
  203.        c.fill = GridBagConstraints.HORIZONTAL;
  204.        c.gridx = 1;
  205.        c.gridy = 2;
  206.        swCP.add(nobField,c);
  207.              
  208.        
  209.        //the specific things regarding to the choise of the user
  210.            
  211.        String priceText, nameText;
  212.        
  213.        if ( what == 0 )
  214.        {
  215.            priceText    =   "Price:*";
  216.            nameText     =   "Purchaser Name:";
  217.        }
  218.        else
  219.        {
  220.            priceText    =   "Rent:*";    
  221.            nameText     =   "Tenant Name:";
  222.        }
  223.        
  224.        //*******************************************************************************
  225.        //price/rent int
  226.        //*******************************************************************************
  227.        JLabel priceLabel        =   new JLabel(priceText);  
  228.        c.fill = GridBagConstraints.HORIZONTAL;
  229.        c.gridx = 0;
  230.        c.gridy = 3;
  231.        swCP.add(priceLabel,c);
  232.        
  233.        final JTextField priceField    =   new JTextField();
  234.            priceField.setPreferredSize(new Dimension(200,20));  
  235.        c.fill = GridBagConstraints.HORIZONTAL;
  236.        c.gridx = 1;
  237.        c.gridy = 3;
  238.        swCP.add(priceField,c);
  239.        
  240.        //*******************************************************************************
  241.        //purchaser/tenant name string
  242.        //*******************************************************************************
  243.        JLabel nameLabel        =   new JLabel(nameText);  
  244.        c.fill = GridBagConstraints.HORIZONTAL;
  245.        c.gridx = 0;
  246.        c.gridy = 4;
  247.        swCP.add(nameLabel,c);
  248.        
  249.        final JTextField nameField    =   new JTextField();
  250.            nameField.setPreferredSize(new Dimension(200,20));  
  251.        c.fill = GridBagConstraints.HORIZONTAL;
  252.        c.gridx = 1;
  253.        c.gridy = 4;
  254.        swCP.add(nameField,c);
  255.        
  256.        //*******************************************************************************
  257.        //create button  
  258.        //*******************************************************************************    
  259.        JButton createBtn        =   new JButton("Insert new property");  
  260.        c.fill = GridBagConstraints.HORIZONTAL;
  261.        c.gridx = 0;
  262.        c.gridy = 5;
  263.        swCP.add(createBtn,c);
  264.        
  265.        //*******************************************************************************
  266.        //notification
  267.        //*******************************************************************************    
  268.        JLabel notificationLabel        =   new JLabel("All field with * are required.");  
  269.        c.fill = GridBagConstraints.HORIZONTAL;
  270.        c.gridx = 1;
  271.        c.gridy = 5;
  272.        swCP.add(notificationLabel,c);
  273.        //validate the form and insert the property
  274.         createBtn.addActionListener(new ActionListener()      
  275.         {
  276.             public void actionPerformed(ActionEvent e)
  277.             {
  278.                 //set some standard values and introduce new vars
  279.                 int error   =   0;
  280.                 String address, name;
  281.                 int price, nob;
  282.                 char location;
  283.                 nob     =   0;
  284.                 price   =   0;
  285.                
  286.                 //check if the values are correct as they are expected
  287.                 address  =   addressField.getText();
  288.                 if ( address.isEmpty() )
  289.                 {
  290.                     JOptionPane.showMessageDialog(frame,
  291.                         "Address cannot be empty!",
  292.                         "Address Error",
  293.                         JOptionPane.ERROR_MESSAGE);
  294.                     error++;
  295.                 }
  296.                 else
  297.                 {
  298.                     address  =   addressField.getText().trim();
  299.                 }
  300.                
  301.                 //location is fixed therefore no validation
  302.                 //just adjusting the value
  303.                 String l        =   locationField.getSelectedItem().toString();
  304.                 location   =   l.charAt(0);
  305.                
  306.                 //check the price
  307.                 try
  308.                 {
  309.                     price   =   Integer.parseInt( priceField.getText() );
  310.                 }
  311.                 catch (NumberFormatException priceE)
  312.                 {
  313.                     JOptionPane.showMessageDialog(frame,
  314.                         "Price must be a number - only digits.",
  315.                         "Price Error",
  316.                         JOptionPane.ERROR_MESSAGE);
  317.                         error++; //set error
  318.                 }
  319.                
  320.                 //check the bedroom
  321.                 try
  322.                 {
  323.                     nob   =   Integer.parseInt( nobField.getText() );
  324.                 }
  325.                 catch (NumberFormatException nobE)
  326.                 {
  327.                     JOptionPane.showMessageDialog(frame,
  328.                         "Number of Bedrooms must be a number - only digits.",
  329.                         "Number of Bedrooms Error",
  330.                         JOptionPane.ERROR_MESSAGE);
  331.                         error++; //set error
  332.                 }
  333.                
  334.                
  335.                 name  =   nameField.getText();
  336.                 if ( name.isEmpty() )
  337.                 {
  338.                     name    =   "";
  339.                 }
  340.                 else
  341.                 {
  342.                     name  =   nameField.getText().trim();
  343.                 }
  344.                
  345.                
  346.                
  347.                 if ( error == 0 )
  348.                 {
  349.                     if ( what == 0 )
  350.                     {
  351.                         PropertyToSell prop =   new PropertyToSell(address, location, price, nob );
  352.                         //if the name is not empty add purchaser
  353.                         if ( name.isEmpty() != true )
  354.                         {
  355.                             prop.setPurchaser(name);
  356.                         }
  357.                         //add the property to the list
  358.                         properties.add(prop);
  359.                     }
  360.                     else
  361.                     {
  362.                         PropertyToLet prop =   new PropertyToLet(address, location, nob, price );
  363.                         //if the name is not empty add tenant
  364.                         if ( name.isEmpty() != true )
  365.                         {
  366.                             prop.addTenant(name);
  367.                         }
  368.                         //add the property to the list
  369.                         properties.add(prop);
  370.                     }
  371.                     //propWindow.dispose();
  372.                    
  373.                     //now clean all the values from the textfields
  374.                     addressField.setText("");
  375.                     locationField.setSelectedItem("Not available.");
  376.                     priceField.setText("");
  377.                     nobField.setText("");
  378.                    
  379.                     propWindow.dispose();
  380.                     //for testing only
  381.                     insertValuesIntoTable();
  382.                 }
  383.             }
  384.         }
  385.         );
  386.      
  387.        //show the window
  388.        propWindow.pack();
  389.        propWindow.setVisible(true);
  390.     }
  391.    
  392.    
  393.     /**
  394.      * create the JMenuBar
  395.      * @return JMenuBar
  396.      */
  397.     private JMenuBar makeMenuBar()
  398.     {
  399.         //creat the menubar
  400.         JMenuBar menuBar    =   new JMenuBar();
  401.        
  402.         //create the file menu for exit && new
  403.         JMenu fileMenu      =   new JMenu("File");
  404.        
  405.         //*********************************************
  406.         //EXIT BUTTON
  407.         //*********************************************
  408.         JMenuItem exitItem  =   new JMenuItem("Exit");
  409.         exitItem.addActionListener( new ActionListener()
  410.         {
  411.             public void actionPerformed(ActionEvent e)
  412.             {
  413.                 System.exit(0);
  414.             }
  415.         });
  416.        
  417.         //*********************************************
  418.         //New  BUTTON
  419.         //*********************************************
  420.         JMenuItem newItem  =   new JMenuItem("New Property");
  421.         newItem.addActionListener( new ActionListener()
  422.         {
  423.             public void actionPerformed(ActionEvent e)
  424.             {
  425.                 Object[] options  =   {"Property to Sell", "Property to Let"};
  426.                
  427.                 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]);
  428.                
  429.                 //if a == 0 -> property to sell
  430.                 //if a == 1 -> property to let
  431.                 createPropertyFrame(a);
  432.             }
  433.         });
  434.        
  435.         //Add the file menu items;
  436.         fileMenu.add(newItem);
  437.         fileMenu.add(exitItem);
  438.        
  439.         //Add the menus to the menubar
  440.         menuBar.add(fileMenu);
  441.        
  442.         //return the menuBar
  443.         return menuBar;
  444.     }
  445.    
  446.     /**
  447.      * @param args
  448.      */
  449.     public static void main(String[] args)
  450.     {
  451.         new EstateAgent();
  452.     }
  453.    
  454.    
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement