-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeliveryCompany.java
More file actions
150 lines (136 loc) · 4.96 KB
/
Copy pathDeliveryCompany.java
File metadata and controls
150 lines (136 loc) · 4.96 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
import java.util.*;
/**
* Model the operation of a taxi company, operating different
* delivery persons. This version operates a single type of delivery person.
*
* @author David J. Barnes
* @version 2024.10.07 DP classes
*/
public class DeliveryCompany {
private String name; //nombre de la compañía
private List<DeliveryPerson> deliveryPersons;
private WareHouse wareHouse;
/**
* Constructor for objects of class DeliveryCompany
*/
public DeliveryCompany(String name) {
this.name = name;
deliveryPersons = new ArrayList<>();
wareHouse = new WareHouse();
}
/**
* @return The name of the company.
*/
public String getName() {
return name;
}
public WareHouse getWareHouse() {
return wareHouse;
}
/**
* @return The list of delivery persons.
*/
public List<DeliveryPerson> getDeliveryPersons() {
return deliveryPersons;
}
/**
* @return The list of orders.
*/
public Set<Order> getOrders() {
return wareHouse.getOrders();
}
/**
* Add a new delivery person.
* @param dp The new delivery person.
*/
public void addDeliveryPerson(DeliveryPerson dp) {
deliveryPersons.add(dp);
}
/**
* Add a new order in the warehouse of the company.
* @param order The new order.
*/
public void addOrder(Order order) {
wareHouse.addOrder(order);
}
/**
* Find the most closed free delivery person to the warehouse's location, if any.
* @return A free delivery person, or null if there is none.
*/
public DeliveryPerson getDeliveryPerson(Order order) {
int i = 0;
boolean enc = false;
for (int j=0;j<deliveryPersons.size();j++) {
deliveryPersons.get(j).setTargetLocation(wareHouse.getLocation());
}
deliveryPersons.sort(new ComparadorDeliveryPerson());
int urgencia = order.getUrgency().getValue();
DeliveryPerson dpLibre = null;
while(i < deliveryPersons.size() && !enc){
DeliveryPerson dp = deliveryPersons.get(i);
if(dp.isFree()){
if(urgencia == 5 && dp.getMaxLoad() == 1 && dp.getNPickupLocations() != 0){
dpLibre = dp;
enc = true;
}
if(urgencia == 3 && dp.getMaxLoad() == 2 && dp.getNPickupLocations() != 0){
dpLibre = dp;
enc = true;
}
if((urgencia == 1 || urgencia == 3) && dp.getMaxLoad() == 4 && dp.getNPickupLocations() != 0){
dpLibre = dp;
enc = true;
}
}
i++;
}
return dpLibre;
}
/**
* Request a pickup for the given order.
* @param order The order to be delivered.
* @return Whether a free delivery person is available.
*/
public boolean requestPickup(Order order) {
boolean solicita = false;
DeliveryPerson dp = getDeliveryPerson(order);
if (dp != null) {
System.out.println("<<<< " + dp.getClass().getName() + " " + dp.getName()+" at " + dp.getLocation() + " go to pick up order from " + order.getSendingName() + " at " + order.getLocation());
wareHouse.addOrder(order);
dp.setPickupLocation(order.getLocation());
dp.setNPickupLocations(dp.getNPickupLocations() - 1);
order.setDeliveryPersonName(dp.getName());
solicita = true;
}
return solicita;
}
/**
* A delivery person has arrived at a pickup point.
* @param dp The delivery person at the pickup point.
*/
public void arrivedAtPickup(DeliveryPerson dp) {
Order order;
if (dp.distanceToTheTargetLocation() == 0) {
Iterator<Order> iterator = this.getWareHouse().getOrders().iterator();
while (iterator.hasNext()) {
Order currentOrder = iterator.next();
if(currentOrder.getLocation().equals(dp.getLocation()) && currentOrder.getDeliveryPersonName().equals(dp.getName())){
dp.pickup(currentOrder);
order = currentOrder;
dp.setNPickupLocations(dp.getNPickupLocations() + 1);
System.out.println("<<<< " + dp + " picks up Order from " + order.getSendingName() + " to: " + order.getDestination());
}
}
}
}
/**
* A delivery person has arrived at an order's destination.
* @param dp The delivery person at the destination.
* @param order The order being dropped off.
*/
public void arrivedAtDestination(DeliveryPerson dp, Order order) {
wareHouse.addDeliveredOrder(order, dp);
System.out.println( "<<<< " + dp + " delivers Order at: " + order.getDeliveryTime() + " from: " + order.getSendingName() + "to: "
+ order.getDestinationName() + "(charge: " + order.charge() + ")");
}
}