c++实现ATM机!非友元!

本实验中共包含两个类:Uer类和ATM类。两个类的类图如下所示:
在这里插入图片描述
Uer类实现管理账户相关信息,方法中包含各个私有成员的get和set方法。
在这里插入图片描述
ATM类实现基本操作:注册,登陆,存钱,取钱,修改余额,修改密码。
注:通过使用get和set函数实现对Uer类中私有成员的访问和修改,避免使用友元,保护封装性

界面

界面设置为两个界面:登陆界面和操作界面。当登陆成功后才能进入操作页面。
登陆界面
在这里插入图片描述
操作界面
在这里插入图片描述

源代码

//ATM机实验
//两个大类,一个类用来管理账号和密码,另一个类实现账号资金的管理

#include<iostream>
#include<string>

using namespace std;
//用户类:用于储存用户名,密码,钱数

class Uer
{
public:
	Uer();
	string get_ID();
	string get_password();
	double get_money();
	void set_ID(string id);
	void set_password(string password);
	void set_money(double money);
private:
	string ID;
	string password;
	double money;
};

Uer::Uer()
{
	money = 0;
}

string Uer::get_ID()
{
	return ID;
}

string Uer::get_password()
{
	return password;
}

double Uer::get_money()
{
	return money;
}

void Uer::set_ID(string id)
{
	this->ID=id;
}

void Uer::set_password(string password)
{
	this->password=password;
}

void Uer::set_money(double money)
{
	this->money=money;
}

//ATM类实现相关的操作:存钱,取钱,查询余额!
//注意问题:存钱,取钱,查询余额要在登陆的基础上进行!
class ATM
{
public:
	ATM();
	int get_i();
	void interface();//登陆界面
	void registe();//注册
	bool land();//登陆
	void deposit();//存钱
	void withdraw();//取钱
	void balance();//查询余额
	void encode();//修改密码
private:
	Uer u;
	int i;//用于判断是否登录成功!
};



ATM::ATM()
{

}

int ATM::get_i()
{
	return i;
}



void ATM::registe()//注册
{
	cout << "请设置您的ID:" << endl;
	string id,password;
	cin >> id;
	u.set_ID(id);
	cout << "请设置您的密码:" << endl;
	cin >> password;
	u.set_password(password);
	cout << "恭喜你拥有自己的小金库了!" << endl;
}

bool ATM::land()//登陆
{
	int m=0;//记录输入密码的次数。
	cout << "请输入您的账户名:" << endl;
	string n;
	cin >> n;
	if (n != u.get_ID())//核对账户名。
		cout << "您输入的用户名不存在!" << endl;
	else
	{
		cout << "请输入您的密码:" << endl;
		for (; m < 6&&i!=1; m++)//6次输入密码错误,卡被锁定。
		{
			string p;
			cin >> p;
			if (p != u.get_password())//核对密码。
				cout << "密码错误!请从新输入密码:" << endl;
			else
			{
				cout << "恭喜小可爱:登陆成功!" << endl;
				return i = 1;
			}
		}
		if(m>=6)
			cout << "您的卡已被锁定!" << endl;
	}
	return i=0;
}

void ATM::deposit()//存款
{
	cout << "请输入您存款的数目:" << endl;
	int  menoy;
	cin >> menoy;
	if ((menoy % 100) == 0)//某些运算符规定只能使用整数
	{
		u.set_money(menoy);
		cout << "小可爱的钱包又变的鼓鼓的!超级开心" << endl;
	}
	else
		cout << "请存入100的整数倍现金!" << endl;

}

void ATM::withdraw()//取款
{
	cout << "请输入您的取款数目:" << endl;
	int menoy;
	cin >> menoy;
	if ((menoy % 100) == 0)
	{
		if (menoy > u.get_money())
			cout << "小金库的余额不足!" << endl;
		else
		{
			int m = u.get_money() - menoy;
			u.set_money(m);
			cout << "恭喜小可爱:喜提现金!" << endl;
		}
	}
	else
		cout << "请输入100的整数倍的钱数!" << endl;
	
}

void ATM::balance()//查询余额
{
	cout << "小可爱现在拥有的资产是:" << u.get_money() << endl;
}

void ATM::encode()//修改密码
{
	cout << "请输入您的旧密码:" << endl;
	string password,newpassword1, newpassword2;
	cin >> password;
	if (password == u.get_password())
	{
		cout << "请输入您的新密码:" << endl;
		cin >> newpassword1;
		for (; newpassword1 != newpassword2;)
		{
			cout << "请再次输入您的新密码:" << endl;
			cin >> newpassword2;
			if (newpassword1 == newpassword2)
			{
				u.set_password(newpassword2);
				cout << "密码设置成功!" << endl;
			}
			else
				cout << "两次密码不匹配!请重新输入" << endl;
		}

	}
}
void main()
{
	ATM a;
	while (1)
	{
		cout << "***********欢迎来到欢乐银行**********" << endl;
		cout << "********请选择您要进行的操作!*******" << endl;
		cout << "              1.  注册               " << endl;
		cout << "              2.  登陆               " << endl;
		int i,j=0;
		cin >> i;
		switch (i)
		{
		case 1:a.registe();       break;
		case 2:
		{
			a.land(); 
			if (a.get_i() == 0) { break; }
			else {
				while (j != 5)
				{
					cout << "*********我们将为您提供下列服务**********" << endl;
					cout << "            1.   存钱                    " << endl;
					cout << "            2.   取钱                    " << endl;
					cout << "          3.   查询余额                  " << endl;
					cout << "          4.   修改密码                  " << endl;
					cout << "          5.   退出登陆                  " << endl;
					cout << "**********请选择您要进行的操作!*********" << endl;
					cin >> j;
					switch (j)
					{
					case 1:a.deposit();      break;
					case 2:a.withdraw();     break;
					case 3:a.balance();      break;
					case 4:a.encode();       break;
					}
				}
			}
		}
		}
	}
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值