-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.cpp
222 lines (209 loc) · 5.61 KB
/
User.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
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
#include "User.h"
#include <iostream>
#include <fstream>
#include "Controller.h"
extern Controller* now;
User::User(int _id, std::string _name, std::string _fileName, std::string _passwd, std::string _tel, std::string _addr)
{
id = _id;
name = _name;
fileName = _fileName;
passwd = _passwd;
tel = _tel;
addr = _addr;
bla = 0;
sendNum = recvNum = 0;
send.clear();
recv.clear();
}
int User::init(const 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 User::init()
{
return init(fileName);
}
int User::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 << "User " << id << std::endl;
fout.close();
return 0;
}
int User::listen()
{
int num;
std::string op,str;
Timestamp low, up;
char buf[30]={0};
while (1)
{
std::cout << "==============================\n"
<< "Please choose a way to continue:\n"
<< "mp [old password] [string]: change your password to [string]\n"
<< "b: query your account balance\n"
<< "c [number]: top up your account with [number] yuan\n"
<< "f [r/s] [lower timestamp] [upper timestamp] [number]: search specified packages with parameters\n"
<< "s [type:1/2/3] [number1] [number2]: send [type] package weighted [number1] to user with ID [number2]\n"
<< "r [number]: confirm receipt of your package of ID [number]\n"
<< "q: quit\n"
<< "==============================\n" << std::endl;
std::cin >> op;
if (op[0] == 'm'&&op.length() >= 2 && op[1] == 'p')
{
std::cin >> str;
if (checkPasswd(str) == EQUAL_TO_OLD_PASSWD)
{
std::cin >> str;
modifyPasswd(str);
}
else
{
std::cin >> str;
std::cout << "Wrong old password. Failed\n" << std::endl;
}
}
else if (op[0] == 'b')
{
std::cout << "The balance in your account: " << bla << std::endl;
}
else if (op[0] == 'c')
{
std::cin >> num;
if (num >= 0)
{
recharge(num);
std::cout << "Done.\n" << std::endl;
}
else
{
std::cout << "Input should be a positive integer. Failed.\n" << std::endl;
}
}
else if (op[0] == 'f')
{
/*
* The [number] field has the highest priority,
* followed by [lower timestamp], [upper timestamp] and [r/s]
*/
std::cin >> str >> low >> up >> num;
if (~num)
{
now->ft->pFilterById(id, num);
}
else if (str[0] == 's')
{
now->ft->pFilterBySendTime(id,low,up);
}
else if (str[0] == 'r')
{
now->ft->pFilterByRecvTime(id, low, up);
}
else
{
std::cout << "Error: bad parameters. Failed.\n" << std::endl;
}
}
else if (op[0] == 's')
{
//std::cout << "Please input the ID of the receiver:" << std::endl;
int _type, _vol;
std::cin >> _type >> _vol >> num;
if (1 <= num && num <= (now->numberUser) && num != id)
{
now->sendNewPackage( _type, _vol, id, num);
std::cout << "Done.\n" << std::endl;
}
else
{
std::cout << "Invalid receiver ID. Failed.\n" << std::endl;
}
}
else if (op[0] == 'r')
{
std::cin >> num;
now->recvPackage(num, id);
std::cout << "Receive operation has done.\n" << std::endl;
}
else if (op[0] == 'q')
{
break;
}
else
{
std::cout << "Error: bad parameters. Please try again.\n" << std::endl;
}
}
return 0;
}
void User::appendSend(const int& packId)
{
sendNum++;
send.push_back(packId);
}
void User::appendRecv(const int& packId)
{
recvNum++;
recv.push_back(packId);
}
std::istream& operator>>(std::istream& in, User& A)
{
in >> A.id >> A.bla >> A.name >> A.tel >> A.passwd;
in >> A.addr;
int t;
A.send.clear();
in >> A.sendNum;
for (int i = 1; i <= A.sendNum; i++)
{
in >> t;
A.send.push_back(t);
}
A.recv.clear();
in >> A.recvNum;
for (int i = 1; i <= A.recvNum; i++)
{
in >> t;
A.recv.push_back(t);
}
return in;
}
std::ostream& operator << (std::ostream& out, User& 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;
out << A.addr << std::endl;
out << A.sendNum << std::endl;
for (auto x : A.send)
out << x <<' ';
out << std::endl;
out << A.recvNum << std::endl;
for (auto x : A.recv)
out << x << ' ';
out << std::endl;
return out;
}