Untitled
unknown
plain_text
a month ago
7.7 kB
0
Indexable
Never
import json class Task: task_counter = 0 def __init__(self, title, description, due_date, status="incomplete"): 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 __str__(self): return f"Task ID: {self.id}, Title: {self.title}, Description: {self.description}, Due Date: {self.due_date}, Status: {self.status}" class personal_task(Task): def __init__(self, title, description, due_date, category): super().__init__(title, description, due_date,status="incomplete") self.category = category def update(self, category=None, **kwargs): super().update_task(**kwargs) if category: self.category = category def __str__(self): return f"{super().__str__()} - Category: {self.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(self, priority=None, **kwargs): super().update_task(**kwargs) if priority: self.priority = priority def __str__(self): return f"{super().__str__()} - Priority: {self.priority}" import json class TaskManager: def __init__(self): self.tasks = [] def add_task(self, task): self.tasks.append(task) def delete_task(self, title): task = self.find_task(title) if task: self.tasks.remove(task) def find_task(self, title): for task in self.tasks: if task.title == title: return task print("Task not found.") return None def update_task(self, title, **kwargs): task = self.find_task(title) if task: task.update(**kwargs) else: print("Task not found.") def show_tasks(self): if not self.tasks: print("No tasks available.") for task in self.tasks: print(task) def save_to_json(self, filename="data.json"): try: 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=4) except (IOError, TypeError) as e: print(f"Failed to save tasks to {filename}: {e}") def load_from_json(self, filename="data.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 = PersonalTask( 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.") except json.JSONDecodeError as e: print(f"Error decoding JSON from {filename}: {e}") def show_menu(): task_manager = TaskManager() task_manager.load_from_json() 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": title = input("Task title 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(title, due_date=new_due_date) elif answer == "title": new_title = input("New title: ") task_manager.update_task(title, title=new_title) elif answer == "description": new_description = input("New description: ") task_manager.update_task(title, description=new_description) elif answer == "status": new_status = input("New status: ") task_manager.update_task(title, status=new_status) elif answer == "priority": new_priority = input("New priority (Low/Medium/High): ") task_manager.update_task(title, priority=new_priority) elif answer == "category": new_category = input("New category (Family/Sports/Friends): ") task_manager.update_task(title, category=new_category) elif choice == "3": title = input("Enter task title to delete: ") task_manager.delete_task(title) print("Task deleted successfully!") elif choice == "4": task_manager.show_tasks() elif choice == "5": task_manager.save_tasks_to_json() # Save tasks before exiting print("Your tasks have been saved successfully.") break show_menu()
Leave a Comment