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