Minor error correction and some bugs, alongside with modify offers quality of life...
authorEneko Pinzolas Murua <kelossus@localhost.localdomain>
Fri, 10 Apr 2015 10:37:22 +0000 (12:37 +0200)
committerEneko Pinzolas Murua <kelossus@localhost.localdomain>
Fri, 10 Apr 2015 10:37:22 +0000 (12:37 +0200)
1  2 
ruralHouses/src/dataAccess/DB4oManager.java
ruralHouses/src/gui/HouseFeaturesGUI.java
ruralHouses/src/gui/ModifyOfferGUI.java
ruralHouses/src/gui/listOfHousesGUI.java

@@@ -5,7 -5,7 +5,7 @@@ import java.io.File
  //import java.util.Vector;
  
  import java.rmi.RemoteException;
- import java.sql.Date;
+ import java.util.Date;
  import java.util.HashSet;
  import java.util.ListIterator;
  import java.util.Vector;
@@@ -37,7 -37,7 +37,7 @@@ public class DB4oManager 
        private static DB4oManager theDB4oManager = null;
  
        private static DB4oManagerAux theDB4oManagerAux;
-       
        static ConfigXML c;
  
        private DB4oManager() throws Exception {
                try {
                        Owner jon = new Owner("Jon");
                        Owner alfredo = new Owner("Alfredo");
-                       jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3);
-                       jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4);
+                       jon.addRuralHouse("Ezkioko", "Ezkioko etxea", "Beatriz", 3, 3, 3, 3,
+                                       3);
+                       jon.addRuralHouse("Eskiatze", "Eskiatzeko etxea", "Guazate", 4, 4, 4,
+                                       4, 4);
                        jon.setBankAccount("1349 5677 21 2133567777");
-                       alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Pitillas", 5, 5, 5, 5,
-                                       5);
-                       alfredo.addRuralHouse("Murgoitz", "", "Murgia", 6, 6, 6, 6, 6);
+                       alfredo.addRuralHouse("Aitonako", "Casa del abuelo", "Vegas", 5,
+                                       5, 5, 5, 5);
+                       alfredo.addRuralHouse("Murgoitz", "", "Cedro", 6, 6, 6, 6, 6);
                        alfredo.setBankAccount("4144 0087 23 9700002133");
                        Account jonAcc = new Account("userJon", "passJon", jon);
                        Account alfredoAcc = new Account("userAlfredo", "passAlfredo",
                        db.close();
                }
        }
 -      @SuppressWarnings("finally")
 +      
 +      
        public Offer createOffer(RuralHouse ruralHouse, Date firstDay,
                        Date lastDay, float price) throws RemoteException, Exception {
                Offer o = null;
  
                try {
  
-                       RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
-                                       null, null, null, null);
+                       RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
+                                       null, null, null);
                        ObjectSet result = db.queryByExample(proto);
                        RuralHouse rh = (RuralHouse) result.next();
                        o = rh.createOffer(theDB4oManagerAux.offerNumber++, firstDay,
  
                try {
  
-                       
                        db.store(offer);
                        db.commit();
  
                }
                return offer;
        }
-       
        @SuppressWarnings("finally")
-       public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException, Exception {
+       public void deleteOffer(RuralHouse rh, Offer offer) throws RemoteException,
+                       Exception {
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
                        db.close();
                }
        }
