from enum import Enum
import heapq
class FreightType(Enum):
EXPEDITED = 1
EXPRESS = 2
REGULAR = 3
class Priority(Enum):
HIGH = 1
MEDIUM = 2
LOW = 3
class Freight:
def __init__(self, freight_id, freight_type, priority):
self.freight_id = freight_id
self.freight_type = freight_type
self.priority = priority
def __lt__(self, other):
# Compare by freight type first, then by priority
if self.freight_type.value != other.freight_type.value:
return self.freight_type.value < other.freight_type.value
elif self.priority.value != other.priority.value:
return self.priority.value < other.priority.value
else:
# For same type and priority, compare by freight ID
return self.freight_id < other.freight_id
def __str__(self):
return f"Freight(freight_id={self.freight_id}, freight_type={self.freight_type}, priority={self.priority})"
class FreightQueue:
def __init__(self):
self.freights = []
def add_freight(self, freight):
heapq.heappush(self.freights, freight)
def remove_freight(self):
if self.freights:
return heapq.heappop(self.freights)
return None
def __str__(self):
return f"FreightQueue(freights={self.freights})"
class FreightYard:
def __init__(self):
self.freight_queue = FreightQueue()
def add_freight_to_queue(self, freight):
self.freight_queue.add_freight(freight)
def exit_freights(self):
while self.freight_queue.freights:
freight = self.freight_queue.remove_freight()
print(f"Freight {freight.freight_id} with type {freight.freight_type} and priority {freight.priority} is exiting the yard.")
def main():
yard = FreightYard()
# Create freights with different types and priorities
f1 = Freight("F001", FreightType.REGULAR, Priority.LOW) # Regular, Low priority
f2 = Freight("F002", FreightType.EXPRESS, Priority.HIGH) # Express, High priority
f3 = Freight("F003", FreightType.REGULAR, Priority.MEDIUM) # Regular, Medium priority
f4 = Freight("F004", FreightType.EXPEDITED, Priority.LOW) # Expedited, Low priority
f5 = Freight("F005", FreightType.EXPRESS, Priority.MEDIUM) # Express, Medium priority
f6 = Freight("F006", FreightType.REGULAR, Priority.HIGH) # Regular, High priority
f7 = Freight("F007", FreightType.REGULAR, Priority.LOW) # Regular, Low priority
f8 = Freight("F008", FreightType.EXPRESS, Priority.MEDIUM) # Express, Medium priority
f9 = Freight("F009", FreightType.REGULAR, Priority.HIGH) # Regular, High priority
f10 = Freight("F010", FreightType.EXPRESS, Priority.LOW) # Express, Low priority
# Add freights to the queue
yard.add_freight_to_queue(f1)
yard.add_freight_to_queue(f2)
yard.add_freight_to_queue(f3)
yard.add_freight_to_queue(f4)
yard.add_freight_to_queue(f5)
yard.add_freight_to_queue(f6)
yard.add_freight_to_queue(f7)
yard.add_freight_to_queue(f8)
yard.add_freight_to_queue(f9)
yard.add_freight_to_queue(f10)
# Exit freights
yard.exit_freights()
if __name__ == "__main__":
main()