forked from SuperDev991/ImportantQuestions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskmanager .cpp
More file actions
249 lines (215 loc) · 7.53 KB
/
taskmanager .cpp
File metadata and controls
249 lines (215 loc) · 7.53 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
#include<bits/stdc++.h>
using namespace std;
void print_help();
int main(int argc, char ** argv) {
if (argc < 2) {
print_help();
}
else {
map < string, int > m;
m["add"] = 1;
m["ls"] = 2;
m["del"] = 3;
m["done"] = 4;
m["report"] = 5;
fstream fout;
multimap < int, string > taskXprior;
fout.open("task.txt", ios:: in );
if (fout.is_open()) {
string line;
while (getline(fout, line)) {
auto priority = stoi(line.substr(0, line.find(' ')));
auto taskname = line.substr(line.find(' ') + 1, line.length());
taskXprior.insert({
priority,
taskname
});
}
fout.close();
fout.open("task.txt", ios::out); {
for (auto it: taskXprior) {
fout << it.first << " " << it.second << endl;
}
fout.close();
}
}
int command = m[argv[1]];
switch (command) {
case 1:
{
try{
auto priority = stoi(argv[2]);
string taskname = argv[3];
taskXprior.insert({priority, taskname});
fout.open("task.txt", ios::out);
if (fout.is_open()) {
for (auto it: taskXprior) {
// cout<<it.first<<" "<<it.second<<endl;
fout << it.first << " " << it.second << endl;
}
fout.close();
}
cout << "Added task: \"" << taskname << "\" with priority " << priority << endl;
}
catch(...){
cout<<"Error: Missing tasks string. Nothing added!"<<endl;
}
}
break;
case 2: {
int pendingt = 0;
for (auto it: taskXprior) {
pendingt++;
}
if(pendingt<1){
cout<<"There are no pending tasks!"<<endl;
}
else{
fout.open("task.txt", ios:: in );
if (fout.is_open()) {
string line;
int count = 1;
while (getline(fout, line)) {
auto priority = stoi(line.substr(0, line.find(' ')));
auto taskname = line.substr(line.find(' ') + 1, line.length());
cout << count << ". " << taskname << " [" << priority << "]" << endl;
count++;
}
fout.close();
}
}
}
break;
case 3: {
int pendingt = 0, completedt = 0;
auto index = stoi(argv[2]);
for (auto it: taskXprior) {
pendingt++;
}
fout.open("completed.txt", ios:: in );
if (fout.is_open()) {
string s;
while (getline(fout, s)) {
completedt++;
}
fout.close();
}
// cout<<pendingt<<" "<<completedt;
if (pendingt < index) {
cout << "Error: item with index " << index << " does not exist. Nothing deleted." << endl;
} else {
auto it = taskXprior.begin();
for (int i = 0; i < index - 1; i++) {
it++;
}
taskXprior.erase(it);
fout.open("task.txt", ios::out); {
for (auto it: taskXprior) {
fout << it.first << " " << it.second << endl;
}
fout.close();
}
cout << "Deleted task #" << index << endl;
}
}
break;
case 4: {
int pendingt = 0, completedt = 0;
auto index = stoi(argv[2]);
for (auto it: taskXprior) {
pendingt++;
}
fout.open("completed.txt", ios:: in );
if (fout.is_open()) {
string s;
while (getline(fout, s)) {
completedt++;
}
fout.close();
}
if (pendingt < index) {
cout << "Error: no incomplete item with index " << index << " exists." << endl;
} else {
int num;
string str; {
auto it = taskXprior.begin();
for (int i = 0; i < index - 1; i++) {
it++;
}
num = it -> first;
str = it -> second;
taskXprior.erase(it);
fout.open("task.txt", ios::out); {
for (auto it: taskXprior) {
fout << it.first << " " << it.second << endl;
}
fout.close();
}
}
fout.open("completed.txt", ios::app);
if (fout.is_open()) {
fout << num << " " << str << endl;
fout.close();
}
cout << "Marked item as done." << endl;
}
// completed.insert({num, str});
}
break;
case 5: {
int pendingt = 0, completedt = 0;
for (auto it: taskXprior) {
pendingt++;
}
fout.open("completed.txt", ios:: in );
if (fout.is_open()) {
string s;
while (getline(fout, s)) {
completedt++;
}
fout.close();
}
cout << "Pending : " << pendingt << endl;
fout.open("task.txt", ios:: in );
if (fout.is_open()) {
string line;
int count = 1;
while (getline(fout, line)) {
auto priority = stoi(line.substr(0, line.find(' ')));
auto taskname = line.substr(line.find(' ') + 1, line.length());
cout << count << ". " << taskname << " [" << priority << "]" << endl;
count++;
}
fout.close();
}
cout << endl;
cout << "Completed : " << completedt << endl;
fout.open("completed.txt", ios:: in );
if (fout.is_open()) {
string line;
int count = 1;
while (getline(fout, line)) {
auto priority = stoi(line.substr(0, line.find(' ')));
auto taskname = line.substr(line.find(' ') + 1, line.length());
cout << count << ". " << taskname << endl;
count++;
}
fout.close();
}
}
break;
default:
print_help();
break;
}
}
}
void print_help() {
cout << "Usage :-\n";
cout << "$ ./task add 2 hello world # Add a new item with priority 2 and text \"hello world\" to the list\n";
cout << "$ ./task ls # Show incomplete priority list items sorted by priority in ascending order\n";
cout << "$ ./task del INDEX # Delete the incomplete item with the given index\n";
cout << "$ ./task done INDEX # Mark the incomplete item with the given index as complete\n";
cout << "$ ./task help # Show usage\n";
cout << "$ ./task report # Statistics\n";
}