-       
  
        /**
         * This method creates a book with a corresponding parameters
                        if (c.isDatabaseLocal() == false)
                                openSDB();
  
-                       RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(),
-                                       null, ruralHouse.getDescription(), ruralHouse.getDistrict(),
-                                       null);
+                       RuralHouse proto = new RuralHouse(ruralHouse.getHouseName(), null,
+                                       ruralHouse.getDescription(), ruralHouse.getDistrict(), null);
                        ObjectSet result = db.queryByExample(proto);
                        RuralHouse rh = (RuralHouse) result.next();
  
  
                try {
  
-                       RuralHouse rhn = (RuralHouse) db
-                                       .queryByExample(
-                                                       new RuralHouse(rh.getHouseName(), null, null,
-                                                                       null, null)).next();
+                       RuralHouse rhn = (RuralHouse) db.queryByExample(
+                                       new RuralHouse(rh.getHouseName(), null, null, null, null))
+                                       .next();
                        if (rhn.overlapsWith(firstDay, lastDay) != null)
                                throw new OverlappingOfferExists();
                        else
                        openDB();
  
                boolean stored = false;
-               RuralHouse house = new RuralHouse(rh.getHouseName(), null, null,
-                               null, null);
+               RuralHouse house = new RuralHouse(rh.getHouseName(), null, null, null,
+                               null);
                try {
                        ObjectSet<Owner> result = db.queryByExample(house);
                        if (result.isEmpty()) {
                return stored;
        }
  
        public void removeHouse(RuralHouse rh, Owner owner) {
  
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
-                       openDB();               
+                       openDB();
                try {
                        ObjectSet<RuralHouse> result = db.queryByExample(rh);
                        if (!result.isEmpty()) {
                                RuralHouse found = (RuralHouse) result.get(0);
//                            db.delete(found.getOwner());
                              // db.delete(found.getOwner());
                                db.store(owner);
                                db.delete(found);
                                db.commit();
                }
  
        }
-       
-       public Vector<RuralHouse> getRuralHousesByTown(String town){
-               RuralHouse rh = new RuralHouse(null,null,null,town,null);
-               
+       public Vector<RuralHouse> getRuralHousesByTown(String town) {
+               RuralHouse rh = new RuralHouse(null, null, null, town, null);
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
                }
  
        }
-       public RuralHouse getRuralHouseByName(String name){
-               RuralHouse rh = new RuralHouse(name,null,null,null,null);
-               
+       public RuralHouse getRuralHouseByName(String name) {
+               RuralHouse rh = new RuralHouse(name, null, null, null, null);
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
                        openDB();
-               
-               try{
+               try {
                        ObjectSet<RuralHouse> result = db.queryByExample(rh);
                        Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
-                       while(result.hasNext())
+                       while (result.hasNext())
                                ruralHouses.add(result.next());
                        db.close();
-                       return ruralHouses.get(0);
-               } catch (NullPointerException e){
+                       if (!ruralHouses.isEmpty())
+                               return ruralHouses.get(0);
+                       else
+                               return null;
+               } catch (NullPointerException e) {
                        return null;
                }
-               
-               
-               
        }
-       
-       public Vector<RuralHouse> getRuralHouses(String town,int nBed , int nKit, int nBath, int nPark, int nLiv){
-               HouseFeatures fea = new HouseFeatures(nBed,nKit,nBath,nLiv,nPark);
-               RuralHouse rh = new RuralHouse(null,null,null,town,fea);
+       public Vector<RuralHouse> getRuralHouses(String town, int nBed, int nKit,
+                       int nBath, int nPark, int nLiv) {
+               HouseFeatures fea = new HouseFeatures(nBed, nKit, nBath, nLiv, nPark);
+               RuralHouse rh = new RuralHouse(null, null, null, town, fea);
                if (c.isDatabaseLocal() == false)
                        openSDB();
                else
                        openDB();
-               
-               try{
+               try {
                        ObjectSet<RuralHouse> result = db.queryByExample(rh);
                        Vector<RuralHouse> ruralHouses = new Vector<RuralHouse>();
-                       while(result.hasNext())
+                       while (result.hasNext())
                                ruralHouses.add(result.next());
                        db.close();
                        return ruralHouses;
-               } catch (NullPointerException e){
+               } catch (NullPointerException e) {
                        return null;
                }
-               
-               
-               
        }
  }
index 0000000,414ff07..ecc44ab
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,260 +1,258 @@@
 -      public HouseFeaturesGUI(RuralHouse RH, Date FirstDay, Date LastDay) {
+ package gui;
+ import java.awt.Color;
+ import java.awt.Component;
+ import java.awt.event.ActionEvent;
+ import java.awt.event.ActionListener;
+ import java.awt.event.MouseAdapter;
+ import java.awt.event.MouseEvent;
+ import java.util.Date;
+ import java.util.Enumeration;
+ import java.util.Vector;
+ import javax.swing.JButton;
+ import javax.swing.JFrame;
+ import javax.swing.JLabel;
+ import javax.swing.JPanel;
+ import javax.swing.JScrollPane;
+ import javax.swing.JTable;
+ import javax.swing.JTextField;
+ import javax.swing.SwingConstants;
+ import javax.swing.border.EmptyBorder;
+ import javax.swing.table.DefaultTableCellRenderer;
+ import javax.swing.table.DefaultTableModel;
+ import businessLogic.BookingManager;
+ import domain.Booking;
+ import domain.Offer;
+ import domain.RuralHouse;
+ import exceptions.OfferCanNotBeBooked;
+ public class HouseFeaturesGUI extends JFrame {
+       /**
+        * 
+        */
+       private static final long serialVersionUID = 1L;
+       private JPanel contentPane;
+       private JLabel lblDistrict;
+       private JTextField District_f;
+       private JLabel lblDescription;
+       private JTextField description_f;
+       private JLabel lblKitchen;
+       private JTextField kitchens_f;
+       private JLabel lblRooms;
+       private JTextField rooms_f;
+       private JLabel lblLivings;
+       private JTextField lRooms_f;
+       private JLabel lblParkings;
+       private JTextField parkings_f;
+       private JLabel lblBaths;
+       private JTextField baths_f;
+       private JTable table;
+       private DefaultTableModel tableModel;
+       private DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();
+       private RuralHouse rh;
+       private JTextField telIn;
+       private int row;
+       /**
+        * Create the frame.
+        */
 -              JTable table = new JTable() {
++      public HouseFeaturesGUI(RuralHouse RH, final Date FirstDay, final Date LastDay) {
+               this.rh = RH;
+               this.getContentPane().setLayout(null);
+               setBounds(100, 100, 500, 583);
+               contentPane = new JPanel();
+               contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
+               setContentPane(contentPane);
+               lblDistrict = new JLabel("District:");
+               lblDistrict.setBounds(23, 67, 70, 14);
+               lblDistrict.setHorizontalAlignment(SwingConstants.RIGHT);
+               District_f = new JTextField();
+               District_f.setEditable(false);
+               District_f.setBounds(103, 64, 86, 20);
+               District_f.setColumns(10);
+               lblDescription = new JLabel("Description:");
+               lblDescription.setBounds(215, 67, 90, 14);
+               lblDescription.setHorizontalAlignment(SwingConstants.RIGHT);
+               description_f = new JTextField();
+               description_f.setEditable(false);
+               description_f.setBounds(238, 99, 178, 129);
+               description_f.setColumns(10);
+               lblKitchen = new JLabel("Kitchens:");
+               lblKitchen.setBounds(23, 211, 70, 14);
+               lblKitchen.setHorizontalAlignment(SwingConstants.RIGHT);
+               kitchens_f = new JTextField();
+               kitchens_f.setEditable(false);
+               kitchens_f.setBounds(103, 202, 86, 20);
+               kitchens_f.setColumns(10);
+               lblRooms = new JLabel("Rooms:");
+               lblRooms.setBounds(23, 102, 70, 14);
+               lblRooms.setHorizontalAlignment(SwingConstants.RIGHT);
+               rooms_f = new JTextField();
+               rooms_f.setEditable(false);
+               rooms_f.setBounds(103, 99, 86, 20);
+               rooms_f.setColumns(10);
+               lblLivings = new JLabel("Living rooms:");
+               lblLivings.setBounds(30, 236, 63, 14);
+               lblLivings.setHorizontalAlignment(SwingConstants.RIGHT);
+               lRooms_f = new JTextField();
+               lRooms_f.setEditable(false);
+               lRooms_f.setBounds(103, 233, 86, 20);
+               lRooms_f.setColumns(10);
+               lblParkings = new JLabel("Parkings:");
+               lblParkings.setBounds(23, 174, 70, 14);
+               lblParkings.setHorizontalAlignment(SwingConstants.RIGHT);
+               parkings_f = new JTextField();
+               parkings_f.setEditable(false);
+               parkings_f.setBounds(103, 171, 86, 20);
+               parkings_f.setColumns(10);
+               lblBaths = new JLabel("Baths:");
+               lblBaths.setBounds(23, 143, 70, 14);
+               lblBaths.setHorizontalAlignment(SwingConstants.RIGHT);
+               baths_f = new JTextField();
+               baths_f.setEditable(false);
+               baths_f.setBounds(103, 140, 86, 20);
+               baths_f.setColumns(10);
+               contentPane.setLayout(null);
+               contentPane.add(lblParkings);
+               contentPane.add(parkings_f);
+               contentPane.add(lblRooms);
+               contentPane.add(rooms_f);
+               contentPane.add(lblBaths);
+               contentPane.add(baths_f);
+               contentPane.add(lblDistrict);
+               contentPane.add(District_f);
+               contentPane.add(lblLivings);
+               contentPane.add(lRooms_f);
+               contentPane.add(lblKitchen);
+               contentPane.add(kitchens_f);
+               contentPane.add(description_f);
+               contentPane.add(lblDescription);
+               parkings_f.setText(Integer.toString(rh.getFeatures().getnParkings()));
+               rooms_f.setText(Integer.toString(rh.getFeatures().getnRooms()));
+               baths_f.setText(Integer.toString(rh.getFeatures().getnBaths()));
+               District_f.setText(rh.getDistrict());
+               kitchens_f.setText(Integer.toString(rh.getFeatures().getnKitchens()));
+               description_f.setText(rh.getDescription());
+               lRooms_f.setText(Integer.toString(rh.getFeatures().getnLivings()));
+               JLabel lblAvailbleOffers = new JLabel("Availble Offers:");
+               lblAvailbleOffers.setBounds(30, 286, 86, 14);
+               contentPane.add(lblAvailbleOffers);
+               JScrollPane scrollPane = new JScrollPane();
+               scrollPane.setBounds(40, 311, 376, 183);
+               contentPane.add(scrollPane);
 -                                      if (FirstDay.before(firstDay) || FirstDay.equals(firstDay)
 -                                                      && LastDay.after(lastDay)
 -                                                      || LastDay.equals(lastDay)) {
 -                                              setBackground(Color.GREEN);
++              table = new JTable() {
+                       private static final long serialVersionUID = 1L;
+                       public boolean isCellEditable(int row, int column) {
+                               return false;
+                       };
+               };
+               tableModel = new DefaultTableModel(null, new String[] { "Offer #",
+                               "FirstDay", "LastDay", "Price" });
+               table.setModel(tableModel);
+               scrollPane.setViewportView(table);
+               JButton btnBookSelected = new JButton("Book SelectedOffer");
+               btnBookSelected.setBounds(238, 505, 178, 23);
+               contentPane.add(btnBookSelected);
+               btnBookSelected.addActionListener(new ActionListener() {
+                       public void actionPerformed(ActionEvent arg0) {
+                               jButton_ActionPerformed(arg0);
+                       }
+               });
+               JLabel lblNewLabel = new JLabel(
+                               "Green: Suit your dates. Red: Do not suit your dates");
+               lblNewLabel.setEnabled(false);
+               lblNewLabel.setBounds(152, 277, 261, 23);
+               contentPane.add(lblNewLabel);
+               
+               JLabel lblNewLabel_1 = new JLabel("Telephone num:");
+               lblNewLabel_1.setBounds(10, 509, 83, 14);
+               contentPane.add(lblNewLabel_1);
+               
+               telIn = new JTextField();
+               telIn.setBounds(99, 505, 129, 20);
+               contentPane.add(telIn);
+               telIn.setColumns(10);
+               table.addMouseListener( new MouseAdapter(){                     
+                       @Override
+                       public void mouseClicked(MouseEvent arg0) {
+                               row = table.getSelectedRow();
+                       }
+               });
+               Enumeration<Offer> rhs = rh.getAllOffers().elements();
+               while (rhs.hasMoreElements()) {
+                       Offer of = rhs.nextElement();
+                       Vector<Object> row = new Vector<Object>();
+                       row.add(of.getOfferNumber());
+                       row.add(of.getFirstDay());
+                       row.add(of.getLastDay());
+                       row.add(of.getPrice());
+                       tableModel.addRow(row);
+               }
+               table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
+                       @Override
+                       public Component getTableCellRendererComponent(JTable table,
+                                       Object value, boolean isSelected, boolean hasFocus,
+                                       int row, int col) {
+                               super.getTableCellRendererComponent(table, value, isSelected,
+                                               hasFocus, row, col);
+                               Date firstDay = (Date) table.getModel().getValueAt(row, 1);
+                               Date lastDay = (Date) table.getModel().getValueAt(row, 2);
+                               if (FirstDay != null && LastDay != null) {
 -                                              setBackground(Color.RED);
++                                      if (LastDay.before(lastDay)  ||  FirstDay.after(firstDay)) {
++                                              setBackground(Color.RED);
+                                               setForeground(Color.BLACK);
+                                       } else {
++                                              setBackground(Color.GREEN);
+                                               setForeground(Color.BLACK);
+                                       }
+                               }
+                               return this;
+                       }
+               });
+       }
+       private void jButton_ActionPerformed(ActionEvent arg0) {
+               BookingManager bookingM = new BookingManager();
+               //RegExp to see if telephone number is correct??TODO
+               Booking book = null;
+               try {
+                       book = bookingM.createBooking(rh, rh.offers.get(row).getFirstDay(), rh.offers.get(row).getLastDay(), telIn.getText());
+               } catch (OfferCanNotBeBooked e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+               if (book != null) {
+                       BookRuralHouseConfirmationWindow confirmWindow = new BookRuralHouseConfirmationWindow(
+                                       book);
+                       confirmWindow.setVisible(true);
+               }
+       }
+ }
@@@ -56,7 -56,7 +56,10 @@@ public class ModifyOfferGUI extends JFr
  
                jComboBox1 = new JComboBox<RuralHouse>(v);
                
