-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskManager.py
More file actions
118 lines (102 loc) · 3.79 KB
/
Copy pathTaskManager.py
File metadata and controls
118 lines (102 loc) · 3.79 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
import json
import os
from datetime import datetime
# Define a Task class
class Task:
def __init__(self, title: str, description: str = "", due_date: str = None, completed: bool = False):
self.title = title
self.description = description
self.due_date = due_date
self.completed = completed
def mark_completed(self):
self.completed = True
def to_dict(self):
return {
"title": self.title,
"description": self.description,
"due_date": self.due_date,
"completed": self.completed
}
@staticmethod
def from_dict(data):
return Task(
title=data.get("title"),
description=data.get("description"),
due_date=data.get("due_date"),
completed=data.get("completed", False)
)
# Task Manager class to handle a collection of tasks
class TaskManager:
def __init__(self, filename="tasks.json"):
self.filename = filename
self.tasks = self.load_tasks()
def add_task(self, task: Task):
self.tasks.append(task)
self.save_tasks()
print(f"Task '{task.title}' added.")
def remove_task(self, task_index: int):
if 0 <= task_index < len(self.tasks):
removed_task = self.tasks.pop(task_index)
self.save_tasks()
print(f"Task '{removed_task.title}' removed.")
else:
print("Invalid task index.")
def list_tasks(self):
if not self.tasks:
print("No tasks available.")
return
for i, task in enumerate(self.tasks):
status = "Done" if task.completed else "Pending"
print(f"{i + 1}. {task.title} - {status}")
if task.description:
print(f" Description: {task.description}")
if task.due_date:
print(f" Due Date: {task.due_date}")
def mark_task_completed(self, task_index: int):
if 0 <= task_index < len(self.tasks):
self.tasks[task_index].mark_completed()
self.save_tasks()
print(f"Task '{self.tasks[task_index].title}' marked as completed.")
else:
print("Invalid task index.")
def save_tasks(self):
with open(self.filename, "w") as f:
json.dump([task.to_dict() for task in self.tasks], f, indent=4)
def load_tasks(self):
if not os.path.exists(self.filename):
return []
with open(self.filename, "r") as f:
data = json.load(f)
return [Task.from_dict(d) for d in data]
# Command-line interface
def main():
manager = TaskManager()
while True:
print("
Task Manager")
print("1. List Tasks")
print("2. Add Task")
print("3. Remove Task")
print("4. Mark Task Completed")
print("5. Exit")
choice = input("Choose an option: ")
if choice == "1":
manager.list_tasks()
elif choice == "2":
title = input("Task title: ")
description = input("Task description (optional): ")
due_date = input("Due date (YYYY-MM-DD) (optional): ")
manager.add_task(Task(title=title, description=description, due_date=due_date))
elif choice == "3":
index = int(input("Task index to remove: ")) - 1
manager.remove_task(index)
elif choice == "4":
index = int(input("Task index to mark completed: ")) - 1
manager.mark_task_completed(index)
elif choice == "5":
print("Exiting Task Manager. Goodbye!")
break
else:
print("Invalid option. Please try again.")
if __name__ == "__main__":
main()