From: unknown Date: Tue, 3 Mar 2015 22:07:56 +0000 (+0100) Subject: The code for different owner operations mostly implemented X-Git-Url: https://xp-dev.com/git/RRRRHHHH_Code/commitdiff_plain/d23286250fb8e8263d35aeef5e6d1683be5ac968 The code for different owner operations mostly implemented --- diff --git a/ruralHouses/db/DBjcampos004.yap b/ruralHouses/db/DBjcampos004.yap deleted file mode 100644 index 34e1d5a..0000000 Binary files a/ruralHouses/db/DBjcampos004.yap and /dev/null differ diff --git a/ruralHouses/src/businessLogic/ApplicationFacadeInterface.java b/ruralHouses/src/businessLogic/ApplicationFacadeInterface.java index 215e60b..8863af3 100644 --- a/ruralHouses/src/businessLogic/ApplicationFacadeInterface.java +++ b/ruralHouses/src/businessLogic/ApplicationFacadeInterface.java @@ -68,6 +68,4 @@ public interface ApplicationFacadeInterface extends Remote { public Vector getAllRuralHouses()throws RemoteException, Exception; - public void close() throws RemoteException; - } \ No newline at end of file diff --git a/ruralHouses/src/businessLogic/FacadeImplementation.java b/ruralHouses/src/businessLogic/FacadeImplementation.java index c4670bb..162a38b 100644 --- a/ruralHouses/src/businessLogic/FacadeImplementation.java +++ b/ruralHouses/src/businessLogic/FacadeImplementation.java @@ -75,9 +75,7 @@ public class FacadeImplementation extends UnicastRemoteObject implements Applica return dbMngr.getAllRuralHouses(); } - public void close() throws RemoteException{ - DB4oManager.close(); - } + public Booking createBooking(RuralHouse ruralHouse, Date firstDate, Date lastDate, String bookTelephoneNumber) throws OfferCanNotBeBooked { @@ -90,7 +88,10 @@ public class FacadeImplementation extends UnicastRemoteObject implements Applica exc.printStackTrace(); return null; } + } + + } diff --git a/ruralHouses/src/businessLogic/HouseManager.java b/ruralHouses/src/businessLogic/HouseManager.java new file mode 100644 index 0000000..a876bcd --- /dev/null +++ b/ruralHouses/src/businessLogic/HouseManager.java @@ -0,0 +1,81 @@ +package businessLogic; + +import java.util.Date; +import java.util.Vector; + +import dataAccess.DB4oManager; +import domain.Account; +import domain.Offer; +import domain.Owner; +import domain.RuralHouse; + +public class HouseManager implements HouseManagerInterface { + DB4oManager dbMngr; + + public HouseManager() { + try { + dbMngr = DB4oManager.getInstance(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Override + public boolean registerNewHouse(int houseNumber, Owner owner, String town, + int nRooms, int nKitchens, int nBaths, int nLivings, int nParkings) { + // TODO Auto-generated method stub + 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; + + } + + @Override + public boolean registerNewHouse(int houseNumber, Owner owner, + String description, String town, int nRooms, int nKitchens, + int nBaths, int nLivings, int nParkings) { + // TODO Auto-generated method stub + 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; + } + + @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; + } + +} diff --git a/ruralHouses/src/businessLogic/HouseManagerInterface.java b/ruralHouses/src/businessLogic/HouseManagerInterface.java new file mode 100644 index 0000000..29c97d5 --- /dev/null +++ b/ruralHouses/src/businessLogic/HouseManagerInterface.java @@ -0,0 +1,25 @@ +package businessLogic; + +import java.util.Date; + +import domain.Offer; +import domain.Owner; +import domain.RuralHouse; + +public interface HouseManagerInterface { + + public boolean registerNewHouse(int houseNumber, Owner owner, String town, + int nRooms, int nKitchens, int nBaths, int nLivings, int nParkings); + + public boolean registerNewHouse(int houseNumber, Owner owner, + 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); + + public Offer setOffers(RuralHouse ruralHouse, Date firstDay, Date lastDay, + float price); + +} diff --git a/ruralHouses/src/businessLogic/LoginManager.java b/ruralHouses/src/businessLogic/LoginManager.java index da6078b..966168c 100644 --- a/ruralHouses/src/businessLogic/LoginManager.java +++ b/ruralHouses/src/businessLogic/LoginManager.java @@ -1,8 +1,5 @@ package businessLogic; -import gui.StartWindow; - -import java.rmi.RemoteException; import java.util.Vector; import dataAccess.DB4oManager; @@ -10,12 +7,11 @@ import domain.Account; import domain.Owner; public class LoginManager implements LoginManagerInterface { - //owners to be separated to accounts later on + DB4oManager dbMngr; - - + public LoginManager() { - + try { dbMngr = DB4oManager.getInstance(); } catch (Exception e) { @@ -24,21 +20,20 @@ public class LoginManager implements LoginManagerInterface { } } - @Override public Owner checkCredentials(String usr, String pwd) { try { Vector account = dbMngr.getAccount(usr, pwd); - if(account.isEmpty()) return null; - else return account.get(0).getOwner(); - + if (account.isEmpty()) + return null; + else + return account.get(0).getOwner(); + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } - - - + } diff --git a/ruralHouses/src/dataAccess/DB4oManager.java b/ruralHouses/src/dataAccess/DB4oManager.java index 6d55e8d..295af73 100644 --- a/ruralHouses/src/dataAccess/DB4oManager.java +++ b/ruralHouses/src/dataAccess/DB4oManager.java @@ -7,7 +7,6 @@ import java.util.Vector; //import java.util.Enumeration; //import java.util.Vector; - import businessLogic.BookingManager; import com.db4o.*; @@ -16,146 +15,209 @@ import configuration.Config; import domain.*; import exceptions.OfferCanNotBeBooked; -public class DB4oManager { +public class DB4oManager { private static ObjectContainer db; - private static DB4oManager theDB4oManager=new DB4oManager(); + private static DB4oManager theDB4oManager = new DB4oManager(); + /** + * + */ private DB4oManager() { - Config c=Config.getInstance(); - String dataBaseOpenMode=c.getDataBaseOpenMode(); + Config c = Config.getInstance(); + String dataBaseOpenMode = c.getDataBaseOpenMode(); DB4oManager.openDatabase(dataBaseOpenMode); } + /** + * @param mode + */ public static void openDatabase(String mode) { - Config c=Config.getInstance(); - String db4oFileName=c.getDb4oFilename(); - if (mode.compareTo("open")==0) { - db=Db4o.openFile(Db4o.newConfiguration(), db4oFileName); + Config c = Config.getInstance(); + String db4oFileName = c.getDb4oFilename(); + if (mode.compareTo("open") == 0) { + db = Db4o.openFile(Db4o.newConfiguration(), db4oFileName); db.ext().configure().updateDepth(5); - System.out.println("DataBase Opened"); - } else if (mode.compareTo("initialize")==0){ - new File(db4oFileName).delete(); - db=Db4o.openFile(Db4o.newConfiguration(), db4oFileName); - db.ext().configure().updateDepth(5); - Owner jon = new Owner("Jon"); - Owner alfredo = new Owner("Alfredo"); - jon.addRuralHouse(1, "Ezkioko etxea","Ezkio"); - jon.addRuralHouse(2, "Eskiatzeko etxea","Jaca"); - jon.setBankAccount("1349 5677 21 2133567777"); - alfredo.addRuralHouse(3, "Casa del abuelo","Pitillas"); - alfredo.addRuralHouse(4, "Refugio","Murgia"); - alfredo.setBankAccount("4144 0087 23 9700002133"); - Account jonAcc = new Account("userJon", "passJon",jon); - Account alfredoAcc = new Account("userAlfredo", "passAlfredo",alfredo); - db.store(jon); - db.store(alfredo); - db.store(jonAcc); - db.store(alfredoAcc); - db.commit(); - System.out.println("DataBase Initialized"); + } else if (mode.compareTo("initialize") == 0) { + try { + new File(db4oFileName).delete(); + db = Db4o.openFile(Db4o.newConfiguration(), db4oFileName); + db.ext().configure().updateDepth(5); + Owner jon = new Owner("Jon"); + Owner alfredo = new Owner("Alfredo"); + jon.addRuralHouse(1, "Ezkioko etxea", "Ezkio", 3, 3, 3, 3, 3); + jon.addRuralHouse(2, "Eskiatzeko etxea", "Jaca", 4, 4, 4, 4, 4); + jon.setBankAccount("1349 5677 21 2133567777"); + alfredo.addRuralHouse(3, "Casa del abuelo", "Pitillas", 5, 5, + 5, 5, 5); + alfredo.addRuralHouse(4, "Murgia", 6, 6, 6, 6, 6); + alfredo.setBankAccount("4144 0087 23 9700002133"); + Account jonAcc = new Account("userJon", "passJon", jon); + Account alfredoAcc = new Account("userAlfredo", "passAlfredo", + alfredo); + db.store(jon); + db.store(alfredo); + db.store(jonAcc); + db.store(alfredoAcc); + db.commit(); + System.out.println("DataBase Initialized"); + } finally { + db.close(); + } + } } - public static ObjectContainer getContainer() { + /** + * @return + */ + public static ObjectContainer getContainer() { return db; } - public static void close(){ - db.close(); - System.out.println("DataBase closed"); + + /** + * @param rh + */ + public void storeRuralHouses(RuralHouse rh) { + DB4oManager.openDatabase("open"); + ObjectContainer db = DB4oManager.getContainer(); + try { + db.store(rh); + db.commit(); + } finally { + db.close(); + } } - + /** - * This method finds all existing owners + * This method finds all existing owners * */ - public Vector getOwners() throws RemoteException, - Exception { - ObjectContainer db=DB4oManager.getContainer(); + public Vector getOwners() throws RemoteException, Exception { + DB4oManager.openDatabase("open"); + ObjectContainer db = DB4oManager.getContainer(); try { - Owner proto = new Owner(null,null); - ObjectSet result = db.queryByExample(proto); - Vector owners=new Vector(); - while(result.hasNext()) - owners.add((Owner)result.next()); + Owner proto = new Owner(null, null); + ObjectSet result = db.queryByExample(proto); + Vector owners = new Vector(); + while (result.hasNext()) + owners.add((Owner) result.next()); return owners; } finally { - //db.close(); + db.close(); } - } - - public Vector getAccount(String usr, String pwd) throws RemoteException, - Exception { - ObjectContainer db=DB4oManager.getContainer(); + } + + /** + * @param usr + * @param pwd + * @return + * @throws RemoteException + * @throws Exception + */ + public Vector getAccount(String usr, String pwd) + throws RemoteException, Exception { + DB4oManager.openDatabase("open"); + ObjectContainer db = DB4oManager.getContainer(); try { - Account proto = new Account(usr,pwd, new Owner(null,null)); - ObjectSet result = db.queryByExample(proto); - Vector accounts=new Vector(); - while(result.hasNext()) - accounts.add((Account)result.next()); + Account proto = new Account(usr, pwd, new Owner(null, null)); + ObjectSet result = db.queryByExample(proto); + Vector accounts = new Vector(); + while (result.hasNext()) + accounts.add((Account) result.next()); return accounts; } finally { - //db.close(); + db.close(); } } - + + /** + * @return + * @throws RemoteException + * @throws Exception + */ public Vector getAllRuralHouses() throws RemoteException, - Exception { - ObjectContainer db=DB4oManager.getContainer(); + Exception { + DB4oManager.openDatabase("open"); + ObjectContainer db = DB4oManager.getContainer(); try { - RuralHouse proto = new RuralHouse(0,null,null,null); - ObjectSet result = db.queryByExample(proto); - Vector ruralHouses=new Vector(); - while(result.hasNext()) - ruralHouses.add((RuralHouse)result.next()); + RuralHouse proto = new RuralHouse(0, null, null, null, 0, 0, 0, 0, + 0); + ObjectSet result = db.queryByExample(proto); + Vector ruralHouses = new Vector(); + while (result.hasNext()) + ruralHouses.add((RuralHouse) result.next()); return ruralHouses; } finally { - //db.close(); + db.close(); } } /** - * This method creates an offer with a house number, first day, last day and price - * precondition: There are no overlapping offers + * This method creates an offer with a house number, first day, last day and + * price precondition: There are no overlapping offers + * * @param House * number, start day, last day and price * @return None */ - public Offer createOffer(RuralHouse ruralHouse, Date firstDay, Date lastDay, - float price) throws RemoteException, Exception { - ObjectContainer db=DB4oManager.getContainer(); - RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), null, - ruralHouse.getDescription(), ruralHouse.getTown()); - ObjectSet result = db.queryByExample(proto); - RuralHouse rh=(RuralHouse)result.next(); - Offer o=rh.createOffer(firstDay, lastDay, price); - db.store(o); - db.commit(); + public Offer createOffer(RuralHouse ruralHouse, Date firstDay, + Date lastDay, float price) throws RemoteException, Exception { + DB4oManager.openDatabase("open"); + ObjectContainer db = DB4oManager.getContainer(); + Offer o; + try { + RuralHouse proto = new RuralHouse(ruralHouse.getHouseNumber(), + null, ruralHouse.getDescription(), ruralHouse.getTown(), 0, + 0, 0, 0, 0); + ObjectSet result = db.queryByExample(proto); + RuralHouse rh = (RuralHouse) result.next(); + o = rh.createOffer(firstDay, lastDay, price); + db.store(o); + db.commit(); + } finally { + db.close(); + } return o; } - public RuralHouse getRuralHouse(RuralHouse rh){ + /** + * @param rh + * @return + */ + public RuralHouse getRuralHouse(RuralHouse rh) { + + DB4oManager.openDatabase("open"); try { - ObjectContainer db=DB4oManager.getContainer(); + ObjectContainer db = DB4oManager.getContainer(); RuralHouse proto = new RuralHouse(rh.getHouseNumber(), null, - rh.getDescription(), rh.getTown()); - ObjectSet result = db.queryByExample(proto); - return rh=(RuralHouse)result.next(); + rh.getDescription(), rh.getTown(), 0, 0, 0, 0, 0); + ObjectSet result = db.queryByExample(proto); + return rh = (RuralHouse) result.next(); } catch (Exception exc) { exc.printStackTrace(); return null; + } finally { + db.close(); } } + /** + * @param offer + * @param clientTelephoneNumber + * @return + * @throws OfferCanNotBeBooked + */ public Booking createBooking(Offer offer, String clientTelephoneNumber) throws OfferCanNotBeBooked { + DB4oManager.openDatabase("open"); try { - Booking b=null; - if (offer!=null) { - b=offer.createBook(clientTelephoneNumber); + Booking b = null; + if (offer != null) { + b = offer.createBook(clientTelephoneNumber); db.store(b); db.store(offer); db.commit(); @@ -164,17 +226,18 @@ public class DB4oManager { } catch (Exception exc) { exc.printStackTrace(); return null; + } finally { + db.close(); } } /** - * This method returns the instance of the DB4oManager class + * This method returns the instance of the DB4oManager class * * @return the db4o manager */ - public static DB4oManager getInstance() { + public static DB4oManager getInstance() { return theDB4oManager; } } - diff --git a/ruralHouses/src/domain/HouseFeatures.java b/ruralHouses/src/domain/HouseFeatures.java new file mode 100644 index 0000000..c26de8a --- /dev/null +++ b/ruralHouses/src/domain/HouseFeatures.java @@ -0,0 +1,21 @@ +package domain; + +public class HouseFeatures { + + private int nRooms; + private int nKitchens; + private int nBaths; + private int nLivings; + private int nParkings; + + public HouseFeatures(int nRooms, int nKitchens, int nBaths, int nLivings, + int nParkings) { + super(); + this.nRooms = nRooms; + this.nKitchens = nKitchens; + this.nBaths = nBaths; + this.nLivings = nLivings; + this.nParkings = nParkings; + } + +} diff --git a/ruralHouses/src/domain/Owner.java b/ruralHouses/src/domain/Owner.java index 35f390c..9a34067 100644 --- a/ruralHouses/src/domain/Owner.java +++ b/ruralHouses/src/domain/Owner.java @@ -7,18 +7,18 @@ import java.util.Vector; public class Owner implements Serializable { private String bankAccount = ""; - private String name=""; + private String name = ""; private Vector ruralHouses; public Owner(String name) { - this.name=name; - ruralHouses=new Vector(); + this.name = name; + ruralHouses = new Vector(); } public Owner(String name, String bankAccount) { - this.bankAccount=bankAccount; - this.name=name; - ruralHouses=new Vector(); + this.bankAccount = bankAccount; + this.name = name; + ruralHouses = new Vector(); } public String getName() { @@ -41,13 +41,24 @@ public class Owner implements Serializable { return ruralHouses; } - public RuralHouse addRuralHouse(int houseNumber, String description, String city) { - RuralHouse rh=new RuralHouse(houseNumber, this, description, city); + 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); + ruralHouses.add(rh); + return rh; + } + + 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); ruralHouses.add(rh); return rh; } - - public String toString(){ + + public String toString() { return name; } diff --git a/ruralHouses/src/domain/RuralHouse.java b/ruralHouses/src/domain/RuralHouse.java index 13a751d..d499c88 100644 --- a/ruralHouses/src/domain/RuralHouse.java +++ b/ruralHouses/src/domain/RuralHouse.java @@ -13,17 +13,30 @@ public class RuralHouse implements Serializable { private String description; private Owner owner; private String town; + private HouseFeatures features; public Vector offers; public RuralHouse() { super(); } - public RuralHouse(int houseNumber, Owner owner, String description, String town) { + public RuralHouse(int houseNumber, Owner owner, String description, String town , int nRooms,int nKitchens,int nBaths, + int nLivings,int nParkings) { this.houseNumber = houseNumber; this.description = description; this.owner = owner; this.town = town; + this.features = new HouseFeatures(nRooms,nKitchens,nBaths,nLivings,nParkings); + offers=new Vector(); + } + + public RuralHouse(int houseNumber, Owner owner, String town , int nRooms,int nKitchens,int nBaths, + int nLivings,int nParkings) { + this.houseNumber = houseNumber; + this.description = null; + this.owner = owner; + this.town = town; + this.features = new HouseFeatures(nRooms,nKitchens,nBaths,nLivings,nParkings); offers=new Vector(); } @@ -140,5 +153,7 @@ public class RuralHouse implements Serializable { } return null; } + + } diff --git a/ruralHouses/src/gui/BookRuralHouseGUI.java b/ruralHouses/src/gui/BookRuralHouseGUI.java index fead030..eadd8b0 100644 --- a/ruralHouses/src/gui/BookRuralHouseGUI.java +++ b/ruralHouses/src/gui/BookRuralHouseGUI.java @@ -136,8 +136,8 @@ public class BookRuralHouseGUI extends JFrame { ApplicationFacadeInterface facade=StartWindow.getBusinessLogic(); Booking book=facade.createBooking(house, firstDay, lastDay, telephone); - System.out.println(book.getPrice()); if (book!=null) { + System.out.println(book.getPrice()); BookRuralHouseConfirmationWindow confirmWindow=new BookRuralHouseConfirmationWindow(book); confirmWindow.setVisible(true); } diff --git a/ruralHouses/src/gui/IntroduceOfferGUI.java b/ruralHouses/src/gui/IntroduceOfferGUI.java index 8d34d9d..ee56946 100644 --- a/ruralHouses/src/gui/IntroduceOfferGUI.java +++ b/ruralHouses/src/gui/IntroduceOfferGUI.java @@ -71,7 +71,7 @@ public class IntroduceOfferGUI extends JFrame { e.printStackTrace(); } if(!ownerHouseList.isEmpty()){ - Frame a = new IntroduceOffer2GUI(ownerHouseList); + Frame a = new IntroduceOffer2GUI(ownerHouseList); a.setVisible(true); } else if(ownerHouseList.isEmpty()) diff --git a/ruralHouses/src/gui/StartWindow.java b/ruralHouses/src/gui/StartWindow.java index 98b912e..f5e5b10 100644 --- a/ruralHouses/src/gui/StartWindow.java +++ b/ruralHouses/src/gui/StartWindow.java @@ -122,11 +122,11 @@ public class StartWindow extends JFrame { //JFrame a = new QueryAvailabilityWindow(); ApplicationFacadeInterface facade=StartWindow.facadeInterface; try { - facade.close(); + setVisible(false); System.exit(1); } - catch (RemoteException e1) { + catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } @@ -160,7 +160,6 @@ public class StartWindow extends JFrame { //System.out.println(e.toString()); e.printStackTrace(); } - int x=0; JFrame a = new StartWindow(); a.setVisible(true); }