-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignmentManager.cpp
More file actions
293 lines (265 loc) · 10.2 KB
/
AssignmentManager.cpp
File metadata and controls
293 lines (265 loc) · 10.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "AssignmentManager.h"
ostream& operator <<(ostream& out, Assignment& rhs) { // Stream out the display data of an Assignment
status stat = rhs.getStatus();
if ((!rhs.isCompleted())) { // If assignment is not completed, don't try to print out the (nonexistent)
// completion date
out << rhs.dateString(rhs.getAssignedDate()) << " " << rhs.dateString(rhs.getDueDate()) << " " << rhs.getDesc()
<< " " << strConvert(stat) << endl;
return out;
}
else
{
out << rhs.dateString(rhs.getAssignedDate()) << " " << rhs.dateString(rhs.getDueDate()) << " " << rhs.getDesc()
<< " " << strConvert(stat) << " " << rhs.dateString(rhs.getCompletedDate()) << endl;
}
return out;
}
status statConvert(string& str){ // Convert status string to status type
try{
if (str == "assigned" || str == "Assigned"){
return Assigned;
}
else if (str == "completed" || str == "Completed"){
return Completed;
}
else if (str == "late" || str == "Late"){
return Late;
}
else{
throw exception("THE ASSIGNMENT STATUS IS NOT VALID\n");
}
}
catch (const exception& e){ // Error caught; the status isn't valid
cout << e.what();
}
}
string strConvert(status& stat){ // Convert a stat back into a string for printing
if (stat == Assigned){
return "Assigned";
}
else if (stat == Completed){
return "Completed";
}
else {
return "Late";
}
}
Assignment AssignmentManager::manualCreate(){ // Create an Assignment out of a string
string due, descript, assigned, stat_str, completed;
bool comp = false;
cout << "Enter the assignment's description." << endl;
getline(cin, descript);
getline(cin, descript);
cout << "Enter the assignment's assigned date. (MM-DD-YEAR)" << endl;
getline(cin, assigned);
cout << "Enter the assignment's due date. (MM-DD-YEAR)" << endl;
getline(cin, due);
cout << "Enter the assignment's status. (Assigned, Late, or Completed)" << endl;
getline(cin, stat_str);
if (stat_str == "Late" || stat_str == "late" || stat_str == "Completed" || stat_str == "completed")
{
cout << "Enter the date the assignment was completed" << endl;
getline(cin, completed);
comp = true;
}
try{ // Try block needed to throw exception for any invalid dates
Date dueDate(due, US); // Create due date out of a string in US format
Date assignDate(assigned, US); // Create assigned date out of a string in US format
if (comp) //If the assignment is completed or late, it will have a completed date value, and use a different constructor
{
Assignment assignment(descript, dueDate, assignDate, completed);
assignment.setStatus(statConvert(stat_str)); // Convert status to status type
lists_change = true;
return assignment;
}
else
{
Assignment assignment(descript, dueDate, assignDate);
assignment.setStatus(statConvert(stat_str)); // Convert status to status type
lists_change = true;
return assignment;
}
}
catch (const exception& e){
cout << e.what() << endl; // Prints "invalid date"
}
}
Assignment AssignmentManager::createAssignment(string& str){ // Create an Assignment out of a string
String_Tokenizer parse_assign(str, ","); // Create tokenizer for splitting assignment data
bool comp = false;
string due, descript, assigned, stat_str, completed;
due = parse_assign.next_token();
descript = parse_assign.next_token();
assigned = parse_assign.next_token();
stat_str = parse_assign.next_token();
descript.erase(0, 1);
assigned.erase(0, 1); // The next_token() function leaves a whitespace after the ',' char, we then erase it because our functions can not handle that.
stat_str.erase(0, 1);
if (stat_str == "Late" || stat_str == "late" || stat_str == "Completed" || stat_str == "completed")
{
completed = parse_assign.next_token();
completed.erase(0, 1);
comp = true;
}
try{ // Try block needed to throw exception for any invalid dates
Date dueDate(due, US); // Create due date out of a string in US format
Date assignDate(assigned, US); // Create assigned date out of a string in US format
if (comp) //If the assignment is completed or late, it will have a completed date value, and use a different constructor
{
Assignment assignment(descript, dueDate, completed, assignDate);
assignment.setStatus(statConvert(stat_str)); // Convert status to status type, then set it in the assignment object.
return assignment;
}
else
{
Assignment assignment(descript, dueDate, assignDate);
assignment.setStatus(statConvert(stat_str)); // Convert status to status type, then set it in the assignment object.
return assignment;
}
}
catch (const exception& e){
cout << e.what() << endl; // Prints "invalid date"
}
}
void AssignmentManager::add(Assignment& assignment, list<Assignment>& the_list){
// Adds an assignment to a list
list<Assignment>::iterator iter;
bool added = false;
if (the_list.size() == 0)
{
the_list.push_front(assignment);
}
else if (the_list.size() == 1)
{
iter = the_list.begin();
if (assignment <= *iter)
the_list.push_front(assignment);
else
the_list.push_back(assignment);
}
else if (the_list.size() > 1) // Can't compare anything with no values in the list.
{
for (iter = the_list.begin(); iter != the_list.end(); iter++) //Start from begining of the list (smallest assigned date values)
{
if (assignment <= *iter) // If the assigned date of the assignment being added is less than the one the iter is on then...
{
if (assignment == (*iter)) //If they are equal then just instert after current iter and leave function.
{
the_list.insert(iter, assignment);
added = true;
iter = the_list.end();
}
else //If the assignment being added is less than the current iterator.
{
if (iter == the_list.begin()) //If the assignment we need to add is the smallest then push front
{
the_list.push_front(assignment);
added = true;
iter = the_list.end();
}
else
{
iter--; // Decrement the iterator to get ready to insert the assignment after the one it has a larger assigned date,
//but smaller assigned date for the next assignment.
the_list.insert(iter, assignment); // Insert assignment where PrevAssignment.assignedDate() < assignemnt.assignedDate()
//and NextAssignemnt.assignedDate() > assignment.assignedDate()
added = true;
iter = the_list.end(); //We just added so in order not to keep adding the current assignment we have to break out of the loop.
}
}
}
}
if (added == false)
the_list.push_back(assignment);
}
}
void AssignmentManager::remove(Assignment& assignment, list<Assignment>& the_list){ // Removes an assignment from a
// given list
the_list.remove(assignment);
}
// Displays assignments from a list
void AssignmentManager::displayList(list<Assignment>& the_list, list<Assignment>::iterator& iter, string type_assign){
cout << type_assign << " assignments: " << endl;
cout << "Assigned Date - Due Date - Description - Status";
if (type_assign == "Completed" || type_assign == "Late" || type_assign == "late" || type_assign == "completed")
cout << " - Completed Date" << endl;
else
cout << endl;
for (iter = the_list.begin(); iter != the_list.end(); iter++)
{
cout << (*iter);
}
cout << endl;
}
// Completes an assignment in a list
void AssignmentManager::complete(list<Assignment>& the_list, list<Assignment>& comp_list,
list<Assignment>::iterator& iter){
try{
string assigned;
cout << "Type in the assigned date: " << endl;
getline(cin, assigned);
Date assignedDate(assigned); // Possible exception here
for (iter = the_list.begin(); iter != the_list.end(); iter++)
{
if (iter->getAssignedDate() == assigned){ // If assignment is found, complete it
string completed;
cout << "Type in the completion date: " << endl;
getline(cin, completed);
Date completedDate(completed);
iter->setCompletedDate(completedDate);
if (iter->getDueDate() < completedDate){ // If the completed date is after due date...
iter->setStatus(Late); // ...It is late
late_count++;
}
else{
iter->setStatus(Completed); // Otherwise it is simply completed
}
add((*iter), comp_list); // Add to completed list
remove((*iter), the_list); // Remove from assigned list
lists_change = true; // A change has been made to one or both lists
break;
}
}
}
catch (exception& e){
cout << e.what() << endl;
}
}
// Edit an assignment in a list
void AssignmentManager::edit(list<Assignment>& the_list, list<Assignment>::iterator& iter){
try{
string assigned;
cout << "Type in the assigned date: " << endl;
getline(cin, assigned);
Date assignedDate(assigned); // Possible exception here
for (iter = the_list.begin(); iter != the_list.end(); iter++)
{
if (iter->getAssignedDate() == assigned) // If assignment is found, edit it
{
string yes_no;
cout << "Edit the due date? (Y/N)" << endl;
getline(cin, yes_no);
if (toupper(yes_no[0]) == 'Y'){
string newDue;
cout << "Type in the new due date: " << endl;
getline(cin, newDue);
Date newDueDate(newDue);
iter->setDueDate(newDueDate); // New due date set
}
cout << "Edit the description? (Y/N)" << endl;
getline(cin, yes_no);
if (toupper(yes_no[0]) == 'Y'){
string describe;
cout << "Type in the new description: " << endl;
getline(cin, describe);
iter->setDesc(describe); // New description set
}
lists_change = true; // A change has been made to the assigned list
break;
}
}
}
catch (exception& e){
cout << e.what() << endl;
}
}