Minor error correction and some bugs, alongside with modify offers quality of life...
[RRRRHHHH_Code] / ruralHouses / src / domain / RuralHouse.java
1 package domain;
2
3 import java.io.Serializable;
4 import java.util.Date;
5 import java.util.Iterator;
6 import java.util.Vector;
7
8 public class RuralHouse implements Serializable {
9
10         private static final long serialVersionUID = 1L;
11
12         private String houseName;
13         private String description;
14         private Owner owner;
15         private String district; 
16         private HouseFeatures features;
17         public Vector<Offer> offers;
18         public boolean isAccepted;
19         
20         public RuralHouse() {
21                 super();
22         }
23
24         public RuralHouse(String houseName, Owner owner, String description, String ds , HouseFeatures features) {
25                 this.houseName = houseName;
26                 this.description = description;
27                 this.owner = owner;
28                 this.district = ds;
29                 this.features = features;
30                 offers=new Vector<Offer>();
31         }
32         
33         
34
35         public String getHouseName() {
36                 return houseName;
37         }
38
39         public void setHouseName(String houseName) {
40                 this.houseName = houseName;
41         }
42
43         public String getDescription() {
44                 return description;
45         }
46         
47         public void setDescription(String description) {
48                 this.description=description;
49         }
50
51         public Owner getOwner() {
52                 return owner;
53         }
54
55         public void setOwner(Owner owner) {
56                 this.owner=owner;
57         }
58         
59         public String getDistrict() {
60                 return district;
61         }
62         
63         public void setDistrict(String ds) {
64                 this.district=ds;
65         }
66         public HouseFeatures getFeatures() {
67                 return features;
68         }
69
70         public void setFeatures(HouseFeatures features) {
71                 this.features = features;
72         }       
73         public String toString() {
74                 return this.houseName + ": " + this.district;
75         }
76         
77         public Offer createOffer(int offerNumber,Date firstDay, Date lastDay, float price)  {
78         Offer off=new Offer(offerNumber,this,firstDay,lastDay,price);
79         offers.add(off);
80         return off;
81         }
82
83
84
85
86
87         @Override
88         public boolean equals(Object obj) {
89                 if (this == obj)
90                         return true;
91                 if (obj == null)
92                         return false;
93                 if (getClass() != obj.getClass())
94                         return false;
95                 RuralHouse other = (RuralHouse) obj;
96                 if (houseName != other.houseName)
97                         return false;
98                 return true;
99         }
100         
101         public String getAccountNumber(int houseName) {
102                 /*try {
103                         dbMngr=DBManager.getInstance();
104                         return dbMngr.getOwner(houseName).getBankAccount();
105
106                 } catch (Exception e) {
107                         System.out.println("Error, accessing to DB Manager: "
108                                         + e.toString());
109                         return null;
110                 }*/ return null;
111         }
112         
113         /**
114          * This method obtains available offers for a concrete house in a certain period 
115          * 
116          * @param houseName, the house number where the offers must be obtained 
117          * @param firstDay, first day in a period range 
118          * @param lastDay, last day in a period range
119          * @return a vector of offers(Offer class)  available  in this period
120          */
121         public Vector<Offer> getOffers( Date firstDay,  Date lastDay) {
122                 Vector<Offer> availableOffers=new Vector<Offer>();
123                 Iterator<Offer> e=offers.iterator();
124                 Offer offer;
125                 while (e.hasNext()){
126                         offer=e.next();
127                         if ( (offer.getFirstDay().compareTo(firstDay)>=0) && (offer.getLastDay().compareTo(lastDay)<=0) && (offer.getBooking()==null) )
128                                 availableOffers.add(offer);
129                 }
130                 return availableOffers;
131         }
132         
133         public Vector<Offer> getAllOffers() {
134                 Vector<Offer> availableOffers=new Vector<Offer>();
135                 Iterator<Offer> e=offers.iterator();
136                 Offer offer;
137                 while (e.hasNext()){
138                         offer=e.next();
139                         if ( (offer.getBooking()==null) )
140                                 availableOffers.add(offer);
141                 }
142                 return availableOffers;
143         }
144         /**
145          * This method obtains the offer that match exactly with a given dates that has not been booked
146          * 
147          * @param firstDay, first day in a period range 
148          * @param lastDay, last day in a period range
149          * @return the  offer(Offer class)  available  for a this period
150          */
151         public Offer findOffer( Date firstDay,  Date lastDay) {         
152                 Iterator<Offer> e=offers.iterator();
153                 Offer offer=null;
154                 while (e.hasNext()){
155                         offer=e.next();
156                         if ( (offer.getFirstDay().compareTo(firstDay)==0) && (offer.getLastDay().compareTo(lastDay)==0) && (offer.getBooking()==null) )
157                                 return offer;
158                 }
159                 return null;            
160         }
161         
162         
163 public Offer overlapsWith( Date firstDay,  Date lastDay) {
164                 
165                 Iterator<Offer> e=offers.iterator();
166                 Offer offer=null;
167                 while (e.hasNext()){
168                         offer=e.next();
169                         if ( (offer.getFirstDay().compareTo(lastDay)<0) && (offer.getLastDay().compareTo(firstDay)>0))
170                                 return offer;
171                 }
172                 return null;
173                 
174         }
175
176 }