From: camjan Date: Wed, 4 Mar 2015 18:42:47 +0000 (+0100) Subject: House Features added and logic for adding options to the owner X-Git-Url: https://xp-dev.com/git/RRRRHHHH_Code/commitdiff_plain/5538790d8fc54f60de9b5c989eae82b446374aca House Features added and logic for adding options to the owner --- diff --git a/ruralHouses/src/businessLogic/ApplicationFacadeInterface.java b/ruralHouses/src/businessLogic/ApplicationFacadeInterface.java index 8863af3..ef3cad2 100644 --- a/ruralHouses/src/businessLogic/ApplicationFacadeInterface.java +++ b/ruralHouses/src/businessLogic/ApplicationFacadeInterface.java @@ -1,7 +1,6 @@ package businessLogic; import java.rmi.*; - import java.util.Vector; import java.util.Date; @@ -9,23 +8,13 @@ import domain.Booking; import domain.Offer; import domain.Owner; import domain.RuralHouse; - -import exceptions.OfferCanNotBeBooked; +import exceptions.OfferCanNotBeBooked; public interface ApplicationFacadeInterface extends Remote { /** - * This method obtains an owner's rural houses - * - * @param owner object - * - * @return a vector of Rural Houses - */ - Vector getRuralHouses(Owner owner) - throws RemoteException; - - /** - * This method creates an offer with a house number, first day, last day and price + * This method creates an offer with a house number, first day, last day and + * price * * @param House * number, start day, last day and price @@ -46,26 +35,38 @@ public interface ApplicationFacadeInterface extends Remote { String telephoneNumber) throws RemoteException, OfferCanNotBeBooked; /** - * This method obtains available offers for a concrete house in a certain period + * This method obtains available offers for a concrete house in a certain + * period * - * @param houseNumber, the house number where the offers must be obtained - * @param firstDay, first day in a period range - * @param lastDay, last day in a period range - * @return a vector of offers(Offer class) available in this period + * @param houseNumber + * , the house number where the offers must be obtained + * @param firstDay + * , first day in a period range + * @param lastDay + * , last day in a period range + * @return a vector of offers(Offer class) available in this period */ - Vector getOffers(RuralHouse houseNumber, Date firstDay, Date lastDay) + Vector getOffers(RuralHouse houseNumber, Date firstDay, Date lastDay) throws RemoteException, Exception; - + /** - * This method finds existing owners + * This method finds existing owners * */ - public Vector getOwners() throws RemoteException, + public Vector getOwners() throws RemoteException, Exception; + + /** + * This method obtains an owner's rural houses + * + * @param owner + * object + * + * @return a vector of Rural Houses + */ + public Vector getRuralHouses(Owner owner) + throws RemoteException; + + public Vector getAllRuralHouses() throws RemoteException, Exception; - - - - public Vector getAllRuralHouses()throws RemoteException, - Exception; - + } \ No newline at end of file diff --git a/ruralHouses/src/businessLogic/FacadeImplementation.java b/ruralHouses/src/businessLogic/FacadeImplementation.java index 162a38b..9c2e8b1 100644 --- a/ruralHouses/src/businessLogic/FacadeImplementation.java +++ b/ruralHouses/src/businessLogic/FacadeImplementation.java @@ -2,23 +2,19 @@ package businessLogic; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; - import java.util.Date; import java.util.Vector; - import java.sql.SQLException; import com.db4o.ObjectContainer; import com.db4o.ObjectSet; -import configuration.Config; +import configuration.Config; import dataAccess.DB4oManager; - import domain.Booking; import domain.Offer; import domain.Owner; import domain.RuralHouse; - import exceptions.OfferCanNotBeBooked; public class FacadeImplementation extends UnicastRemoteObject implements ApplicationFacadeInterface { @@ -31,17 +27,6 @@ public class FacadeImplementation extends UnicastRemoteObject implements Applica dbMngr = DB4oManager.getInstance(); } - /** - * This method obtains an owner's rural houses - * - * @param owner object - * - * @return a vector of Rural Houses - */ - public Vector getRuralHouses(Owner owner) - throws RemoteException { - return owner.getRuralHouses(); - } public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay, float price) throws RemoteException, Exception { @@ -70,11 +55,23 @@ public class FacadeImplementation extends UnicastRemoteObject implements Applica return dbMngr.getOwners(); } + /** + * This method obtains an owner's rural houses + * + * @param owner object + * + * @return a vector of Rural Houses + */ + public Vector getRuralHouses(Owner owner) + throws RemoteException { + return owner.getRuralHouses(); + } + + public Vector getAllRuralHouses() throws RemoteException, Exception { return dbMngr.getAllRuralHouses(); } - public Booking createBooking(RuralHouse ruralHouse, Date firstDate, Date lastDate, String bookTelephoneNumber) diff --git a/ruralHouses/src/businessLogic/HouseManager.java b/ruralHouses/src/businessLogic/HouseManager.java index a876bcd..21b8925 100644 --- a/ruralHouses/src/businessLogic/HouseManager.java +++ b/ruralHouses/src/businessLogic/HouseManager.java @@ -1,10 +1,12 @@ package businessLogic; +import java.rmi.RemoteException; import java.util.Date; import java.util.Vector; import dataAccess.DB4oManager; import domain.Account; +import domain.HouseFeatures; import domain.Offer; import domain.Owner; import domain.RuralHouse; @@ -25,16 +27,14 @@ public class HouseManager implements HouseManagerInterface { public boolean registerNewHouse(int houseNumber, Owner owner, String town, int nRooms, int nKitchens, int nBaths, int nLivings, int nParkings) { // TODO Auto-generated method stub + boolean stored = false; if (nKitchens < 1 || nLivings < 3 || nBaths < 2) return false; - try { - this.dbMngr.storeRuralHouses(new RuralHouse(houseNumber, owner, - town, nRooms, nKitchens, nBaths, nLivings, nParkings)); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return true; + HouseFeatures feature = new HouseFeatures(nRooms, nKitchens, nBaths, + nLivings, nParkings); + stored = this.dbMngr.storeRuralHouses(new RuralHouse(houseNumber, + owner, town, feature)); + return stored; } @@ -43,39 +43,30 @@ public class HouseManager implements HouseManagerInterface { String description, String town, int nRooms, int nKitchens, int nBaths, int nLivings, int nParkings) { // TODO Auto-generated method stub + boolean stored = false; if (nKitchens < 1 || nLivings < 3 || nBaths < 2) return false; - try { - this.dbMngr.storeRuralHouses(new RuralHouse(houseNumber, owner, - description, town, nRooms, nKitchens, nBaths, nLivings, - nParkings)); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return true; + HouseFeatures feature = new HouseFeatures(nRooms, nKitchens, nBaths, + nLivings, nParkings); + stored = this.dbMngr.storeRuralHouses(new RuralHouse(houseNumber, + owner, description, town, feature)); + return stored; } - @Override - public void modifyHouse(int houseNumber, Owner owner, String description, - String town, int nRooms, int nKitchens, int nBaths, int nLivings, - int nParkings) { - // TODO Auto-generated method stub - - } - - //Maybe returning the offer is not necessary. Depends on GUI implementation. - @Override - public Offer setOffers(RuralHouse ruralHouse, Date firstDay, Date lastDay, - float price) { - // TODO Auto-generated method stub - try { - return this.dbMngr.createOffer(ruralHouse, firstDay, lastDay, price); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; + public void removeHouse(int houseNumber) { + this.dbMngr.removeHouse(houseNumber); } + // For future implementation + // @Override + // public void modifyHouse(int houseNumber, Owner owner, String description, + // String town, int nRooms, int nKitchens, int nBaths, int nLivings, + // int nParkings) { + // // TODO Auto-generated method stub + // + // } + + // Maybe returning the offer is not necessary. Depends on GUI + // implementation. + } diff --git a/ruralHouses/src/businessLogic/HouseManagerInterface.java b/ruralHouses/src/businessLogic/HouseManagerInterface.java index 29c97d5..7a0656f 100644 --- a/ruralHouses/src/businessLogic/HouseManagerInterface.java +++ b/ruralHouses/src/businessLogic/HouseManagerInterface.java @@ -1,6 +1,8 @@ package businessLogic; +import java.rmi.RemoteException; import java.util.Date; +import java.util.Vector; import domain.Offer; import domain.Owner; @@ -15,11 +17,12 @@ public interface HouseManagerInterface { String description, String town, int nRooms, int nKitchens, int nBaths, int nLivings, int nParkings); - public void modifyHouse(int houseNumber, Owner owner, - String description, String town, int nRooms, int nKitchens, - int nBaths, int nLivings, int nParkings); + // For future implementation + // public void modifyHouse(int houseNumber, Owner owner, + // String description, String town, int nRooms, int nKitchens, + // int nBaths, int nLivings, int nParkings); + + public void removeHouse(int houseNumber); - public Offer setOffers(RuralHouse ruralHouse, Date firstDay, Date lastDay, - float price); } diff --git a/ruralHouses/src/dataAccess/DB4oManager.java b/ruralHouses/src/dataAccess/DB4oManager.java index 295af73..26fa6f3 100644 --- a/ruralHouses/src/dataAccess/DB4oManager.java +++ b/ruralHouses/src/dataAccess/DB4oManager.java @@ -79,15 +79,23 @@ public class DB4oManager { /** * @param rh */ - public void storeRuralHouses(RuralHouse rh) { + public boolean storeRuralHouses(RuralHouse rh) { DB4oManager.openDatabase("open"); + boolean stored = false; ObjectContainer db = DB4oManager.getContainer(); + RuralHouse house = new RuralHouse(rh.getHouseNumber(), null, null, + null, null); try { - db.store(rh); - db.commit(); + ObjectSet result = db.queryByExample(house); + if (result.isEmpty()) { + db.store(rh); + db.commit(); + stored = true; + } } finally { db.close(); } + return stored; } /** @@ -144,8 +152,7 @@ public class DB4oManager { DB4oManager.openDatabase("open"); ObjectContainer db = DB4oManager.getContainer(); try { - RuralHouse proto = new RuralHouse(0, null, null, null, 0, 0, 0, 0, - 0); + RuralHouse proto = new RuralHouse(0, null, null, null, null); ObjectSet result = db.queryByExample(proto); Vector ruralHouses = new Vector(); while (result.hasNext()) @@ -171,8 +178,8 @@ public class DB4oManager { Offer o; try { RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), - null, ruralHouse.getDescription(), ruralHouse.getTown(), 0, - 0, 0, 0, 0); + null, ruralHouse.getDescription(), ruralHouse.getTown(), + ruralHouse.getFeatures()); ObjectSet result = db.queryByExample(proto); RuralHouse rh = (RuralHouse) result.next(); o = rh.createOffer(firstDay, lastDay, price); @@ -194,7 +201,7 @@ public class DB4oManager { try { ObjectContainer db = DB4oManager.getContainer(); RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null, - rh.getDescription(), rh.getTown(), 0, 0, 0, 0, 0); + rh.getDescription(), rh.getTown(), rh.getFeatures()); ObjectSet result = db.queryByExample(proto); return rh = (RuralHouse) result.next(); } catch (Exception exc) { @@ -231,6 +238,24 @@ public class DB4oManager { } } + public void removeHouse(int houseNumber) { + ObjectContainer db = DB4oManager.getContainer(); + RuralHouse house = new RuralHouse(houseNumber, null, null, null, null); + try { + ObjectSet result = db.queryByExample(house); + if (!result.isEmpty()) { + RuralHouse found = (RuralHouse) result.get(0); + db.delete(found); + db.commit(); + } + } catch (Exception exc) { + exc.printStackTrace(); + } finally { + db.close(); + } + + } + /** * This method returns the instance of the DB4oManager class * diff --git a/ruralHouses/src/domain/Owner.java b/ruralHouses/src/domain/Owner.java index 9a34067..35b0296 100644 --- a/ruralHouses/src/domain/Owner.java +++ b/ruralHouses/src/domain/Owner.java @@ -43,8 +43,8 @@ public class Owner implements Serializable { public RuralHouse addRuralHouse(int houseNumber, String town, int nRooms, int nKitchens, int nBaths, int nLivings, int nParkings) { - RuralHouse rh = new RuralHouse(houseNumber, this, town, nRooms, - nKitchens, nBaths, nLivings, nParkings); + HouseFeatures feature = new HouseFeatures(nRooms, nKitchens, nBaths, nLivings, nParkings); + RuralHouse rh = new RuralHouse(houseNumber, this, town,feature); ruralHouses.add(rh); return rh; } @@ -52,8 +52,8 @@ public class Owner implements Serializable { public RuralHouse addRuralHouse(int houseNumber, String description, String town, int nRooms, int nKitchens, int nBaths, int nLivings, int nParkings) { - RuralHouse rh = new RuralHouse(houseNumber, this, description, town, - nRooms, nKitchens, nBaths, nLivings, nParkings); + HouseFeatures feature = new HouseFeatures(nRooms, nKitchens, nBaths, nLivings, nParkings); + RuralHouse rh = new RuralHouse(houseNumber, this, description, town,feature); ruralHouses.add(rh); return rh; } diff --git a/ruralHouses/src/domain/RuralHouse.java b/ruralHouses/src/domain/RuralHouse.java index d499c88..50e46fa 100644 --- a/ruralHouses/src/domain/RuralHouse.java +++ b/ruralHouses/src/domain/RuralHouse.java @@ -20,23 +20,21 @@ public class RuralHouse implements Serializable { super(); } - public RuralHouse(int houseNumber, Owner owner, String description, String town , int nRooms,int nKitchens,int nBaths, - int nLivings,int nParkings) { + public RuralHouse(int houseNumber, Owner owner, String description, String town ,HouseFeatures features) { this.houseNumber = houseNumber; this.description = description; this.owner = owner; this.town = town; - this.features = new HouseFeatures(nRooms,nKitchens,nBaths,nLivings,nParkings); + this.features = features; offers=new Vector(); } - public RuralHouse(int houseNumber, Owner owner, String town , int nRooms,int nKitchens,int nBaths, - int nLivings,int nParkings) { + public RuralHouse(int houseNumber, Owner owner, String town , HouseFeatures features) { this.houseNumber = houseNumber; this.description = null; this.owner = owner; this.town = town; - this.features = new HouseFeatures(nRooms,nKitchens,nBaths,nLivings,nParkings); + this.features = features; offers=new Vector(); } @@ -71,7 +69,13 @@ public class RuralHouse implements Serializable { public void setTown(String town) { this.town=town; } - + public HouseFeatures getFeatures() { + return features; + } + + public void setFeatures(HouseFeatures features) { + this.features = features; + } public String toString() { return this.houseNumber + ": " + this.town; } @@ -136,6 +140,7 @@ public class RuralHouse implements Serializable { return availableOffers; } + /** * This method obtains the offer that match exactly with a given dates that has not been booked * diff --git a/ruralHouses/src/gui/IntroduceOfferGUI.java b/ruralHouses/src/gui/LoginGUI.java similarity index 70% rename from ruralHouses/src/gui/IntroduceOfferGUI.java rename to ruralHouses/src/gui/LoginGUI.java index ee56946..ef333d6 100644 --- a/ruralHouses/src/gui/IntroduceOfferGUI.java +++ b/ruralHouses/src/gui/LoginGUI.java @@ -14,7 +14,7 @@ import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color; -public class IntroduceOfferGUI extends JFrame { +public class LoginGUI extends JFrame { private static final long serialVersionUID= 1L; private JPanel jContentPane = null; @@ -23,7 +23,7 @@ public class IntroduceOfferGUI extends JFrame { private LoginManagerInterface loginManager = new LoginManager(); private JLabel loginFeedback; - public IntroduceOfferGUI() { + public LoginGUI() { super(); initialize(); } @@ -60,25 +60,8 @@ public class IntroduceOfferGUI extends JFrame { JButton loginButton = new JButton("Login"); loginButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - Owner owner = loginManager.checkCredentials(usernameField.getText(),new String(passwordField.getPassword())); - if(owner==null){ - loginFeedback.setText("Incorrect username or password"); - }else{ - Vector ownerHouseList=null; - try{ - ownerHouseList = StartWindow.getBusinessLogic().getRuralHouses(owner); - }catch (Exception e){ - e.printStackTrace(); - } - if(!ownerHouseList.isEmpty()){ - Frame a = new IntroduceOffer2GUI(ownerHouseList); - a.setVisible(true); - } - else if(ownerHouseList.isEmpty()) - loginFeedback.setText("Login OK , but no houses in your name"); - } - - } + jButton_ActionPerformed(arg0); + } }); loginButton.setBounds(164, 179, 117, 25); jContentPane.add(loginButton); @@ -91,7 +74,28 @@ public class IntroduceOfferGUI extends JFrame { } return jContentPane; } + + private void jButton_ActionPerformed(ActionEvent e){ + Owner owner = loginManager.checkCredentials(usernameField.getText(),new String(passwordField.getPassword())); + if(owner==null){ + loginFeedback.setText("Incorrect username or password"); + }else{ + Vector ownerHouseList=null; + try{ + ownerHouseList = StartWindow.getBusinessLogic().getRuralHouses(owner); + }catch (Exception e1){ + e1.printStackTrace(); + } + if(!ownerHouseList.isEmpty()){ + this.setVisible(false); + Frame a = new IntroduceOffer2GUI(ownerHouseList); + a.setVisible(true); + } + else if(ownerHouseList.isEmpty()) + loginFeedback.setText("Login OK , but no houses in your name"); + } + } } // @jve:decl-index=0:visual-constraint="222,33" diff --git a/ruralHouses/src/gui/StartWindow.java b/ruralHouses/src/gui/StartWindow.java index f5e5b10..54fe389 100644 --- a/ruralHouses/src/gui/StartWindow.java +++ b/ruralHouses/src/gui/StartWindow.java @@ -87,7 +87,7 @@ public class StartWindow extends JFrame { createOfferButton.setAlignmentX(Component.CENTER_ALIGNMENT); createOfferButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { - JFrame a = new IntroduceOfferGUI(); + JFrame a = new LoginGUI(); a.setVisible(true); } });