Untitled
class Task: task_counter = 0 def __init__(self, title, description, due_date, status): Task.task_counter += 1 self.id = Task.task_counter self.title = title self.description = description self.due_date = due_date self.status = status def update_task(self, title=None, description=None, due_date=None, status=None): if title: self.title = title if description: self.description = description if due_date: self.due_date = due_date if status: self.status = status def delete_task(self): del self def display_task(self): print(f"Task ID: {self.id}") print(f"Title: {self.title}") print(f"Description: {self.description}") print(f"Due Date: {self.due_date}") print(f"Status: {self.status}\n") class personal_task(Task): def __init__(self, title, description, due_date, category): super().__init__(title, description, due_date, status) self.category = category class WorkTask(Task): def __init__(self, title, description, due_date, priority, status="Incomplete"): super().__init__(title, description, due_date, status) self.priority = priority def update_task(self, title=None, description=None, due_date=None, status=None, priority=None): super().update_task(title, description, due_date, status) if priority: self.priority = priority def display_task(self): super().display_task() print(f"Priority: {self.priority}\n") import json class TaskManager: def __init__(self): self.tasks = [] def add_task(self, task): self.tasks.append(task) def delete_task(self, task_id): task = self.find_task_by_id(task_id) if task: self.tasks.remove(task) print(f"Task with ID {task_id} has been deleted.") def update_task_id(self, task_id, **kwargs): task = self.find_task_by_id(task_id) if task: task.update_task(**kwargs) def display_all_tasks(self): for task in self.tasks: task.display_task() def find_task_by_id(self, task_id): for task in self.tasks: if task.id == task_id: return task return None def save_to_json(self, filename="tasks.json"): tasks_data = [ { "title": task.title, "description": task.description, "due_date": task.due_date, "status": task.status, "priority": getattr(task, "priority", None), "category": getattr(task, "category", None), } for task in self.tasks ] with open(filename, "w") as file: json.dump(tasks_data, file, indent=3) def load_from_json(self, filename="tasks.json"): try: with open(filename, "r") as file: tasks_data = json.load(file) for task_data in tasks_data: if task_data.get("priority") is not None: task = WorkTask( title=task_data["title"], description=task_data["description"], due_date=task_data["due_date"], priority=task_data["priority"], status=task_data["status"], ) elif task_data.get("category") is not None: task = personal_task( title=task_data["title"], description=task_data["description"], due_date=task_data["due_date"], category=task_data["category"], status=task_data["status"], ) else: task = Task( title=task_data["title"], description=task_data["description"], due_date=task_data["due_date"], status=task_data["status"], ) self.add_task(task) except FileNotFoundError: print(f"File {filename} not found.") def show_menu(): task_manager = TaskManager() while True: print("\n--- Task Manager ---") print("1. Create a new task") print("2. Update a task") print("3. Delete a task") print("4. Show all tasks") print("5. Exit") choice = input("Enter your choice: ") if choice == "1": title = input("Task title: ") description = input("Task description: ") due_date = input("Due date: ") kindOfTask = input("Choose (workTask/personalTask): ") if kindOfTask == "personalTask": category = input("Choose category (Family/Sports/Friends): ") task = personal_task(title, description, due_date, category) task_manager.add_task(task) elif kindOfTask == "workTask": priority = input("Task priority (Low/Medium/High): ") task = WorkTask(title, description, due_date, priority) task_manager.add_task(task) elif choice == "2": task_id = int(input("Task ID to update: ")) answer = input("What would you like to update? (title, description, due date, status, priority, category) ") if answer == "due date": new_due_date = input("New due date: ") task_manager.update_task_id(task_id, due_date=new_due_date) elif answer == "title": new_title = input("New title: ") task_manager.update_task_id(task_id, title=new_title) elif answer == "description": new_description = input("New description: ") task_manager.update_task_id(task_id, description=new_description) elif answer == "status": new_status = input("New status: ") task_manager.update_task_id(task_id, status=new_status) elif answer == "priority": new_priority = input("New priority (Low/Medium/High): ") task_manager.update_task_id(task_id, priority=new_priority) elif answer == "category": new_category = input("New category (Family/Sports/Friends): ") task_manager.update_task_id(task_id, category=new_category) elif choice == "3": task_id = int(input("Enter task ID to delete: ")) task_manager.delete_task(task_id) print("Task deleted successfully!") elif choice == "4": task_manager.display_all_tasks() elif choice == "5": task_manager.save_to_json() # Save tasks before exiting print("Your tasks have been saved successfully.") break show_menu()
Leave a Comment