-
Notifications
You must be signed in to change notification settings - Fork 0
/
Admin.cpp
166 lines (157 loc) · 4.36 KB
/
Admin.cpp
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
#include "Admin.h"
#include <fstream>
#include "Controller.h"
extern Controller* now;
int Admin::init(std::string _fileName)
{
std::ifstream fin(_fileName);
if (!fin)
{
std::cout << "ERROR: Failed to open file " << _fileName << std::endl;
return OPEN_FILE_FAIL;
}
fileName = _fileName;
fin >> (*this);
fin.close();
return OPEN_FILE_SUCCESS;
}
int Admin::init()
{
return init(fileName);
}
int Admin::save()
{
static char buf[30] = { 0 };
if (fileName.empty())
{
std::cout << "Empty filename. Failed.\n" << std::endl;
return 1;
}
std::ofstream fout(fileName);
fout << (*this);
fout.clear();
fout.close();
sprintf_s(buf, "key/%s.txt", name.c_str());
fout.open(buf);
fout << passwd << std::endl;
fout << "Admin " << id << std::endl;
fout.close();
return 0;
}
int Admin::listen()
{
int num, user_id;
std::string op, str;
Timestamp low, up;
char buf[30]={0};
while (1)
{
std::cout << "==============================\n"
<< "Please choose a way to continue:\n"
<< "mp [string]: change your password to [string]\n"
<< "b: query your account balance\n"
<< "a: add a new courier\n"
<< "d [courier_id]: delete the courier with [courier_id]\n"
<< "r [package_id] [courier_id]: re-assign [package_id] to [courier_id]\n"
<< "f1 [user id] [r/s] [lower timestamp] [upper timestamp] [number]: search specified packages with parameters\n"
<< "f2 [courier_id] [senderID] [receiverID] [packageID] [stauts:0/1/2/3]:\n"
<< " search specified packages with parameters, note [courier_id] cannot be empty\n"
<< "u: show all users' profile\n"
<< "c: show all couriers' profile\n"
<< "q: quit\n"
<< "==============================\n" << std::endl;
std::cin >> op;
if (op[0] == 'm' && op.length() >= 2 && op[1] == 'p')
{
std::cin >> str;
modifyPasswd(str);
}
else if (op[0] == 'b')
{
std::cout << "The balance in your account: " << bla << std::endl;
}
else if (op[0] == 'a')
{
now->registerCourier();
}
else if (op[0] == 'd')
{
std::cin >> num;
now->deleteCourier(num);
}
else if (op[0] == 'r')
{
int pi, ci;
std::cin >> pi >> ci;
now->reAssignPackage(pi, ci);
}
else if (op[0] == 'f' && op.length() > 1 && op[1] == '1')
{
/*
* The [number] field has the highest priority,
* followed by [r/s] [lower timestamp], [upper timestamp] and [user id]
*/
std::cin >> user_id >> str >> low >> up >> num;
if (~num)
{
now->ft->pFilterById(user_id, num);
}
else if (str[0] == 's')
{
now->ft->pFilterBySendTime(user_id, low, up);
}
else if (str[0] == 'r')
{
now->ft->pFilterByRecvTime(user_id, low, up);
}
else
{
std::cout << "Bad parameters, failed.\n" << std::endl;
}
}
else if (op[0] == 'f' && op.length() > 1 && op[1] == '2')
{
int ci, si, ri, pi;
std::cin >> ci >> si >> ri >> pi >> num;
if (~pi)
{
now->ft->pFilterById(0, pi);
}
else
{
now->ft->pFilterCourier(ci, si, ri, num);
}
}
else if (op[0] == 'u')
{
now->ft->uFilter();
}
else if (op[0] == 'c')
{
now->ft->cFilter();
}
else if (op[0] == 'q')
{
break;
}
else
{
std::cout << "Invalid input, try again." << std::endl;
}
}
return 0;
}
std::istream& operator>>(std::istream& in, Admin& A)
{
in >> A.id >> A.bla >> A.name >> A.tel >> A.passwd;
return in;
}
std::ostream& operator << (std::ostream& out, Admin& A)
{
out << A.id << std::endl;
out << A.bla << std::endl;
out << A.name << std::endl;
out << A.tel << std::endl;
out << A.passwd << std::endl;
return out;
}