예제 #1
0
def place_order():
    option_place_order = int(
        input(
            "You will have to input the index of the product you would like to order. If you need to see the list of products, "
            "select option 2.\n1. Prepare order\n2. Display all products\n3. Go back\n"
        ))
    if option_place_order == 1:
        index_product = int(
            input(
                "Introduce the index of the product you want to order(starting from 1):\n"
            ))
        try:
            products = Products.load_products()
            if 0 < index_product <= products.__len__():
                product_to_order = products[index_product - 1]
                quantity = 0
                loop = True
                while loop:
                    quantity = int(
                        input(
                            "How many products of this kind you would like to order?\n"
                        ))
                    if quantity > 0:
                        loop = False
                delivery_address = input(
                    "Please write the address where this order should be delivered:\n"
                )
                prepared_order = Order(product_to_order.__dict__,
                                       type(product_to_order).__name__,
                                       quantity, delivery_address)
                Orders.add_order(prepared_order)
                input(
                    f"Your order has been placed successfully. Press enter key in order to continue\n"
                )
        except JSONDecodeError:
            input(
                "Error on retrieving the products. Press enter key in order to continue\n"
            )
    elif option_place_order == 2:
        display_products()
        place_order()
    elif option_place_order == 3:
        print("Going back...\n")
    else:
        error_handler()
        remove_product()