コード例 #1
0
class CheckoutManager(object):
    """CheckoutManager object for handling a collection of checkout queues that process customers.'"""
    def __init__(self, n_checkouts, checkout_capacity, staging_capacity):
        self.checkouts = [ArrayQueue(checkout_capacity) for _ in xrange(n_checkouts)]
        self.staging_queue = ArrayQueue(staging_capacity)
        self.finished_customers = []

    def enqueue_random(self, customer):
        """Adds the given customer to a random checkout."""
        checkout = random.choice(self.checkouts)
        customer.queue(checkout)

    def enqueue_shortest(self, customer):
        """Adds the given customer to the checkout with the shortest queue."""
        checkout = min(self.checkouts, key=len)
        customer.queue(checkout)

    def enqueue_staging(self, customer):
        """Adds the customer to a staging queue. The customer will move from the staging queue to an empty checkout as
        one becomes available."""
        customer.queue(self.staging_queue)

    def has_item(self):
        """Checks if any checkout has a customer in it, or if the staging queue has a customer in it."""
        for checkout in self.checkouts:
            if checkout.has_item():
                return True
        return False

    def update(self):
        """Called once per game cycle."""
        for checkout in self.checkouts:
            if checkout.has_item():
                if checkout[0].checked_out():                       # remove finished customer
                    customer = checkout.dequeue()
                    customer.leave()

            if checkout.empty() and self.staging_queue.has_item():  # add waiting customer
                customer = self.staging_queue.dequeue()
                customer.queue(checkout)

            if checkout.has_item():
                checkout[0].checkout()                              # checkout front customer