Пример #1
0
def list_categories():
    # display the existing categories or the categories with the products
    option_list_categories = int(
        input(
            "List the categories only or the categories with products?\n1. Categories only\n2. Categories and products\n3. Go back\n"
        ))
    if option_list_categories == 1:
        try:
            categories = Categories.load_categories()
            for index, cat in enumerate(categories, start=1):
                print(f"{index}. {cat.name}")
            input("Press enter key in order to continue\n")
        except JSONDecodeError:
            input(
                "Error on retrieving the categories. Press enter key in order to continue\n"
            )
    elif option_list_categories == 2:
        try:
            categories = Categories.load_categories()
            products = Products.load_products()
            for index, cat in enumerate(categories, start=1):
                print(f"{index}. {cat.name}")
                for prod in products:
                    if prod.get_category_name() == cat.name:
                        print(f"\t{prod}")
            input("Press enter key in order to continue\n")
        except JSONDecodeError:
            input(
                "Error on retrieving the categories. Press enter key in order to continue\n"
            )
    elif option_list_categories == 3:
        print("Going back...\n")
    else:
        error_handler()
        list_categories()
Пример #2
0
def display_products():
    # display all existing products
    try:
        products = Products.load_products()
        for index, prod in enumerate(products, start=1):
            print(f"{index}. {prod}")
        input("\nPress enter key in order to continue\n")
    except JSONDecodeError:
        input(
            "Error on retrieving the products. Press enter key in order to continue\n"
        )
Пример #3
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()
Пример #4
0
def remove_product():
    option_remove_product_menu = int(
        input(
            "You will have to input the index of the product you would like to remove. If you need to see the list of products, "
            "select option 2.\n1. Remove product\n2. Display all products\n3. Go back\n"
        ))
    if option_remove_product_menu == 1:
        index_product_to_remove = int(
            input(
                "Introduce the index of the product to be removed(starting from 1):\n"
            ))
        try:
            products = Products.load_products()
            if 0 < index_product_to_remove <= products.__len__():
                product_to_remove = products[index_product_to_remove - 1]
                Products.remove_product(product_to_remove)
                input(
                    "Product -" + str(product_to_remove) +
                    "- removed successfully\nPress enter key in order to continue\n"
                )
            else:
                product_option = int(
                    input(
                        "This product does not exist in the list. Input 1 to try again or any other number to return to the store menu:\n"
                    ))
                if product_option == 1:
                    remove_product()
        except JSONDecodeError:
            input(
                "Error on retrieving the products. Press enter key in order to continue\n"
            )
    elif option_remove_product_menu == 2:
        display_products()
        remove_product()
    elif option_remove_product_menu == 3:
        print("Going back...\n")
    else:
        error_handler()
        remove_product()
Пример #5
0
def remove_category():
    option_remove_category = int(
        input(
            "Warning! Deleting a category will also delete all the products inside of it.\n1. Continue\n2. Go back\n"
        ))
    if option_remove_category == 1:
        category_to_remove = Category(
            input("Introduce the name of the category to be removed:\n"))
        try:
            categories = Categories.load_categories()
            if categories.count(category_to_remove) > 0:
                products = Products.load_products()
                for prod in products:
                    if prod.get_category_name() == category_to_remove.name:
                        Products.remove_product(prod)
                Categories.remove_category(category_to_remove)
                input(
                    "Category -" + str(category_to_remove) +
                    "- and all its products were removed successfully.\nPress enter key in order to continue\n"
                )
            else:
                category_option = int(
                    input(
                        "This category does not exist in the list. Input 1 to try entering another category or any other number to return to the store menu:\n"
                    ))
                if category_option == 1:
                    remove_category()
        except JSONDecodeError:
            input(
                "Error on retrieving the categories. Press enter key in order to continue\n"
            )
    elif option_remove_category == 2:
        print("Going back...\n")
    else:
        error_handler()
        remove_category()