bank.cpp

来自「是银行atm机的基本功能实现」· C++ 代码 · 共 101 行

CPP
101
字号
// Bank.cpp: implementation of the Bank class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Bank.h"
#include "AccountStandard.h"
#include "AccountVIP.h"
#include "ErrorCode.h"
#include "Error.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Bank::Bank()
{
	m_iAccountNum = 53100001;	//随便取的一个大数,作为帐号的开头
}

Bank::~Bank()
{
	int iCount=m_AccountList.GetCount();
	for(int i=0;i<iCount;i++){
		Account *pAccount = (Account *)m_AccountList.RemoveHead();
		delete pAccount;
	}
}

int Bank::NewAccount(int Type, int *pID)
{
	Account *pAccount=NULL;
	switch(Type){
	case 1:
			pAccount = new AccountStandard();
			break;
	case 2:
			pAccount = new AccountVIP();
			break;
	default:
			Error::SetLastError("Error account type.");
			return PARAMERROR;					//参数错误
	}
	pAccount->SetID(m_iAccountNum++);			//生成ID
	*pID=pAccount->GetID();
	m_AccountList.AddAccount(pAccount);			//加入列表当中
	return NOERROR;
}

int Bank::GetBalance(int id, double *Balance)
{
	Account *pAccount = m_AccountList.GetAccountByID(id);
	if(pAccount==NULL){
		Error::SetLastError("No such account id.");
		return NOSUCHACCOUNT;					//返回错误代码:没有该用户
	}
	*Balance=pAccount->GetBalance();
	return NOERROR;
}

int Bank::Save(int id, double Amount)
{
	Account *pAccount = m_AccountList.GetAccountByID(id);
	if(pAccount==NULL){
		Error::SetLastError("No such account id.");
		return NOSUCHACCOUNT;					//返回错误代码:没有该用户
	}
	return pAccount->Save(Amount);
}

int Bank::Withdraw(int id, double Amount)
{
	Account *pAccount = m_AccountList.GetAccountByID(id);
	if(pAccount==NULL){
		Error::SetLastError("No such account id.");
		return NOSUCHACCOUNT;					//返回错误代码:没有该用户
	}
	return pAccount->Withdraw(Amount);	
}

int Bank::Transfer(int idfrom, int idto, double Amount)
{
	int iRet;
	Account *pAccountFrom = m_AccountList.GetAccountByID(idfrom);
	if(pAccountFrom==NULL){
		Error::SetLastError("No such account id.");
		return NOSUCHACCOUNT;					//返回错误代码:没有该用户
	}
	iRet = pAccountFrom->Withdraw(Amount);	
	if(iRet!=0){
		return iRet;							//返回在取钱过程中的错误
	}
	Account *pAccountTo = m_AccountList.GetAccountByID(idto);
	if(pAccountTo==NULL){
		Error::SetLastError("No such account id.");
		return NOSUCHACCOUNT;					//返回错误代码:没有该用户
	}
	iRet = pAccountTo->Save(Amount);	
	return iRet;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?