הערה (קובץ קוד)
קובץ זה הוא קובץ קוד מסוג CPP.
#include <iostream>
#include <stdexcept>
#include "TaskManager.h"
TaskManager::TaskManager() : amountOfPeople(0), tasks(0) {
for (int i = 0; i < MAX_PERSONS; i++) {
people[i] = nullptr;
}
}
TaskManager::~TaskManager() {
for (int i = 0; i < amountOfPeople; i++) {
delete people[i];
}
}
void TaskManager::assignTask(const string& personName, const Task& task) {
Task editedTask = task;
editedTask.setId(tasks++);
for (int i = 0; i < amountOfPeople; i++) {
if (people[i]->getName() == personName) {
people[i]->assignTask(editedTask);
return;
}
}
if (amountOfPeople >= MAX_PERSONS) {
throw std::runtime_error("The system is already full with people.");
}
Person* newPerson = new Person(personName);
try {
newPerson->assignTask(editedTask);
} catch (...) {
delete newPerson;
throw;
}
people[amountOfPeople++] = newPerson;
}
void TaskManager::completeTask(const string& personName) {
for (int i = 0; i < amountOfPeople; i++) {
if (people[i]->getName() == personName) {
people[i]->completeTask();
return;
}
}
}
void TaskManager::bumpPriorityByType(TaskType type, int priority) {
if (priority <= 0) {
return;
}
for (int i = 0; i < amountOfPeople; i++) {
SortedList<Task> result;
for (const Task& task: people[i]->getTasks()) {
if (task.getType() == type) {
Task editedTask(task.getPriority()+priority, task.getType(),
task.getDescription());
editedTask.setId(task.getId());
result.insert(editedTask);
}
else {
result.insert(task);
}
}
people[i]->setTasks(result);
}
}
void TaskManager::printAllEmployees() const {
for (int i = 0; i < amountOfPeople; i++) {
std::cout << (*people[i]) << std::endl;
}
}
void TaskManager::printAllTasks() const {
SortedList<Task> allTasks;
for (int i = 0; i < amountOfPeople; i++) {
const SortedList<Task>& current = people[i]->getTasks();
for (const Task& task: current) {
allTasks.insert(task);
}
}
// print all the tasks
for (const Task& task: allTasks) {
std::cout << task << std::endl;
}
}
void TaskManager::printTasksByType(TaskType type) const {
SortedList<Task> allTasks;
for (int i = 0; i < amountOfPeople; i++) {
const SortedList<Task>& current = people[i]->getTasks();
for (const Task& task: current) {
if (task.getType() == type) {
allTasks.insert(task);
}
}
}
// print all the tasks
for (const Task& task: allTasks) {
std::cout << task << std::endl;
}
}