一个简易的ATM机存取款系统

模拟银行的自动取款机的使用,实现查询余额、取款、存款、转账、退出系统等功能。不少于10名用户的信息,假设每个用户仅一个账户。

基本要求:

1、基于显示器、键盘完成ATM机中基本人机交互。

2、设计应用程序所需要的类。

3、将所有交易相关的操作设计成基类,从该基类派生出查询余额、取款、存款、转账等子类。

4、完成该应用程序的所有功能。

 

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>             //提供大量基于迭代器的非成员模板函数
using namespace std;

class Account //用户
{
private:
	string name;//姓名
	long long id;//账号
	string password;//密码
	double money;//余额
public:
	Account(string name, long long id, string password, double money) : id(id), name(name), password(password), money(money) {}
	void save(double num) { money += num; }//存款函数
	void withdraw(double num) { money -= num; }//取款函数

	//以下基本函数用于取出类的私有成员
	string get_name() { return name; }
	long long get_id() { return id; }
	string get_password() { return password; }
	double get_money() { return money; }
};

class ATM
{
public:
	vector<Account> account;//账户表,是这个程序的数据库
	int id = -1;//当前是第几个账户,-1表示未登录成功

	double find_money() { return account[id].get_money(); }//查询余额
	bool get_money(double num)//返回是否取款成功
	{
		if (account[id].get_money() >= num)
			return account[id].withdraw(num), 1;   //进行取款,并返回值1(真)
		return 0;
	}
	void save_money(double num) { account[id].save(num); };//存款函数
	bool transfer_money(double num, long long idd)//向idd账户转账num元,返回是否转账成功
	{
		for (int i = 0; i < account.size(); i++)
			if (idd == account[i].get_id())
			{
				if (account[i].get_money() < num)
					return 0;                  //转账金额大于余额,转账失败
				account[i].save(num);
				account[id].withdraw(num);
				return 1;
			}
		return 0;
	}
};

ATM atm;

bool regist()//登陆界面,返回是否登陆成功
{
	cout << "\n\n-+-+-+-+-+-+-+-+-+-+登陆界面-+-+-+-+-+-+-+-+-+-+\n";
	cout << "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n";
	cout << " 请输入账号与密码,以空格隔开";
	long long acc; 
	char na[100];

	scanf("%lld%s", &acc, na);
	string m(na);

	for (int i = 0; i < atm.account.size(); i++)
		if (atm.account[i].get_id() == acc && atm.account[i].get_password() == m)//如果账户和密码都正确
		{
			atm.id = i;
			return 1;
		}
	return 0;
}

int main_menu()//个人主页
{
	printf("-+-+-+-+-+-+-+-+-+欢迎您,%s-+-+-+-+-+-+-+-+-+\n", atm.account[atm.id].get_name().c_str());
	cout << "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n";
	cout << "\t0.退卡\n";
	cout << "\t1.存款\n";
	cout << "\t2.取款\n";
	cout << "\t3.查询账户信息\n";
	cout << "\t4.转账\n";
	cout << "-+-+-+-+-+-+-+-+-+请输入相应号码进行操作:" << endl;
	int opt;
	scanf("%d", &opt);
	return opt;
}

void read_account()//从数据库中读取账户信息
{
	atm.account.push_back(Account("张三", 111111, "abc", 10000));
	atm.account.push_back(Account("李四", 222222, "abc", 10000));
	atm.account.push_back(Account("马云", 333333, "abc", 10000));
	atm.account.push_back(Account("王二麻子", 444444, "abc", 10000));
	atm.account.push_back(Account("五", 555555, "abc", 10000));
	atm.account.push_back(Account("六", 666666, "abc", 10000));
	atm.account.push_back(Account("七", 777777, "abc", 10000));
	atm.account.push_back(Account("八", 888888, "abc", 10000));
	atm.account.push_back(Account("九", 999999, "abc", 10000));
	atm.account.push_back(Account("十", 000000, "abc", 10000));
}

int main()
{
	read_account();//导入数据库

	while (1)
	{
		if (regist())
		{
			while (1)
			{
				int opt = main_menu();
				if (opt == 0)
				{
					cout << "退卡成功,请取走您的卡!";
					break;
				}
				else if (opt == 1)
				{
					cout << "输入金额:  ";
					double num;
					scanf("%lf", &num);
					atm.save_money(num);
					cout << "存款  " << num << "  元成功!" << endl;
				}
				else if (opt == 2)
				{
					cout << "输入金额:  ";
					double num;
					scanf("%lf", &num);
					if (atm.get_money(num)) cout << "取款  " << num << "  元成功!" << endl;
					else cout << "余额不足,取款失败!"<<endl;
				}
				else if (opt == 3)//查询余额
				{
					cout << "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n";
					cout << "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n";
					cout << "姓名:      " << atm.account[atm.id].get_name().c_str() << endl;
					cout << "账户:      " << atm.account[atm.id].get_id() << endl;
					printf("余额:      %.2lf\n", atm.account[atm.id].get_money());
					cout << "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n";
					cout << "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n";
				}
				else if (opt == 4)//转账
				{
					cout << "输入要转入的账户及金额:   ";

					long long id; double num;
					cin >> id >> num;

					if (atm.transfer_money(num, id)) printf("向 %lld 转账 %.2lf 元成功!\n", id, num);
					else cout << "账户错误或余额不足,转账失败!\n";
				}
				else
					cout << "请输入正确的操作序号!" << endl;
			}
		}
		else
			cout << "账号或密码错误,登陆失败!请重新登陆!\n";
	}
}

 不得不承认自己是初学者,菜鸡一个,所以代码难免有不完善的地方,欢迎各位大佬指点。第一次写博客,希望能在CSDN上学习到更多专业知识。建议在VS上调试,我在上面运行挺好的。

用户信息已在ATM中存贮,其中字符串内容为密码。(见上代码) 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值