PYTHON:Write a program that represents a grocery store lineup, using the Queue c
ID: 3664939 • Letter: P
Question
PYTHON:Write a program that represents a grocery store lineup, using the Queue class. The program will ask the user if they want to add a person to the lineup, serve the next person in the line, or quit the program. The program should never allow more than 3 people in the lineup at once. If the user tries to add another person to the lineup when there are already 3 people, the program should print an error message and continue. If the user tries to serve the next person when there is no one in the lineup, the program should inform the user that the lineup is empty. When a person is served, the program should print the name of that person.
FILE queue.py
# This file implements the Queue class and can be used to test the Queue.
class Queue:
# Constructor, which creates a new empty queue:
def __init__(self):
# TODO: You must implement this constructor!
# Adds a new item to the back of the queue, and returns nothing:
def queue(self, item):
# TODO: You must implement this method!
# Removes and returns the front-most item in the queue.
# Returns nothing if the queue is empty.
def dequeue(self):
# TODO: You must implement this method!
# Returns the front-most item in the queue, and DOES NOT change the queue.
def peek(self):
# TODO: You must implement this method!
# Returns True if the queue is empty, and False otherwise:
def is_empty(self):
# TODO: You must implement this method!
# Returns the number of items in the queue:
def size(self):
# TODO: You must implement this method!
# Removes all items from thq queue, and sets the size to 0:
def clear(self):
# TODO: You must implement this method!
# Returns a string representation of the queue:
def __str__(self):
# TODO: You must implement this method!
# Main function, which can be used to test your queue implementation:
def main():
string_queue = Queue()
int_queue = Queue()
print("Initial size of empty Queue is zero:", int_queue.size() == 0)
print("is_empty() works correctly:", int_queue.is_empty())
string_queue.queue("Hello")
string_queue.queue("World")
print("Queue size updates after adding items:", string_queue.size() == 2)
print("Queue keeps correct ordering:", string_queue.peek() == "Hello")
print("peek() does not change Queue:", string_queue.peek() == "Hello"
and string_queue.size() == 2)
print("String representation of queue:", string_queue)
string_queue.clear()
print("peek() returns nothing when Queue is empty:",
string_queue.peek() == None)
print("clear() sets size to 0:", string_queue.size() == 0)
print("dequeue() returns nothing when Queue is empty:",
string_queue.dequeue() == None)
for i in range(0, 1000):
int_queue.queue(i)
correctOrder = True
for i in range(0, 200):
if int_queue.dequeue() != i:
correctOrder = False
print("Queue keeps correct ordering using dequeue():", correctOrder)
print("Queue size updates after removing items:", int_queue.size() == 800)
print("peek() works correctly after dequeue():", int_queue.peek() == 200)
main()
USER INPUT EXAMPLE
Add, Serve, or Exit: Serve
The lineup is already empty.
Add, Serve, or Exit: Add
Enter the name of the person to add: Bob
Add, Serve, or Exit: Add
Enter the name of the person to add: Anna
Add, Serve, or Exit: Add
Enter the name of the person to add: Sarah
Add, Serve, or Exit: Add
You cannot add more people, the lineup is full!
Add, Serve, or Exit: Serve
Bob has been served.
Add, Serve, or Exit: Serve
Anna has been served.
Add, Serve, or Exit: Exit
Explanation / Answer
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.