项目实训--设计一个银行类(银行类与链表类、节点类构成依赖关系--和类的组合关系不太一样)

前端之家收集整理的这篇文章主要介绍了项目实训--设计一个银行类(银行类与链表类、节点类构成依赖关系--和类的组合关系不太一样)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011,烟台大学计算机学院学生
* All rights reserved.
* 文件名称:银行类(银行类与链表类、节点类构成依赖关系)

* 作 者: 雷恒鑫
* 完成日期: 2012 年 08 月24 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出

* 程序头部的注释结束

*/

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 creat_number();

	int get_number();

	string get_userName();

	string get_passWord();

	double get_balance();

	Record *find_record(int number);

	void open_account();

	void close_account();

	void find_account();

	void deposit();

	void withdraw();

	void transfer_account();
};
#endif


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


Node.h

#ifndef 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 display_Node();
};
#endif


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


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,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 << "请输入您的账号..." << endl;   //cout<<"please input number..."<<endl;
	
	cin >> number;
	
	cout << "number:" << number << endl;
	
	return number;
}

string Bank::get_userName()
{
	string userName;
	
	cout << "请输入您的用户名..." << endl; //  cout << "please input userName..." << endl;
	
	cin >> userName;
	
	cout << "userName:" << userName << endl;
	
	return userName;
}

string Bank::get_passWord()
{
	string passWord;
	
	cout << "请输入您的密码..." << endl;		//cout<<"please input passWord..."<<endl;
	
	cin >> passWord;
	
	cout << "passWord:" << passWord << endl;
	
	return passWord;
}

double Bank::get_balance()
{
	double balance;
	
	cout << "请输入您的存款金额..." << endl;     //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();
	
	//使用输入数据创建Record记录
	
	Record *r = this->make_record(userName,passWord,balance);
	
	//创建Node节点
	
	Node *n = this->list->make_node(r);
	
	//将Node节点插入到LinkList链表
	
	this->list->insert_node(n);
	
	cout<<"开户成功!按任意键返回..."<<endl;    //cout<<"open account succeed..."<<endl;

	getchar(); 
	
    return; 
}

void Bank::close_account()//销户
{
	string passWord;
	
	int i,flag;
	
	//1.获取用户输入的账号信息
	
	int number = this->get_number();
	
	//查找账户位置
	
	Node *n = this->list->find_node(number);
	
	if(n != NULL)
	{
		cout << "请输入您的密码..." << endl;		//	cout << "Please input Your PassWord..." << endl;
		
		cin >> passWord;
		
		if(passWord == n->get_Record()->get_passWord())
		{
			if(n->get_Record()->get_flag() == 1)//1为正常状态
			{
				cout << "确认将此账户销户吗?【1:销户|其他:不销户】" << endl;
				
				cin >> i;
				if(i == 1)
				{
					flag = -1;
					
					n->get_Record()->set_flag(flag);
					
					cout<<"销户成功!按任意键返回..."<<endl;

					getchar();  

					return; 
				}
				else
				{
					cout << "用户取消销户!按任意键返回..." << endl;

					getchar();  

					return; 
				}
			}
			else
			{
				cout << "该账户已销户!按任意键返回..." << endl;

				getchar();  

				return; 
			}
		}
		else
		{
			cout << "密码不正确!按任意键返回..." << endl;

			getchar();  

			return; 
		}
	}
	else
	{
		cout << "查无此账户!按任意键返回..." << endl;

		getchar();  

		return; 
	}
}

void Bank::find_account()
{
	//1.获取用户输入的账号信息
	
	int number = this->get_number();
	
	//查找账户位置
	
	Node *n = this->list->find_node(number);
	
	if(n != NULL)
	{
		n->display_Node();
	}
	else
	{
		cout << "查无此账户!按任意键返回..." << endl;		//cout << "No Found Account Data..." << endl;

		getchar();  

		return; 
	}
}


void Bank::deposit()
{
	//1.获取用户输入的账号信息
	
	int number = this->get_number();
	
	//查找账户位置
	
	Node *n = this->list->find_node(number);
	
	if(n != NULL)
	{
		n->display_Node();
		
		cout << "请输入您的存款金额..." << endl;		//cout << "Please input cash data..." << endl;
		
		double cash;
		
		cin >> cash;
		
		double balance = n->get_Record()->get_balance();
		
		balance = balance + cash;
		
		n->get_Record()->set_balance(balance);

		cout << "存款成功!按任意键返回..." << endl;

		getchar();  

		return; 

	}
	else
	{
		cout<<"查无此账户!按任意键返回..."<<endl;			//cout<<"No Found Account Data..."<<endl;

		getchar();  

		return; 
	}
}

void Bank::withdraw()
{
	string passWord;
	
	double cash;
	
	//1.获取用户输入的账号信息
	
	int number = this->get_number();
	
	//查找账户位置
	
	Node *n = this->list->find_node(number);
	
	if(n!=NULL)
	{
		cout << "请输入您的密码..." << endl;		//	cout << "Please input Your PassWord..." << endl;
		
		cin >> passWord;
		
		if(passWord == n->get_Record()->get_passWord())
		{
			if(n->get_Record()->get_flag() == 1)//1为正常状态
			{
				cout << "请输入您的取款金额..." << endl;	//	cout << "Please input Withdraw data..." << endl;
				
				cin >> cash;
				
				if(cash <= n->get_Record()->get_balance())
				{
					cash = n->get_Record()->get_balance() - cash;
					
					n->get_Record()->set_balance(cash);
					
					cout << "取款成功!按任意键返回..." << endl;		//cout<<"Withdraw success ..."<<endl;

					getchar(); 
					
					return; 
				}
				else
				{
					cout << "取款金额大于余额!按任意键返回..." << endl;		//cout << "Balance shortage!!! Please re-enter withdraw data..." << endl;

					getchar(); 
					
					return; 
				}
			}
			else
			{
				cout << "该账户已销户!按任意键返回..." << endl;

				getchar(); 
					
				return; 
			}
		}
		else
		{
			cout << "密码不正确!按任意键返回..." << endl;

			getchar(); 
					
			return; 
		}
	}
	else
	{
		cout << "查无此账户!按任意键返回..." << endl;

		getchar(); 
					
		return; 
	}
}