--              comboBox_o = new JComboBox<Offer>();
++              comboBox_o = new JComboBox<Offer>(((RuralHouse)jComboBox1.getSelectedItem()).getAllOffers());
++              DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
++              jTextField1.setText(dateformat1.format(((Offer) comboBox_o.getSelectedItem()).getFirstDay()));
++              jTextField2.setText(dateformat1.format(((Offer) comboBox_o.getSelectedItem()).getLastDay()));
                
                jComboBox1.setBounds(new Rectangle(115, 12, 115, 20));
                jLabel1.setText("List of houses:");
@@@ -21,6 -21,8 +21,8 @@@ import javax.swing.JTable
  import javax.swing.table.DefaultTableModel;
  
  import java.awt.Component;
+ import java.awt.event.MouseAdapter;
+ import java.awt.event.MouseEvent;
  import java.sql.Date;
  import java.util.Enumeration;
  import java.util.LinkedList;
@@@ -31,60 -33,66 +33,66 @@@ import javax.swing.Box
  public class listOfHousesGUI extends JFrame {
  
        private JPanel contentPane;
-       private JTextField textField;
        private JTable table;
        private DefaultTableModel tableModel;
-       /**
+       private Vector<RuralHouse> houses;
+       /**
         * Create the frame.
         */
-       public listOfHousesGUI(Vector<RuralHouse> houses) {
+       public listOfHousesGUI(Vector<RuralHouse> rhs) {
                try {
-                       init(houses);
+                       this.houses=rhs;
+                       init();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
  
-       private void init(Vector<RuralHouse> houses) throws Exception {
+       private void init() throws Exception {
                setBounds(100, 100, 600, 450);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
                contentPane.setLayout(null);
  
-               JLabel lblNewLabel = new JLabel("List of available houses in:");
+               JLabel lblNewLabel = new JLabel();
                lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
-               lblNewLabel.setBounds(23, 41, 325, 33);
+               lblNewLabel.setBounds(23, 41, 536, 33);
                contentPane.add(lblNewLabel);
-               textField = new JTextField();
-               textField.setEnabled(false);
-               textField.setEditable(false);
-               textField.setFont(new Font("Tahoma", Font.PLAIN, 27));
-               textField.setBounds(373, 41, 152, 33);
-               contentPane.add(textField);
-               textField.setColumns(10);
                if (houses.isEmpty())
-                       textField.setText("No houses Matching");
+                       lblNewLabel.setText("There are not houses matching your search");
                else
-                       textField.setText(houses.get(0).getDistrict());
+                       lblNewLabel.setText("List of houses that match your search:");
                JScrollPane scrollPane = new JScrollPane();
                scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
                scrollPane.setBounds(23, 113, 536, 271);
                contentPane.add(scrollPane);
  
-               table = new JTable();
 -              JTable table = new JTable() {
++              table = new JTable() {
+               private static final long serialVersionUID = 1L;
+               public boolean isCellEditable(int row, int column) {                
+                       return false;               
+               };
+           };
                scrollPane.setViewportView(table);
                tableModel = new DefaultTableModel(null, new String[] {
                                "House Name", "Bedrooms", "Kitchens", "Baths", "Parkings",
                                "Livings" });
+               
                //Maybe there is a better way to avoid interaction.
-               table.setEnabled(false);
+               //table.setEnabled(false);
                table.setModel(tableModel);
                Enumeration<RuralHouse> en = houses.elements();
                RuralHouse rh;
+               table.addMouseListener( new MouseAdapter(){                     
+                       @Override
+                       public void mouseClicked(MouseEvent arg0) {
+                               int row = table.getSelectedRow();
+                               HouseFeaturesGUI feat = new HouseFeaturesGUI(houses.get(row),null,null);
+                               feat.setVisible(true);
+                       }
+               });
                while (en.hasMoreElements()) {
                        rh = en.nextElement();
                        Vector<Object> row = new Vector<Object>();
                        row.add(rh.getFeatures().getnBaths());
                        row.add(rh.getFeatures().getnParkings());
                        row.add(rh.getFeatures().getnLivings());
-                       System.out.println(rh.getFeatures().getnLivings());
                        tableModel.addRow(row);
                }