-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
117 lines (110 loc) · 2.71 KB
/
user.cpp
File metadata and controls
117 lines (110 loc) · 2.71 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
#include "user.h"
#include "userdao.h"
Userdao f;
User::User() {
this->id = 0;
this->passWord = "null";
this->userName = "null";
}
User::~User() {
}
std::string User::getUserName() {
return this->userName;
}
int User::getUserId() {
return this->id;
}
std::string User::getPassword() {
return this->passWord;
}
bool User::login(std::string fileName,std::string uName, std::string pass) {
std::cout << "Signing in, please wait...\n" ;
bool res = false;
std::ifstream in;
char line[1024] = { '\0' };
in.open(fileName);
int i = 0;
std::string result;
while (in.getline(line, sizeof(line)))
{
if (i++ == 0) continue; //跳过计数器
result = f.charToStr(line);
std::vector<std::string> temp = f.split(result, "#");
//std::cout << temp.at(1) << std::endl;
if (temp.at(1).compare(uName) == 0 && temp.at(2).compare(pass) == 0) {
std::cout << "Welcome , " <<uName<< std::endl;
this->id = atoi(temp.at(0).c_str());
this->passWord = pass;
this->userName = uName;
res = true;
break;
}
}
in.close();
return res;
}
int User::rigister(std::string fileName) {
std::string username;
std::string password;
std::string re_password;
std::cout << "Input Your Username:\n" ;
std::cin >> username;
std::cout << "Input Your Password:\n";
std::cin >> password;
std::cout << "Repeat Your Password:\n";
std::cin >> re_password;
if (password.compare(re_password) == 0) {
std::cout << "Rigister Success!\n";
int newid = f.addUser(fileName, username + "#" + password);
this->id = newid;
this->passWord = password;
this->userName = username;
return newid;
}
return 0;
}
void User::modifyInfo(std::string fileName) {
if (this->id == 0) {
std::cout << "用户未登录!\n";
}
else {
std::string username;
std::string password;
std::string re_password;
std::string in;
std::cout << "要修改用户名吗[y/n]\n";
std::cin >> in;
if (in.compare("y") == 0 || in.compare("Y") == 0) {
std::cout << "输入新用户名:";
std::cin >> username;
}
else if (in.compare("n") == 0 || in.compare("N") == 0) {
username = this->userName;
}
else {
std::cout << "输入违规,退出!" << std::endl;
return;
}
std::cout << "要修改密码吗[y/n]\n";
std::cin >> in;
if (in.compare("y") == 0 || in.compare("Y") == 0) {
std::cout << "输入新密码:";
std::cin >> password;
}
else if (in.compare("n") == 0 || in.compare("N") == 0) {
password = this->passWord;
}
else {
std::cout << "输入违规,退出!" << std::endl;
return;
}
std::cout << "确认输入密码";
std::cin >> re_password;
if (password.compare(re_password) == 0) {
f.modifyContentByUserid(fileName, this->id, username + "#" + password);
this->passWord = password;
this->userName = username;
std::cout << "修改成功!\n";
}
}
}