|
| 1 | +import validation |
| 2 | +import uuid |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import utility |
| 6 | + |
| 7 | + |
| 8 | +def MainMenu(): |
| 9 | + """ |
| 10 | + Presents the main menu to the user and gets their choice. |
| 11 | +
|
| 12 | + Returns: |
| 13 | + int: The user's choice (1-4). |
| 14 | + """ |
| 15 | + print(" TODO LIST ".center(21, '=')) |
| 16 | + print("1. Show Current Tasks") |
| 17 | + print("2. Add A Task") |
| 18 | + print("3. Complete A Task") |
| 19 | + print("4. Exit") |
| 20 | + choice=validation.GetIntValue("What would you like to do? ", 1, 4) |
| 21 | + return choice |
| 22 | + |
| 23 | +def RunProgram(): |
| 24 | + """ |
| 25 | + Runs the main program loop for the to-do list application. |
| 26 | + Initializes the ToDoList, presents the main menu, and processes user input |
| 27 | + until the user chooses to exit. Saves tasks to a file upon exiting. |
| 28 | + """ |
| 29 | + todoList=ToDoList() |
| 30 | + userChoice=0 |
| 31 | + while userChoice != 4: |
| 32 | + userChoice=MainMenu() |
| 33 | + if userChoice==1: |
| 34 | + todoList.show_tasks() |
| 35 | + elif userChoice==2: |
| 36 | + todoList.add_task() |
| 37 | + elif userChoice==3: |
| 38 | + todoList.complete_task() |
| 39 | + else: |
| 40 | + print("Thank you for using the program!") |
| 41 | + todoList.save_tasks_to_file() |
| 42 | + |
| 43 | +class ToDoList: |
| 44 | + |
| 45 | + def __init__(self): |
| 46 | + self.__todo_list = [] |
| 47 | + self.__read_from_file() |
| 48 | + |
| 49 | + def __get_task_count(self): |
| 50 | + """Returns the number of items in the todo list""" |
| 51 | + return len(self.__todo_list) |
| 52 | + |
| 53 | + def __read_from_file(self): |
| 54 | + """ |
| 55 | + Reads tasks from the 'todoList.txt' file and populates the todo list. |
| 56 | + If the file doesn't exist, it creates a new one. Handles potential |
| 57 | + IOErrors during file operations. |
| 58 | + """ |
| 59 | + stream = None |
| 60 | + try: |
| 61 | + stream = open("todoList.txt", 'rt') |
| 62 | + lines = stream.readlines() |
| 63 | + for line in lines: |
| 64 | + task = tuple(line.strip().split(';')) |
| 65 | + self.__todo_list.append(task) |
| 66 | + except FileNotFoundError: |
| 67 | + print("No todolist file detected. Creating new one") |
| 68 | + newList = open('todoList.txt', 'w') |
| 69 | + newList.close() |
| 70 | + except IOError as exception: |
| 71 | + print("I/O error occurred: ", os.strerror(exception.errno)) |
| 72 | + sys.exit() |
| 73 | + finally: |
| 74 | + if stream: |
| 75 | + stream.close() |
| 76 | + |
| 77 | + def save_tasks_to_file(self): |
| 78 | + """ |
| 79 | + Saves the current list of tasks to the 'todoList.txt' file. |
| 80 | + Each task is written as a line in the file, with task details |
| 81 | + separated by semicolons. Handles potential IOErrors during file operations. |
| 82 | + """ |
| 83 | + try: |
| 84 | + stream = open("todoList.txt", 'wt') |
| 85 | + for task in self.__todo_list: |
| 86 | + output = ';'.join(task) |
| 87 | + stream.write(output + "\n") |
| 88 | + stream.close() |
| 89 | + except IOError as exception: |
| 90 | + print("I/O error occurred: ", os.strerror(exception.errno)) |
| 91 | + |
| 92 | + def __check_task_exists(self, task_name): |
| 93 | + """Checks if the task entered by user already exists in the list |
| 94 | +
|
| 95 | + Returns: true if exists, false if not |
| 96 | + """ |
| 97 | + if self.__get_task_count() == 0: |
| 98 | + return False |
| 99 | + |
| 100 | + for task in self.__todo_list: |
| 101 | + if task_name == task[1]: |
| 102 | + return True |
| 103 | + return False |
| 104 | + |
| 105 | + def show_tasks(self): |
| 106 | + """ |
| 107 | + Displays the current tasks in the to-do list. |
| 108 | +
|
| 109 | + The tasks are retrieved from the internal todo list and printed |
| 110 | + to the console, along with their deadline. If the list is empty, |
| 111 | + a message indicating that there are no current tasks is displayed. |
| 112 | + """ |
| 113 | + |
| 114 | + utility.clear_console() |
| 115 | + print("Current Tasks".center(20, '*')) |
| 116 | + print('-' * 20) |
| 117 | + if self.__get_task_count() == 0: |
| 118 | + print("No current tasks") |
| 119 | + else: |
| 120 | + for i in range(len(self.__todo_list)): |
| 121 | + print(f"{i+1}. {self.__todo_list[i][1]} | {self.__todo_list[i][2]}") |
| 122 | + print('-' * 20) |
| 123 | + print() |
| 124 | + |
| 125 | + def add_task(self): |
| 126 | + |
| 127 | + """ |
| 128 | + Adds a new task to the to-do list. |
| 129 | +
|
| 130 | + Prompts the user for the task description and deadline. |
| 131 | + Generates a unique ID for the task and stores the task |
| 132 | + details in the todo list. Prevents duplicate task names. |
| 133 | + """ |
| 134 | + while True: |
| 135 | + task = input("What is the task? ") |
| 136 | + if self.__check_task_exists(task): |
| 137 | + print( |
| 138 | + "It looks like you already have a task with that name.\nPlease enter a new one." |
| 139 | + ) |
| 140 | + continue |
| 141 | + break |
| 142 | + deadline = input("When is it due? ") |
| 143 | + id = str(uuid.uuid4()) |
| 144 | + taskTuple = (id, task, deadline) |
| 145 | + self.__todo_list.append(taskTuple) |
| 146 | + |
| 147 | + def complete_task(self): |
| 148 | + |
| 149 | + """ |
| 150 | + Marks a task as complete by removing it from the to-do list. |
| 151 | +
|
| 152 | + Displays the current list of tasks to the user and prompts them |
| 153 | + to select the task they want to complete. Removes the selected |
| 154 | + task from the list and redisplays the updated list. |
| 155 | + If the list is empty, informs the user that there are no tasks |
| 156 | + to complete. |
| 157 | + """ |
| 158 | + if self.__get_task_count()==0: |
| 159 | + print("\nThere are currently no tasks to complete.\n") |
| 160 | + return |
| 161 | + |
| 162 | + self.show_tasks() |
| 163 | + task = validation.GetIntValue("Which task do you want to complete? ", 1, |
| 164 | + len(self.__todo_list)) |
| 165 | + self.__todo_list.pop(task - 1) |
| 166 | + self.show_tasks() |
| 167 | + |
| 168 | +if __name__ == '__main__': |
| 169 | + RunProgram() |
0 commit comments