您的位置:首页 > 其它

银行管理系统(校企作业)

2016-06-22 16:59 323 查看
功能比较简陋,用的tinyxml

主函数:

#include "User.h"
#include <iostream>
using namespace std;
int operation;
void Register(ListUser& Root);
void OperationInterface(Middle* current, ListUser& Root)
{
operation = 1;
int judge;
cout << " 0.返回主界面" << endl;
cout << " 1.查询信息" << endl;
cout << " 2.存款" << endl;
cout << " 3.取款" << endl;
cout << " 4.销户" << endl;
while(true)
{
cin >> judge;
switch(judge)
{
case 0: return;
case 1: current->person.displayInfo(); break;
case 2: current->person.saveMoney(); break;
case 3: current->person.takeMoney(); break;
case 4: Root.ldelete(current->person); break;
default : cout << " 请输入0-4" << endl; break;
}
}
}

void Login(ListUser& Root)
{
Middle* current = nullptr;
string name;
string pword;
cout << " 用户名:" << ends;
cin >> name;
cout << " 密 码:" << ends;
cin >> pword;
current = Root.login(name,pword);
if(!current)
{
cout << " 您还未开户,请先开户" << endl;
Register(Root);
}
else
OperationInterface(current,Root);
}

void Register(ListUser& Root)
{
Middle* current = nullptr;
User people;
people.setData();
if(Root.check(people))
{
cout << " 您已经开户,请登录" << endl;
Login(Root);
}
else
{
current = Root.push(people);
cout << " 开户成功" << endl;
OperationInterface(current,Root);
}
}

int main()
{
ListUser Root;
int judge;
cout << " 1.开户" << endl;
cout << " 2.登录" << endl;
cout << " 0.退出" << endl;
while(true)
{
operation = 0;
cin >> judge;
if(judge == 1)
Register(Root);
else if(judge == 2)
Login(Root);
else if(judge == 0)
break;
else
cout << " 请输入0-2" << endl;
if(operation)
{
cout << " 1.开户" << endl;
cout << " 2.登录" << endl;
cout << " 0.退出" << endl;
}
}
return 0;
}


User.h:
#ifndef USER_H_INCLUDED
#define USER_H_INCLUDED

#include <string>
class User
{
public:
User():name(""),idCard(""),password(""),numberOfCard(""),money(0),VIP(0){}

User(std::string nam,std::string card,std::string paword,std::string noc,double mon,int vip):
name(nam),idCard(card),password(paword),numberOfCard(noc),money(mon),VIP(vip){}

User(const User& use):name(use.name),idCard(use.idCard),password(use.password),
numberOfCard(use.numberOfCard),money(use.money),VIP(use.VIP){}
std::string getPassword(){ return password;}
std::string getName(){ return name;}
std::string getidCard(){ return idCard;}
std::string getnumberOfCard(){ return numberOfCard;}
double getMoney(){ return money;}
int getVIP(){ return VIP;}
void setData();
void saveMoney();
void takeMoney();
void displayInfo();
private:
std::string name;
std::string idCard;
std::string password;
std::string numberOfCard;//ka hao
double money;
int VIP;
};

struct Middle
{
Middle():next(nullptr){}
Middle(User& p):person(p),next(nullptr){}
User person;
Middle* next;
};

class ListUser
{
public:
ListUser();
~ListUser();
Middle* push(User&);
void ldelete(User&);
bool check(User&);
Middle* login(std::string& name, std::string& pword);
private:
Middle* head;
};

#endif // USER_H_INCLUDED


User.cpp:
#include "User.h"
#include <cstdlib>
#include "tinystr.h"
#include "tinyxml.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ends;

void User::setData()
{
cout << " 姓 名:" << ends;
cin >> name;
cout << " 身份证:" << ends;
cin >> idCard;
cout << " 密 码:" << ends;
cin >> password;
cout << " 卡 号:" << ends;
cin >> numberOfCard;
cout << " 存款数:" << ends;
cin >> money;
cout << " VIP[y(1)/n(0)]:" << ends;
cin >> VIP;
}

void User::saveMoney()
{
double nmoney;
cout << "Please input the number of money:";
cin >> nmoney;
this->money += nmoney;
cout << "Money was saved." << endl;
}

void User::takeMoney()
{
double nmoney;
cout << "Please input the number of money:";
cin >> nmoney;
if(this->money >= nmoney || VIP)
{
this->money -= nmoney;
cout << "Money has been taken." << endl;
if(this->money < 0)
cout << "You have overdraft, please timely repayment." << endl;
}
else
cout << "Your balance is not enough." << endl;
}

void User::displayInfo()
{
cout << "Name:" << this->name << endl;
cout << "Password:" << this->password << endl;
cout << "ID Card:" << this->idCard << endl;
cout << "Card Number:" << this->numberOfCard << endl;
cout << "Money:" << this->money << endl;
if(VIP)
cout << "VIP: yes" << endl;
else
cout << "VIP: no" << endl;
}

