-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBranch.java
More file actions
416 lines (348 loc) · 9.59 KB
/
Copy pathBranch.java
File metadata and controls
416 lines (348 loc) · 9.59 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package components;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
/**
* class Branch represents a post office
* @author Nadav Ishai, ID: 206119893
*
*/
public class Branch implements Node, Runnable, Cloneable{
private static int branchNum = -1;
private int branchId;
private String branchName;
protected ArrayList <Truck> listTrucks = new ArrayList <Truck>();
protected ArrayList <Package> listPackages = new ArrayList <Package>();
private static ArrayList <Branch> BranchesList = new ArrayList <Branch>();
private ArrayList <Branch> mementoBranches = new ArrayList <Branch>();
protected double xmiddle;
protected double ymiddle;
protected boolean threadSuspend = false;
protected boolean stop = false;
protected double hubx;
protected double huby;
// private ArrayList<Thread> tvan = new ArrayList<Thread>();
// private ArrayList<Thread> mementotvantvan = new ArrayList<Thread>();
public Branch(){
branchId = branchNum;
if (branchId == -1)
branchName = "HUB";
else
branchName = "Branch " + branchId;
branchNum += 1;
listTrucks = new ArrayList <Truck>();
listPackages = new ArrayList <Package>();
BranchesList.add(this);
System.out.printf("Creating Branch %d, branch name:%s, packages: %d, trucks: %d\n", branchId, branchName, listPackages.size(), listTrucks.size());
}
Branch(String branchName){
if (branchId == -1)
{
branchId= - 1;
this.branchName = "HUB";
}
else
{
branchId = (int)(branchName.charAt(branchName.length() - 1) - '0');
this.branchName = branchName;
}
branchNum += 1;
listTrucks = new ArrayList <Truck>();
listPackages = new ArrayList <Package>();
BranchesList.add(this);
}
Branch(Branch b){
branchId = b.GetBranchId();
branchName = b.GetBranchName();
listTrucks = new ArrayList <Truck>();
for(Truck t : b.listTrucks)
if(t instanceof Van)
listTrucks.add(new Van(t));
listPackages = new ArrayList <Package>();
for(Package p : b.listPackages)
{
if(p instanceof SmallPackage)
listPackages.add(new SmallPackage(p));
else if(p instanceof StandardPackage)
listPackages.add(new StandardPackage(p));
}
xmiddle = b.GetXMiddle();
ymiddle = b.GetYMiddle();
threadSuspend = b.GetThreadSuspend();
stop = b.GetStop();
hubx = b.GetHubX();
huby = b.GetHubY();
}
Branch(Hub b, boolean flag){
branchId = -1;
branchName = "HUB";
}
/**
* Function AddStandardTruck adding a new StandardTruck to the Hub truck's list
*/
public void AddStandardTruck() {
listTrucks.add(new StandardTruck());
}
/**
* Function AddNonStandardTruck adding a new NonStandardTruck to the Hub truck's list
*/
public void AddNonStandardTruck() {
listTrucks.add(new NonStandardTruck());
}
/**
* Function AddNonStandardTruck adding a new Van to the Branch truck's list
*/
public void AddVan(int size) {
for(int i = 0; i < size; i++)
listTrucks.add(new Van());
}
/**
* Function GetBranchName return the branch name
* @return branchName
*/
public String GetBranchName() {
return branchName;
}
/**
* Function GetBranchId return the branch id
* @return branchId
*/
public int GetBranchId() {
return branchId;
}
/**
* Function GetTruckList return the branch truck's list
* @return listTrucks
*/
public ArrayList<Truck> GetTruckList(){
return listTrucks;
}
/**
* Function GetPackagesList return the branch packages's list
* @return listPackages
*/
public ArrayList<Package> GetPackagesList(){
return listPackages;
}
/**
* Function collectPackage is searching for an available Van and link him to package that waits to collection and determine the Van driving time
*/
public void collectPackage(Package p) {
for (int j = 0; j < GetTruckList().size(); j++)
{
if(GetTruckList().get(j).GetAavilableStatus())
{
int time = ((p.GetSenderAddress().Get_Street() % 10) + 1) * 10;
GetTruckList().get(j).SetTimeLeft(time);
GetTruckList().get(j).SetSaveTime(time);
GetTruckList().get(j).SetToNoAvailable();
p.addTracking(GetTruckList().get(j), Status.COLLECTION);
GetTruckList().get(j).GetPackagesList().add(p);
System.out.printf("Van %d is collecting package %d, time to arrive: %d\n", GetTruckList().get(j).GetTruckId(), p.GetPackageId(), time);
break;
}
}
}
/**
* Function deliverPackage is searching for an available Van and link him to package that waits to distribution and determine the Van driving time
*/
public void deliverPackage(Package p) {
for (int j = 0; j < GetTruckList().size(); j++) {
if(GetTruckList().get(j).GetAavilableStatus())
{
int time = ((p.GetDestinationAddress().Get_Street() % 10) + 1) * 10;
GetTruckList().get(j).SetTimeLeft(time);
GetTruckList().get(j).SetSaveTime(time);
GetTruckList().get(j).SetToNoAvailable();
p.addTracking(GetTruckList().get(j), Status.DISTRIBUTION);
GetTruckList().get(j).GetPackagesList().add(p);
break;
}
}
}
/**
* Function work perform one work unit of all the vans and run the branch's vans work function
*/
public synchronized void work() {
for (int i = 0; i < GetPackagesList().size(); i++)
{
if(GetPackagesList().get(i).equals("CREATION"))
{
GetPackagesList().get(i).ChangeStatus(Status.COLLECTION);
synchronized(this) {
collectPackage(GetPackagesList().remove(i));
}
}
else if(GetPackagesList().get(i).equals("DELIVERY"))
{
GetPackagesList().get(i).ChangeStatus(Status.DISTRIBUTION);
synchronized(this) {
deliverPackage(GetPackagesList().remove(i));
}
}
}
for (int i = 0; i < GetTruckList().size(); i++)
GetTruckList().get(i).work();
}
/**
* Function GetHubX returns hub's x middle position
* @return int hubx
*/
public double GetHubX() {
return hubx;
}
/**
* Function GetHubY returns hub's y middle position
* @return int huby
*/
public double GetHubY() {
return huby;
}
/**
* Function DrawBranch draws all the branches of the office
* @param g Graphics parameter
* @param width the branch width
* @param height the branch height
*/
public void DrawBranch(Graphics g, int width, int height) {
// synchronized(this) {
Graphics2D g2d = (Graphics2D) g;
int branch_number = Hub.branches.size() - 1;
int id = Hub.branches.indexOf(this) + 1;
double middle;
double xstart = 20, ystart;
double space = (int)(450 / branch_number);
middle = (branch_number / 2) + 1;
ystart = (int)(280 +((id - middle) * space));
xmiddle = width / 2 + xstart;
ymiddle = height / 2 + ystart;
if(listPackages.size() == 0)
g2d.setColor(Color.cyan);
else
g2d.setColor(Color.blue);
g2d.fill(new Rectangle2D.Double(xstart, ystart, width, height));
}
// }
public void DrawPath(Graphics g, int xhub, int yhub) {
Graphics2D g2d = (Graphics2D) g;
int branch_number = BranchesList.size() - 1;
double xstart = xmiddle + 20, ystart = ymiddle, xend = 1200 - 60, yend;
yend = 200 + ((180 / branch_number) * (branchId + 1));
hubx = xend;
huby = yend;
g2d.setColor(new Color(0, 150, 0));
g2d.draw(new Line2D.Double(xstart, ystart, xend, yend));
}
/**
* Function GeXMiddle returns current brunch x middle position
* @return int xmiddle
*/
public double GetXMiddle() {
return xmiddle;
}
/**
* Function GeYMiddle returns current brunch y middle position
* @return int ymiddle
*/
public double GetYMiddle() {
return ymiddle;
}
/**
* Function run starts the branches work
*/
@Override
public void run() {
for(Truck van : GetTruckList())
{
Thread t = new Thread((Runnable)van);
t.start();
}
while(true) {
try {
Thread.sleep(500);
synchronized(this) {
while (threadSuspend)
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if (stop) return;
work();
}
}
/**
* Function SetSuspend suspends the branch thread
*/
@Override
public synchronized void setSuspend(){
for(Truck truck : GetTruckList())
truck.setSuspend();
threadSuspend = true;
}
/**
* Function SetResume resumes the branch thread
*/
@Override
public synchronized void setResume() {
for(Truck truck : GetTruckList())
truck.setResume();
threadSuspend = false;
notifyAll();
}
/**
* Function SetStop stops the branch thread
*/
public void setStop() {
for(Truck truck : GetTruckList())
truck.setStop();
stop = true;
}
/**
* function to clone a branch
*/
public Branch clone() {
Branch b = new Branch();
for(Truck v : GetTruckList())
b.listTrucks.add(new Van());
return b;
}
/**
* function to set branch list
* @param b
*/
public static void SetStaticBranchesList(ArrayList<Branch> b) {
BranchesList = b;
}
/**
* function to branch list
*
*/
public ArrayList <Branch> GetStaticBranchesList() {
return BranchesList;
}
/**
* function to return thread suspend
*
*/
public boolean GetThreadSuspend() {
return threadSuspend;
}
/**
* function to return stop
*
*/
public boolean GetStop() {
return stop;
}
/**
* function to deacrese branch id
*
*/
public static void DecreaseBranchId() {
branchNum -= 1;
}
}