-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyDao.java
More file actions
183 lines (164 loc) · 4.82 KB
/
PropertyDao.java
File metadata and controls
183 lines (164 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package estate;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PropertyDao {
private final Lock lock = new ReentrantLock();
public int[] getNumOfProperties() {
lock.lock();
try {
List<Property> propertyList = getProperties();
int houses = 1;
int apartments = 1;
for(int i = 0; i < propertyList.size(); i++ ) {
if(propertyList.get(i).getType().equals("H")) {
houses++;
} else {
apartments++;
}
}
int[] result = {houses, apartments};
return result;
} finally {
lock.unlock();
}
}
@SuppressWarnings("unchecked")
public List<Property> getProperties() {
List<Property> propertyList = new LinkedList<Property>();
lock.lock();
try {
try {
File file = new File("Properties.dat");
if (!file.exists()) {
// Create initial properties if the file doesn't exist
Property p1 = new Property("H1", 3, 2, 375000, "H");
p1.setStartTime(new Date().getTime());
p1.setEndTime(generateTime(2));
Property p2 = new Property("H2", 5, 3, 360000, "H");
p2.setStartTime(new Date().getTime());
p2.setEndTime(generateTime(3));
Property p3 = new Property("H3", 3, 3, 500000, "H");
p3.setStartTime(new Date().getTime());
p3.setEndTime(generateTime(4));
propertyList.add(p1);
propertyList.add(p2);
propertyList.add(p3);
savePropertyList(propertyList);
}
else{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
propertyList = (List<Property>) ois.readObject();
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// Update property sold status if end date has been reached
setSoldStatus(propertyList);
return propertyList;
} finally {
lock.unlock();
}
}
private void updateProperty(Property home){
lock.lock();
try {
try {
File file = new File("Properties.dat");
String property = home.getName();
List<Property> props = getProperties();
for(int i = 0; i < props.size(); i++) {
if(property.equals(props.get(i).getName())){
props.remove(i);
break;
}
}
props.add(home);
FileOutputStream change = new FileOutputStream(file, false);
ObjectOutputStream oos = new ObjectOutputStream(change);
oos.writeObject(props);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
}
public String bidding(String name1, int bid)
{
List<Property> props = getProperties();
for(int i = 0; i < props.size(); i++) {
if(props.get(i).getName().equals(name1)){
if(bid > props.get(i).getBid() && bid > props.get(i).getPrice()){
props.get(i).setBid(bid);
updateProperty(props.get(i));
return "Successful bid";
}
}
}
return "Unsuccessful bid";
}
public void addProperty(Property p) {
List<Property> propertyList = getProperties();
// Add to list and save
propertyList.add(p);
savePropertyList(propertyList);
}
private void savePropertyList(List<Property> propertyList){
lock.lock();
try {
try {
File file = new File("Properties.dat");
FileOutputStream fos;
fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(propertyList);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
}
private long generateTime(int months){
Date date = new Date();
long current = date.getTime();
Long month = 2628000000L;
// 2628000000 is a month in milliseconds
Long endDate = current +(new Long(months)*month);
return endDate;
}
private void setSoldStatus(List<Property> p) {
lock.lock();
try {
for(int i = 0; i < p.size(); i++) {
Date date = new Date();
if(date.getTime() > p.get(i).getEndTime()) {
// Time has ended for the property so its status is set to sold
p.get(i).setSold(true);
}
}
} finally {
lock.unlock();
}
}
}