Question
Write a program to simulate a grocery waiting queue. Your program should ask the user if they want to add a customer to the queue,
Write a program to simulate a grocery waiting queue. Your program should ask the user if they want to add a customer to the queue, serve the next customer in the queue, or exit. When a customer is served or added to the queue, the program should print out the name of that customer and the remaining customers in the queue.
The store has two queues: one is for normal customers, another is for VIP customers. Normal customers can only wait in the normal customers queue, and VIP customers can only wait in the VIP customers queue.
Each time a new customer comes; the user should input the customer name and whether he/she is VIP, then put the user in the corresponding queue.
You should always serve VIP customers first! When add/ serve customers, you also need to print out the current customer name, and names of all customers in each queue.
If you want to add one customer but the corresponding queue is full, your program should print: Error: Normal customers queue is full or Error: VIP customers queue is full. When you want to serve one customer, if both queues are empty, your program should print: Error: Both queues are empty.
You may restrict the capacity of each queue to 3 for testing simplicity.
Each queue (normal or VIP) should be an instance of the CircularQueue class. Therefore, this class should be included in your program.
Following is the output from a sample run of the program:
Add, Serve, or Exit: add
Enter the name of the person to add: Alice
Is the customer VIP? False
add Alice to the line.
people in the line: ]Alice]
VIP customers queue: ]]
Add, Serve, or Exit: add
Enter the name of the person to add: Bob
Is the customer VIP? False
add Bob to the line.
people in the line: ]Alice, Bob]
VIP customers queue: ]]
Add, Serve, or Exit: add
Enter the name of the person to add: John
Is the customer VIP? False
add John to the line.
people in the line: ]Alice, Bob, John]
VIP customers queue: ]]
Add, Serve, or Exit: add
Enter the name of the person to add: Mike
Is the customer VIP? True
add Mike to VIP line.
people in the line: ]Alice, Bob, John]
VIP customers queue: ]Mike]
Add, Serve, or Exit: add
Enter the name of the person to add: Lucy
Is the customer VIP? True
add Lucy to the line. people in the line: ]Alice, Bob, John]
VIP customers queue: ]Mike, Lucy]
Add, Serve, or Exit: add
Enter the name of the person to add: Wilson
Is the customer VIP? False
Error: Normal customer queue is full
people in the line: ]Alice, Bob, John]
VIP customers queue: ]Mike, Lucy]
Add, Serve, or Exit: serve
Mike has been served
people in the line: ]Alice, Bob, John]
VIP customers queue: ]Lucy]
Add, Serve, or Exit: serve
Lucy has been served
people in the line: ]Alice, Bob, John]
VIP customers queue: ]]
Add, Serve, or Exit: serve
Alice has been served
people in the line: ]Bob, John]
VIP customers queue: ]]
Add, Serve, or Exit: serve
Bob has been served
people in the line: ]John]
VIP customers queue: ]]
Add, Serve, or Exit: serve
John has been served
people in the line: ]]
VIP customers queue: ]] Add, Serve, or Exit: serve
Error: Queues are empty
people in the line: ]]
VIP customers queue: ]]
Add, Serve, or Exit: exit Quitting
Circular Queue class:
class CircularQueue: # Constructor, which creates a new empty queue: def __init__(self, capacity): if type(capacity) != int or capacity<=0: raise Exception ('Capacity Error') self.__items = [] self.__capacity = capacity self.__count=0 self.__head=0 self.__tail=0
# Adds a new item to the back of the queue, and returns nothing: def enqueue(self, item): if self.__count== self.__capacity: raise Exception('Error: Queue is full') if len(self.__items) < self.__capacity: self.__items.append(item) self.__count = self.__count + 1; else: self.__items[self.__tail]=item self.__count = self.__count + 1; self.__tail=(self.__tail +1) % self.__capacity
# Removes and returns the front-most item in the queue. # Returns nothing if the queue is empty. def dequeue(self): if self.__count == 0: raise Exception('Error: Queue is empty') item= self.__items[self.__head] self.__items[self.__head]=None self.__count -=1 self.__head=(self.__head+1) % self.__capacity return item
# Returns the front-most item in the queue, and DOES NOT change the queue. def peek(self): if self.__count == 0: raise Exception('Error: Queue is empty') return self.__items[self.__head]
# Returns True if the queue is empty, and False otherwise: def isEmpty(self): return self.__count == 0
# Returns True if the queue is full, and False otherwise: def isFull(self): return self.__count == self.__capacity
# Returns the number of items in the queue: def size(self): return self.__count
# Returns the capacity of the queue: def capacity(self): return self.__capacity
# Removes all items from the queue, and sets the size to # clear() should not change the capacity def clear(self): self.__items = [] self.__count=0 self.__head=0 self.__tail=0
# Returns a string representation of the queue def __str__(self): str_exp = "]" i=self.__head for j in range(self.__count): str_exp += str(self.__items[i]) + " " i=(i+1) % self.__capacity return str_exp + "]"
# # Returns a string representation of the object CircularQueue def __repr__(self): return str(self.__items) + ' Head=' + str(self.__head) + ' Tail='+str(self.__tail) + ' ('+str(self.__count)+'/'+str(self.__capacity)+')'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started