void Bank::transfer_account()
{
	string passWord;
	
	double cash;
	
	int other_number;
	
	//1.获取用户输入的账号信息
	
	int number = this->get_number();
	
	//查找账户位置
	
	Node *n = this->list->find_node(number);
	
	if(n != NULL)
	{
		cout << "Please input Your PassWord..." << endl;
		
		cin >> passWord; 
		
		if(passWord == n->get_Record()->get_passWord())
		{
			if(n->get_Record()->get_flag() == 1)//1为正常状态
			{
				cout << "请输入您要转账的账号..." << endl;
				
				cin >> other_number;
				
				//查找账户位置
				
				Node *n_other = this->list->find_node(other_number);
				
				if(n != NULL)
				{
					if(n_other->get_Record()->get_flag() == 1)//1为正常状态
					{
						cout << "请输入转账金额..." << endl;
						
						cin >> cash;
						
						if(cash <= n->get_Record()->get_balance())
						{
							n_other->get_Record()->set_balance((n_other->get_Record()->get_balance() + cash));
							
							n->get_Record()->set_balance((n->get_Record()->get_balance() - cash));
							
							cout << "转账成功!按任意键返回..." <<endl;
							
							getchar();  
							
							return;  
						}
						else
						{
							cout << "您的转账金额大于大于存款金额!转账非法!按任意键返回..." << endl;

							getchar(); 
					
							return; 
						}
						
					}
					else
					{
						cout << "该账户已销户!按任意键返回..." << endl;

						getchar(); 
					
						return; 
					}
				}
				else
				{
					cout << "查无此账户!按任意键返回..." << endl;

					getchar(); 
					
					return; 
				}
			}
			else
			{
				cout << "该账户已销户!按任意键返回..." << endl;

				getchar(); 
					
				return; 
			}
		}
		else
		{
			cout << "密码不正确!按任意键返回..." << endl;

			getchar(); 
					
			return; 
		}
	}
	else
	{
		cout << "查无此账户!按任意键返回..." << endl;

		getchar(); 
					
		return; 
	}      
}


LinkList.cpp

#include "LinkList.h"
#include <iostream>
using namespace std;

LinkList::LinkList()
{
	this->head = NULL;

	this->len = 0;
}

LinkList::~LinkList()
{
	Node *p,*q=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->get_next();
	}

	p->set_next(node);

	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;
}

void LinkList::display_LinkList()
{
	cout<<"显示链表..."<<endl;		//cout<<"Print LinkList..."<<endl;

	//cout<<"Head:"<<this->head<<endl;

	Node *p = this->head;

	if(p == NULL)
	{
		cout << "链表为空..." << endl;		//cout << "LinkList is NULL..." << endl;	

		cout << "len:" << this->len << endl;

		cout << "链表结束(显示完毕)..." << endl;		//cout << "End of LinkList..." << endl;		

		return;
	}
	while (p != NULL)
	{
		p->display_Node();

		p = p->get_next();
	}
	cout << "len:" << this->len << endl;

    cout << "链表结束(显示完毕)..." << endl;		//cout << "End of LinkList..." << endl;		


	return;
}


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::display_Node()
{
	cout << "打印节点(元素)..." << endl;		//cout<<"Print Node elements..."<<endl;

	if(this->record != NULL)
	{

	//cout<<"Record*:"<<this->record->display_Record()<<endl;

	this->record->display_Record();

	cout << "next*:" << this->next << endl;

	}
	else
	{
		cout << "节点所指向的记录是空的..." << endl;		//cout<<"Record is NULL..."<<endl;
	}

	cout << "节点显示结束..." << endl;				//cout<<"End of Node..."<<endl;
}

/*  void display_Node()为了易于了解也可以改成如下结构
void display_Node()
{
	Record *r=this->record;
	cout<<"Print Node elements..."<<endl;
	if(this->record!=NULL)
	{
	r->display_Record();

		cout<<"next*:"<<this->next<<endl;
	}
	else
	{
		cout<<"Record is NULL..."<<endl;
	}
	cout<<"End of Node..."<<endl;
}
*/


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 Elements..." << endl;

	cout << "Number:" << endl;

	cout << "UserName:" << this->userName << endl;

	cout << "PassWord:" << this->passWord << endl;

	cout << "Balance:" << this->balance << endl;

	cout << "Flag:" << this->flag << endl;

	cout << "End of Record..." << endl;
}


TestBank.cpp

#include "Bank.h"
#include <iostream>
using namespace std;

int main()
{
	Bank *b=new Bank();
	b->open_account();
	b->find_account();
	cout<<endl;

	b->open_account();
	b->find_account();
	cout<<endl;

	b->transfer_account();
	b->find_account();
	b->find_account();
	cout<<endl;

	return 0;
}


运行结果:

经验积累:

1.我学会了如何利用类间的依赖关系设计出银行(Bank)类;

2.我对指针的了解更全面了。

原文链接:https://www.f2er.com/javaschema/286663.html

猜你在找的设计模式相关文章