-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_tracker.py
More file actions
295 lines (238 loc) · 7.67 KB
/
task_tracker.py
File metadata and controls
295 lines (238 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# build a task tracker, which works from the terminal
# users should be able to:
# add, update, and delete tasks (manage tasks)
# mark a task as in progress or done
# list all the tasks that are done
# list all the tasks that are not done
# list all the tasks that are in progress
# all data should be stored in a JSON File
import json
import os
# JSON file where tasks will be stored
TASKS_FILE = "tasks.json"
tasks = {}
# LOAD TASKS FROM JSON FILE
def load_tasks():
"""
Loads tasks from the JSON file into the global 'tasks' dictionary.
If the file does not exist or contains invalid data, we start fresh.
"""
global tasks
if os.path.exists(TASKS_FILE):
try:
with open(TASKS_FILE, "r") as f:
tasks = json.load(f)
except (json.JSONDecodeError, IOError):
print("Could not read tasks file. Starting with an empty task list.")
tasks = {}
else:
tasks = {}
# SAVE TASKS TO JSON FILE
def save_tasks():
"""
Saves the current tasks dictionary to the JSON file.
Ensures data persists between program runs.
"""
with open(TASKS_FILE, "w") as f:
json.dump(tasks, f, indent=4)
# ADD A NEW TASK
def add_task():
"""
Allows the user to create a new task with:
- name
- description
- initial progress status
"""
task_add = input("\nEnter the name of the new task: ").strip()
if not task_add:
print("Task name cannot be empty.")
return
# Prevent duplicate task names
if task_add in tasks:
print("A task with that name already exists.")
return
task_description = input("Enter a description for the task: ").strip()
print("\nSelect the task's starting progress:")
print("1) Not done")
print("2) In progress")
print("3) Done")
choice = input("Enter a number (1-3, default 1): ").strip()
# Determine initial progress state
if choice == "2":
task_progression = "in progress"
elif choice == "3":
task_progression = "done"
else:
task_progression = "not done"
# Store the new task in the global dictionary
tasks[task_add] = {
"description": task_description,
"progress": task_progression
}
save_tasks()
print(f"\nTask '{task_add}' added.")
# UPDATE AN EXISTING TASK
def update_tasks():
"""
Allows the user to update:
- task name (renaming)
- description
- progress state
"""
selected_task = input("\nEnter the name of the task you want to update: ").strip()
# Ensure task exists
if selected_task not in tasks:
print("That task does not exist.")
return
print("\nWhat would you like to update?")
print("1) Task Name")
print("2) Task Description")
print("3) Task Progress")
try:
choice_selected = int(input("Enter your choice: ").strip())
except ValueError:
print("Invalid input. Must be a number.")
return
# OPTION 1: RENAME TASK
if choice_selected == 1:
new_task_name = input("Enter the new task name: ").strip()
# Check validity of new name
if not new_task_name:
print("Task name cannot be empty.")
return
if new_task_name in tasks:
print("A task with that name already exists.")
return
# Move data to new key and remove old key
tasks[new_task_name] = tasks.pop(selected_task)
save_tasks()
print(f"Task renamed to '{new_task_name}'.")
# OPTION 2: CHANGE DESCRIPTION
elif choice_selected == 2:
new_description = input("Enter the new description: ").strip()
tasks[selected_task]["description"] = new_description
save_tasks()
print("Task description updated.")
# OPTION 3: CHANGE PROGRESS STATE
elif choice_selected == 3:
print("\nChoose new progress status:")
print("1) Not done")
print("2) In progress")
print("3) Done")
prog_choice = input("Enter a number (1-3): ").strip()
if prog_choice == "1":
new_progress = "not done"
elif prog_choice == "2":
new_progress = "in progress"
elif prog_choice == "3":
new_progress = "done"
else:
print("Invalid choice.")
return
tasks[selected_task]["progress"] = new_progress
save_tasks()
print("Task progress updated.")
else:
print("Invalid selection.")
# DELETE A TASK
def delete_tasks():
"""
Deletes a task entirely from the dictionary.
"""
selected_task = input("\nEnter the name of the task to delete: ").strip()
if selected_task in tasks:
tasks.pop(selected_task)
save_tasks()
print(f"Task '{selected_task}' deleted.")
else:
print("That task does not exist.")
# MANAGE TASKS (ADD / UPDATE / DELETE)
def managing_tasks():
"""
Menu for general task management.
"""
print("\nMANAGE TASKS")
print("1) Add Task")
print("2) Update Task")
print("3) Delete Task")
try:
management_choice = int(input("Enter your choice: ").strip())
except ValueError:
print("Invalid input. Must be a number.")
return
if management_choice == 1:
add_task()
elif management_choice == 2:
update_tasks()
elif management_choice == 3:
delete_tasks()
else:
print("Invalid choice.")
# LIST TASKS (OPTIONALLY FILTERED BY STATUS)
def list_tasks(filter_status=None):
"""
Lists tasks optionally filtered by:
- 'not done'
- 'in progress'
- 'done'
"""
if not tasks:
print("\nNo tasks available.")
return
count = 0
for name, data in tasks.items():
status = data["progress"]
# If filter is applied, skip tasks that don't match
if filter_status and status != filter_status:
continue
count += 1
print(f"\nTASK: {name}")
print(f"DESCRIPTION: {data['description']}")
print(f"PROGRESS: {status}")
if count == 0:
print("\nNo tasks match that status.")
# STATUS LIST FUNCTIONS
def list_done_tasks():
print("\nDONE TASKS")
list_tasks("done")
def list_not_done_tasks():
print("\nNOT DONE TASKS")
list_tasks("not done")
def list_in_progress_tasks():
print("\nIN PROGESS TASKS")
list_tasks("in progress")
# MAIN MENU LOOP
def main_menu():
"""
Main menu shown when the program starts.
Runs until the user chooses to quit.
"""
load_tasks() # Load any existing tasks from JSON file
while True:
print("\nTASK TRACKER")
print("1) Manage tasks")
print("2) List all tasks")
print("3) List tasks that are done")
print("4) List tasks that are not done")
print("5) List tasks that are in progress")
print("6) Quit")
choice = input("Choose an option: ").strip()
if choice == "1":
managing_tasks()
elif choice == "2":
print("\nALL TASKS")
list_tasks()
elif choice == "3":
list_done_tasks()
elif choice == "4":
list_not_done_tasks()
elif choice == "5":
list_in_progress_tasks()
elif choice == "6":
print("Exiting program.")
break
else:
print("Invalid choice. Please try again.")
# PROGRAM ENTRY POINT
if __name__ == "__main__":
main_menu()