Modify the supermarket checkout simulator so that it simulates a store with many checkout lines that have their own cash
Posted: Fri Jul 01, 2022 5:43 am
Modify the supermarket checkout simulator so that it simulates astore with many checkout lines that have their own cashierscontaining the customers processed per cashier, average wait timein each checkout line and the amount of customers left in eachcheckout line. Add the number of cashiers as a new user input.
In the cashier.py file, complete thefollowing:
In the marketmodel.py file, complete thefollowing:
To test your program run the main method inthe marketapp.py file.
Your program's output should look like the following:
File: abstractcollection.py Author: Ken Lambert class Abstract Collection (object): """An abstract collection implementation.""" # Constructor def _init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present. """ self.size=0 if sourceCollection: for item in sourceCollection: self.add(item) # Accessor methods. def isEmpty(self): """Returns True if len(self) == 0, or False otherwise.""" return len(self) == 0 def __len__(self): """Returns the number of items in self.""" return self.size def_str__(self): """Returns the string representation of self.""" return "[" +", ".join(map(str, self)) + "]" def add_(self, other): """Returns a new bag containing the contents of self and other.""" result = type(self) (self) for item in other: result.add(item) return result defeq (self, other): """Returns True if self equals other, or False otherwise.""m if self is other: return True if type (self) != type(other) or \ len(self) != len(other): return False other Iteriter (other). for item in self: if item != next(other Iter): return False return True def count (self, item): """Returns the number of instances of item in self.""" total = 0 for nextItem in self: if nextItem == item: total + 1 return total
File: cashier.py Project 8.4 Models multiple cashiers. from linkedqueue import LinkedQueue class Cashier(object): """Represents a cashier.""" definit__(self): ""Maintains a cashier number, a queue of customers, number of customers served, total customer wait time, and a current customer being processed.""" # Write your code here self totalCustomerWait Time = 0 self.customersServed=0 self.currentCustomer = None self.queue = LinkedQueue () def addCustomer (self, c): """Adds an arriving customer to my line.""" self.queue.add(c) def serveCustomers (self, current Time): """Serves my cuatomers during a given unit of time.""" if self.current Customer is None: # No customers if self.queue.isEmpty(): return else: # Pop first waiting customer and tally results self.currentCustomer = self.queue.pop() self totalCustomerWait Time += \ self.customersServed + 1 #Give a unit of service self.currentCustomer.serve() # If current customer is finished, send it away if self.currentCustomer.getAmountOfServiceNeeded() == 0: self.current Customer = None current Time - \ self.currentCustomer.getArrival Tim def str__(self): """Returns my results: my total customers served, my average wait time per customer, and customers left on my queue.""" result = "TOTALS FOR THE CASHIER\n" + \ "Number of customers served: str(self.customersServed) + "\n" if self.customers Served != 0: " + \ aveWait Time = self.totalCustomerWait Time /\ self.customersServed return result result += "Number of customers left in queue: " + \ str(len(self.queue)) + "\n" + \ "Average time customers spend\n" + \ "waiting to be served: "%5.2f" % aveWait Time " + \
|| || || File: customer.py Project 8.4 Customer's processing time varies around the average, so give it a random time between 1 and average time * 2 + 1. import random class Customer(object): """Represents a customer.""" @classmethod def generateCustomer (cls, probability of New Arrival, arrival Time, averageTimePerCustomer): """Returns a Customer object if the probability of arrival is greater than or equal to a random number. Otherwise, returns None, indicating no new customer. || || || if random.random() <= probabilityOfNewArrival: return Customer (arrival Time, averageTimePerCustomer) else: return None definit__(self, arrival Time, serviceNeeded): """Maintains the arrival time and amount of service needed. """ self. arrival Time = arrival Time self. amount of ServiceNeeded = serviceNeeded def getArrival Time (self): """Returns the arrival time.""" return self. arrival Time def getAmountOfServiceNeeded(self): """Returns the amount of service needed. """ return self.amount Of ServiceNeeded def serve (self): """Accepts a unit of service from the cashier.' self. amountOfServiceNeeded -= 1
File: linkedqueue.py Project 8.4 from node import Node. from abstract collection import AbstractCollection class LinkedQueue (AbstractCollection): """A link-based queue implementation. """ # Constructor def __init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" self.front = self.rear = None AbstractCollection. __init__(self, sourceCollection) # Accessor methods. defiter__(self): """Supports iteration over a view of self.""" pass def peek (self): Returns the item at the front of the queue. Precondition: the queue is not empty. Raises: KeyError if the stack is empty.""" if self.isEmpty(): raise KeyError("The queue is empty.") return self.front.data # Mutator methods def clear(self): """Makes self become empty.""" pass def add(self, item): """Adds item to the rear of the queue.""" newNode = Node (item, None) if self.isEmpty(): else: self.front = newNode. self.rear.next = newNode newNode self.rear self.size += 1 def pop (self): Removes and returns the item at the front of the queue. Precondition: the queue is not empty. Raises: KeyError if the queue is empty. ostcondition: the front item is removed from if self.isEmpty(): raise KeyError("The queue is empty.") oldItem= self.front.data self.front = self.front.next if self.front is None: self.rear None self.size = 1 return oldItem queue."""
|| || || File: marketapp.py Project 8.4 Models multiple cashiers. Terminal-based simulation of a supermarket checkout process. || || || from marketmodel import Market Model def main(): print("Welcome to the Market Simulator! \n") lengthOfSimulation = int(input("Enter the total running time: ")) averageTimePerCus = int(input("Enter the average processing time per custo probability of NewArrival = float(input("Enter the probability of a new arri numCashiers = int(input("Enter the number of cashiers: ")) if lengthOf Simulation < 1 or lengthOfSimulation > 1000: print("Running time must be an integer greater than 0" + \ "\nand less than or equal to 1000") elif averageTimePerCus <= 0 or averageTimePerCus >= lengthOfSimulation: print("Average time per customer must be an integer" + "\ngreater than 0 and less than running time") elif probabilityOfNewArrival <= 0 or probabilityOfNewArrival > 1: print("Probability must be geater than 0" + \ "\nand less than or equal to 1") elif numCashiers <= 0: print("Number of cashiers must be >= 0") else: model = Market Model(lengthOf Simulation, averageTimePerCus, probabilityOfNewArrival, numCashiers) model. runSimulation () print("\n" + "-" * 40) print (model) if _name__ == '__main__": main() 11
|| || || File: node.py Author: Ken Lambert || || || class Node(object): T """Nodes for singly linked structures. """ definit__(self, data, next = None): """Instantiates a Node with default next of None""" self.data = data self.next = next
In the cashier.py file, complete thefollowing:
In the marketmodel.py file, complete thefollowing:
To test your program run the main method inthe marketapp.py file.
Your program's output should look like the following:
File: abstractcollection.py Author: Ken Lambert class Abstract Collection (object): """An abstract collection implementation.""" # Constructor def _init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present. """ self.size=0 if sourceCollection: for item in sourceCollection: self.add(item) # Accessor methods. def isEmpty(self): """Returns True if len(self) == 0, or False otherwise.""" return len(self) == 0 def __len__(self): """Returns the number of items in self.""" return self.size def_str__(self): """Returns the string representation of self.""" return "[" +", ".join(map(str, self)) + "]" def add_(self, other): """Returns a new bag containing the contents of self and other.""" result = type(self) (self) for item in other: result.add(item) return result defeq (self, other): """Returns True if self equals other, or False otherwise.""m if self is other: return True if type (self) != type(other) or \ len(self) != len(other): return False other Iteriter (other). for item in self: if item != next(other Iter): return False return True def count (self, item): """Returns the number of instances of item in self.""" total = 0 for nextItem in self: if nextItem == item: total + 1 return total
File: cashier.py Project 8.4 Models multiple cashiers. from linkedqueue import LinkedQueue class Cashier(object): """Represents a cashier.""" definit__(self): ""Maintains a cashier number, a queue of customers, number of customers served, total customer wait time, and a current customer being processed.""" # Write your code here self totalCustomerWait Time = 0 self.customersServed=0 self.currentCustomer = None self.queue = LinkedQueue () def addCustomer (self, c): """Adds an arriving customer to my line.""" self.queue.add(c) def serveCustomers (self, current Time): """Serves my cuatomers during a given unit of time.""" if self.current Customer is None: # No customers if self.queue.isEmpty(): return else: # Pop first waiting customer and tally results self.currentCustomer = self.queue.pop() self totalCustomerWait Time += \ self.customersServed + 1 #Give a unit of service self.currentCustomer.serve() # If current customer is finished, send it away if self.currentCustomer.getAmountOfServiceNeeded() == 0: self.current Customer = None current Time - \ self.currentCustomer.getArrival Tim def str__(self): """Returns my results: my total customers served, my average wait time per customer, and customers left on my queue.""" result = "TOTALS FOR THE CASHIER\n" + \ "Number of customers served: str(self.customersServed) + "\n" if self.customers Served != 0: " + \ aveWait Time = self.totalCustomerWait Time /\ self.customersServed return result result += "Number of customers left in queue: " + \ str(len(self.queue)) + "\n" + \ "Average time customers spend\n" + \ "waiting to be served: "%5.2f" % aveWait Time " + \
|| || || File: customer.py Project 8.4 Customer's processing time varies around the average, so give it a random time between 1 and average time * 2 + 1. import random class Customer(object): """Represents a customer.""" @classmethod def generateCustomer (cls, probability of New Arrival, arrival Time, averageTimePerCustomer): """Returns a Customer object if the probability of arrival is greater than or equal to a random number. Otherwise, returns None, indicating no new customer. || || || if random.random() <= probabilityOfNewArrival: return Customer (arrival Time, averageTimePerCustomer) else: return None definit__(self, arrival Time, serviceNeeded): """Maintains the arrival time and amount of service needed. """ self. arrival Time = arrival Time self. amount of ServiceNeeded = serviceNeeded def getArrival Time (self): """Returns the arrival time.""" return self. arrival Time def getAmountOfServiceNeeded(self): """Returns the amount of service needed. """ return self.amount Of ServiceNeeded def serve (self): """Accepts a unit of service from the cashier.' self. amountOfServiceNeeded -= 1
File: linkedqueue.py Project 8.4 from node import Node. from abstract collection import AbstractCollection class LinkedQueue (AbstractCollection): """A link-based queue implementation. """ # Constructor def __init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" self.front = self.rear = None AbstractCollection. __init__(self, sourceCollection) # Accessor methods. defiter__(self): """Supports iteration over a view of self.""" pass def peek (self): Returns the item at the front of the queue. Precondition: the queue is not empty. Raises: KeyError if the stack is empty.""" if self.isEmpty(): raise KeyError("The queue is empty.") return self.front.data # Mutator methods def clear(self): """Makes self become empty.""" pass def add(self, item): """Adds item to the rear of the queue.""" newNode = Node (item, None) if self.isEmpty(): else: self.front = newNode. self.rear.next = newNode newNode self.rear self.size += 1 def pop (self): Removes and returns the item at the front of the queue. Precondition: the queue is not empty. Raises: KeyError if the queue is empty. ostcondition: the front item is removed from if self.isEmpty(): raise KeyError("The queue is empty.") oldItem= self.front.data self.front = self.front.next if self.front is None: self.rear None self.size = 1 return oldItem queue."""
|| || || File: marketapp.py Project 8.4 Models multiple cashiers. Terminal-based simulation of a supermarket checkout process. || || || from marketmodel import Market Model def main(): print("Welcome to the Market Simulator! \n") lengthOfSimulation = int(input("Enter the total running time: ")) averageTimePerCus = int(input("Enter the average processing time per custo probability of NewArrival = float(input("Enter the probability of a new arri numCashiers = int(input("Enter the number of cashiers: ")) if lengthOf Simulation < 1 or lengthOfSimulation > 1000: print("Running time must be an integer greater than 0" + \ "\nand less than or equal to 1000") elif averageTimePerCus <= 0 or averageTimePerCus >= lengthOfSimulation: print("Average time per customer must be an integer" + "\ngreater than 0 and less than running time") elif probabilityOfNewArrival <= 0 or probabilityOfNewArrival > 1: print("Probability must be geater than 0" + \ "\nand less than or equal to 1") elif numCashiers <= 0: print("Number of cashiers must be >= 0") else: model = Market Model(lengthOf Simulation, averageTimePerCus, probabilityOfNewArrival, numCashiers) model. runSimulation () print("\n" + "-" * 40) print (model) if _name__ == '__main__": main() 11
|| || || File: node.py Author: Ken Lambert || || || class Node(object): T """Nodes for singly linked structures. """ definit__(self, data, next = None): """Instantiates a Node with default next of None""" self.data = data self.next = next