Home > Net >  How to create a queue of dictionaries per customer?
How to create a queue of dictionaries per customer?

Time:01-10

I am a beginner at python. We have an assignment that is requiring us to create a queue of dictionaries. I am unsure how to do this inside of a list. Here is my code (btw this is play code, not used functionally) My main goal is to have a better understanding of this. Code:

class Queue:
    def __init__(self):
        self.items = []
        
    def size(self):
        return len(self.items)

    def enqueue(self, item):
        self.items.append(item)

    def dequeue(self):
        if self.size() == 0:
            return None
        return self.items.pop(0)

    def show_queue(self):
        print(self.items)  


# CODE CHALLENGE
class IceCreamShop:
    order = {
        "customer" : "",
        "flavor" : "",
        "scoops" : "",
    }
    def __init__(self, flavors):
        self.flavors = flavors
        order = Queue()
        
        
    def take_order(self, customer, flavor, scoops):
        if flavor in self.flavors and scoops in range(1, 4):
            for orders in self.order:
                self.order["customer"] = customer
                self.order["flavor"] = flavor
                self.order["scoops"] = scoops
                print("Order Created!")
                print(self.order)
                return self.order         
        else:
            print("Sorry, we don't have that flavor.\n Pick another flavor or less scoops.")
            
            
    def show_all_orders(self):
        for orders in self.order.values():
            print(self.order)
            
    def next_order(self):
        # show next order in queue
        print("Next Order...")
           
            
shop = IceCreamShop(["rocky road", "mint chip", "pistachio"])
shop.take_order("Zachary", "pistachio", 3)
shop.take_order("Marcy", "mint chip", 1)
shop.take_order("Leopold", "vanilla", 2)
shop.take_order("Bruce", "rocky road", 0)
shop.show_all_orders()
shop.next_order()
shop.show_all_orders()

What I don't understand is,

  1. How do I save a dictionary for each customer inside of a queue?
  2. How would I "dequeue" and "enqueue" the dictionary per customer
  3. In the "take_orders" function, it does return 2 different customers, Zach and Marcy
  4. But in the "show_all_orders" function, it is only printing out Marcy? Why is this?

EDIT: I used the solution given below, and used a for loop to print out the values in a slightly different way. I am just sharing here in case someone else needs..

# CODE CHALLENGE
class IceCreamShop:
    def __init__(self, flavors):
        self.flavors = flavors
        self.orders = Queue()
        
        
    def take_order(self, customer, flavor, scoops):
        if flavor in self.flavors and scoops in range(1, 4):
                order = dict()
                order["customer"] = customer
                order["flavor"] = flavor
                order["scoops"] = scoops
                self.orders.enqueue(order)
                print("Order Created!")
      
        else:
            print("\nSorry, we don't have that flavor.\nPick another flavor or less scoops.\n")
            
            
    def show_all_orders(self):
        print("\nAll Pending Ice Cream Orders:")
        for orders in self.orders.items:
            print(("Customer: "   orders["customer"]), ("-- Flavor: "   orders["flavor"]), ("-- Scoops:"   str(orders["scoops"])))


            
    def next_order(self):
        print("\nNext Order Up!")
        order = self.orders.dequeue()
        print(("Customer: "   order["customer"]), ("-- Flavor: "   order["flavor"]), ("-- Scoops:"   str(order["scoops"])))
                 
shop = IceCreamShop(["rocky road", "mint chip", "pistachio"])
shop.take_order("Zachary", "pistachio", 3)
shop.take_order("Marcy", "mint chip", 1)
shop.take_order("Leopold", "vanilla", 2)
shop.take_order("Bruce", "rocky road", 0)
shop.show_all_orders()
shop.next_order()
shop.show_all_orders()

CodePudding user response:

  1. let's create queue in __init__ of IceCreamShop class to store orders

    self.orders = Queue()
    
  2. You don't need to have an order blank template in IceCreamShop class. Because you can create local variable order in function take_order and then add this order to queue self.orders

    def take_order(self, customer, flavor, scoops):
        if flavor in self.flavors and scoops in range(1, 4):
            order = dict()
            order["customer"] = customer
            order["flavor"] = flavor
            order["scoops"] = scoops
            self.orders.enqueue(order)
    
            print("Order Created!")
            print(order)
            # return self.order
        else:
            print("Sorry, we don't have that flavor.\n Pick another flavor or less scoops.")
    
  3. Let's change function show_all_orders

    def show_all_orders(self):
        self.orders.show_queue()
    
  4. Here we can give next order to customer and remove from queue

    def next_order(self):
        # show next order in queue
        print("Next Order...")
        return self.orders.dequeue()
    
  • Related