//构造函数,从文件读取数据
ListUser::ListUser():head(new Middle)
{
TiXmlDocument *myDocument = new TiXmlDocument("log.xml");
myDocument->LoadFile();
TiXmlElement *RootElement = myDocument->RootElement();

TiXmlElement *CurrentUser = RootElement->FirstChildElement();

std::string name,idCard,password,numberOfCard;
double money;
int VIP;

while(CurrentUser != NULL)
{
TiXmlElement *NameElement = CurrentUser->FirstChildElement();
TiXmlElement *IdCardElement = NameElement->NextSiblingElement();
TiXmlElement *PasswordElement = IdCardElement->NextSiblingElement();
TiXmlElement *NumberOfCardElement = PasswordElement->NextSiblingElement();
TiXmlElement *MoneyElement = NumberOfCardElement->NextSiblingElement();
TiXmlElement *VIPElement = MoneyElement->NextSiblingElement();

name = NameElement->FirstChild()->Value();
idCard = IdCardElement->FirstChild()->Value();
password = PasswordElement->FirstChild()->Value();
numberOfCard = NumberOfCardElement->FirstChild()->Value();
money = atof(MoneyElement->FirstChild()->Value());
VIP = atoi(VIPElement->FirstChild()->Value());

User people(name,idCard,password,numberOfCard,money,VIP);
Middle* newPoint = new Middle(people);
newPoint->next = head->next;
head->next = newPoint;

CurrentUser = CurrentUser->NextSiblingElement();
}
}

//析构函数,保存数据到xml文件
ListUser::~ListUser()
{
TiXmlDocument *myDocument = new TiXmlDocument();
TiXmlElement *RootElement = new TiXmlElement("UserList");
myDocument->LinkEndChild(RootElement);
Middle* REhead = head;
Middle* temp;
while(REhead->next)
{
char str1[20];
char vip[2];
TiXmlElement* UserElement = new TiXmlElement("User");
RootElement->LinkEndChild(UserElement);

TiXmlElement *NameElement = new TiXmlElement("Name");
TiXmlElement *IdCardElement = new TiXmlElement("IdCard");
TiXmlElement *PasswordElement = new TiXmlElement("Password");
TiXmlElement *NumberOfCardElement = new TiXmlElement("NumberOfCard");
TiXmlElement *MoneyElement = new TiXmlElement("Money");
TiXmlElement *VIPElement = new TiXmlElement("VIP");

UserElement->LinkEndChild(NameElement);
UserElement->LinkEndChild(IdCardElement);
UserElement->LinkEndChild(PasswordElement);
UserElement->LinkEndChild(NumberOfCardElement);
UserElement->LinkEndChild(MoneyElement);
UserElement->LinkEndChild(VIPElement);

TiXmlText *NameContent = new TiXmlText((char*)REhead->next->person.getName().data());
TiXmlText *IdCardContent = new TiXmlText((char*)REhead->next->person.getidCard().data());
TiXmlText *PasswordContent = new TiXmlText((char*)REhead->next->person.getPassword().data());
TiXmlText *NumberOfCardContent = new TiXmlText((char*)REhead->next->person.getnumberOfCard().data());
sprintf(str1,"%f",REhead->next->person.getMoney());
TiXmlText *MoneyContent = new TiXmlText(str1);
sprintf(vip,"%d",REhead->next->person.getVIP());
TiXmlText *VIPContent = new TiXmlText(vip);

NameElement->LinkEndChild(NameContent);
IdCardElement->LinkEndChild(IdCardContent);
PasswordElement->LinkEndChild(PasswordContent);
NumberOfCardElement->LinkEndChild(NumberOfCardContent);
MoneyElement->LinkEndChild(MoneyContent);
VIPElement->LinkEndChild(VIPContent);

myDocument->SaveFile("log.xml");

temp = REhead;
REhead = REhead->next;
delete temp;
}
delete REhead;
}

//添加用户
Middle* ListUser::push(User& person)
{
Middle* newPoint = new Middle(person);
newPoint->next = head->next;
head->next = newPoint;
return newPoint;
}

//按照我的写法,只能先登录之后再销户,如果是登录之后销户,则不需要对比密码
void ListUser::ldelete(User& nperson)
{
Middle* REhead = head;
Middle* temp;
while(REhead->next)
{
if(REhead->next->person.getName() == nperson.getName())
{
temp = REhead->next;
REhead->next = temp->next;
delete temp;
cout << "Success!" << endl;
break;
}
REhead = REhead->next;
}
}

//检查用户是否已经开户
bool ListUser::check(User& nperson)
{
Middle* REhead = head;
while(REhead->next)
{
if(REhead->next->person.getName() == nperson.getName())
return true;
REhead = REhead->next;
}
return false;
}

//登录
Middle* ListUser::login(std::string& name,std::string& pword)
{
Middle* REhead = head;
while(REhead->next)
{
if(REhead->next->person.getName() == name)
{
if(REhead->next->person.getPassword() == pword)
return REhead->next;
else
{
std::string ppword;
cout << " 密码错误,你还可以输入两次密码,如果输入错误,则会关闭系统" << endl;
int n = 2;
while(n--)
{
cin >> ppword;
if(REhead->next->person.getPassword() == ppword)
return REhead->next;
else
cout << " 输入错误" << endl;
}
exit(1);
}
}
REhead = REhead->next;
}
return nullptr;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: