Exemplo n.º 1
0
    def insert_buy(self, quantity, price):
        """
            Insert buy order in the order book and execute orders if it's possible.
        :param quantity: int
            The amount to buy
        :param price: double
            The unit price to buy
        :return: None
        """

        buy_order = Order(quantity, price, type_of_order='buy')

        if quantity != 0:
            self._buy_orders.append(buy_order)
            self._buy_orders.sort()
            self._buy_orders.reverse()
            print('--- Insert {order} on {book}'.format(
                order=buy_order.__str__(), book=self._name))

        while len(self._sell_orders) != 0 and self._sell_orders[0].get_price(
        ) <= buy_order.get_price() and buy_order.get_qty() > 0:

            # Case quantity sell order > first buy order in our book
            if buy_order.get_qty() > self._sell_orders[0].get_qty():
                deal = Deal(self._sell_orders[0].get_qty(),
                            self._sell_orders[0].get_price(), self._name)
                self._execute_deals.append(deal)

                new_qty = buy_order.get_qty() - self._sell_orders[0].get_qty()

                # Fill the first buy order
                self._sell_orders.remove(self._sell_orders[0])

                buy_order.set_qty(new_qty)
                print(deal.__str__())

            # Case quantity sell order < first buy order in our book
            else:
                deal = Deal(buy_order.get_qty(),
                            self._sell_orders[0].get_price(), self._name)
                self._execute_deals.append(deal)

                # Update of the new quantity
                self._sell_orders[0].set_qty(self._sell_orders[0].get_qty() -
                                             buy_order.get_qty())

                buy_order.set_qty(0)

                self._buy_orders.remove(self._buy_orders[0])

                if self._sell_orders[0].get_qty() == 0:
                    self._sell_orders.remove(self._sell_orders[0])

                print(deal.__str__())

        print(self.get_status())

        return None
Exemplo n.º 2
0
class OrderScreen(Screen):
    def __init__(self):
        pass

    def update_ui(self):
        self.current_order = Order(self.terminal.code, self.terminal.takeout)
        self.terminal.log_ui.configure(height="5", pady="20")
        self.terminal.window.geometry("480x640")
        self.terminal.status_text.set(self.current_order)
        self.terminal.main.config(text="Proceed to checkout", command=lambda: self.terminal.segue_to(CheckoutScreen()))
        self.terminal.secondary.config(text="Cancel the order", command=lambda: self.terminal.segue_to(CheckoutScreen()))
        self.update_pizza_list()

    def update_pizza_list(self):
        menu = MenuAdapter()
        available = menu.update()
        print(available)

        for widget in self.terminal.pizzas.winfo_children():
            widget.destroy()

        Label(self.terminal.pizzas, text="CHOOSE YOUR PIZZA", pady="3",
              font=("Helvetica", 16, "bold")).pack(pady="10")
        for pizza in available:
            pizza_button = Button(self.terminal.pizzas, command=lambda p=pizza: self.select_pizza(p),
                                  text="{} - ${}".format(pizza.title, pizza.cost),
                                  width="20", pady=10, font=("Helvetica", 20, "bold"))
            pizza_button.pack(pady="5")

    def select_pizza(self, pizza):
        try:
            self.current_order.add(pizza)

            if len(self.current_order.order_list) > 4:
                self.terminal.status_text.set(self.current_order.__str__() + "\n DISCOUNT 20%")
            else:
                self.terminal.status_text.set(self.current_order)

            self.update_log(pizza.bake())
            self.update_log("{} pizza is baked!".format(pizza.title))

            self.update_log(pizza.pack(self.terminal.takeout))
            self.update_log("{} pizza is packed!".format(pizza.title))

        except Exception as e:
            self.update_log(e.__str__())

    def update_log(self, message):
        self.terminal.log.write(message)
        self.terminal.log_text.set(self.terminal.log.history)
