C++代理模式实现验证登录

C++代理模式实现验证登录

#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <sstream>
#include <iomanip>
#include <algorithm>

using namespace std;

class Stream //系统类(建立类让代理类和类产生关联)
{
public:
	virtual void Run() = 0;
};

class AgencyStream :public Stream//真正要访问的类
{
public:
	void Run() {
		cout << "系统启动" << endl;
	}
};

class Verification :public Stream
{
public:
	Verification(string UserName, string Password) :UserName(UserName), Password(Password) {
		this->stream = new AgencyStream;
	}


	void Run() {
		if (Login()==true)
		{
			this->stream->Run();
		}
		else
		{
			//cout << "账号或者密码错误,系统启动失败" << endl;
		}
	}

private:
	bool Login() {
		ifstream ifs;
		ifs.open("Login.txt", ios::in);
		char title[256];
		ifs.getline(title, 256);

		//cout << title << endl;
		char str[64];
		string username;
		string password;
		map<string, string>user;

		while (ifs.getline(str, 64))
		{
			istringstream ist(str);
			ist >> username >> password;
			ist.clear();
			user.insert(make_pair(username, password));
		}

		map<string, string>::iterator it = user.find(UserName);
		if (it!= user.end())
		{
			if ((*it).second== Password)
			{
				return true;
			}
			else
			{
				cout << "密码错误" << endl;
			}			
		}
		else
		{
			cout << "账号不存在" << endl;
			return false;
		}
	}
	~Verification() {
		if (stream)
		{
			delete stream;
		}
	}

private:
	AgencyStream* stream;
	string UserName;
	string Password;
};
void test() {
	string UserName;
	string Password;
	cout << "请输入账号" << endl;
	cin >> UserName;
	cout << "请输入密码" << endl;
	cin >> Password;
	Verification*my = new Verification(UserName, Password);
	my->Run();
}

void main() {
	test();
}

效果图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值