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 |
#include <iostream> #include <vector> #include <string> #include <iomanip> class Task { public: Task(const std::string& name, const std::string& assignedTo, int priority) : name(name), assignedTo(assignedTo), priority(priority), isComplete(false) {} void markComplete() { isComplete = true; } void print() const { std::cout << std::setw(20) << std::left << name << std::setw(20) << std::left << assignedTo << std::setw(10) << std::left << priority << (isComplete ? "Complete" : "Incomplete") << std::endl; } private: std::string name; std::string assignedTo; int priority; bool isComplete; }; class Project { public: void addTask(const std::string& name, const std::string& assignedTo, int priority) { tasks.emplace_back(name, assignedTo, priority); } void markTaskComplete(int index) { if (index >= 0 && index < tasks.size()) { tasks[index].markComplete(); } else { std::cerr << "Error: Invalid task index." << std::endl; } } void printTasks() const { std::cout << std::setw(20) << std::left << "Task Name" << std::setw(20) << std::left << "Assigned To" << std::setw(10) << std::left << "Priority" << "Status" << std::endl; for (const auto& task : tasks) { task.print(); } } private: std::vector<Task> tasks; }; int main() { Project project; int choice; do { std::cout << "\nProject Management Simulation\n"; std::cout << "1. Add Task\n"; std::cout << "2. Mark Task Complete\n"; std::cout << "3. Print Tasks\n"; std::cout << "4. Exit\n"; std::cout << "Enter your choice: "; std::cin >> choice; std::cin.ignore(); // To ignore the newline character after integer input if (choice == 1) { std::string name, assignedTo; int priority; std::cout << "Enter task name: "; std::getline(std::cin, name); std::cout << "Enter assigned to: "; std::getline(std::cin, assignedTo); std::cout << "Enter priority (1-10): "; std::cin >> priority; std::cin.ignore(); // To ignore the newline character after integer input project.addTask(name, assignedTo, priority); } else if (choice == 2) { int index; std::cout << "Enter task index to mark as complete: "; std::cin >> index; project.markTaskComplete(index); } else if (choice == 3) { project.printTasks(); } else if (choice == 4) { std::cout << "Exiting program.\n"; } else { std::cerr << "Invalid choice. Please try again.\n"; } } while (choice != 4); return 0; } |
Explanation
- Class
Task
:- Purpose: Represents a single task within a project.
- Attributes:
name
: Name of the task.assignedTo
: Person responsible for the task.priority
: Priority level of the task (e.g., 1-10).isComplete
: Status indicating whether the task is complete.
- Methods:
markComplete()
: Marks the task as complete.print()
: Prints task details in a formatted manner.
- Class
Project
:- Purpose: Manages a collection of tasks.
- Attributes:
tasks
: Vector ofTask
objects.
- Methods:
addTask()
: Adds a new task to the project.markTaskComplete()
: Marks a specific task as complete by its index.printTasks()
: Prints the details of all tasks.
- Main Function:
- Setup: Creates an instance of
Project
. - Menu: Provides a menu for the user to add tasks, mark tasks as complete, print tasks, or exit the program.
- Input Handling: Reads user input to manage tasks and interact with the project management system.
- Setup: Creates an instance of
Usage
- Task Management: Allows adding new tasks, marking tasks as complete, and printing task details.
- User Interaction: Provides a simple text-based menu to interact with the project management system.