From 5e95c52fb07201eb6cb52c4f57772674cb545425 Mon Sep 17 00:00:00 2001 From: Jake Ard Date: Thu, 23 Sep 2021 12:09:04 -0600 Subject: [PATCH] added most to all of UI and SQL commands, user input errors --- .gitignore | 1 + planner.db | Bin 8192 -> 0 bytes planner.py | 158 +++++++++++++++++++++++++++++++++-------------------- 3 files changed, 99 insertions(+), 60 deletions(-) delete mode 100644 planner.db diff --git a/.gitignore b/.gitignore index 5ceb386..fa467a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ venv +checklist.txt \ No newline at end of file diff --git a/planner.db b/planner.db deleted file mode 100644 index c0c6c7fd7f4b1a6a96820d8308ced74c57aeffd8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8192 zcmeI%Jx;?g6bEqIv6xy6%3v5O9veuAg@uuVvY<$aHUq06u4=Wx$Z>@jI8ryxz!5kD z3wOW-Wu^<;|HJS}9uN%PPCuNbh? zo7Z8=;_8j1hJXMBAOHafKmY;|fB*y_009UDzuTw==n)^XOfA4+tilrxJ87mTqQiXS2`W UiuMk6g&gIgc8|U_tI%D219W6Lt^fc4 diff --git a/planner.py b/planner.py index 9102e4c..8afc7f7 100644 --- a/planner.py +++ b/planner.py @@ -1,98 +1,136 @@ import sqlite3 as sql +import time connect = sql.connect('planner.db') cursor = connect.cursor() -cursor.execute("CREATE TABLE IF NOT EXISTS tasks (type TEXT, task TEXT, time REAL)") +cursor.execute("CREATE TABLE IF NOT EXISTS tasks (task_id INTEGER PRIMARY KEY, task TEXT, type TEXT, time REAL)") + +def get_choice(max, phrase, do_phrase=True): + choice = 0 + while choice > max or choice < 1: + try: + if do_phrase: + print(phrase) + choice = int(input('-> ')) + if choice > max or choice < 1: + print('\nNot a valid number.') + time.sleep(.5) + if choice <= max and choice > 0: + return choice + except ValueError: + print('\nNot a valid number.') + time.sleep(.5) + +def get_tasks(): + cursor.execute("SELECT * FROM tasks") + return cursor.fetchall() + +def display_tasks(tasks, numbers=True): + if numbers: + print('\n{:<5} {:<15} {:<15} {:<15}'.format('ID', 'Task', 'Type', 'Time')) + print('{:<5} {:<15} {:<15} {:<15}'.format('---', '-----', '-----', '-----')) + for task in tasks: + print('{:<5} {:<15} {:<15} {:<15}'.format(task[0] + 1, task[1], task[2], task[3])) + else: + print('\n{:<15} {:<15} {:<15}'.format('Task', 'Type', 'Time')) + print('{:<15} {:<15} {:<15}'.format('-----', '-----', '-----')) + for task in tasks: + print('{:<15} {:<15} {:<15}'.format(task[1], task[2], task[3])) print('Welcome to your planner!') choice = None while choice != 3: - print('\nWhat would you like to do?') - print('1). View Tasks') - print('2). Edit Planner') - print('3). Quit') - try: - choice = int(input('-> ')) - except ValueError: - print('\nNot a valid number.') + # Get the choice number to know what to do: view the tasks, edit the tasks, or end the program + choice = get_choice(3, '\nWhat would you like to do?\n1). View Tasks\n2). Edit Planner\n3). Quit') if choice == 1: - cursor.execute("SELECT * FROM tasks") - print('\n{:<15} {:<15} {:<15}'.format('Type', 'Task', 'Time')) - - for task in cursor.fetchall(): - print('{:<15} {:<15} {:<15}\n'.format(task[0], task[1], task[2])) + #if user chooses choice 1, display all tasks, the task type, and the task time + tasks = get_tasks() + display_tasks(tasks, False) if choice == 2: - print('\nWould you like to:') - print('1). Add') - print('2). Edit') - print('3). Delete') - print('4). Reset') - print('5). Go back') - try: - choice = int(input('-> ')) - except ValueError: - print('\nNot a valid number.') - + # if user choose choice 2, display the choices for editing and allow for answer + choice = get_choice(5, '\nWould you like to:\n1). Add\n2). Edit\n3). Delete\n4). Reset planner\n5). Go back') if choice == 1: + # if choice is 1 (add a new task) ask for the task name, what type of task it is, and how long it will take task = input('\nTask: ') type = input('Type of task: ') - time = float(input('Time to complete in hours: ')) - values = (type, task, time) - cursor.execute("INSERT INTO tasks VALUES (?, ?, ?)", values) + hours = -1 + while hours < 0: + try: + hours = float(input('Time to complete in hours: ')) + except ValueError: + print('\nNot a valid number.\n') + time.sleep(.5) + tasks = get_tasks() + values = (len(tasks), task, type, hours) + cursor.execute("INSERT INTO tasks VALUES (?, ?, ?, ?)", values) #insert the ID, and the inputs from the user to the database connect.commit() - elif choice == 2: - cursor.execute("SELECT * FROM tasks") - tasks = cursor.fetchall() - print('\nWhich number task would you like to edit? ') - for i, task in enumerate(tasks): - print(f'{i + 1}. {task[1]}') - choice = int(input('-> ')) - task_name =tasks[choice - 1][1] - print('\nWould you like to edit:') - print('1). Task') - print('2). Type') - print('3). Time') - try: - choice = int(input('-> ')) - except ValueError: - print('\nNot a valid number.') + tasks = get_tasks() + display_tasks(tasks) + choice = get_choice(len(tasks), '\nWhich number task would you like to edit? ') + task_id = choice - 1 + choice = get_choice(3, '\nWould you like to edit:\n1). Task\n2). Type\n3). Time') + if choice == 1: task = input('\nTask: ') - values = (task, task_name) - cursor.execute("UPDATE tasks SET task = ? WHERE task = ?", values) + values = (task, task_id) + cursor.execute("UPDATE tasks SET task = ? WHERE task_id = ?", values) connect.commit() elif choice == 2: - type = input('Type of task: ') - values = (type, task_name) - cursor.execute("UPDATE tasks SET type = ? WHERE task = ?", values) + type = input('\nType of task: ') + values = (type, task_id) + cursor.execute("UPDATE tasks SET type = ? WHERE task_id = ?", values) connect.commit() elif choice == 3: choice = None - time = float(input('Time to complete in hours: ')) - values = (time, task_name) - cursor.execute("UPDATE tasks SET time = ? WHERE task = ?", values) + hours = -1 + while hours < 0: + try: + hours = float(input('\nTime to complete in hours: ')) + if hours < 0: + print('\nNot a valid number.') + time.sleep(.5) + except ValueError: + print('\nNot a valid number.') + time.sleep(.5) + hours = -1 + values = (hours, task_id) + cursor.execute("UPDATE tasks SET time = ? WHERE task_id = ?", values) connect.commit() - else: - print('Not a valid number.') - - - + elif choice == 3: - delete = int(input('\nWhich number item would you like to delete? ')) + tasks = get_tasks() + choice = 0 + while choice < 1 or choice > len(tasks): + display_tasks(tasks) + choice = get_choice(len(tasks), '\nWhich number task would you like to delete?') + task_id = choice - 1 + values = (task_id,) + cursor.execute("DELETE FROM tasks WHERE task_id = ?", values) + connect.commit() + tasks = get_tasks() + for task in tasks: + if task[0] > task_id: + values = (task[0] - 1, task_id) + cursor.execute("UPDATE tasks SET task_id = ? WHERE task_id = ?", values) + connect.commit() choice = None + + + + elif choice == 4: - verify = input('\nAre you sure you want to reset the planner? (y/n) ') + verify = input('\nAre you sure you want to reset the planner (y/n)? ') elif choice == 5: pass else: - print('Not a valid number.') + pass