-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShipmentTracking.java
More file actions
80 lines (66 loc) · 3.44 KB
/
ShipmentTracking.java
File metadata and controls
80 lines (66 loc) · 3.44 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
//-----------------------------------------------------
// Title: Shipment Tracking
// Author: Mahmut Emre Demir
// ID: 59344436266
// Section: 2
// Assignment: 4
// Description: This class stores all the shipments in a priority queue and a current time object.
// scheduleShipment() method is used to add a shipment to the priority queue.
// startShipment() method is used to print the shipment that is currently being processed.
// completeShipment() method is used to print the shipment that is completed.
// runTime() method is used to simulate the shipment process. It takes the time as a parameter and processes the shipments accordingly.
//-----------------------------------------------------
import java.util.PriorityQueue;
public class ShipmentTracking {
private static PriorityQueue<Shipment> allShipments = new PriorityQueue<Shipment>(new ShipmentComparator());
private static Long currentTime = 0L;
public static PriorityQueue<Shipment> getAllShipments() {
return allShipments;
}
public static void setAllShipments(PriorityQueue<Shipment> allShipments) {
ShipmentTracking.allShipments = allShipments;
}
public static Long getCurrentTime() {
return currentTime;
}
public static void setCurrentTime(Long currentTime) {
ShipmentTracking.currentTime = currentTime;
}
public static void runTime(Long time) {
Long remainingTime = time;
// Firstly, checking if there are any shipments to process
if (!getAllShipments().isEmpty()) {
// Processing the shipments until there's no time left or no shipments left
while (remainingTime > 0 && !getAllShipments().isEmpty()) {
// Getting the shipment with the earliest deadline and creating a new shipment object
Shipment shipment = getAllShipments().poll();
if (shipment != null) {
// Starting the shipment
startShipment(shipment);
// Checking if there is enough time to complete the shipment
if (shipment.getDuration() <= remainingTime) {
// If there is enough time completing the shipment and updating the remaining time
setCurrentTime(getCurrentTime() + shipment.getDuration());
completeShipment(shipment);
remainingTime -= shipment.getDuration();
} else {
// If there is not enough time to complete the shipment updating the remaining time and adding the shipment back to the queue
setCurrentTime(getCurrentTime() + remainingTime);
scheduleShipment(new Shipment(shipment.getName(), shipment.getDeadLine(), shipment.getDuration() - remainingTime));
break; // Breaking the loop since there is not enough time to complete this shipment
}
}
}
}
}
public static void scheduleShipment(Shipment shipment) {
getAllShipments().add(shipment);
System.out.println(getCurrentTime() + ": adding " + shipment.toString());
}
public static void startShipment(Shipment shipment) {
System.out.println(getCurrentTime() + ": busy with " + shipment.toString());
}
public static void completeShipment(Shipment shipment) {
System.out.println(getCurrentTime() + ": done with " + shipment.getName());
}
}