-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_management.py
147 lines (120 loc) · 4.85 KB
/
task_management.py
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
import csv
class Task:
def __init__(self, title, priority):
self.title = title
self.priority = priority
self.completed = False
def mark_completed(self):
self.completed = True
class TaskManager(Task):
def __init__(self):
self.tasks = []
def add_task(self, title, priority):
task = Task(title, priority)
self.tasks.append(task)
print("Task added successfully.")
def view_tasks(self):
pending_tasks = [task for task in self.tasks if not task.completed]
if not pending_tasks:
print("No pending tasks.")
return
for index, task in enumerate(pending_tasks, start=1):
print(
f"{index}. {task.title} - Priority: {task.priority} - Completed: {'Yes' if task.completed else 'No'}"
)
def complete_task(self, task_index):
if task_index < 0 or task_index >= len(self.tasks):
print("Invalid task index.")
return
pending_tasks = [task for task in self.tasks if not task.completed]
if task_index >= len(pending_tasks):
print("Invalid task index.")
return
task = pending_tasks[task_index]
task.mark_completed()
print("Task marked as completed.")
self.view_tasks()
def view_completed_tasks(self):
completed_tasks = [task for task in self.tasks if task.completed]
if completed_tasks:
for index, task in enumerate(completed_tasks, start=1):
print(f"{index}. {task.title} - Priority: {task.priority}")
else:
print("No completed tasks.")
def delete_task(self, task_index):
if task_index < 0 or task_index >= len(self.tasks):
print("Invalid task index.")
return
del self.tasks[task_index]
print("Task deleted successfully.")
def search_tasks_by_priority(self, priority):
priority = priority.lower()
matching_tasks = [
task
for task in self.tasks
if task.priority.lower() == priority and not task.completed
]
if matching_tasks:
for index, task in enumerate(matching_tasks, start=1):
print(
f"{index}. {task.title} - Priority: {task.priority} - Completed: {'Yes' if task.completed else 'No'}"
)
else:
print(f"No tasks found with priority '{priority}'.")
def save_tasks_to_file(self, filename):
with open(filename, "w", newline="") as file:
writer = csv.writer(file)
for task in self.tasks:
writer.writerow([task.title, task.priority, task.completed])
def load_tasks_from_file(self, filename):
try:
with open(filename, "r") as file:
reader = csv.reader(file)
for row in reader:
if len(row) >= 3:
title, priority, completed = row
task = Task(title, priority)
task.completed = completed == "True"
self.tasks.append(task)
else:
print("Invalid data format in the tasks file.")
except FileNotFoundError:
print("No tasks file found.")
def main():
task_manager = TaskManager()
task_manager.load_tasks_from_file("tasks.csv")
while True:
print("\n🟡🟢🟠 Task Management System 🟠🟢🟡")
print("1. Add New Task ➕")
print("2. View Tasks ")
print("3. Complete Task ✔")
print("4. View Completed Tasks✅")
print("5. Delete Task ️")
print("6. Search Tasks By Priority ")
print("7. Exit ❌")
choice = input("Enter your choice: ")
if choice == "1":
title = input("Enter task title: ")
priority = input("Enter task priority (high/medium/low): ")
task_manager.add_task(title, priority)
elif choice == "2":
task_manager.view_tasks()
elif choice == "3":
task_index = int(input("Enter the index of the task to mark as completed: ")) - 1
task_manager.complete_task(task_index)
elif choice == "4":
task_manager.view_completed_tasks()
elif choice == "5":
task_index = int(input("Enter the index of the task to delete: ")) - 1
task_manager.delete_task(task_index)
elif choice == "6":
priority = input("Enter the priority to search (high/medium/low): ")
task_manager.search_tasks_by_priority(priority)
elif choice == "7":
task_manager.save_tasks_to_file("tasks.csv")
print("Exiting...")
break
else:
print("Invalid choice. Please enter a number between 1 and 7.")
if __name__ == "__main__":
main()