-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTruck.java
More file actions
193 lines (160 loc) · 5.6 KB
/
Copy pathTruck.java
File metadata and controls
193 lines (160 loc) · 5.6 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
import java.util.*;
public class Truck {
private final int id;
private final List<Container> containers = new ArrayList<>();
private final long creationTime;
private volatile long firstContainerLoadTime = 0; // Track when loading actually starts
private volatile String destination;
private volatile String status;
private static final int MAX_CONTAINERS = 18;
private static final String DEFAULT_DESTINATION = "Distribution Centre";
public Truck(int id) {
this.id = id;
this.creationTime = System.currentTimeMillis();
this.destination = DEFAULT_DESTINATION;
this.status = "LOADING";
}
public synchronized boolean loadContainer(Container container) {
if (containers.size() < MAX_CONTAINERS) {
// Track when loading actually starts (first container)
if (containers.isEmpty()) {
firstContainerLoadTime = System.currentTimeMillis();
}
containers.add(container);
System.out.printf("Truck-%d: Loaded Container #%d (%d/%d containers) (Thread: %s)%n",
id, container.getId(), containers.size(), MAX_CONTAINERS,
Thread.currentThread().getName());
if (containers.size() >= MAX_CONTAINERS) {
status = "FULL";
}
return true;
}
return false;
}
public synchronized boolean isFull() {
return containers.size() >= MAX_CONTAINERS;
}
public int getId() {
return id;
}
public synchronized int getContainerCount() {
return containers.size();
}
/**
* Get truck creation time
* @return Timestamp when truck was created
*/
public long getCreationTime() {
return creationTime;
}
/**
* Get time when first container was loaded (loading started)
* @return Timestamp when loading began, or 0 if no containers loaded
*/
public long getFirstContainerLoadTime() {
return firstContainerLoadTime;
}
/**
* Get wait time (time from creation to first container load)
* @return Wait time in milliseconds
*/
public long getWaitTime() {
if (firstContainerLoadTime == 0) {
return System.currentTimeMillis() - creationTime; // Still waiting
}
return firstContainerLoadTime - creationTime;
}
/**
* Get loading time (time from first container to departure)
* @return Loading time in milliseconds, or 0 if not departed
*/
public long getLoadingTime() {
if (firstContainerLoadTime == 0) {
return 0; // No loading started yet
}
return System.currentTimeMillis() - firstContainerLoadTime;
}
/**
* Get truck destination
* @return Destination name
*/
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public synchronized List<Container> getContainers() {
return new ArrayList<>(containers);
}
public int getMaxCapacity() {
return MAX_CONTAINERS;
}
public synchronized double getUtilization() {
return (containers.size() * 100.0) / MAX_CONTAINERS;
}
public synchronized int getAvailableSpace() {
return MAX_CONTAINERS - containers.size();
}
public synchronized boolean isEmpty() {
return containers.isEmpty();
}
public synchronized int getTotalBoxes() {
int totalBoxes = 0;
for (Container container : containers) {
totalBoxes += container.getBoxCount();
}
return totalBoxes;
}
public synchronized double getEstimatedWeight() {
double totalWeight = 0.0;
for (Container container : containers) {
totalWeight += container.getEstimatedWeight();
}
return totalWeight;
}
public synchronized List<String> getAllTrackingNumbers() {
List<String> allTrackingNumbers = new ArrayList<>();
for (Container container : containers) {
allTrackingNumbers.addAll(container.getTrackingNumbers());
}
return allTrackingNumbers;
}
public synchronized String depart() {
status = "DEPARTED";
long departureTime = System.currentTimeMillis();
long loadingDuration = departureTime - creationTime;
return String.format("Truck-%d departed to %s with %d containers (%d boxes) after %.2f seconds",
id, destination, containers.size(), getTotalBoxes(), loadingDuration / 1000.0);
}
public synchronized boolean validateLoad() {
for (Container container : containers) {
if (!container.validateContents()) {
return false;
}
}
return true;
}
@Override
public String toString() {
return String.format("Truck{id=%d, containers=%d/%d, status='%s', destination='%s'}",
id, getContainerCount(), MAX_CONTAINERS, status, destination);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Truck truck = (Truck) obj;
return id == truck.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}