|
1 | 1 | import sqlite3 as sql
|
| 2 | +import time |
2 | 3 |
|
3 | 4 | connect = sql.connect('planner.db')
|
4 | 5 | cursor = connect.cursor()
|
5 | 6 |
|
6 | | -cursor.execute("CREATE TABLE IF NOT EXISTS tasks (type TEXT, task TEXT, time REAL)") |
| 7 | +cursor.execute("CREATE TABLE IF NOT EXISTS tasks (task_id INTEGER PRIMARY KEY, task TEXT, type TEXT, time REAL)") |
| 8 | + |
| 9 | +def get_choice(max, phrase, do_phrase=True): |
| 10 | + choice = 0 |
| 11 | + while choice > max or choice < 1: |
| 12 | + try: |
| 13 | + if do_phrase: |
| 14 | + print(phrase) |
| 15 | + choice = int(input('-> ')) |
| 16 | + if choice > max or choice < 1: |
| 17 | + print('\nNot a valid number.') |
| 18 | + time.sleep(.5) |
| 19 | + if choice <= max and choice > 0: |
| 20 | + return choice |
| 21 | + except ValueError: |
| 22 | + print('\nNot a valid number.') |
| 23 | + time.sleep(.5) |
| 24 | + |
| 25 | +def get_tasks(): |
| 26 | + cursor.execute("SELECT * FROM tasks") |
| 27 | + return cursor.fetchall() |
| 28 | + |
| 29 | +def display_tasks(tasks, numbers=True): |
| 30 | + if numbers: |
| 31 | + print('\n{:<5} {:<15} {:<15} {:<15}'.format('ID', 'Task', 'Type', 'Time')) |
| 32 | + print('{:<5} {:<15} {:<15} {:<15}'.format('---', '-----', '-----', '-----')) |
| 33 | + for task in tasks: |
| 34 | + print('{:<5} {:<15} {:<15} {:<15}'.format(task[0] + 1, task[1], task[2], task[3])) |
| 35 | + else: |
| 36 | + print('\n{:<15} {:<15} {:<15}'.format('Task', 'Type', 'Time')) |
| 37 | + print('{:<15} {:<15} {:<15}'.format('-----', '-----', '-----')) |
| 38 | + for task in tasks: |
| 39 | + print('{:<15} {:<15} {:<15}'.format(task[1], task[2], task[3])) |
7 | 40 |
|
8 | 41 | print('Welcome to your planner!')
|
9 | 42 |
|
10 | 43 | choice = None
|
11 | 44 | while choice != 3:
|
12 | | - print('\nWhat would you like to do?') |
13 | | - print('1). View Tasks') |
14 | | - print('2). Edit Planner') |
15 | | - print('3). Quit') |
16 | | - try: |
17 | | - choice = int(input('-> ')) |
18 | | - except ValueError: |
19 | | - print('\nNot a valid number.') |
| 45 | + # Get the choice number to know what to do: view the tasks, edit the tasks, or end the program |
| 46 | + choice = get_choice(3, '\nWhat would you like to do?\n1). View Tasks\n2). Edit Planner\n3). Quit') |
20 | 47 |
|
21 | 48 | if choice == 1:
|
22 | | - cursor.execute("SELECT * FROM tasks") |
23 | | - print('\n{:<15} {:<15} {:<15}'.format('Type', 'Task', 'Time')) |
24 | | - |
25 | | - for task in cursor.fetchall(): |
26 | | - print('{:<15} {:<15} {:<15}\n'.format(task[0], task[1], task[2])) |
| 49 | + #if user chooses choice 1, display all tasks, the task type, and the task time |
| 50 | + tasks = get_tasks() |
| 51 | + display_tasks(tasks, False) |
27 | 52 |
|
28 | 53 | if choice == 2:
|
29 | | - print('\nWould you like to:') |
30 | | - print('1). Add') |
31 | | - print('2). Edit') |
32 | | - print('3). Delete') |
33 | | - print('4). Reset') |
34 | | - print('5). Go back') |
35 | | - try: |
36 | | - choice = int(input('-> ')) |
37 | | - except ValueError: |
38 | | - print('\nNot a valid number.') |
39 | | - |
| 54 | + # if user choose choice 2, display the choices for editing and allow for answer |
| 55 | + choice = get_choice(5, '\nWould you like to:\n1). Add\n2). Edit\n3). Delete\n4). Reset planner\n5). Go back') |
40 | 56 | if choice == 1:
|
| 57 | + # 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 |
41 | 58 | task = input('\nTask: ')
|
42 | 59 | type = input('Type of task: ')
|
43 | | - time = float(input('Time to complete in hours: ')) |
44 | | - values = (type, task, time) |
45 | | - cursor.execute("INSERT INTO tasks VALUES (?, ?, ?)", values) |
| 60 | + hours = -1 |
| 61 | + while hours < 0: |
| 62 | + try: |
| 63 | + hours = float(input('Time to complete in hours: ')) |
| 64 | + except ValueError: |
| 65 | + print('\nNot a valid number.\n') |
| 66 | + time.sleep(.5) |
| 67 | + tasks = get_tasks() |
| 68 | + values = (len(tasks), task, type, hours) |
| 69 | + cursor.execute("INSERT INTO tasks VALUES (?, ?, ?, ?)", values) #insert the ID, and the inputs from the user to the database |
46 | 70 | connect.commit()
|
47 | | - |
48 | 71 | elif choice == 2:
|
49 | | - cursor.execute("SELECT * FROM tasks") |
50 | | - tasks = cursor.fetchall() |
51 | | - print('\nWhich number task would you like to edit? ') |
52 | | - for i, task in enumerate(tasks): |
53 | | - print(f'{i + 1}. {task[1]}') |
54 | | - choice = int(input('-> ')) |
55 | | - task_name =tasks[choice - 1][1] |
56 | | - print('\nWould you like to edit:') |
57 | | - print('1). Task') |
58 | | - print('2). Type') |
59 | | - print('3). Time') |
60 | | - try: |
61 | | - choice = int(input('-> ')) |
62 | | - except ValueError: |
63 | | - print('\nNot a valid number.') |
| 72 | + tasks = get_tasks() |
| 73 | + display_tasks(tasks) |
| 74 | + choice = get_choice(len(tasks), '\nWhich number task would you like to edit? ') |
| 75 | + task_id = choice - 1 |
| 76 | + choice = get_choice(3, '\nWould you like to edit:\n1). Task\n2). Type\n3). Time') |
| 77 | + |
64 | 78 | if choice == 1:
|
65 | 79 | task = input('\nTask: ')
|
66 | | - values = (task, task_name) |
67 | | - cursor.execute("UPDATE tasks SET task = ? WHERE task = ?", values) |
| 80 | + values = (task, task_id) |
| 81 | + cursor.execute("UPDATE tasks SET task = ? WHERE task_id = ?", values) |
68 | 82 | connect.commit()
|
69 | 83 | elif choice == 2:
|
70 | | - type = input('Type of task: ') |
71 | | - values = (type, task_name) |
72 | | - cursor.execute("UPDATE tasks SET type = ? WHERE task = ?", values) |
| 84 | + type = input('\nType of task: ') |
| 85 | + values = (type, task_id) |
| 86 | + cursor.execute("UPDATE tasks SET type = ? WHERE task_id = ?", values) |
73 | 87 | connect.commit()
|
74 | 88 | elif choice == 3:
|
75 | 89 | choice = None
|
76 | | - time = float(input('Time to complete in hours: ')) |
77 | | - values = (time, task_name) |
78 | | - cursor.execute("UPDATE tasks SET time = ? WHERE task = ?", values) |
| 90 | + hours = -1 |
| 91 | + while hours < 0: |
| 92 | + try: |
| 93 | + hours = float(input('\nTime to complete in hours: ')) |
| 94 | + if hours < 0: |
| 95 | + print('\nNot a valid number.') |
| 96 | + time.sleep(.5) |
| 97 | + except ValueError: |
| 98 | + print('\nNot a valid number.') |
| 99 | + time.sleep(.5) |
| 100 | + hours = -1 |
| 101 | + values = (hours, task_id) |
| 102 | + cursor.execute("UPDATE tasks SET time = ? WHERE task_id = ?", values) |
79 | 103 | connect.commit()
|
80 | | - else: |
81 | | - print('Not a valid number.') |
82 | | - |
83 | | - |
84 | | - |
| 104 | + |
85 | 105 |
|
86 | 106 |
|
87 | 107 | elif choice == 3:
|
88 | | - delete = int(input('\nWhich number item would you like to delete? ')) |
| 108 | + tasks = get_tasks() |
| 109 | + choice = 0 |
| 110 | + while choice < 1 or choice > len(tasks): |
| 111 | + display_tasks(tasks) |
| 112 | + choice = get_choice(len(tasks), '\nWhich number task would you like to delete?') |
| 113 | + task_id = choice - 1 |
| 114 | + values = (task_id,) |
| 115 | + cursor.execute("DELETE FROM tasks WHERE task_id = ?", values) |
| 116 | + connect.commit() |
| 117 | + tasks = get_tasks() |
| 118 | + for task in tasks: |
| 119 | + if task[0] > task_id: |
| 120 | + values = (task[0] - 1, task_id) |
| 121 | + cursor.execute("UPDATE tasks SET task_id = ? WHERE task_id = ?", values) |
| 122 | + connect.commit() |
89 | 123 | choice = None
|
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
90 | 128 | elif choice == 4:
|
91 | | - verify = input('\nAre you sure you want to reset the planner? (y/n) ') |
| 129 | + verify = input('\nAre you sure you want to reset the planner (y/n)? ') |
92 | 130 | elif choice == 5:
|
93 | 131 | pass
|
94 | 132 | else:
|
95 | | - print('Not a valid number.') |
| 133 | + pass |
96 | 134 |
|
97 | 135 |
|
98 | 136 |
|
|
0 commit comments