Untitled

 avatar
unknown
plain_text
a year ago
15 kB
3
Indexable
from time import sleep

# User choose for main menu
def home_menu():
    print("-" * 30)
    print("1. Inventory Management")
    print("2. Sales Management")
    print("3. Exit")

    choice = check_input_range(1, 3)

    if choice == 1:
        return "Inventory Management"
    elif choice == 2:
        return "Sales Management"
    elif choice == 3:
        return "Exit"

# User choose for menu of 3 choice
def menu_choice(choice1, choice2, choice3, choice4 = ""):
    print("-" * 30)
    print(f"1. {choice1}")
    print(f"2. {choice2}")
    print(f"3. {choice3}")
    if choice4 != "":
        print(f"4. {choice4}")
        choice = check_input_range(1, 4)
    else:
        choice = check_input_range(1, 3)
    print("-" * 30)


    if choice == 1:
        return choice1
    elif choice == 2:
        return choice2
    elif choice == 3:
        return choice3
    elif choice == 4:
        return choice4

# Limit user input range based on the menu
def check_input_range(lower_limit, upper_limit):
    while True:
        try:
            print("-" * 30)
            input_choice = int(input(">>> "))
            if input_choice < lower_limit or input_choice > upper_limit:
                raise ValueError
        except:
            print_message("Invalid Input! Please Try Again!")
            sleep(1)
        else:
            return input_choice

# Print important one line message
def print_message(message):
    length = len(message)

    if length <= 30:
        print("*" * 30)
        print(message)
        print("*" * 30)
    else:
        print("*" * len(message))
        print(message)
        print("*" * len(message))


# Inventory Management

# User choose for inventory management
def product_setting_choice():
    print("-" * 30)
    print("1. View / Update Blooms")
    print("2. Add New Bloom")
    print("3. View / Update Add-ons")
    print("4. Add New Add-on")
    print("5. Back to Home Menu")

    choice = check_input_range(1, 5)

    if choice == 1:
        return "View / Update Blooms"
    elif choice == 2:
        return "Add New Bloom"
    elif choice == 3:
        return "View / Update Add-ons"
    elif choice == 4:
        return "Add New Add-on"
    elif choice == 5:
        return "Back to Home Menu"

# Align the string
def get_aligned_string(string, length):
    if len(string) >= length:
        return string
    else:
        right_padding = length - len(string)
        aligned_string = string + " " * right_padding
        return aligned_string

# Sort blooms by price
def sort_by_price():
    with open("blooms.txt") as file:
        next(file)
        recorded_list = file.readlines()
        loop_count = len(recorded_list)
        sorted_list = []
        cheapest_bloom_line = ""
        cheapest_bloom_price = int(recorded_list[0].split(",")[-2])
        for count in range(0, loop_count):
            for line in recorded_list:
                current_bloom_price = int(line.split(",")[-2])
                if current_bloom_price <= cheapest_bloom_price:
                    cheapest_bloom_price = current_bloom_price
                    cheapest_bloom_line = line
            recorded_list.remove(cheapest_bloom_line)
            sorted_list.append(cheapest_bloom_line)
            if count != loop_count - 1:
                cheapest_bloom_price = int(recorded_list[0].split(",")[-2])
        return sorted_list

# View/Filter bloom/add-on
def view_items(file_path, mode, category = ""):
    with open(file_path, "r") as file:
        first_line = True
        recorded_category = ""
        recorded_status = ""
        sorted_list = []
        for line in file:
            attribute_list = line.strip().split(",")
            row_line = "-" * (30 * len(attribute_list))

            if mode == "Filter":
                recorded_category = line.split(",")[2].strip()

            elif mode == "Sort":
                sorted_list = sort_by_price()

            elif mode == "Available":
                recorded_status = line.split(",")[-1].strip()

            if first_line: # Treated as Header
                print(row_line)
                for attribute in attribute_list:
                    print(get_aligned_string(attribute, 30), end = "")
                print("\n" + row_line)
                first_line = False
            
            else:
                if mode == "Filter" or mode == "Available":
                    if recorded_category == category or recorded_status == "Available":
                        for attribute in attribute_list:
                            print(get_aligned_string(attribute, 30), end = "")
                        print("\n")

                elif mode == "View":
                    for attribute in attribute_list:
                        print(get_aligned_string(attribute, 30), end = "")
                    print("\n")

                elif mode == "Sort":
                    print(row_line)
                    for line in sorted_list:
                        attribute_list = line.strip().split(",")
                        for attribute in attribute_list:
                            print(get_aligned_string(attribute, 30), end = "")
                        print("\n")
        print(row_line)

