To create a queue system in Python that handles in-store orders, we'll need to implement a basic queue data structure to manage the list of people waiting for the new item. Below is a sample implementation for your queueactivity.py
file, which includes comments to describe each section of the code.
queueactivity.py
# Queue class to handle enqueueing and dequeueing of customers
class Queue:
def __init__(self):
# Initialize an empty list to represent the queue
self.items = []
def is_empty(self):
# Return True if the queue is empty, otherwise False
return len(self.items) == 0
def enqueue(self, item):
# Add an item to the end of the queue
self.items.append(item)
def dequeue(self):
# Remove an item from the front of the queue and return it
if not self.is_empty():
return self.items.pop(0)
return None # Return None if the queue is empty
def size(self):
# Return the number of items in the queue
return len(self.items)
def peek(self):
# Return the front item in the queue without removing it
if not self.is_empty():
return self.items[0]
return None # Return None if the queue is empty
# Initialize the queue
order_queue = Queue()
# List of people who signed up for the item in chronological order
customers = [
"Howard Jefferson",
"Philip Jordan",
"Margaret Kelly",
"Irvin Shaw",
"Loretta Cabrera",
"Ron Gomez",
"Cecil Davis",
"Jack Ludington",
"Leona Terry",
"Delbert Bailey"
]
# Enqueue each customer into the queue
for customer in customers:
order_queue.enqueue(customer)
# Display the current number of people in the queue
print(f"There are currently {order_queue.size()} people in line waiting to order.")
# Get the next person in line and display the notification
next_customer = order_queue.peek()
if next_customer:
print(f"Attention {next_customer} - your order is ready.")
Explanation of the Code:
-
Queue Class: We define a
Queue
class that will manage our customer queue. This class includes methods for common queue operations like:__init__
: Initializes an empty list to hold the queue items.is_empty
: Checks if the queue is empty.enqueue
: Adds a customer to the end of the queue.dequeue
: Removes and returns the customer at the front of the queue.size
: Returns the current number of customers in the queue.peek
: Returns the customer at the front of the queue without removing them.
-
Initial Setup: We create an instance of the
Queue
class and define a list of customers who signed up for the item. -
Enqueue Customers: We loop through the list of customers and enqueue each one into the queue.
-
Display Current Queue: We print the number of customers currently waiting in line.
-
Display Notification: Finally, we check who is at the front of the queue and display their notification.
With this code, you can effectively manage the queue system and ensure that customers are notified in a clear and organized manner when their order is ready.