C++实现ATM

为了方面用户的及时取款,查询,及时修改密码的方便,本课题设计并模拟银行的自动取款机使用过程中的界面和用户交互过程。实现查询银行卡余额、取款、修改密码、退出系统等功能。

  1. 卡号、密码输入最多三次,否则直接退出系统。
  2. 取款功能:取款金额受卡余额、机单笔最大取款金额及机当前剩余金额的限制。
  3. 查询功能:实现查询余额功能。
  4. 更改密码:实现密码更改功能,对于密码要有2次验证。
  5. 锁卡功能:实现卡被锁的功能。
  6. 退卡功能:实现退出系统功能。
class User
{
public:
	firend class ATM;
	User(char * id, char * passwd, float money);
	User();
	~User();
	char * getId();//活得卡号
	char * get_passwd();//获取密码
	void set_passwd();//设置密码
	float get_money();//获取余额
private:
	char * id;
	char * passwd;
	float money;	
}

用户类包括:获取卡号,密码,余额,设置密码等函数。为获取用户信息而做的功能,通过以上函数,经过主函数建立对象,传入实参后可获取对象(用户)信息。

class ATM
{
public:
	ATM(User & u) : u(u) {}
	void login();//登陆界面
	bool checkUser(char id[], char passwd[]);//核对密码
	void getMoney();//取款
	void deposit();
	void changePasswd();//更改密码
	void functionInterface();//功能界面
	void exit();//退出
private:
	int times;
	User & u;	
}

ATM类中包括:登陆界面,核对密码,取款,查询,更改密码,功能界面,锁卡,退出等函数。通过函数实现以上等功能。

我在这里要声明一下,用户的用户名、密码、卡上的金钱,这些都是以文件的形式存储在txt文本文档中。这样可以记录余额,这些函数的具体实现如下:

