-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeliveryPerson.java
More file actions
426 lines (386 loc) · 11.3 KB
/
Copy pathDeliveryPerson.java
File metadata and controls
426 lines (386 loc) · 11.3 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
417
418
419
420
421
422
423
424
425
426
import java.util.*;
/**
* Model the common elements of delivery persons.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
* @version 2024.10.07 DP classes
*/
public abstract class DeliveryPerson
{
// The Delivery Company of this DeliveryPerson.
private DeliveryCompany company;
// Where the person is.
private Location location;
// Where the person is headed.
private Location targetLocation;
// Record how often the person has nothing to do.
private int idleCount;
//name of the delivery person
private String name;
//number of orders distributed
private int ordersDelivered;
//orders to be delivered
private Set<Order> ordersToDeliver;
//valuation
private int valuation;
//maximum number of orders that can be transported
private int maxLoad;
//total accumulated amount that has to be collected
private int totalCharged;
//number of pickupLocations that the delivery person have
private int nPickupLocations;
/**
* Constructor of class DeliveryPerson
* @param company The delivery person's company. Must not be null.
* @param location The delivery person's starting point. Must not be null.
* @throws NullPointerException If company or location is null.
*/
public DeliveryPerson(DeliveryCompany company, Location location, String name, int maxLoad)
{
if(company == null) {
throw new NullPointerException("company");
}
if(location == null) {
throw new NullPointerException("location");
}
this.company = company;
this.location = location;
this.name = name;
this.targetLocation = null;
this.idleCount = 0;
this.ordersDelivered= 0;
this.ordersToDeliver = new TreeSet<>(new ComparadorUrgenciaOrder());
this.valuation = 0;
this.maxLoad = maxLoad;
this.totalCharged = 0;
this.nPickupLocations = maxLoad;
}
/**
* @return the name of the delivery person
*/
public String getName()
{
return name;
}
/**
* Change the name of the DelievryPerson.
* @param newName The new name of the DeliveryPerson.
*/
public void setName(String newName){
name = newName;
}
/**
* Get the location.
* @return Where this delivery person is currently located.
*/
public Location getLocation()
{
return location;
}
/**
* Set the current location.
* @param location Where it is. Must not be null.
* @throws NullPointerException If location is null.
*/
public void setLocation(Location location)
{
if(location != null) {
this.location = location;
}
else {
throw new NullPointerException();
}
}
/**
* Get the target location.
* @return Where this delivery person is currently headed, or null
* if it is idle.
*/
public Location getTargetLocation()
{
return targetLocation;
}
/**
* Set the required target location.
* @param location Where to go. Must not be null.
* @throws NullPointerException If location is null.
*/
public void setTargetLocation(Location location)
{
if(location != null) {
targetLocation = location;
}
else {
throw new NullPointerException();
}
}
/**
* Receive a pickup location. This becomes the
* target location.
* @param location The pickup location.
*/
public void setPickupLocation(Location location)
{
setTargetLocation(location);
}
/**
* @return on how many steps this delivery person has been idle.
*/
public int getIdleCount()
{
return idleCount;
}
/**
* Get the first Order of ordersToDeliver
* @return The first Order of ordersToDeliver.
*/
public Order getFirstOrder(){
return ((TreeSet<Order>)ordersToDeliver).first();
}
/**
* Get the set of Orders OrdesToDeliver
* @return OrdersToDeliver of the delivery person.
*/
public Set<Order> getOrdersToDeliver(){
return ordersToDeliver;
}
/**
* Get the maxLoad
* @return The maxLoad of the delivery person.
*/
public int getMaxLoad(){
return maxLoad;
}
public int getNPickupLocations(){
return nPickupLocations;
}
public void setNPickupLocations(int newNPickupLocations){
nPickupLocations = newNPickupLocations;
}
/**
* Get the valuation
* @return The valuation of the delivery person.
*/
public int getValuation(){
return valuation;
}
/**
* Can the Delivery Person carry more Orders?
* @return Whether or not this delivery person can carry more orders.
*/
public void setValuation(int newValuation){
valuation = newValuation;
}
/**
* Change the value of IdleCount.
* @param newIdleCount The new IdleCount.
*/
public void setIdleCount(int newIdleCount){
idleCount = newIdleCount;
}
/**
* @return how many orders this delivery person has delivered.
*/
public int ordersDelivered()
{
return ordersDelivered;
}
/**
* Change the value of ordersDelivered.
* @param i The new number of orders delivered.
*/
public void setOrdersDelivered(int i){
ordersDelivered = i;
}
/**
* Receive an order.
* Set order's destination as its target location.
* @param order The order.
*/
public void pickup(Order order)
{
((TreeSet)ordersToDeliver).add(order);
setTargetLocation(getFirstOrder().getDestination());
}
/**
* @return the company the Delivery Person works for.
*/
public DeliveryCompany getDeliveryCompany(){
return company;
}
/**
* Changes the company where the Delivery Person works.
* @param newCompany The new company.
*/
public void setDeliveryCompany(DeliveryCompany newCompany){
company = newCompany;
}
/**
* Has the delivery person a target Location?
* @return Whether or not this delivery person has a target Location.
*/
public boolean hasTargetLocation(){
return getTargetLocation() != null;
}
/**
* Clear the target location.
*/
public void clearTargetLocation()
{
targetLocation = null;
}
/**
* Increment the number of steps on which this delivery person
* has been idle.
*/
public void incrementIdleCount()
{
idleCount++;
}
/**
* Increases the amount of money.
* @param charge The charge to add.
*/
public int obtainTotalCharge(){
return totalCharged;
}
/**
* increases the amount of money.
* @param charge The charge to add.
*/
public void incTotalCharged(int charge){
totalCharged = totalCharged + charge;
}
/**
* Update the valuation of the Delivery Person.
* @param newValuation The new valuation to add.
*/
public void updateValuation(int newValuation){
valuation = valuation + newValuation;
}
/**
* Return details of the delivery person, such as where he/she is.
* @return A string representation of the delivery person.
*/
public String toString()
{
return getClass().getName() + " " +getName()+" at " + getLocation();
}
/**
* Is the delivery person free?
* @return Whether or not this delivery person is free.
*/
public boolean isFree()
{
boolean free = false;
if(ordersToDeliver.isEmpty()){
free = true;
}
else{
if(ordersToDeliver.size() < maxLoad){
free = true;
}
}
return free;
}
/**
* Notify the company of our arrival at a pickup location.
*/
public void notifyPickupArrival()
{
company.arrivedAtPickup(this);
}
/**
* Notify the company of our arrival at an order's destination.
*/
public void notifyOrderArrival(Order order)
{
company.arrivedAtDestination(this, order);
}
/**
* Deliver the order.
*/
public void deliverOrder()
{
notifyOrderArrival(getFirstOrder());
clearTargetLocation();
incTotalCharged(getFirstOrder().charge());
updateValuation(getFirstOrder().calculateEvaluationDP());
incrementOrdersDelivered();
((TreeSet)ordersToDeliver).pollFirst();
if(!ordersToDeliver.isEmpty()){
setTargetLocation(getFirstOrder().getDestination());
}
}
/**
* Increment the number of orders this delivery person has delivered.
*/
protected void incrementOrdersDelivered()
{
ordersDelivered++;
}
/**
* Get the distance to the target location from the current location.
* @return distance to target location.
*/
public int distanceToTheTargetLocation()
{
return location.distance(targetLocation);
}
/**
* Carry out a delivery person's actions.
*/
public void act()
{
if(!hasTargetLocation()){
incrementIdleCount();
}
else{
location = location.nextLocation(targetLocation);
System.out.println("@@@ " + getClass().getName() + ": " + getName() + " moving to: " + getLocation().getX() + " - " + getLocation().getY());
if(this.isFree() && this.getLocation().equals(this.getTargetLocation())){
//Si encuentra que tiene algun Order cuyo destino coincide con la posicion de dp significa que lo va a entregar
//Sino es que va a coger el Order
boolean enc = false;
Iterator<Order> iterator = this.getOrdersToDeliver().iterator();
while (iterator.hasNext() && !enc) {
if(iterator.next().getDestination().equals(this.getLocation())){
enc = true;
}
}
if(enc){
while(this.getLocation().equals(this.getTargetLocation())){
deliverOrder();
}
}
else{
notifyPickupArrival();
}
}
else{
if(!this.isFree()){
while(this.getLocation().equals(this.getTargetLocation())){
deliverOrder();
}
}
}
}
}
public String showInitialInfo(){
return toString();
}
public String showInfo(){
return toString() + "go to pick up order from " + getFirstOrder().getSendingName() + "at location " + getFirstOrder().getLocation();
}
/**
* Return details of the delivery person, such as the name, the location,
* number of delivered orders and time (steps) without moving.
* @return A string representation of the delivery person.
*/
public String showFinalInfo()
{
return toString() + " - orders delivered: " + ordersDelivered() + " - non active for: " + getIdleCount() +" times - total to be collected: "
+ obtainTotalCharge() + " - valuation: " + getValuation();
}
}