为了方便我将账号,密码和存款金额全声明成全局的,这样写函数方便。
#include <iostream>
using namespace std;
int LogIn(string strAcc,string strPsw); //登陆
int LogOut(int& iInfo); //登出
double ShowBalance(string strAcc); //余额查询
void DrawMoney(); //取款
int DrawMoney_1(string strAcc,double dbAmount);
int SaveMoney(string strAcc,double dbAmout); //存款
/*转账
//返回0表示余额不足
//1表示成功
//2表示同账号不可转账
//3表示转入账号不存在*/
int CarryOver(string strAcc_1,string strAcc_2,double dbAm);
void QuitSys(); //退出
/*修改密码函数
strPSW:为原密码
返回值:修改密码是否成功
*/
bool AmendPSW(string& strPSW);
/*计算利息的函数
dbAm为本金,dbRa为利率,iTe为存期
函数返回为利息*/
double CountAccrual(double dbAm,double dbRa,int iTe);
const string strAccount_1 = "ac1"; //用户1的账号
const string strAccount_2 = "ac2"; //用户2的账号
string strPSW_1 = "123456"; //用户1在银行里的密码
string strPSW_2 = "123123"; //用户2在银行里的密码
double dbBalance_1 = 5000; //账号1的余额
double dbBalance_2 = 8000; //账号2的余额
int main()
{
string strAccount; //记录用户输入的账号
string strPassword; //记录用户输入的密码
char cInput; //记录用户输入的选项
int iLogInInfo = 1; //用于记录用户的登陆状态
//0表示账号密码正确登陆成功
//1表示账号不存在或者当前没有账号登陆
//2表示账号存在,密码不正确
cout<<"中国银行欢迎您"<<endl;
while(1)
{
//这是个大循环,包括登陆和业务内容
int iReTry = 0; //密码输错的重试次数
while( iLogInInfo != 0 )
{
cout<<"账号:";
cin>>strAccount; //输入账号
if( strAccount == "q" || strAccount == "Q" )
{
QuitSys();
return 0;
}
cout<<"密码:";
cin>>strPassword; //在这用户输入密码
if( strPassword == "q" || strPassword == "Q" )
{
QuitSys();
return 0;
}
iReTry = 0; //重新输入账号和密码时将重试次数归0
do
{
iLogInInfo = LogIn(strAccount,strPassword);
switch( iLogInInfo )
{
case 1:
cout<<"账号不存在!\n";
break;
case 2:
cout<<"密码不正确!\n";
iReTry++;
cout<<"可重试次数:"<<4-iReTry<<endl;
if( (3 - iReTry ) == 0)
{
iLogInInfo = 1;
cout<<"请重确认账号和密码!\n";
break;
}
cout<<"密码:";
cin>>strPassword;
if( strPassword == "q" || strPassword == "Q" )
{
QuitSys();
return 0;
}
break;
case 0:
break;
}
}
while( iLogInInfo == 2 );
}
while( iLogInInfo == 0 )
{
//这是业务内容的循环
cout<<"1.查询\n"
<<"2.取款\n"
<<"3.存款\n"
<<"4.转账\n"
<<"5.修改密码\n"
<<"0.登出\n";
cin>>cInput; //在这用户输入选项
double dbAm; //输入的取款、存款、转账金额
string strIntoAcc; //输入的转入账号
switch(cInput)
{
case '1':
cout<<ShowBalance(strAccount)<<endl;
break;
case '2':
cout<<"请输入金额:";
cin>>dbAm;
if( DrawMoney_1(strAccount,dbAm) )
cout<<"取款成功。\n";
else
cout<<"余额不足。\n";
break;
case '3':
cout<<"请输入金额:";
cin>>dbAm;
if( SaveMoney(strAccount,dbAm) )
cout<<"存款成功。\n";
break;
case '4':
int iCOInfo; //转账状态
cout<<"请输入转入账号:";
cin>>strIntoAcc;
cout<<"请输入金额:";
cin>>dbAm;
iCOInfo = CarryOver(strAccount,strIntoAcc,dbAm);
if( iCOInfo == 1 )
cout<<"转账成功。\n";
else if( iCOInfo == 0 )
cout<<"余额不足。\n";
else if( iCOInfo == 2 )
cout<<"同账号间不可转账。\n";
else if( iCOInfo == 3 )
cout<<"转入账号不存在。\n";
break;
case '5':
if( strAccount == strAccount_1 )
AmendPSW( strPSW_1 );
else if( strAccount == strAccount_2 )
AmendPSW( strPSW_2 );
break;
case '0':
if( LogOut( iLogInInfo ) == 1 )
cout<<"登出成功!\n";
break;
default:
cout<<"输入错误,请不要着急!\n";
}
}
}
return 0;
}
int DrawMoney_1(string strAcc,double dbAmount)
{
if( strAcc == strAccount_1 )
{
if( dbAmount > dbBalance_1 )
return 0 ;
else
dbBalance_1 = dbBalance_1 - dbAmount;
}
else if( strAcc == strAccount_2 )
{
if( dbAmount > dbBalance_2 )
return 0;
else
dbBalance_2 = dbBalance_2 - dbAmount;
}
return 1;
}
void QuitSys()
{
cout<<"谢谢,欢迎下次再来!\n";
}
int LogIn(string strAcc,string strPsw)
{
int iError=0; //登陆失败的错误号,0为成功
//先判断账号是否存在
if( (strAcc != strAccount_1) && (strAcc != strAccount_2) )
iError = 1;//账号不存在
else if( strAcc == strAccount_1 )//对账号1进行密码核对
{
if( strPsw != strPSW_1 )
iError = 2;//密码错误!
else
iError = 0;
}
else if( strAcc == strAccount_2 )//对账号2进行密码核对
{
if( strPsw != strPSW_2 )
iError = 2;
else
iError = 0;
}
return iError;
}
int LogOut(int& iInfo)
{
iInfo = 1;
return iInfo;
}
double ShowBalance(string strAcc)
{
if( strAcc == strAccount_1 )
return dbBalance_1;
else if( strAcc == strAccount_2 )
return dbBalance_2;
}
void DrawMoney()
{
double dbAmount=0; //取款额
double dbAccrual=0; //利息
double dbRate=0; //利率
int iTerm=0; //存期
char cChoose; //选择的存期
cout<<"请输入金额:";
cin>>dbAmount;
cout<<"1.活期\n"
<<"2.3个月\n"
<<"3.6个月\n"
<<"4.一年\n"
<<"5.二年\n"
<<"6.三年\n"
<<"7.五年\n";
cout<<"选择存期:";
cin>>cChoose;
//这里的利率没做转化,到函数里再做转化
if(cChoose=='1')
{
dbRate=0.5;
cout<<"嗯,确实还没到死期";
}
else if(cChoose=='2')
{
dbRate=3.1;
iTerm=3*30;
}
else if(cChoose=='3')
{
dbRate=3.3;
iTerm=6*30;
}
else if(cChoose=='4')
{
dbRate=3.5;
iTerm=360;
}
else if(cChoose=='5')
{
dbRate=4.4;
iTerm=2*360;
}
else if(cChoose=='6')
{
dbRate=5;
iTerm=3*360;
}
else if(cChoose=='7')
{
dbRate=5.5;
iTerm=5*360;
}
else
{
cout<<"输入错误,拜拜!\n";
}
dbAccrual=CountAccrual(dbAmount,dbRate,iTerm);
cout<<"本金:"<<dbAmount<<"利息:"<<dbAccrual<<endl;
}
int SaveMoney(string strAcc,double dbAmout)
{
if( strAcc == strAccount_1 )
dbBalance_1 = dbBalance_1 + dbAmout;
else if( strAcc == strAccount_2 )
dbBalance_2 = dbBalance_2 + dbAmout;
return 1;
}
int CarryOver(string strAcc_1,string strAcc_2,double dbAm)
{
//strAcc_1为当前账号
//strAcc_2为转入账号
//dbAm金额
if( strAcc_1 == strAccount_1 )
{
if( dbAm > dbBalance_1 )
return 0;
else if( strAcc_2 == strAccount_2 )
{
dbBalance_1 = dbBalance_1 - dbAm;
dbBalance_2 = dbBalance_2 + dbAm;
}
else if( strAcc_2 == strAccount_1 )
return 2;
else if( strAcc_2 != strAccount_1 && strAcc_2 != strAccount_2 )
return 3;
}
else if( strAcc_1 == strAccount_2 )
{
if( dbAm > dbBalance_2 )
return 0;
else if( strAcc_2 == strAccount_1 )
{
dbBalance_2 = dbBalance_2 - dbAm;
dbBalance_1 = dbBalance_1 + dbAm;
}
else if( strAcc_2 == strAccount_2 )
return 2;
else if( strAcc_2 != strAccount_1 && strAcc_2 != strAccount_2 )
return 3;
}
return 1;
}
double CountAccrual(double dbAm,double dbRa,int iTe)
{
//这里将年利率转化为日利率
double dbAc;//利息
dbAc=dbAm*iTe*dbRa/100/360;
return dbAc;
}
bool AmendPSW(string& strPSW)
{
char cYesOrNo;
bool bIsTrue=false;
string strInputPSW_1;//第一次输入新密码
string strInputPSW_2;//第二次输入新密码
while(1)
{
cout<<"请输入新密码:";
cin>>strInputPSW_1;
cout<<"请再次输入新密码:";
cin>>strInputPSW_2;
if(strInputPSW_1==strInputPSW_2)
{
cout<<"恭喜!修改成功!\n";
bIsTrue = true;
strPSW = strInputPSW_1;
break;
}
else
{
cout<<"两次输入不一致,修改失败,是否再试一次?Y/N"<<endl;
cin>>cYesOrNo;
if( cYesOrNo=='N' || cYesOrNo=='n' )
{
bIsTrue = false;
break;
}
}
}
return bIsTrue;
}