# Get a valid bloom/add-on id
def get_valid_item_id(file_path, product):
    with open(file_path, "r") as file:
        while True:
            file.seek(0)
            input_item_id = input(f"Enter The {product} ID: ")
            for line in file:
                recorded_id = line.split(",")[0].strip()
                if input_item_id == recorded_id:
                    return input_item_id
                else:
                    continue
            print_message("Please Enter A Valid ID!")

# Update bloom/add-on
def update_item(file_path, input_item_id, for_update, position):
    with open(file_path, "r+") as file:
        lines = file.readlines()
        for index, line in enumerate(lines):
            if input_item_id == line.split(",")[0].strip():
                recorded = line.split(",")[position].strip()
                updated_line = line.replace(recorded, for_update)
                lines[index] = updated_line
        file.seek(0)
        for line in lines:
            file.write(line)
        file.truncate()
    print("Update Successfully!")

# View and update bloom/add-on
def view_and_update(file_path, product):
    while True:
        choice = menu_choice("View", "Update", "Back")

        if choice == "View":
            view_items(file_path, "View")

        elif choice == "Update":
            view_items(file_path, "View")
            input_item_id = get_valid_item_id(file_path, product)

            while True:
                choice = menu_choice("Price", "Status", "Back")

                if choice == "Price":
                    update_item(file_path, input_item_id, input("Enter New Price: "), -2)
                elif choice == "Status":
                    update_item(file_path, input_item_id, input("Enter New Status: "), -1)
                elif choice == "Back":
                    break

        elif choice == "Back":
            break

# User choose category
def category_choice(mode):
    print("-" * 30)
    print("1. Romantic")
    print("2. Birthday")
    print("3. Grand Opening")
    print("4. Condolence")
    print("5. Anniversary")

    if mode == "Sales":
        print("6. Go Back")
        choice = check_input_range(1, 6)
    elif mode == "Inventory":
        choice = check_input_range(1, 5)
    
    if choice == 1:
        return "Romantic"
    elif choice == 2:
        return "Birthday"
    elif choice == 3:
        return "Grand Opening"
    elif choice == 4:
        return "Condolence"
    elif choice == 5:
        return "Anniversary"
    elif choice == 6:
        return "Go Back"

# Get the valid price
def get_valid_price(product):
    while True:
        try:
            input_price = int(input(f"Enter The {product} Price: "))
            if input_price < 0:
                raise ValueError
        except:
            print("Invalid Input! Please Try Again!")
        else:
            return input_price

# Get the bloom id prefix
def get_bloom_id_prefix(bloom_category):

    if bloom_category == "Romantic":
        return "R"
    elif bloom_category == "Birthday":
        return "B"
    elif bloom_category == "Grand Opening":
        return "GO"
    elif bloom_category == "Condolence":
        return "C"
    elif bloom_category == "Anniversary":
        return "A"

# Get the largest bloom id
def get_largest_bloom_id(input_prefix):
    with open("blooms.txt", "r") as file:
        matched_bloom_id = []
        recorded_bloom_prefix = ""
        next(file)
        for line in file:
            recorded_bloom_id = line.split(",")[0]
            
            if len(input_prefix) == 1:
                recorded_bloom_prefix = line.split(",")[0].strip()[0]
            elif len(input_prefix) == 2:
                recorded_bloom_prefix = line.split(",")[0].strip()[:2]

            if input_prefix == recorded_bloom_prefix:
                matched_bloom_id.append(recorded_bloom_id)

        largest_bloom_id = max(matched_bloom_id)
        return largest_bloom_id

# Get the largest bloom id's numeric part
def get_numeric_part(bloom_id):
    if len(bloom_id) == 4:
        get_numeric_part = bloom_id[1:]
    elif len(bloom_id) == 5:
        get_numeric_part = bloom_id[2:]

    return get_numeric_part

# Auto generate a new bloom id
def generate_bloom_id(bloom_category):
    input_prefix = get_bloom_id_prefix(bloom_category)
    largest_bloom_id = get_largest_bloom_id(input_prefix)
    numeric = int(get_numeric_part(largest_bloom_id))
    new_numeric = numeric + 1
    new_bloom_id = input_prefix + "{:03d}".format(new_numeric)
    return new_bloom_id

