-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.cpp
More file actions
128 lines (112 loc) · 4.2 KB
/
task.cpp
File metadata and controls
128 lines (112 loc) · 4.2 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "task.hpp" // also includes vector
#include <iostream>
#include <ctime>
#include <string>
#include "subtask.hpp"
task::~task() {
for (size_t i = 0; i < subs.size(); i++) {
delete subs.at(i);
}
}
int task::get_priority() {
return priority;
}
void task::set_priority(int p) {
priority = p;
}
bool task::complete() { // returns current state of completion
//if (!has_subtasks()) { // if task has no subs, return state of current task
// return isComplete;
//}
//for (unsigned i = 0; i < subs.size(); i++) {
// if (!(subs.at(i).complete())) { // if any one subtask is incomplete, return false
// return false;
// }
//}
//return true; // if task has subtasks and none are incomplete, return true
return isComplete;
}
void task::mark_as_complete() { // sets current task to complete
if (!has_subtasks()) {
isComplete = true; // if task has no subs, is okay to mark as complete
}
else {
bool hasIncompleteSubs = false;
for (size_t i = 0; i < subs.size(); i++) { // check each subtasks, if any one of them is incomplete,
if (!(subs.at(i)->complete())) { // main task can not be marked as complete, so abort
std::cout << "subtask \"" << subs.at(i)->getName() << "\" must be completed before \"" << this->getName() << "\" can be completed." << std::endl;
isComplete = false; // should be defaulted to false but better safe
hasIncompleteSubs = true;
}
}
if (hasIncompleteSubs) {
return;
}
std::cout << "Task Completed"<< std::endl;
isComplete = true; // if this point is reached, no subtasks are incomplete, so the task can be completed
}
}
void task::mark_as_incomplete() {
isComplete = false;
}
vector<subtask*> task::get_subtasks() {
return subs;
}
void task::add_subtask(subtask* sub) { // pushes new subtask to subs vector (default)
// instantiate new default subtask
subs.push_back(sub);
}
void task::add_subtask(string nm, string d, int p) { // pushes new subtask to subs vector (parameterized)
subtask *newSub = new subtask(d, p);
newSub->setName(nm);
subs.push_back(newSub);
}
void task::complete_subtask(string nm) {
subtask *sub = search(nm);
sub->mark_as_complete();
}
bool task::has_subtasks() { // returns true if a task has subtasks
return !subs.empty(); // if subs.empty() is true, has_subtasks() == false
} // basically, has_subtasks() is the negation of subs.empty()
subtask* task::remove_subtask(string nm){
subtask *sub = search(nm);
if (sub == nullptr) {
return nullptr;
}
for (size_t i = 0; i < subs.size(); i++) {
if (subs.at(i) == sub) {
subs.erase(subs.begin() + i);
return sub;
}
}
return nullptr;
}
void task::print_subtasks() {
if (subs.empty()) {
std::cout << "Task \"" << this->getName() << "\" has no subtasks." << std::endl;
}
else {
for (size_t i = 0; i < subs.size(); i++) {
std::cout <<"S"<< (i+1) << " Name: " << subs.at(i)->getName() << std::endl;
std::cout << " Description: " << subs.at(i)->getDescription() << std::endl;
std::cout << " Due Date: " << subs.at(i)->get_date() << std::endl;
std::cout << " Priority: " << subs.at(i)->get_priority() << std::endl;
std::cout << " Completion: " << std::boolalpha << subs.at(i)->complete() << std::endl;
}
}
}
subtask * task::search(string nm) {
subtask *sub = nullptr;
bool found = false;
for (size_t i = 0; i < subs.size(); i++) {
if (subs.at(i)->getName() == nm) { // if a subtask in subs has the same name as what is passed in, point subToComplete to it
sub = subs.at(i);
found = true;
}
}
if (!found) {
std::cout <<'\"' << this->getName() << "\" has no subtask titled \"" << nm << '\"' << std::endl;
return nullptr;
}
return sub;
}