-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.h
248 lines (228 loc) · 6.05 KB
/
user.h
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
#ifndef FINAL_USER_H
#define FINAL_USER_H
#include "library.h"
const string admin = "admin";
const string key = "123456";
//const string acount[3]={"aa","bb","cc"};
//const string acount_key[3]={"11","22","33"};
//const string name[3]={"one","two","three"};
class User{
protected:
string Name;
string Account;
string Key;
public:
User(){}
User(string ad,string ke,string na){
Account=ad;
Key=ke;
Name=na;
}
string Get_account(){
return Account;
}
string Get_key(){
return Key;
}
string Get_name(){
return Name;
}
bool Edit_name(string na){
Name=na;
return true;
}
bool Edit_account(string ac){
Account=ac;
return true;
}
bool Edit_key(string ke){
Key=ke;
return true;
}
};
class Accountistrator: protected User{
private:
public:
bool sucess;
Accountistrator(string ad,string ke,string na="Admin"):User(ad,ke,na){
if(ad==admin && ke==key)
{
cout << "*欢迎您admin,您的角色是管理员!" << endl;
sucess = true;
}else{
cout << "*用户名或者密码错误,请重新输入" << endl;
sucess = false;
}
}
};
class Consumer:protected User{
private:
int warning;
vector<int> record;
public:
Consumer(){}
Consumer(string ad,string ke,string na):User(ad,ke,na){
warning=0;
}
bool Show_record(){
cout<<"浏览记录[";
for(auto i:record){
cout<<i<<' ';
}
cout<<"]"<<endl;
return true;
}
bool Delet_record(){
record.clear();
return true;
}
bool Add_warning(){
warning++;
return true;
}
bool Add_record(int i){
record.push_back(i);
return true;
}
bool Edit_key(string ke){
Key=ke;
return true;
}
int warnings(){
return warning;
}
/*Consumer(const Consumer &sensor) {
this->Edit_name(sensor.Name);
this->Edit_account(sensor.Account);
this->Edit_key(sensor.Key);
}*/
Consumer &operator=(const Consumer &c) {
if(this == &c) {
return *this;
}
Name=c.Name;
Account=c.Account;
Key=c.Key;
return *this;
}
using User::Get_name;
using User::Get_account;
using User::Get_key;
using User::Edit_account;
using User::Edit_key;
using User::Edit_name;
friend ostream& operator<<(ostream &stream, Consumer &ob);
friend istream& operator>>(istream &stream, Consumer &ob);
};
ostream& operator<<(ostream &stream, Consumer &ob){
stream<<"用户昵称:"<<ob.Get_name()<<" 用户密码:"<<ob.Get_key()<<" 用户账号:"<<ob.Get_account()<<endl;
return stream;
}
istream& operator>>(istream &stream, Consumer &ob){
string na,ac,ke;
cout<<"请输入用户名:";
cin>>ac;
cout<<"请输入密码:";
cin>>ke;
cout<<"请输入昵称:";
cin>>na;
ob.Edit_name(na);
ob.Edit_key(ke);
ob.Edit_account(ac);
return stream;
}
class ConsumerGroup{
private:
vector<Consumer> CGroup;
int sum;
bool isNameMatch(Consumer v, string& targetName) {
return v.Get_name() == targetName;
}
void read_txt_consumer(const string& path){
ifstream file(path);
string line;
cout<<"*从数据库中读取已存入用户列表..."<<endl;
while (getline(file, line)) {
vector<string> parts = split(line, ' ', false);
string part1 = parts[0];
string part2 = parts[1];
string part3 = parts[2];
cout<<"*正在读取第"<<sum+1<<"个用户信息: "<<endl;
Consumer C(part1,part2,part3);
CGroup.push_back(C);
sum++;
//Show_consumerlist();
}
cout<<"*读取结束"<<endl;
file.close();
}
public:
bool sucess;
ConsumerGroup(string path="E:\\project\\c++\\final\\consumer.txt"){
sum=0;
read_txt_consumer(path);
}
auto Search_account(string ac){
auto it = find_if(CGroup.begin(), CGroup.end(), [&ac](Consumer &p) {return p.Get_account() == ac;});
if(it!=CGroup.end())
{
return it;
}
else
cout<<"*未找到";
}
auto Log_In(string ac,string ke){
auto it = find_if(CGroup.begin(), CGroup.end(), [&ac](Consumer &p) {return p.Get_account() == ac;});
sucess= false;
if(it==CGroup.end()){
cout<<"*未找到该用户,请重新输入"<<endl;
} else{
if(it->Get_key()==ke){
sucess= true;
cout<<"*登陆成功,您的身份是会员"<<endl;
return it;
}
}
}
int ConsumerGroup_size() const{
return sum;
}
int Consumer_num(){
return CGroup.size();
}
bool Add_consumer(Consumer s){
CGroup.push_back(s);
sum++;
return true;
}
bool Delet_consumer(string ac){
auto it = CGroup.erase(std::remove_if(CGroup.begin(), CGroup.end(), [&](const Consumer& v) {
return isNameMatch(v, ac);
}), CGroup.end());
if(it!=CGroup.end()){
sum--;
return true;}
else
return false;
}
bool Show_consumerlist(){
int i=0;
for (vector<Consumer>::iterator it = CGroup.begin(); it != CGroup.end(); ++it) {
cout<<"******第"<<i++<<"个用户********"<<endl;
cout << *it <<endl;
}
return true;
}
bool warming(string ac){
auto it = find_if(CGroup.begin(), CGroup.end(), [&ac](Consumer &p) {return p.Get_account() == ac;});
if(it==CGroup.end()){
cout<<"*未找到该用户,请重新输入"<<endl;
return false;
} else{
it->Add_warning();
cout<<"*警告成功"<<endl;
return true;
}
}
};
#endif //FINAL_USER_H