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