#define LEN 32
//User类
User::User() {}
User::User(char * id, char * passwd, float money)
{
	this->id = new char[LEN];
	memset(this->id, 0, sizeof(char) * LEN);
	strcpy_s(this->id, LEN - 1, id);
	this->passwd = new char[LEN];
	memset(this->passwd, 0, sizeof(char) * LEN);
	strcpy_s(this->passwd, LEN - 1, passwd);
	this->money = money;
}
User::~User()
{
	delete[] id;
}
char * User::getId()
{
	return this->id;
}
char * User::get_passwd()
{
	return this->passwd;
}
float User::get_money()
{
	return this->money;
}
void User::set_passwd()
{
	char newpasswd[LEN];
	char oldpasswd[LEN];
	int i = 0;
	while(true)
	{
		cout << "Please input your old passwd : ";
		cin >> oldpasswd;
		if(0 == strcmp(oldpasswd,  get_passwd() && i < 3)
		{
			cout << "Please input your new passwd: ";
			cin >> newpasswd;
			strcpy_s(get_passwd(), LEN - 1, newpasswd);
			fstream clean("userinfo.txt" , ios::out);//先清空文件里面的内容
			ofstream out("userinfo.txt, ios::app);//以追加的方式打开文件
			//将信息写入文件
			out << getId();
			out << " ";//用空格来隔开,不然读文件都不全
			out << get_passwd();
			out << " ";
			out << get_money();
			break;
		}
		else if(i = 3)
		{
			cout << "please try again tomorrow" << endl;
			break;
		}
		i++;
	}
}

ATM:

void ATM::login()
{
	cout << "Welcome to the ATM! " << endl;
	cout << "Please input your ID and PASSWD "" << endl;
}
bool ATM::checkUser(char id[], char passwd[])
{
	while(true)
	{
		if(0 == strcmp(id, u.id) && 0 == strcmp(passwd, u.passwd))
		{
			cout << "land successfully" << endl;
			return true;
		}
		else
		{
			cout << "Incorrect id or passwd" << endl;
			return false;
		}
	}
}
void ATM::getMoney()
{
	int order = 0;
	while(true)
	{
		cout << "**************************************" << endl;
		cout << "*******Please Input Your Oeder********" << endl;
		cout << "*1.100                          2.200*" << endl;
		cout << ”*3.500                         4.1000*" << endl;
		cout << "*5.Other Amount                      *" << endl;
		cout << "*6.Balance Inquiry                   *" << endl;
		cout << "*7.LOG OUT                           *" << endl;
		cout << "**************************************" << endl;
		cin >> order;
		switch(order)
		{
		case 1:
			if(u.money < 100)
			{
				cout << " not sufficient funds " << endl;
			}
			else
			{
				u.money = u.money - 100;
				cout << "Please withdraw your money at the exit" << endl;
				//写文件,记录取钱后的数值
				fstream clean("userinfo.txt, ios::out);
				ofstream out("userinfo.txt", ios::app);
				out << u.id;
				out << " ";
				out << u.passwd;
				out << " ";
				out << u.money;
			}
			break;
		case 2:
			if(u.money < 200)
			{
				cout << " not sufficient funds " << endl;
			}
			else
			{
				u.money = u.money - 200;
				cout << "Please withdraw your money at the exit" << endl;
				fstream clean("userinfo.txt, ios::out);
    				ofstream out("userinfo.txt", ios::app);
    				out << u.id;
    				out << " ";
    				out << u.passwd;
   				 out << " ";
    				out << u.money;
  			 }
  			 break;
  		  case 3:
  			 if(u.money < 500)
  			 {		
    				cout << " not sufficient funds " << endl;
   			}
  			 else
   			{
    				u.money = u.money - 500;
   				cout << "Please withdraw your money at the exit" << endl;
   				fstream clean("userinfo.txt, ios::out);
        			ofstream out("userinfo.txt", ios::app);
        			out << u.id;
        			out << " ";
        			out << u.passwd;
        			out << " ";
        			out << u.money;
     			 }
      			break;
    		case 4:
      			if(u.money < 1000)
      			{  
       				cout << " not sufficient funds " << endl;
      			}
      			else
      			{
        			u.money = u.money - 1000;
       				cout << "Please withdraw your money at the exit" << endl;
     			 	fstream clean("userinfo.txt, ios::out);
           			ofstream out("userinfo.txt", ios::app);
           			out << u.id;
           			out << " ";
           			out << u.passwd;
           			out << " ";
           			out << u.money;
         		}
         		break;
      		case 5:
      			float money;
      			cout << "Please enter your withdrawal amount:";
      			cin >> money;
      			if(u.money < money)
      			{
      				cout << "Please withdra your money at the exit" << endl;
      				fstream clean("userinfo.txt", ios::out);
      				ofstream out("userinfo.txt", ios::app);
      				out << u.id;
      				out << " ";
      				out << u.passwd;
      				out << " ";
      				out << u.money;
      			}
      			break;
      		case 6:
      			cout << "The balance in your card is:" << u.money << endl;
      			break;
      		case 7:
      			ATM::exit();
    		}
    		if(order == 7)
    		{
    			break;
    		}
	}
}
void ATM::changePasswd()
{
	u.set_passwd();
}
void ATM::functionInterface()
{
	int order;
	while(true)
	{
		cout << "1.withdrawal"<< endl;
		cout << "2.deposit" << endl;
		cout << "3.inquiry" << endl;
		cout << "4.change PASSWD" << endl;
		cout << "5.LOG OUT" << endl;
		cin >> order;
		switch(order)
		{
		case 1:
			ATM::getMoney();
			break;
		case 2:
			ATM::deposit();
			break;
		case 3:
			cout << " The balance in your card is:" << u.money << endl;
			break;
		case 4:
			ATM::changePasswd();
		case 5:
			ATM::exit();
		}
		if(5 == order)
		{
			break;
		}
	}
}
void ATM::deposit()
{
	cout << "Please deposit cash" << endl;
	float money;
	cin >> money;
	u.money = u.money + money;
	cout << "success" << endl;
	cout << "Your total cash is:" << u.money << endl;
	fstream clean("userinfo.txt", ios::out);
        ofstream out("userinfo.txt", ios::app);
        out << u.id;
        out << " ";
        out << u.passwd;
        out << " ";
        out << u.money;
}
void ATM::exit()
{
	cout << "exit !" << endl;
	cout << "have a nice day!" <<endl;
	return;
}

main.cpp中代码如下:

#includ"ATM.h"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
	char id[LEN];
	char passwd[LEN];
	float money;
	int i = 0;
	ifstream in("userinfo.txt");
	in >> id;
	in >> passwd:
	in >> money;
	User u(id, passwd, money);
	ATM atm(u);
	atm.login;
	while(i < 3)
	{
		cout << "ID:";
		cin >> id;
		cout "PASSWD:";
		cin >> passwd;
		if(true == atm.checkUser(id, passwd))
		{
			atm.functionInterface();
			break;
		}
		else
		{
			cout << "please try again" << endl;
		}
		if(2 == i)
		{
			cout << " Card has been locked " << endl;
			cout << ”please contact us" << endl;
		}
		i++;
	}
	return 0;
}

关于ATM所有的东西这里就完了,
关于锁卡这一块,我做的有些瑕疵,这里我说一下我的思路,新建一个.txt文件,专门存放被锁的卡的信息,在用户输入时,如果输入超过三次,则将用户的信息写入这个文件中,并提示用户,卡已经被锁了。另外,每次用户输入的时候都要和这个文件中的信息进行匹配,如果输入的用户名和这个文件的信息一样,则禁止登陆。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值