4個頭文件
[cpp]
#include "LinkList.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 creat_number();
int get_number();
string get_userName();
string get_passWord();
double get_balance();
Record *find_record(int number);
void open_acount();
void close_acount();//銷戶
void find_acount();
void deposit();//存款
void withdraw();//取款
};
[html]
#include "Node.h"
class LinkList
{
private:
Node *head;
int len;
public:
LinkList();
~LinkList();
void set_head(Node *head);
Node *get_head();//獲得頭節點
int get_len();
Node *make_node(Record *record);//制作一個節點,目的是插入record
void insert_node(Node *node);//插入一個節點
Node *find_node(int number);//找到一個節點,通過賬戶(number)查找
void display_LinkList();
};
[html]
#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 display_Node();
};
[cpp]
#include <iostream>
#include <string>
using namespace std;
class Record
{
private:
int number;
double salary;
string userName;
string passWord;
int flag;
public:
Record();
void set_number(int number);
void set_salary(double salary);
void set_userName(string userName);
void set_passWord(string passWord);
void set_flag(int flag);
int get_number();
double get_salary();
string get_userName();
string get_passWord();
int get_flag();
void display();
};
4個資源文件文件
[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;//自動調用~LinkList()
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(std::string userName, std::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_salary(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<<"請輸入賬號....."<<endl;
cin>>number;
cout<<"賬號:"<<number<<endl;
return number;
}
string Bank::get_userName()
{
string userName;
cout<<"請輸入用戶名....."<<endl;
cin>>userName;
cout<<"用戶名:"<<userName<<endl;
return userName;
}
string Bank::get_passWord()
{
string paseWord;
cout<<"請輸入密碼....."<<endl;
cin>>paseWord;
// cout<<"paseWord:"<<paseWord<<endl;
return paseWord;
}
double Bank::get_balance()
{
double balance;
cout<<"請輸入金額....."<<endl;
cin>>balance;
cout<<"金額:"<<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_acount()
{
//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節點插入到linkliast
this->list->insert_node(n);
cout<<"您的賬號是"<<this->account_number<<endl;
}
void Bank::close_acount()
{
int i=3;
int flag;
// 1 獲取賬戶信息
int number = this->get_number();
string passWord = this->get_passWord();
// 2 在鏈表中找到賬節點,找到賬戶信息
Node *node=this->list->find_node(number);
if(node == NULL)
{
cout<<"賬戶不存在"<<endl;
return ;
}
Record *record =node ->get_record();
// 3 密碼驗證
for(i=3;i>=1;i--)
{
if(record->get_passWord() == passWord)
// 4 用戶狀態改變
{
do
{
cout<<"確定要銷戶?(0 銷戶,1 不銷戶)"<<endl;
cin>>flag;
if(flag == 1)
{
record->set_flag(flag);
return ;
}
else if(flag == 0)
{
record->set_flag(flag);
record->set_salary(0);
return ;
}
else
{
cout<<"輸入錯誤,請重新輸入"<<endl;
}
}
while(1);
cout<<"還有"<<i<<"次機會"<<endl;
}
}
}
void Bank::deposit()
{
int balance=0;
//1 獲取賬戶賬號以及密碼
int number = this->get_number();
string passWord = this->get_passWord();
//2 從鏈表中找到賬戶信息
Node *node=this->list->find_node(number);
if(node == NULL)
{
cout<<"賬戶不存在"<<endl;
return ;
}
Record *record =node ->get_record();
//3 密碼驗證,存款
if(record->get_passWord() == passWord)
{
double balance=this->get_balance()+record->get_salary();
record->set_salary(balance);
cout<<"存款成功"<<endl;
return ;
}
}
void Bank::withdraw()
{
int balance=0;
//1 獲取賬戶賬號以及密碼
int number = this->get_number();
string passWord = this->get_passWord();
//2 從鏈表中找到賬戶信息
Node *node=this->list->find_node(number);
if(node == NULL)
{
cout<<"賬戶不存在"<<endl;
return ;
}
Record *record =node ->get_record();
//3 密碼驗證,取款
if(record->get_passWord() == passWord)
{
double balance=record->get_salary()-this->get_balance();
if(balance>=0)
{
record->set_salary(balance);
cout<<"取款成功"<<endl;
return ;
}
else
{
cout<<"您的余額不足"<<endl;
return ;
}
}
}
void Bank::find_acount()
{
int number = this->get_number();
string passWord = this->get_passWord();
//2 從鏈表中找到賬戶信息
Node *node=this->list->find_node(number);
if(node == NULL)
{
cout<<"賬戶不存在"<<endl;
return ;
}
Record *record =node ->get_record();
if(record->get_passWord() == passWord)
{
record->display();
return ;
}
else
{
cout<<"密碼輸入錯誤"<<endl;
}
}
[cpp]
#include "LinkList.h"
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;
}
Node *LinkList::get_head()
{
return this->head;
}
int LinkList::get_len()
{
return 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 ;//必須有return,否則執行while,與p->set_next(node)沖突報告異常
}
while(p->get_next() != NULL)
{
p = p->get_next();
}
p->set_next(node);
this->len++;
return ;
}
Node *LinkList::find_node(int number)
{
Node *p;//要查找的節點
Record *r;//因該是按照賬戶信息查找(number)
p=this->head;
while(p != NULL)//如果在節點內找到信息,則返回節點,否則,指向下一個節點
{
r=p->get_record();
if (r->get_number()==number)
{
return p;
}
else
{
p=p->get_next();
}
}
return NULL;
}
void LinkList::display_LinkList()
{
Node *p;
p=this->head;
cout<<"Print LinkList.."<<endl;
if(p == NULL)
{
cout<<"The LinkList is Null"<<endl;
cout<<this->len<<endl;
}
else
{
while(p != NULL)
{
p->display_Node();
p=p->get_next();
}
}
cout<<"The len is"<<this->len<<endl;
cout<<"End of linkList.."<<endl;
}
[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::display_Node()
{
Record *p;
if(this->record != NULL)
{
p=this->record;
p->display();
}
else
{
cout<<"Rcord is null"<<endl;
}
cout<<this->next<<endl;
}
[cpp]
#include "Record.h"
#include <string>
#include <iostream>
using namespace std;
Record::Record()
{
this->flag=0;
this->number=0000;
this->passWord="";
this->salary=0.0;
this->userName="";
}
void Record::set_number(int number)
{
this->number=number;
}
void Record::set_passWord(string passWord)
{
this->passWord=passWord;
}
void Record::set_salary(double salary)
{
this->salary=salary;
}
void Record::set_userName(string userName)
{
this->userName=userName;
}
void Record::set_flag(int flag)
{
this->flag=flag;
}
int Record::get_flag()
{
return flag;
}
int Record::get_number()
{
return number;
}
double Record::get_salary()
{
return salary;
}
string Record::get_userName()
{
return userName;
}
string Record::get_passWord()
{
return passWord;
}
void Record::display()
{
cout<<"**************************************************************************"<<endl;
cout<<"**用戶名:"<<get_userName()<<" **賬號:"<<get_number()<<" **余額:" <<get_salary()<< " ||賬戶狀態(1正常0銷戶):"<<get_flag()<<"**"<<endl;
cout<<"**************************************************************************"<<endl;
}
main
[cpp]
#include "Bank.h"
#include "iostream"
using namespace std;
int flag;
void meanu()
{
cout<<"Welcome to the bank"<<endl;
cout<<"***********************************************"<<endl;
cout<<"** 1 開戶 ** 2 取款 ** 3 存款 **"<<endl;
cout<<"***********************************************"<<endl;
cout<<"** 4 銷戶 ** 5 賬戶信息 ** 6 退出 **"<<endl;
cout<<"***********************************************"<<endl;
cout<<"請選擇:"<<endl;
} www.2cto.com
int main()
{
Bank *bank = new Bank();
//LinkList *list = bank->get_list();
do
{
meanu();
cin>>flag;
switch(flag)
{
case 1:bank->open_acount();break;
case 2:bank->withdraw();break;
case 3:bank->deposit();break;
case 4:bank->close_acount();break;
case 5:bank->find_acount();break;
case 6:exit(1);break;
default:cout<<"錯誤選擇"<<endl;
}
}while (1);
system("pause");
return 0;
}