Merge branch 'master' of https://xp-dev.com/git/RRRRHHHH_Code
[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         private Vector<Offer> offers;
18         private boolean isAccepted;
19
20
21         public RuralHouse() {
22                 super();
23         }
24
25         public RuralHouse(String houseName, Owner owner, String description,
26                         String ds, HouseFeatures features) {
27                 this.houseName = houseName;
28                 this.description = description;
29                 this.owner = owner;
30                 this.district = ds;
31                 this.features = features;
32                 offers = new Vector<Offer>();
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
67         public HouseFeatures getFeatures() {
68                 return features;
69         }
70
71         public void setFeatures(HouseFeatures features) {
72                 this.features = features;
73         }
74
75         public String toString() {
76                 return this.houseName + ": " + this.district;
77         }
78
79         public Offer createOffer(int offerNumber, Date firstDay, Date lastDay,
80                         float price) {
81                 Offer off = new Offer(offerNumber, this, firstDay, lastDay, price);
82                 offers.add(off);
83                 return off;
84         }
85
86         @Override
87         public boolean equals(Object obj) {
88                 if (this == obj)
89                         return true;
90                 if (obj == null)
91                         return false;
92                 if (getClass() != obj.getClass())
93                         return false;
94                 RuralHouse other = (RuralHouse) obj;
95                 if (houseName == null) {
96                         if (other.houseName != null)
97                                 return false;
98                 } else if (!houseName.equals(other.houseName))
99                         return false;
100                 return true;
101         }
102
103         /**
104          * This method obtains available offers for a concrete house in a certain
105          * period
106          * 
107          * @param houseName
108          *            , the house number where the offers must be obtained
109          * @param firstDay
110          *            , first day in a period range
111          * @param lastDay
112          *            , last day in a period range
113          * @return a vector of offers(Offer class) available in this period
114          */
115         public Vector<Offer> getOffers(Date firstDay, Date lastDay) {
116                 Vector<Offer> availableOffers = new Vector<Offer>();
117                 Iterator<Offer> e = offers.iterator();
118                 Offer offer;
119                 while (e.hasNext()) {
120                         offer = e.next();
121                         if ((offer.getFirstDay().compareTo(firstDay) >= 0)
122                                         && (offer.getLastDay().compareTo(lastDay) <= 0)
123                                         && (!offer.isBooked()))
124                                 availableOffers.add(offer);
125                 }
126                 return availableOffers;
127         }
128
129         public Vector<Offer> getAllOffers() {
130
131                 return this.offers;
132         }
133
134         /**
135          * This method obtains the offer that match exactly with a given dates that
136          * has not been booked
137          * 
138          * @param firstDay
139          *            , first day in a period range
140          * @param lastDay
141          *            , last day in a period range
142          * @return the offer(Offer class) available for a this period
143          */
144         public Offer findOffer(Date firstDay, Date lastDay) {
145                 Iterator<Offer> e = offers.iterator();
146                 Offer offer = null;
147                 while (e.hasNext()) {
148                         offer = e.next();
149                         if ((offer.getFirstDay().compareTo(firstDay) == 0)
150                                         && (offer.getLastDay().compareTo(lastDay) == 0)
151                                         && (!offer.isBooked()))
152                                 return offer;
153                 }
154                 return null;
155         }
156
157         public Offer overlapsWith(Date firstDay, Date lastDay) {
158
159                 Iterator<Offer> e = offers.iterator();
160                 Offer offer = null;
161                 while (e.hasNext()) {
162                         offer = e.next();
163                         if ((offer.getFirstDay().compareTo(lastDay) < 0)
164                                         && (offer.getLastDay().compareTo(firstDay) > 0))
165                                 return offer;
166                 }
167                 return null;
168
169         }
170
171         public boolean isAccepted() {
172                 return isAccepted;
173         }
174
175         public void setAccepted(boolean isAccepted) {
176                 this.isAccepted = isAccepted;
177         }
178
179 }