示例#1
0
文件: UI.py 项目: VicSera/UBB
def command_interface():
    """
    The main user interface.
    """
    os.system('clear')
    expenses = Expense.initialize_list()  # Initial list with sample values
    expenses_history = [copy.deepcopy(expenses)]

    options = {
        'add': Functions.add,
        'insert': Functions.insert,
        'remove': Functions.remove,
        'list': Functions.list_elements,
        'sum': Functions.sum_category,
        'max': Functions.max_per_day,
        'maxday': Functions.max_day,
        'sort': Functions.sort_by,
        'filter': Functions.filter,
        'undo': None,
        'exit': menu_exit
    }

    while True:
        print("\n         MAIN MENU          \n\n"
              "   Please input a command   \n\n")
        user_input = input("--> ")
        os.system('clear')
        operands = user_input.split()
        if operands[0] not in options.keys():
            print("Invalid command '" + str(operands[0]) + "'")
        elif operands[0] == 'undo':
            expenses = Functions.undo(
                expenses_history
            )  # treat undo separately because of the history change
        else:
            options[operands[0]](expenses, operands[1:])

            if expenses != expenses_history[
                    -1]:  # only append if there was a change
                expenses_history.append(
                    copy.deepcopy(expenses)
                )  # append a copy of current expenses to the history
示例#2
0
文件: UI.py 项目: VicSera/UBB
def menu_interface():
    """
    The main menu-based user interface
    """
    os.system('clear')

    expenses = Expense.initialize_list()
    expenses_history = [copy.deepcopy(expenses)]

    menu_options = [
        "Add an element to the list", "Insert an element to the list",
        "List elements", "Sort elements", "Filter out certain elements",
        "Get maximum", "Sum elements", "Undo", "Exit"
    ]

    functions = {
        menu_options[0]: menu_add,
        menu_options[1]: menu_insert,
        menu_options[2]: menu_list,
        menu_options[3]: menu_sort,
        menu_options[4]: menu_filter,
        menu_options[5]: menu_max,
        menu_options[6]: menu_sum,
        menu_options[7]: None,
        menu_options[8]: menu_exit
    }

    while True:
        function_to_call = functions[Parser.choose(
            menu_options, "Please choose one of the following options:")]
        os.system('clear')

        if function_to_call is None:
            expenses = Functions.undo(expenses_history)
            continue

        function_to_call(expenses)

        if expenses != expenses_history[-1]:
            expenses_history.append(copy.deepcopy(expenses))