|
| 1 | +//----------------------------------------------------- |
| 2 | +// Title: Shipment Tracking |
| 3 | +// Author: Mahmut Emre Demir |
| 4 | +// ID: 59344436266 |
| 5 | +// Section: 2 |
| 6 | +// Assignment: 4 |
| 7 | +// Description: This class stores all the shipments in a priority queue and a current time object. |
| 8 | +// scheduleShipment() method is used to add a shipment to the priority queue. |
| 9 | +// startShipment() method is used to print the shipment that is currently being processed. |
| 10 | +// completeShipment() method is used to print the shipment that is completed. |
| 11 | +// runTime() method is used to simulate the shipment process. It takes the time as a parameter and processes the shipments accordingly. |
| 12 | +//----------------------------------------------------- |
| 13 | + |
| 14 | +import java.util.PriorityQueue; |
| 15 | + |
| 16 | +public class ShipmentTracking { |
| 17 | + private static PriorityQueue<Shipment> allShipments = new PriorityQueue<Shipment>(new ShipmentComparator()); |
| 18 | + private static Long currentTime = 0L; |
| 19 | + |
| 20 | + |
| 21 | + public static PriorityQueue<Shipment> getAllShipments() { |
| 22 | + return allShipments; |
| 23 | + } |
| 24 | + |
| 25 | + public static void setAllShipments(PriorityQueue<Shipment> allShipments) { |
| 26 | + ShipmentTracking.allShipments = allShipments; |
| 27 | + } |
| 28 | + |
| 29 | + public static Long getCurrentTime() { |
| 30 | + return currentTime; |
| 31 | + } |
| 32 | + |
| 33 | + public static void setCurrentTime(Long currentTime) { |
| 34 | + ShipmentTracking.currentTime = currentTime; |
| 35 | + } |
| 36 | + |
| 37 | + public static void runTime(Long time) { |
| 38 | + Long remainingTime = time; |
| 39 | + |
| 40 | + // Firstly, checking if there are any shipments to process |
| 41 | + if (!getAllShipments().isEmpty()) { |
| 42 | + |
| 43 | + // Processing the shipments until there's no time left or no shipments left |
| 44 | + while (remainingTime > 0 && !getAllShipments().isEmpty()) { |
| 45 | + // Getting the shipment with the earliest deadline and creating a new shipment object |
| 46 | + Shipment shipment = getAllShipments().poll(); |
| 47 | + if (shipment != null) { |
| 48 | + // Starting the shipment |
| 49 | + startShipment(shipment); |
| 50 | + // Checking if there is enough time to complete the shipment |
| 51 | + if (shipment.getDuration() <= remainingTime) { |
| 52 | + // If there is enough time completing the shipment and updating the remaining time |
| 53 | + setCurrentTime(getCurrentTime() + shipment.getDuration()); |
| 54 | + completeShipment(shipment); |
| 55 | + remainingTime -= shipment.getDuration(); |
| 56 | + } else { |
| 57 | + // If there is not enough time to complete the shipment updating the remaining time and adding the shipment back to the queue |
| 58 | + setCurrentTime(getCurrentTime() + remainingTime); |
| 59 | + scheduleShipment(new Shipment(shipment.getName(), shipment.getDeadLine(), shipment.getDuration() - remainingTime)); |
| 60 | + break; // Breaking the loop since there is not enough time to complete this shipment |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + public static void scheduleShipment(Shipment shipment) { |
| 69 | + getAllShipments().add(shipment); |
| 70 | + System.out.println(getCurrentTime() + ": adding " + shipment.toString()); |
| 71 | + } |
| 72 | + |
| 73 | + public static void startShipment(Shipment shipment) { |
| 74 | + System.out.println(getCurrentTime() + ": busy with " + shipment.toString()); |
| 75 | + } |
| 76 | + |
| 77 | + public static void completeShipment(Shipment shipment) { |
| 78 | + System.out.println(getCurrentTime() + ": done with " + shipment.getName()); |
| 79 | + } |
| 80 | +} |
0 commit comments