|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <string> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +// Class representing a single Task |
| 7 | +class Task { |
| 8 | +private: |
| 9 | + string title; |
| 10 | + bool isCompleted; |
| 11 | + |
| 12 | +public: |
| 13 | + // Constructor |
| 14 | + Task(string t) { |
| 15 | + title = t; |
| 16 | + isCompleted = false; |
| 17 | + } |
| 18 | + |
| 19 | + // Mark task as completed |
| 20 | + void markCompleted() { |
| 21 | + isCompleted = true; |
| 22 | + } |
| 23 | + |
| 24 | + // Get task title |
| 25 | + string getTitle() const { |
| 26 | + return title; |
| 27 | + } |
| 28 | + |
| 29 | + // Check completion status |
| 30 | + bool getStatus() const { |
| 31 | + return isCompleted; |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +// Class representing the To-Do List |
| 36 | +class ToDoList { |
| 37 | +private: |
| 38 | + vector<Task> tasks; |
| 39 | + |
| 40 | +public: |
| 41 | + // Add a new task |
| 42 | + void addTask(const string& title) { |
| 43 | + tasks.push_back(Task(title)); |
| 44 | + cout << "Task added successfully!\n"; |
| 45 | + } |
| 46 | + |
| 47 | + // Display all tasks |
| 48 | + void showTasks() const { |
| 49 | + if (tasks.empty()) { |
| 50 | + cout << "No tasks available!\n"; |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + cout << "\n------ To-Do List ------\n"; |
| 55 | + for (size_t i = 0; i < tasks.size(); ++i) { |
| 56 | + cout << i + 1 << ". " << tasks[i].getTitle() |
| 57 | + << " [" << (tasks[i].getStatus() ? "Done" : "Pending") << "]\n"; |
| 58 | + } |
| 59 | + cout << "------------------------\n"; |
| 60 | + } |
| 61 | + |
| 62 | + // Mark a task as completed |
| 63 | + void markTaskCompleted(int index) { |
| 64 | + if (index < 1 || index > (int)tasks.size()) { |
| 65 | + cout << "Invalid task number!\n"; |
| 66 | + return; |
| 67 | + } |
| 68 | + tasks[index - 1].markCompleted(); |
| 69 | + cout << "Task marked as completed!\n"; |
| 70 | + } |
| 71 | + |
| 72 | + // Delete a task |
| 73 | + void deleteTask(int index) { |
| 74 | + if (index < 1 || index > (int)tasks.size()) { |
| 75 | + cout << "Invalid task number!\n"; |
| 76 | + return; |
| 77 | + } |
| 78 | + tasks.erase(tasks.begin() + index - 1); |
| 79 | + cout << "Task deleted successfully!\n"; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +// Main program |
| 84 | +int main() { |
| 85 | + ToDoList todo; |
| 86 | + int choice; |
| 87 | + string title; |
| 88 | + int index; |
| 89 | + |
| 90 | + cout << "\n===== TO-DO LIST APPLICATION =====\n"; |
| 91 | + cout << "Manage your daily tasks efficiently.\n"; |
| 92 | + |
| 93 | + do { |
| 94 | + cout << "\nMenu:\n"; |
| 95 | + cout << "1. Add Task\n"; |
| 96 | + cout << "2. View Tasks\n"; |
| 97 | + cout << "3. Mark Task as Completed\n"; |
| 98 | + cout << "4. Delete Task\n"; |
| 99 | + cout << "5. Exit\n"; |
| 100 | + cout << "Enter your choice: "; |
| 101 | + cin >> choice; |
| 102 | + cin.ignore(); // To handle newline |
| 103 | + |
| 104 | + switch (choice) { |
| 105 | + case 1: |
| 106 | + cout << "Enter task title: "; |
| 107 | + getline(cin, title); |
| 108 | + todo.addTask(title); |
| 109 | + break; |
| 110 | + |
| 111 | + case 2: |
| 112 | + todo.showTasks(); |
| 113 | + break; |
| 114 | + |
| 115 | + case 3: |
| 116 | + cout << "Enter task number to mark completed: "; |
| 117 | + cin >> index; |
| 118 | + todo.markTaskCompleted(index); |
| 119 | + break; |
| 120 | + |
| 121 | + case 4: |
| 122 | + cout << "Enter task number to delete: "; |
| 123 | + cin >> index; |
| 124 | + todo.deleteTask(index); |
| 125 | + break; |
| 126 | + |
| 127 | + case 5: |
| 128 | + cout << "Exiting program. Goodbye!\n"; |
| 129 | + break; |
| 130 | + |
| 131 | + default: |
| 132 | + cout << "Invalid choice. Try again!\n"; |
| 133 | + } |
| 134 | + } while (choice != 5); |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
| 138 | + |
| 139 | + |
0 commit comments