Record.h
#ifndef HEADER_RECORD
#define HEADER_RECORD
#include <string>
using namespace std;
class Record
{
private:
int number;
string userName;
string passWord;
double balance;
int flag;
public:
Record();
void set_number(int number);
void set_userName(string userName);
void set_passWord(string passWord);
void set_balance(double balance);
void set_flag(int flag);
int get_number();
string get_userName();
string get_passWord();
double get_balance();
int get_flag();
void display_Record();
};
#endif
Record.cpp
#include "Record.h"
#include <iostream>
using namespace std;
Record::Record()
{
this->number = 0;
this->userName = "";
this->passWord = "";
this->balance = 0;
this->flag = -1;
}
void Record::set_number(int number)
{
this->number = number;
}
void Record::set_userName(string userName)
{
this->userName = userName;
}
void Record::set_passWord(string passWord)
{
this->passWord = passWord;
}
void Record::set_balance(double balance)
{
this->balance = balance;
}
void Record::set_flag(int flag)
{
this->flag = flag;
}
int Record::get_number()
{
return this->number;
}
string Record::get_userName()
{
return this->userName;
}
string Record::get_passWord()
{
return this->passWord;
}
double Record::get_balance()
{
return this->balance;
}
int Record::get_flag()
{
return this->flag;
}
void Record::display_Record()
{
cout << "Print Record elenment..." << endl;
cout << "$------------------------$" << endl;
cout << " Number : " << this->number << endl;
cout << " UserName: " << this->userName << endl;
cout << " PassWord: " << this->passWord << endl;
cout << " Balance : " << this->balance << endl;
cout << " flag : ";
switch(this->flag)
{
case 0:
cout << "closed" << endl;
break;
case 1:
cout << "normal" << endl;
break;
}
cout << "$------------------------$" << endl;
cout << "End of Record..." << endl;
}
Node.h
#ifndef HEADER_NODE //如果没有定义 HEADER_NODE
#define HEADER_NODE //避免同一个文件被包含多次
#include "Record.h"
class Node
{
private:
Record * record;
Node * next;
public:
Node();
~Node();
void set_record(Record * record);
void set_next(Node * next);
Record * get_record();
Node * get_next();
void dispay_Node();
};
#endif
Node.cpp
#include "Node.h"
#include <iostream>
using namespace std;
Node::Node()
{
this->record = NULL;
this->next = NULL;
}
Node::~Node()
{
delete this->record; //先释放资源
this->record = NULL;
this->next=NULL;
}
void Node::set_record(Record * record)
{
this->record = record;
}
void Node::set_next(Node * next)
{
this->next = next;
}
Record * Node::get_record()
{
return this->record;
}
Node * Node::get_next()
{
return this->next;
}
void Node::dispay_Node()
{
cout << "Print Node elements..." << endl;
//cout << "Record * : " << this->record << endl; //输出地址值
if (this->record != NULL) //record的内容是否为空,若为空,则出错,空指针异常
{
Record * r = this->record;
r->display_Record();
}
else
{
cout << "Record is NULL..." << endl;
}
cout << "Next * : " << this->next << endl;
cout << "End of Node..." << endl;
}
LinkList.h
#ifndef HEADER_LINKLIST
#define HEADER_LINKLIST
#include "Node.h"
class LinkList
{
private:
Node * head;
int len;
public:
LinkList();
~LinkList();
void set_head(Node * head);
void set_len(int len);
Node * get_head();
int get_len();
Node * make_node(Record * record);
void insert_node(Node * node);
Node * find_node(int number);
void display_LinkList();
};
#endif;
LinkList.cpp
#include "LinkList.h"
#include <iostream>
using namespace std;
LinkList::LinkList()
{
this->head = NULL;
this->len = 0;
}
LinkList::~LinkList()
{
Node * p, * q;
p = this->head;
while(p != NULL)
{
q = p;
p = p->get_next();
delete q;
}
this->head = NULL;
this->len = 0;
}
void LinkList::set_head(Node * head)
{
this->head = head;
}
void LinkList::set_len(int len)
{
this->len = len;
}
Node * LinkList::get_head()
{
return this->head;
}
int LinkList::get_len()
{
return this->len;
}
Node * LinkList::make_node(Record * record) //创建结点
{
Node * node = new Node();
node->set_record(record);
node->set_next(NULL);
return node;
}
void LinkList::insert_node(Node * node)
{
Node * p = this->head;
if(p == NULL) //此链表为空链表
{
this->head = node;
this->len++;
return;
}
while(p->get_next() != NULL) //p不是最后一个
{
p = p->get_next();
}
p->set_next(node); //p是最后一个,将p的next指向插入的结点
this->len++;
}
Node * LinkList::find_node(int number)
{
Node * p = this->head;
while (p != NULL)
{
Record * r = p->get_record();
if(r->get_number() == number)
{
return p;
}
else
{
p = p->get_next();
}
}
return p; //没找到,条件为p==NULL,返回的p是NULL。
}
void LinkList::display_LinkList()
{
cout << "Print LinkList..." << endl;
//cout << "Head: " << this->head << endl; //输出head指针
Node * p = this->head;
if(p == NULL)
{
cout << "LinkList is NULL..." << endl;
cout << "Len: " << this->len << endl;
cout << "End of LinkList..." << endl;
return;
}
while(p != NULL)
{
p->dispay_Node();
p = p->get_next();
}
cout << "Len: " << this->len << endl;
cout << "End of LinkList..." << endl;
}
Bank.h
#ifndef HEADER_BANK
#define HEADER_BANK
#include "LinkList.h"
using namespace std;
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 creat_number();
int get_number();
string get_userName();
string get_passWord();
double get_balance();
Record * find_record(int account_number);
void open_account();
void find_account();
void close_account();
void deposit();
void withdraw();
void transfer();
};
#endif
Bank.cpp
#include "Bank.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->creat_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::creat_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 account_number)
{
Node * n = this->list->find_node(account_number);
if (n != NULL)
{
return n->get_record();
}
return NULL;
}
void Bank::open_account()
{
//获取用户输入的帐户信息
string userName = this->get_userName();
string passWord = this->get_passWord();
double balance = this->get_balance();
//使用输入数据创建Recore对象
Record * r = this->make_record(userName, passWord, balance);
//创建Node节点,封装Record对象
Node * n = this->list->make_node(r);
//将Node节点插入到LinkList链表
this->list->insert_node(n);
cout << "open account..." << endl;
r->display_Record();
}
void Bank::close_account()
{
int i = 1;
// 获取用户输入的账户信息
int number = this->get_number();
// 查找要销户的账户
Record * back_position = this->find_record(number);
//根据返回的状态确定输出内容
if (back_position == NULL)
{
cout << "-Not Found...-" << endl;
getchar();
return;
}
if (back_position->get_flag() == 0)
{
cout << "-The account can't clese again...-" << endl;
getchar();
return;
}
string passWord = this->get_passWord();
while (i <= 3)
{
if (passWord != back_position->get_passWord())
{
if (i > 2)
{
cout << "-So Sorry...-" << endl;
exit(0);
}
cout << "Wrong password, please input again: ";
passWord = this->get_passWord();
i = i+1;
}
else
{
break;
}
}
char userAnswer;
cout << "Close account? [1:Yes | else:No] " << endl;
cin >> userAnswer;
if (userAnswer == '1')
{
back_position->set_flag(0);
back_position->set_balance(0);
back_position->display_Record();
cout << "-Succeed close account...-" << endl;
getchar();
return;
}
cout << "-User cancel close account...-" << endl;
getchar();
return;
}
void Bank::find_account()
{
int i = 1;
// 获取用户输入的账户信息
int number = this->get_number();
// 查找账户
Record * back_position = this->find_record(number);
//根据返回的状态确定输出内容
if (back_position == NULL)
{
cout << "-Not Found...-" << endl;
getchar();
return;
}
string passWord = this->get_passWord();
while (i <= 3)
{
if (passWord != back_position->get_passWord())
{
if (i > 2)
{
cout << "-So Sorry...-" << endl;
exit(0);
}
cout << "Wrong password, please input again: ";
passWord = this->get_passWord();
i = i+1;
}
else
{
break;
}
}
back_position->display_Record();
cout << "-Succeed find account...-" << endl;
getchar();
return;
}
void Bank::deposit()
{
int i = 1;
// 获取用户输入的账户信息
int number = this->get_number();
// 查找账户
Record * back_position = this->find_record(number);
//根据返回的状态确定输出内容
if (back_position == NULL)
{
cout << "-Not Found...-" << endl;
getchar();
return;
}
if (back_position->get_flag() == 0)
{
cout << "-Account has closed...-" << endl;
return;
}
string passWord = this->get_passWord();
while (i <= 3)
{
if (passWord != back_position->get_passWord())
{
if (i > 2)
{
cout << "-So Sorry...-" << endl;
exit(0);
}
cout << "Wrong password, please input again: ";
passWord = this->get_passWord();
i = i+1;
}
else
{
break;
}
}
//back_position->display_Record();
double cash;
cout << "Please enter the amount of deposit: ";
cin >> cash;
back_position->set_balance(back_position->get_balance() + cash);
back_position->display_Record();
cout << "-Succeed doposit...-" << endl;
getchar();
return;
}
void Bank::withdraw()
{
int i = 1;
// 获取用户输入的账户信息
int number = this->get_number();
// 查找账户
Record * back_position = this->find_record(number);
//根据返回的状态确定输出内容
if(back_position == NULL)
{
cout << "-Not Found...-" << endl;
getchar();
return;
}
if (back_position->get_flag() == 0)
{
cout << "-Account has closed...-" << endl;
return;
}
string passWord = this->get_passWord();
while (i <= 3)
{
if (passWord != back_position->get_passWord())
{
if (i > 2)
{
cout << "-So Sorry...-" << endl;
exit(0);
}
cout << "Wrong password, please input again: ";
passWord = this->get_passWord();
i = i+1;
}
else
{
break;
}
}
back_position->display_Record();
double cash;
cout << "Please enter the amount of take out: ";
cin >> cash;
while (back_position->get_balance() < cash)
{
cout << "Your lack of balance, please input again: ";
cin >> cash;
}
back_position->set_balance(back_position->get_balance() - cash);
back_position->display_Record();
cout << "-Succeed withdraw...-" << endl;
getchar();
return;
}
void Bank::transfer()
{
int i = 1;
// 获取用户输入的账户信息
int number = this->get_number();
// 查找账户
Record * back_position = this->find_record(number);
//根据返回的状态确定输出内容
if(back_position == NULL)
{
cout << "-Not Found...-" << endl;
getchar();
return;
}
if(back_position->get_flag() == 0)
{
cout << "-Account has closed...-" << endl;
getchar();
return;
}
string passWord = this->get_passWord();
while (i <= 3)
{
if (passWord != back_position->get_passWord())
{
if (i > 2)
{
cout << "-So Sorry...-" << endl;
exit(0);
}
cout << "Wrong password, please input again: ";
passWord = this->get_passWord();
i = i+1;
}
else
{
break;
}
}
back_position->display_Record();
double cash;
cout << "Please input account number : ";
cin >> cash;
while (back_position->get_balance() < cash)
{
cout << "Your lack of balance, please input again: ";
cin >> cash;
}
back_position->set_balance(back_position->get_balance() - cash);
back_position->display_Record();
// 资金转入
cout << "Please input the number of transfer into: " << endl;
// 获取用户输入的账户信息
int number_in = this->get_number();
// 查找账户
Record * back_in_position = this->find_record(number_in);
// 根据返回的状态确定输出内容
if(back_in_position == NULL)
{
cout << "-Not Found...-" << endl;
getchar();
return;
}
if(back_in_position->get_flag() == 0)
{
cout << "Account has closed..." << endl;
getchar();
return;
}
back_in_position->set_balance(back_in_position->get_balance() + cash);
//back_in_position->display_Record();
cout << "-Succeed transfer account...-" << endl;
}
Main.cpp
#include "Bank.h"
#include <iostream>
using namespace std;
void login(Bank * bank);
void mainmenu(Bank * bank);
void welcome();
void welcome()
{
cout << "+-------------------------------------------+" << endl;
cout << "| |" << endl;
cout << "| -WELCOME TO SHINIGAMI BANK- |" << endl;
cout << "| |" << endl;
cout << "+-------------------------------------------+" << endl;
}
void mainmenu(Bank * bank)
{
int choice = -1;
do
{
cout << endl;
cout << "+-------------------------------------------+" << endl;
cout << "| 1 open account 4 deposit |" << endl;
cout << "| 2 close account 5 withdraw |" << endl;
cout << "| 3 find account 6 transfer account |" << endl;
cout << "| 0 exit |" << endl;
cout << "+-------------------------------------------+" << endl;
cout << "Please input choice..." << endl;
cin >> choice;
switch(choice)
{
case 0:
cout << "-Thanks for using...-" << endl;
exit(0);
break;
case 1:
bank->open_account();
break;
case 2:
bank->close_account();
break;
case 3:
bank->find_account();
break;
case 4:
bank->deposit();
break;
case 5:
bank->withdraw();
break;
case 6:
bank->transfer();
break;
}
}
while(true);
}
void login(Bank * bank)
{
string admin_userName;
string admin_passWord;
cout << "Please input admin userName..." << endl;
cin >> admin_userName;
cout << "Please input admin passWord..." << endl;
cin >> admin_passWord;
for (int i = 1; i <= 3; i++)
{
if (admin_userName == "admin" && admin_passWord == "admin")
{
mainmenu(bank);
}
else
{
if (i <= 2)
{
cout << "Please input again..." << endl;
cout << "Please input admin userName..." << endl;
cin >> admin_userName;
cout << "Please input admin passWord..." << endl;
cin >> admin_passWord;
continue;
}
else
{
cout << "-So Sorry...-" << endl;
break;
}
}
}
}
int main()
{
welcome();
Bank * bank = new Bank();
login(bank);
return 0;
}
应该早发的,前几天登不上,后来又一系列事···就这样吧。还请多多指教。