# Auto generate an add-on id
def generate_addon_id():
    with open("addons.txt", "r") as file:
        count = 0
        for line in file:
            count += 1
            print(count)
        new_id = "ADD" + "{:03d}".format(count)
        print(new_id)
        return new_id

# Create new line for new item
def new_item_line(product, id, name, price, category = ""):
    if product == "Bloom":
        new_bloom_line = f"{id},{name},{category},{price},Available\n"
        return new_bloom_line

    elif product == "Add-On":
        new_addon_line = f"{id},{name},{price},Available\n"
        return new_addon_line

# Write new line to file
def append_to_file(file_path, new_line):
    with open(file_path, "a") as file:
        file.write(new_line)
    sleep(1)
    print_message("Update Successfully!")

# Get the info and add new item to file
def add_new_item(file_path, product):
    item_name = input(f"Enter the {product} name: ")
    item_price = get_valid_price(product)

    if product == "Bloom":
        bloom_category = category_choice("Inventory")
        bloom_id = generate_bloom_id(bloom_category)
        new_bloom = new_item_line(product, bloom_id, item_name, item_price, bloom_category)
        append_to_file(file_path, new_bloom)

    elif product == "Add-On":
        addon_id = generate_addon_id()
        new_addon = new_item_line(product, addon_id, item_name, item_price)
        append_to_file(file_path, new_addon)



def get_checked_bloom_status():
    with open("blooms.txt", "r") as file:
        next(file)
        checked_status = []
        for line in file:
            bloom_id = line.split(",")[0].strip()
            status = line.split(",")[-1].strip()

            if status == "Available":
                checked_status.append(bloom_id)
        return checked_status




# Main Program for Inventory Management
print_message("Beautiful Blooms Shop 🌺")

while True:

    choice = home_menu()

    if choice == "Inventory Management":
        
        while True:

            choice = product_setting_choice()

            if choice == "View / Update Blooms":
                view_and_update("blooms.txt", "Bloom")

            elif choice == "Add New Bloom":
                add_new_item("blooms.txt", "Bloom")

            elif choice == "View / Update Add-ons":
                view_and_update("addons.txt", "Add-On")

            elif choice == "Add New Add-on":
                add_new_item("addons.txt", "Add-On")

            elif choice == "Back to Home Menu":
                break
    
    elif choice == "Sales Management":
        
        while True:

            choice = menu_choice("Create Order", "View / Update Order", "Back to Home Menu")

            if choice == "Create Order":
                view_items("blooms.txt", "View")

                while True:
                    choice = menu_choice("Filter Bloom by Category", "Sort Bloom by Price", "Order Bloom", "Go Back")

                    if choice == "Filter Bloom by Category":

                        while True:
                            choice = category_choice("Sales")

                            if choice == "Romantic":
                                view_items("blooms.txt", "Filter", "Romantic")
                            elif choice == "Birthday":
                                view_items("blooms.txt", "Filter", "Birthday")
                            elif choice == "Grand Opening":
                                view_items("blooms.txt", "Filter", "Grand Opening")
                            elif choice == "Condolence":
                                view_items("blooms.txt", "Filter", "Condolence")
                            elif choice == "Anniversary":
                                view_items("blooms.txt", "Filter", "Anniversary")
                            elif choice == "Go Back":
                                break

                            choice = menu_choice("Order Item", "Back to Filter Category", "Back to Main Menu")

                            if choice == "Order Bloom":
                                None
                            elif choice == "Back to Filter Category":
                                continue
                            elif choice == "Back to Main Menu":
                                break

                    elif choice == "Sort Bloom by Price":
                        view_items("blooms.txt", "Sort")

                    elif choice == "Order Bloom":
                        view_items("blooms.txt", "View")
                        while True:
                            bloom_id = get_valid_item_id("blooms.txt", "Bloom")
                            available = get_checked_bloom_status()

                            if bloom_id in available:
                                print_message("Order successfully!")
                                view_items("addons.txt", "Available")

                                break
                            else:
                                print_message("This bloom is currently unavailable!")

                    elif choice == "Go Back":
                        break

            elif choice == "View / Update Order":
                None

            elif choice == "Back to Home Menu":
                break
    
    elif choice == "Exit":
        print_message("Bye!")
        break