Exemplo n.º 3
0
class Terminal:
    menu = {"Pepperoni": Pepperoni(), "BBQ": BBQ(), "Seafood": Seafood()}
    screen = 0
    log_history = ""

    def __init__(self):
        self.window = Tk()
        self.window.title("Pizza Time Terminal")
        self.window.geometry("480x240")
        self.window.configure()

        header = Label(self.window,
                       text="Welcome to Pizza Time!",
                       font=("Helvetica", 27, "bold"),
                       pady="20")

        self.status_text = StringVar()
        self.status_text.set("Is it a takeout?")
        status = Label(self.window,
                       textvariable=self.status_text,
                       font=("Helvetica", 20))

        self.pizzas = Frame(self.window)

        self.log_text = StringVar()
        self.log = Label(self.window,
                         textvariable=self.log_text,
                         font=("Helvetica", 16, "italic"),
                         fg="grey",
                         anchor="s")

        navbar = Frame(width="5")
        self.main = Button(navbar,
                           text="Yes",
                           command=lambda: self.set_current_order(True),
                           width="100",
                           font=("Helvetica", 20),
                           pady="10")
        self.secondary = Button(navbar,
                                text="No",
                                command=lambda: self.set_current_order(False),
                                width="100",
                                font=("Helvetica", 20),
                                pady="10")

        header.pack(side=TOP)
        status.pack(side=TOP)

        self.pizzas.pack(side=TOP)

        self.main.pack()
        self.secondary.pack()
        navbar.pack(side=BOTTOM)
        self.log.pack(side=BOTTOM)

    def update_status(self, message):
        self.status_text.set(message)

    def update_pizzas(self):
        available = []

        for pizza in asyncio.run(check_availability()):
            available.append(self.menu[pizza])

        for widget in self.pizzas.winfo_children():
            widget.destroy()

        Label(self.pizzas,
              text="CHOOSE YOUR PIZZA",
              pady="3",
              font=("Helvetica", 16, "bold")).pack(pady="10")
        for pizza in available:
            pizza_button = Button(self.pizzas,
                                  command=lambda p=pizza: self.select_pizza(p),
                                  text="{} - ${}".format(
                                      pizza.title, pizza.cost),
                                  width="20",
                                  pady=10,
                                  font=("Helvetica", 20, "bold"))
            pizza_button.pack(pady="5")

    def update_navbar(self):
        if self.screen == 1:
            self.window.geometry("480x640")
            self.update_pizzas()
            self.main.config(text="Proceed to checkout", command=self.checkout)
            self.secondary.config(text="Cancel the order", command=self.reset)
        if self.screen == 2:
            self.window.geometry("480x240")
            self.main.config(text="Create another order", command=self.reset)
            self.secondary.config(text="Exit the terminal",
                                  command=self.window.destroy)

    def set_current_order(self, takeout):
        self.current_order = Order(self.__code, takeout)
        self.screen = 1
        self.log.configure(height="5", pady="20")
        self.update_navbar()
        self.update_status(self.current_order)

    def select_pizza(self, pizza):
        try:
            self.current_order.add(pizza)

            if len(self.current_order.order_list) > 4:
                self.update_status(self.current_order.__str__() +
                                   "\n DISCOUNT 20%")
            else:
                self.update_status(self.current_order)

            packing = threading.Thread(target=pizza.pack,
                                       args=(self.current_order.takeout, ))
            baking = threading.Thread(target=pizza.bake)

            baking.start()
            packing.start()

            baking.join()
            self.update_log("{} pizza is baked!".format(pizza.title))
            packing.join()
            self.update_log("{} pizza is packed!".format(pizza.title))
        except Exception as e:
            self.update_log(e.__str__())

    def update_log(self, message):
        self.log_history += "\n" + message
        self.log_text.set(self.log_history)

    def start(self, code):
        self.__code = code
        self.window.mainloop()

    def checkout(self):
        self.screen = 2
        self.update_status(
            "Order #{} is ready.\nYou can pick up your order {}".format(
                self.__code, Handout.location_of_pickup))
        self.pizzas.destroy()
        self.log.destroy()
        self.update_navbar()
        self.__code += 1

    def reset(self):
        self.window.destroy()
        self.__init__()
        self.screen = 0
        self.log_history = ""