Hard-to-find technology and rare items are highly sought-after. Imagine that a small shop has asked you to create a queue system to handle its in-store orders for a new, limited item. The shop has accepted sign-ups to the queue by email and has a list of everyone who signed up in chronological order to ensure fairness once the store opens. The shop intends to show the name of the next person in line on the in-store monitors and displays to make sure it’s seen.

In this activity, you’re tasked with designing a queue system that will display the name of each person in the queue in the proper order.
Step 1: Create the Queue

Create a new PythonAnywhere file called queueactivity.py.

We can reuse some of the code from the unit as the base for our queue, but we’ll be using enqueue to add our own data.

The store has a list of 10 people who have signed up to purchase the item, so you’ll need to add their names in chronological order:

Howard Jefferson
Philip Jordan
Margaret Kelly
Irvin Shaw
Loretta Cabrera
Ron Gomez
Cecil Davis
Jack Ludington
Leona Terry
Delbert Bailey

Add comments to your code to describe what you expect each section to do.
Step 2: Create the Display

Next, we want to format our print output to be easily readable by those viewing the screen. Script your output to print this:

There are currently 10 people in line waiting to order.
Attention Howard Jefferson - your order is ready.

1 answer

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:

  1. 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.
  2. Initial Setup: We create an instance of the Queue class and define a list of customers who signed up for the item.

  3. Enqueue Customers: We loop through the list of customers and enqueue each one into the queue.

  4. Display Current Queue: We print the number of customers currently waiting in line.

  5. 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.