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