-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask_manager_oop.py
More file actions
206 lines (162 loc) · 4.95 KB
/
Copy pathtask_manager_oop.py
File metadata and controls
206 lines (162 loc) · 4.95 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
"""
Task Manager Application
------------------------
Object-Oriented task manager using JSON for data storage.
Features:
- Add tasks (with optional notes)
- View all tasks
- Mark tasks as completed
- Rename tasks
- Remove tasks
- Persistent storage using JSON
Author: Ahmed Kandeel
"""
import json
class TaskManager:
FILE_NAME = "Tasks.json"
def __init__(self):
self.tasks = self.load_tasks()
# =========================
# File Handling
# =========================
def load_tasks(self):
"""Load tasks from JSON file"""
try:
with open(self.FILE_NAME, "r") as file:
return json.load(file)
except FileNotFoundError:
return []
def save_tasks(self):
"""Save tasks to JSON file"""
with open(self.FILE_NAME, "w") as file:
json.dump(self.tasks, file, indent=4)
# =========================
# Helper Methods
# =========================
def choose_task(self, tasks, message):
"""
Display tasks and allow user to choose one
Returns index of selected task or None
"""
for i, task in enumerate(tasks, 1):
print(f"{i}- {task['task']}")
try:
choice = int(input(message))
if 1 <= choice <= len(tasks):
return choice - 1
except ValueError:
pass
print("Invalid choice!")
return None
# =========================
# Core Methods
# =========================
def add_task(self):
task_name = input("Enter task name: ").strip().title()
if not task_name or task_name.isdigit():
print("Invalid task name!")
return
task_data = {
"task": task_name,
"completed": False
}
add_note = input("Do you want to add a note? (y/n): ").lower()
if add_note in ("y", "yes"):
note = input("Enter note: ").strip().capitalize()
task_data["note"] = note
self.tasks.append(task_data)
self.save_tasks()
print("Task added successfully ✅")
def view_tasks(self):
if not self.tasks:
print("No tasks found!")
return
for i, task in enumerate(self.tasks, 1):
status = "✔️" if task["completed"] else "❌"
print(f"{i}. {task['task']} {status}")
if "note" in task:
print(f" 📝 Note: {task['note']}")
print("-" * 30)
def mark_task(self):
"""
Mark only incomplete tasks as completed.
We use a filtered list but modify the original task objects.
"""
incomplete_tasks = [
task for task in self.tasks if not task["completed"]
]
if not incomplete_tasks:
print("No incomplete tasks!")
return
index = self.choose_task(
incomplete_tasks,
"Choose completed task number: "
)
if index is None:
return
incomplete_tasks[index]["completed"] = True
self.save_tasks()
print("Task marked as completed 🎉")
def rename_task(self):
if not self.tasks:
print("No tasks to rename!")
return
index = self.choose_task(
self.tasks,
"Choose task to rename: "
)
if index is None:
return
new_name = input("Enter new task name: ").strip().title()
if not new_name or new_name.isdigit():
print("Invalid name!")
return
self.tasks[index]["task"] = new_name
self.save_tasks()
print("Task renamed successfully ✏️")
def remove_task(self):
if not self.tasks:
print("No tasks to remove!")
return
index = self.choose_task(
self.tasks,
"Choose task to remove: "
)
if index is None:
return
removed = self.tasks.pop(index)
self.save_tasks()
print(f"Task '{removed['task']}' removed successfully 🗑️")
# =========================
# Application Runner
# =========================
def run(self):
print("Welcome to Task Manager 👋")
while True:
print("""
1- Add Task
2- Mark Task as Completed
3- View Tasks
4- Remove Task
5- Rename Task
6- Quit
""")
choice = input("Choose an option: ").strip()
if choice == "1":
self.add_task()
elif choice == "2":
self.mark_task()
elif choice == "3":
self.view_tasks()
elif choice == "4":
self.remove_task()
elif choice == "5":
self.rename_task()
elif choice == "6":
print("Goodbye 👋")
break
else:
print("Invalid choice!")
if __name__ == "__main__":
app = TaskManager()
app.run()