头文件 Bank.h
#ifndef HEADER_BANK
#define HEADER_BANK
#include"LinkList.h"
#include "Record.h"
class Bank
{
private:
LinkList * list;
int account_number;
public:
Bank();
~Bank();
void set_list(LinkList * list);
LinkList * get_list();
LinkList * make_List();//工厂模式
Record * make_record(string userName, string passWord, double balance);
int create_number();//生成账号
int get_number();
string get_userName();
string get_passWord();
double get_balance();//接收用户键盘输入,供其它函数使用
Record * find_record(int number);
void open_account();//开户
void find_account();//查询,供用户使用
void close_account();//销户
void deposit();//存款
void withdraw();//取款
void tranfser();//转账
};
#endif
源文件 Bank.h
#include "Bank.h"
#include<conio.h>
#include<iostream>
using namespace std;
Bank::Bank()
{
this->list = this->make_List();
this->account_number = 10000;
}
Bank::~Bank()
{
delete this->list;
this->list = NULL;
}
void Bank::set_list(LinkList * list)
{
this->list = list;
}
LinkList * Bank::get_list()
{
return this->list;
}
LinkList * Bank::make_List()
{
return new LinkList();
}
Record * Bank::make_record(string userName, string passWord, double balance)
{
Record * r = new Record();
int number = this->create_number();
r->set_number(number);
r->set_userName(userName);
r->set_passWord(passWord);
r->set_balance(balance);
r->set_flag(1);
return r;
}
int Bank::create_number()
{
return (this->account_number = this->account_number + 1);
}
int Bank::get_number()
{
int number;
cout << "Please input number..." << endl;
cin >> number;
cout << "Number : " << number << endl;
return number;
}
string Bank::get_userName()
{
string userName;
cout << "Please input userName..." << endl;
cin >> userName;
cout << "UserName : " << userName << endl;
return userName;
}
string Bank::get_passWord()
{
string passWord;
cout << "Please input passWord..." << endl;
cin >> passWord;
cout << "PassWord : " << passWord << endl;
return passWord;
}
double Bank::get_balance()
{
double balance;
cout << "Please input balance..." << endl;
cin >> balance;
cout << "balance : " << balance << endl;
return balance;
}
Record * Bank::find_record(int number)
{
Node * n = this->list->find_node(number);
if(n != NULL)
{
return n->get_record();
}
return NULL;
}
void Bank::open_account()
{
//1 获取用户输入的账户信息
string userName = this->get_userName();
string passWord = this->get_passWord();
double balance = this->get_balance();
//2 使用输入数据创建Record记录
Record * r = this->make_record(userName, passWord, balance);
//3 创建Node节点,封装Record记录
Node * n = this->list->make_node(r);
//4 将Node节点插入到LinkList链表
this->list->insert_node(n);
cout << "open account..." << endl;
getch();
}
void Bank::find_account()//查询
{
int number = this->get_number();
Record * r = this->find_record(number);
//判断账户是否存在
if(r == NULL)
{
cout << "查无此账户,按任意键返回..." << endl;
getch();
return;
}
//判断用户密码是否正确
string passWord;
passWord = this->get_passWord();
for(int i = 0; r->get_passWord() != passWord; i++)
{
if(i == 2)
{
cout << "密码错误以3次,按任意键返回..." << endl;
getch();
return;
}
cout << "密码不正确!请重新输入..." << endl;
passWord = this->get_passWord();
}
r->display_Record();
cout << "查询成功,按任意键返回..." << endl;
getch();
}
void Bank::close_account()//销户
{
int number = this->get_number();
Record * r = this->find_record(number);
//判断账户是否存在
if(r == NULL)
{
cout << "查无此账户,按任意键返回..." << endl;
getch();
return;
}
//判断用户密码是否正确
string passWord;
passWord = this->get_passWord();
for(int i = 0; r->get_passWord() != passWord; i++)
{
if(i == 2)
{
cout << "密码错误以3次,按任意键返回...";
getch();
return;
}
cout << "密码不正确!请重新输入..." << endl;
passWord = this->get_passWord();
}
//判断是否已经销户,如果销户就不能再次销户
if(r->get_flag() == 0)
{
cout << "该帐户已经销户,不能再次销户,按任意键返回..." << endl;
getch();
return;
}
//让用户确认选择
cout << "确认将帐户" << r->get_userName() << "销户吗?" << "[1:销户 | 其他:不销户]" << endl;
int userAnswer;
cin >> userAnswer;
if(userAnswer == 1)
{
r->set_flag(0);
r->set_balance(0);
r->display_Record();
cout << "销户成功!按任意键返回..." << endl;
getch();
return;
}
r->display_Record();
cout << "用户取消销户!按任意键返回..." << endl;
getch();
}
void Bank::deposit()//存款
{
double cash, balance;
int number = this->get_number();
Record * r = this->find_record(number);
//判断账户是否存在
if(r == NULL)
{
cout << "查无此账户,按任意键返回..." << endl;
getch();
return;
}
//判断账户是否销户,如果销户则不能存款
if(r->get_flag() == 0)
{
cout << "该帐户已经注销,不能存款!按任意键返回..." << endl;
getch();
return;
}
r->display_Record();
cout << "请输入存款金额:" << endl;
cin >> cash;
balance = r->get_balance() + cash;
r->set_balance(balance);
r->display_Record();
cout << "存款成功!按任意键返回..." << endl;
getch();
}
void Bank::withdraw()//取款
{
double cash, balance;
int number = this->get_number();
Record * r = this->find_record(number);
//判断账户是否存在
if(r == NULL)
{
cout << "查无此账户,按任意键返回..." << endl;
getch();
return;
}
//判断用户密码是否正确
string passWord;
passWord = this->get_passWord();
for(int i = 0; r->get_passWord() != passWord; i++)
{
if(i == 2)
{
cout << "密码错误以3次,按任意键返回...";
getch();
return;
}
cout << "密码不正确!请重新输入..." << endl;
passWord = this->get_passWord();
}
//判断账户是否销户,如果销户则不能取款
if(r->get_flag() == 0)
{
cout << "该帐户已经销户,不能取款,按任意键返回..." << endl;
getch();
return;
}
cout << "请输入取款金额:" << endl;
cin >> cash;
//判断账户余额是否充足,如果不足则不能取款
if(r->get_balance() < cash)
{
cout << "帐户余额不足!按任意键返回..." << endl;
getch();
return;
}
r->display_Record();
balance = r->get_balance() - cash;
r->set_balance(balance);
r->display_Record();
cout << "取款成功!按任意键返回..." << endl;
getch();
}
void Bank::tranfser()//转账
{
double cash, balance1, balance2;
cout << "请输入要转出的账户账号:" << endl;
int number1 = this->get_number();
Record * r1 = this->find_record(number1);
//判断账户是否存在
if(r1 == NULL)
{
cout << "查无此账户,按任意键返回..." << endl;
getch();
return;
}
//判断用户密码是否正确
string passWord1;
passWord1 = this->get_passWord();
for(int i = 0; r1->get_passWord() != passWord1; i++)
{
if(i == 2)
{
cout << "密码错误以3次,按任意键返回...";
getch();
return;
}
cout << "密码不正确!请重新输入..." << endl;
passWord1 = this->get_passWord();
}
//判断账户是否销户,如果销户则不能取款
if(r1->get_flag() == 0)
{
cout << "该帐户已经销户,不能转出,按任意键返回..." << endl;
getch();
return;
}
//判断账户余额是否充足,如果不足则不能取款
if(r1->get_balance() < cash)
{
cout << "帐户余额不足!不能转出,按任意键返回..." << endl;
getch();
return;
}
cout << "请输入要转入的账户账号:" << endl;
int number2 = this->get_number();
Record * r2 = this->find_record(number2);
//判断账户是否存在
if(r2 == NULL)
{
cout << "查无此账户,按任意键返回..." << endl;
getch();
return;
}
//判断账户是否销户,如果销户则不能存款
if(r2->get_flag() == 0)
{
cout << "该帐户已经注销,不能转入!按任意键返回..." << endl;
getch();
return;
}
cout << "请输入转出金额:" << endl;
cin >> cash;
cout << "转出账户信息" << endl;
r1->display_Record();
balance1 = r1->get_balance() - cash;
r1->set_balance(balance1);
cout << "转出账户转出后账户信息" << endl;
r1->display_Record();
cout << "转入账户信息" << endl;
r2->display_Record();
balance2 = r2->get_balance() + cash;
r2->set_balance(balance2);
cout << "转入账户转入后账户信息" << endl;
r2->display_Record();
cout << "转账成功!按任意键返回..." << endl;
getch();
}
上机感言:项目已接近尾声了,心中有一丝的欣喜,一个项目是一点点的积累,从小逐渐变大到最后完成我们所需的功能