#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <sstream>
#include <algorithm>
#include "User.h"
using namespace std;
map<int,User> user_map;
map<int,User> admin_map;
void user_signin();
void load_admin()
{
ifstream fp("./admin.txt");
if (!fp.is_open()) {
return;
}
string line;
while (getline(fp, line)) {
stringstream ss;
ss.str(line);
User user;
int id;
string password;
ss >> id >> password;
user.set_id(id);
user.set_password(password);
admin_map.insert(map<int, User>::value_type(user.getid(),user));
}
fp.close();
}
void load_user()
{
ifstream fp("./user.txt");
if (!fp.is_open()) {
return;
}
string line;
while (getline(fp, line)) {
stringstream ss;
ss.str(line);
User user;
int id;
string password;
ss >> id >> password;
user.set_id(id);
user.set_password(password);
user_map.insert(map<int, User>::value_type(user.getid(),user));
}
fp.close();
}
void save_admin()
{
ofstream fp("./admin.txt");
if (!fp.is_open()) {
return;
}
for (map<int, User>::iterator it = admin_map.begin(); it != admin_map.end(); ++it) {
fp << it->second.getid() << " " << it->second.getpassword() << endl;
}
fp.close();
}
void save_user()
{
ofstream fp("./user.txt");
if (!fp.is_open()) {
return;
}
for (map<int, User>::iterator it = user_map.begin(); it != user_map.end(); ++it) {
fp << it->second.getid() << " " << it->second.getpassword() << endl;
}
fp.close();
}
void admin_login() {
cout<<"请输入账号:"<<endl;
int i;
cin>>i;
cout<<"请输入密码:"<<endl;
string password;
cin>>password;
map<int, User>::iterator it = admin_map.find(i);
if (it == admin_map.end()) {
cout << "管理员账号不存在" << endl;
return;
}
if (it->second.getpassword() != password) {
cout << "管理员密码不正确" << endl;
return;
}
cout << "管理员登录成功" << endl;
}
void user_login() {
cout<<"请输入账号:"<<endl;
int i;
cin>>i;
cout<<"请输入密码:"<<endl;
string password;
cin>>password;
map<int, User>::iterator it = user_map.find(i);
if (it == user_map.end()) {
cout << "用户账号不存在" << endl;
return;
}
if (it->second.getpassword() != password) {
cout << "用户密码不正确" << endl;
return;
}
cout << "用户登录成功" << endl;
}
void user_signin(){
cout<<"请输入账号:"<<endl;
User user;
int i;
cin>>i;
if (user_map.find(i) != user_map.end()) {
cout << "用户已存在" << endl;
return;
}
user.set_id(i);
cout<<"请输入密码:"<<endl;
string j;
cin>>j;
user.set_password(j);
user_map.insert(map<int, User>::value_type(user.getid(),user));
}
int main() {
load_admin();
load_user();
bool flag = true;
while (flag) {
cout<<"欢迎使用本系统!"<<endl;
cout<<"-----------------------------"<<endl;
cout<<"1.管理员登陆"<<endl;
cout<<"2.用户登陆"<<endl;
cout<<"3.用户注册"<<endl;
cout<<"4.退出"<<endl;
cout<<"-----------------------------"<<endl;
cout<<"请输入你的选择:"<<endl;
int choice;
cin>>choice;
switch (choice) {
case 1:
admin_login();
break;
case 2:
user_login();
break;
case 3:
user_signin();
break;
case 4:
flag = false;
break;
}
}
save_admin();
save_user();
return 0;
}
User.h
#ifndef MAIN_CPP_USER_H
#define MAIN_CPP_USER_H
#include <string>
using namespace std;
class User {
private:
int id;
string password;
public:
User(){}
User(int i,string p):id{i},password(p){
};
~User(){}
void set_id(int id){
this->id=id;
};
int getid(){
return id;
}
void set_password(string password){
this->password=password;
}
string getpassword(){
return password;
}
};
#endif