您的位置:首页 > 其它

项目52-XML解析的银行管理系统

2016-06-26 14:07 309 查看
#include <iostream>
#include <string>
#include"tinyxml.h"
#include"tinystr.h"
using namespace std;

class Account
{
public:
Account(string a,string b,double bal);
virtual void saving(double a);//虚基类 存款
virtual void getOutMoney(double a);//取款
virtual void showAccountInfo();//显示账户信息

string getId();//获取账号
double getbalance(){return balance;}

public:
string Account_name;//账号属性
string name;//存款人姓名
double balance;//余额
int type1;
class Account *next;

};
/*---------------------------------------------------------------------------*/
class NormalAccount:public Account//普通账户
{
public:
NormalAccount(string a,string b,double bal):Account(a,b,bal){}
void getOutMoney(double a);
void showAccountInfo();

};

/*---------------------------------------------------------------------------*/
class VIPAccount:public Account//VIp储户
{
public:
VIPAccount(string a,string b,double bal,double d,double e):Account(a,b,bal),tzsx(d),tzze(e){type1=1;}
void getOutMoney(double a);
void showAccountInfo();
double gettzsx(){return tzsx;}
double gettzze(){return tzze;}

private:
double tzsx;//透支上限
double tzze;//透支总额
};
/*------------------------------------------------------------------------------------------------*/
Account::Account(string a,string b,double bal)//基类Accout的成员函数
{
Account_name = a;
name = b;
balance = bal;
type1=0;
}

void Account::saving(double a)
{
balance = balance + a;

}

void Account::getOutMoney(double a)
{
if(a > balance)
cout<<"余额不足"<<endl;
else
{
balance = balance - a;
cout << "已取出" << a << "元,余额还有" <<balance<<"元"<<endl;
}
}

void Account::showAccountInfo()
{
cout << "用户账号:" << Account_name << endl;
cout << "开户姓名:" << name <<endl;
cout << "账户余额:" << balance <<endl;
}

string Account::getId()
{
return Account_name;
}

/*--------------------------------------------------------------------------*/
void NormalAccount::getOutMoney(double a)//普通用户
{
if(a > balance)
cout<<"余额不足"<<endl;
else
{
balance = balance - a;
cout << "已取出" << a << "元,余额还有" <<balance<<"元"<<endl;
}
}
void NormalAccount:: showAccountInfo()
{
cout<<"用户账号:"<<Account_name<<endl;
cout<<"用户姓名:"<<name<<endl;
cout<<"账户余额:"<<balance<<endl;
}
/*--------------------------------------------------------------------------*/
void VIPAccount::getOutMoney(double a)//VIP用户
{
if(a >balance + tzsx -tzze)
cout<<"不可透支"<<endl;
else
balance = balance - a;

}

void VIPAccount::showAccountInfo()
{
cout << "用户账号为:" << Account_name << endl;
cout << "开户人姓名:" << name <<endl;
cout << "账户余额为:" << balance <<endl;
cout << "透支上限为:" << tzsx <<endl;
cout << "透支总额为:" << tzze <<endl;

}
/*-------------------------------------------------------------------------------*/
class Bank                             //银行类
{
public:
Bank();//默认构造函数
void append1();//生成普通储户
void append2();//生成VIP储户
void deleteUser();//删除储户
void query();//查询储户
bool CreateXmlFile();
bool ReadXmlFile();
Account *getMyAcc(string name);
addlist(Account *acc);
Account *accountList;
private:

int accNum;//用户人数
double Balance;

};
Bank::addlist(Account *acc)
{
Account *p;
if(accountList==NULL)
{
accountList =acc;
acc->next=NULL;
}
else
{
p = accountList;
while(p)
{
if(p->next==NULL)
{
p->next =acc;
acc->next =NULL;

}
p= p->next;

}

}

}
Bank::Bank()
{
accountList =NULL;
return ;
}

void Bank::append1()
{
string str1,str2;

cout << "请输入普通用户账号(格式为N001)" << endl;
cin>>str1;
cout << "请输入开户人姓名" << endl;
cin>>str2;
Account *acc = new NormalAccount(str1,str2,0);
cout<<"增加普通账户成功"<<endl;

addlist(acc);
CreateXmlFile();

}

void Bank::append2()
{
string str1,str2;

cout << "请输入VIP用户账号(格式为VIP1)" << endl;
cin>>str1;
cout << "请输入开户人姓名" << endl;
cin>>str2;
Account *acc = new VIPAccount(str1,str2,0,5000,0) ;
cout<<"增加高级账户成功"<<endl;

addlist(acc);
CreateXmlFile();

}

void Bank::deleteUser()
{
int a=0;
string num;
cout << "请输入要注销的用户账号" << endl;
cin>>num;

Account *p,*pre;
if (accountList==NULL)
{
cout<<"无此用户"<<endl;
return;
}
p =pre=accountList;
while(p)
{

if(p->getId()==num)
{
a=1;
if(p==accountList)
{
accountList = accountList->next;
delete(p);
p=NULL;
cout<<"删除账户成功"<<endl;
}
else
{
pre->next =p->next;
delete(p);
p=NULL;
cout<<"删除账户成功"<<endl;
}
}
else
{
pre =p;
p= p->next;
}
}
if(a==0)
cout<<"没有查到此用户"<<endl;
CreateXmlFile();

}

Account *Bank::getMyAcc(string name)
{
Account *p;
p = accountList;
while(p)
{
if(p->getId()==name)
{
return p;

}
p= p->next;

}
return NULL;
}

void Bank::query()
{
string n;
cout << "请输入您要查询的用户账号" << endl;
cin>>n;
int a=0;

Account *p;
for (p=accountList; p!=NULL; p=p->next)
{
if(p->getId()!=n)
{

continue;
}

else
{
cout<<"当前账号为:";
cout<<p-> getId();
p->showAccountInfo();
a=1;
break;
}

}
if(a==0)
cout<<"无此用户"<<endl;
}
/*---------------------------------------------------------------------------------*/

bool Bank::CreateXmlFile()
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
Account *p;

//创建一个XML的文档对象。
remove("account.xml");
TiXmlDocument *myDocument = new TiXmlDocument();
//创建一个根元素并连接。
TiXmlElement *RootElement = new TiXmlElement("Bank");
myDocument->LinkEndChild(RootElement);

for(p=accountList;p!=NULL;p=p->next)
{
//创建一个Person元素并连接。
TiXmlElement *PersonElement = new TiXmlElement("Account");
RootElement->LinkEndChild(PersonElement);
//设置Person元素的属性。
if(p->type1==0)
PersonElement->SetAttribute("ID", "normalACC");
else
PersonElement->SetAttribute("ID", "vipACC");

//创建name元素、age元素并连接。
TiXmlElement *AccountElement = new TiXmlElement("account_no");
TiXmlElement *NameElement = new TiXmlElement("name");
TiXmlElement *MoneyElement = new TiXmlElement("money");
PersonElement->LinkEndChild(AccountElement);
PersonElement->LinkEndChild(NameElement);
PersonElement->LinkEndChild(MoneyElement);
//设置name元素和age元素的内容并连接。
TiXmlText *accountContent = new TiXmlText((char*)p->getId().data());
TiXmlText *nametContent = new TiXmlText((char*)p->name.data());

char zz[32];
//sprintf(zz,"%f",p->balance);

itoa((int)p->balance,zz,10);

TiXmlText *moneyContent = new TiXmlText(zz);
AccountElement->LinkEndChild(accountContent);
NameElement->LinkEndChild(nametContent);
MoneyElement->LinkEndChild(moneyContent);

}

myDocument->SaveFile("account.xml");//保存到文件
return true;
}
bool Bank::ReadXmlFile()
{//读取Xml文件,并遍历

//创建一个XML的文档对象。
TiXmlDocument *myDocument = new TiXmlDocument("account.xml");

if (!myDocument->LoadFile())
{
accountList =NULL;
return 0;
}
//获得根元素,即Persons。
TiXmlElement *RootElement = myDocument->RootElement();
//输出根元素名称,即输出Persons。
cout << RootElement->Value() << endl;

//获得第一个Person节点。
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
//获得第一个Person的name节点和age节点和ID属性。

while(FirstPerson)
{

TiXmlElement *accountElement = FirstPerson->FirstChildElement();
TiXmlElement *NameElement = accountElement->NextSiblingElement();
TiXmlElement *moneyElement = NameElement->NextSiblingElement();
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
Account  *acc ;

int money =  atoi(moneyElement->FirstChild()->Value());
if(!strcmp(IDAttribute->Value(),"normalACC"))
{
acc  = new NormalAccount( accountElement->FirstChild()->Value(),NameElement->FirstChild()->Value(),money);
}
else
{
acc  = new VIPAccount( accountElement->FirstChild()->Value(),NameElement->FirstChild()->Value(),money,0,0);
}
if(acc!=NULL)
addlist(acc);

FirstPerson = FirstPerson->NextSiblingElement();
}

return true;
}

int main()
{

Bank bank;
bank.ReadXmlFile();

while(1)
{
cout << "?????1.增加账户 2.删除账户 3.查询账户 4.取款存款 5.退出系统?????" << endl;

cout << "请选择服务项目:";

int n;
cin>>n;
if(n == 1)             //增加账户
{
int n;
cout << "1.增加普通账户 2.增加高级账户" << endl;
cout << "请选择项目:" ;
cin>>n;

if(n == 1)
bank.append1();

if(n == 2)
bank.append2();

}

if (n == 2)              //删除账户
{
bank.deleteUser();
}

if (n == 3)            //查询账户
{
bank.query();
}

if (n == 4)
{

string n;
cout<<"请输入您要存取款的账号"<<endl;
cin>>n;
Account *p=bank.getMyAcc(n);
if (p==NULL)
{
cout<<"没有账户"<<endl;
}
else
{
int choice;

cout<<"1.存款"<<endl;
cout<<"2.取款"<<endl;
cout<<"请选择"<<endl;

cin>>choice;

if(choice == 1)
{
double jine;
cout<<"请输入存款金额"<<endl;
cin >> jine;
p->saving(jine);
bank.CreateXmlFile();
}
if(choice==2)
{
double jine;
cout<<"请输入取款金额"<<endl;
cin>>jine;
p->getOutMoney(jine);
bank.CreateXmlFile();

}
p->showAccountInfo();
}

}

if (n == 5)
return 0;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: