实训项目报告书
项目完成人: 刘镇
一、需求分析:
用例图:
二、概要设计:
类间关系图:
三、详细设计:
类图:
四、编码实现:
//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 << "打印用户信息中..." << endl;
cout << "+---------------------------------------+" << endl;
cout << "|" << "账号" << '\t' << "|" << "户名" << '\t' << "|" << "密码" << '\t' << "|" << "余额" << '\t' << "|" << "状态" << '\t' << "|"<< endl;
cout << "|----------------------------------------" << endl;
cout << "|" << this->number << '\t' << "|" << this->userName << '\t' << "|" << this->passWord << '\t' << "|" << this->balance << '\t' << "|" ;
if(this->flag == 1)
{
cout << "正常";
}
else
{
cout << "销户";
}
cout << '\t' << "|" << endl;
cout << "|---------------------------------------+" << endl;
}
//Node.h:
#ifndef HEADER_NODE
#define HEADER_NODE
#include "Record.h"
using namespace std;
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
//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;
if(this->record != NULL) //当出现空链表时执行
{
Record * r = this->record;
r->display_Record();
}
else
{
cout << "记录为空..." << endl;
}
cout << "Next * : " << this->next << endl;
cout << "结点结束..." << endl;
}
//Linklist.h:
#ifndef HEADER_LINKLIST
#define HEADER_LINKLIST
#include "Node.h"
using namespace std;
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->get_next();
}
p->set_next(node);
this->len++;
return;
}
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; //return NULL;
}
void Linklist::display_Linklist()
{
cout << "打印用户信息中..." << endl;
Node * p = this->head;
if(p == NULL)
{
cout << "链表为空..." << endl;
cout << "链长: " << this->len << endl;
cout << "记录结束..." << endl;
return;
}
while(p != NULL)
{
p->display_Node();
p = p->get_next();
}
cout << "链长: " << this->len << endl;
cout << "链表结束..." << 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_linklist();
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 close_account();
void find_account();
void deposit();
void withdraw();
void transfer_account();
};
#endif
//Bank.cpp:
#include<conio.h>
#include "Bank.h"
#include <iostream>
using namespace std;
Bank::Bank()
{
this->list = this->make_linklist();
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_linklist()
{
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_passWord(password);
r->set_userName(username);
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;
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 password;
cout << "请输入密码..." << endl;
cin >> password;
cout << "密码: " << password << endl;
return password;
}
double Bank::get_balance()
{
double balance;
cout << "请输入金额..." << endl;
cin >> balance;
cout << "金额:" << 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();
// 使用输入数据创建Record对象
Record * r = this->make_record(userName, passWord, balance);
// 创建Node结点,封装Record对象
Node * n = this->list->make_node(r);
// 将Node结点插入到Linklist链表
this->list->insert_node(n);
//显示账户信息
r->display_Record();
cout << "开户成功!按任意键退出..." << endl;
getch();
return;
}
//销户功能:
void Bank::close_account()
{
// 获取销户账号
int Number = this->get_number();
// 查找账户
Record * back_record = this->find_record(Number);
if(back_record == NULL)
{
cout << "查无此帐户!按任意键退出..." << endl;
getchar();
return;
}
//获取账户密码,判断密码是否正确
string passWord = this->get_passWord();
if(passWord != back_record->get_passWord())
{
cout << "密码不正确!按任意键退出..." << endl;
getchar();
return;
}
//判断账户状态
if(back_record->get_flag() == 0)
{
cout << "该帐户已经销户,不能再次销户!" << endl;
getchar();
return;
}
//获取用户输入的选择信息,确认是否销户
string userAnswer;
cout << "确认销户? [1:销户| 其他:不销户] " << endl;
cin >> userAnswer;
//销户
if(userAnswer == "1")
{
back_record->set_flag(0);
back_record->set_balance(0);
back_record->display_Record();
cout << "销户成功!按任意键退出..." << endl;
getchar();
return;
}
cout << "销户用户取消销户!" << endl;
return;
}
//查询功能:
void Bank::find_account()
{
// 获取查询账号
int Number = this->get_number();
// 查找账户
Record * back_record = this->find_record(Number);
if(back_record == NULL)
{
cout << "查无此帐户!按任意键退出..." << endl;
getch();
return;
}
//获取账户密码,判断密码是否正确
string passWord = this->get_passWord();
if(passWord != back_record->get_passWord())
{
cout << "密码不正确!按任意键退出..." << endl;
getch();
return;
}
//显示查询到的用户信息
back_record->display_Record();
cout << "查询成功!按任意键退出..." << endl;
getch();
return;
}
//存款功能:
void Bank::deposit()
{
//获取存款账号
int Number = this->get_number();
//查找账号
Record * back_record = this->find_record(Number);
//判断用户是否存在
if(back_record == NULL)
{
cout << "查无此帐户!按任意键退出..." << endl;
getch();
return;
}
//显示账户信息
back_record->display_Record();
//判断账户是否销户
if(back_record->get_flag() == 0)
{
cout << "该帐户已经销户!按任意键退出..." << endl;
getch();
return;
}
double cash = 0;
cout << "输入存款金额: " << endl;
cin >> cash;
cout << "存款金额为:" << cash << endl;
cash = back_record->get_balance() + cash;
back_record->set_balance(cash);
//显示账户信息
back_record->display_Record();
cout << "存款结束! 按任意键退出..." << endl;
getch();
return;
}
//取款功能:
void Bank::withdraw()
{
// 获取取款账号
int Number = this->get_number();
// 查找账户
Record * back_record = this->find_record(Number);
if(back_record == NULL)
{
cout << "查无此帐户!按任意键退出..." << endl;
getch();
return;
}
//获取账户密码,判断密码是否正确
string passWord = this->get_passWord();
if(passWord != back_record->get_passWord())
{
cout << "密码不正确!按任意键退出..." << endl;
getch();
return;
}
// 确定用户账户状态
if(back_record->get_flag() == 0)
{
cout << "该帐户已经销户!按任意键退出..." << endl;
getch();
return;
}
double cash = 0;
cout << "请输入取款金额:" << endl;
cin >> cash;
if(cash > back_record->get_balance())
{
cout << "账户余额不足!按任意键退出..." << endl;
getch();
return;
}
cash = back_record->get_balance() - cash;
back_record->set_balance(cash);
//显示账户信息
back_record->display_Record();
cout << "取款结束! 按任意键退出..." << endl;
getch();
return;
}
//转账功能:
void Bank::transfer_account()
{
// 获取转入账号
int number;
cout << "请输入转出账号:" << endl;
cin >> number;
// 查找账号
Record * back_record = this->find_record(number);
if(back_record == NULL)
{
cout << "查无此帐户!按任意键退出..." << endl;
getch();
return;
}
//获取账户密码,判断密码是否正确
string passWord;
cout << "请输入账户密码:" << endl;
cin >> passWord;
if(passWord != back_record->get_passWord())
{
cout << "密码不正确!按任意键退出..." << endl;
getch();
return;
}
// 确定用户账户状态
if(back_record->get_flag() == 0)
{
cout << "该帐户已经销户!按任意键退出..." << endl;
getch();
return;
}
//获取转账账号
int transfer_number;
cout << "请输入转入账号:" << endl;
cin >> transfer_number;
//查找转账账户
Record * transfer_record = this->find_record(transfer_number);
if(back_record == NULL)
{
cout << "查无此帐户!按任意键退出..." << endl;
getch();
return;
}
// 确定用户账户状态
if(back_record->get_flag() == 0)
{
cout << "该帐户已经销户!按任意键退出..." << endl;
getch();
return;
}
//获取转账金额
double cash = 0;
cout << "请输入转入金额:" << endl;
cin >> cash;
//判断转账金额是否合法
if(cash > back_record->get_balance())
{
cout << "账户余额不足!按任意键退出..." << endl;
getch();
return;
}
//资金转账
transfer_record->set_balance(transfer_record->get_balance() + cash);
back_record->set_balance(back_record->get_balance() - cash);
//显示当前帐户信息及转账账户信息
cout << "转出帐户信息:" << endl;
back_record->display_Record();
cout << "转入账户信息:" << endl;
transfer_record->display_Record();
//转账结束
cout << "转账结束! 按任意键退出..." << endl;
getch();
return;
}
//Main.cpp:
#include "Bank.h"
#include <iostream>
using namespace std;
void welcome();
void login(Bank * bank);
void mainmenu(Bank * bank);
int main()
{
Bank * bank = new Bank();
welcome();
login(bank);
return 0;
}
void welcome()
{
cout << "+--------------------------------------+" << endl;
cout << "| |" << endl;
cout << "| 欢迎使用储蓄综合业务平台 |" << endl;
cout << "| |" << endl;
cout << "+--------------------------------------+" << endl;
}
void mainmenu(Bank * bank)
{
int choice = -1;
do
{
cout << endl;
cout << "+---------------------------------+" << endl;
cout << "| 开户请按1 取款请按5 |" << endl;
cout << "| 销户请按2 转账请按6 |" << endl;
cout << "| 查询请按3 退出请按0 |" << endl;
cout << "| 存款请按4 |" << endl;
cout << "+---------------------------------+" << endl;
cout << "请输入您的选择: " << endl;
cin >> choice;
switch(choice)
{
case 0:
exit(0);
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_account();
break;
default:
cout << "无效选项!" << endl;
break;
}
}
while(true);
}
void login(Bank * bank)
{
string admin_userName;
string admin_passWord;
for(int i = 1; i <= 3; ++i)
{
cout << endl;
cout << "请输入您用的用户名: " << endl;
cin >> admin_userName;
cout << "请输入您的密码: " << endl;
cin >> admin_passWord;
if((admin_userName == "admin") && (admin_passWord == "admin"))
{
mainmenu(bank);
}
else
{
if(i < 2)
{
cout << "用户名或密码输入错误,请重新输入!" << endl;
}
else
{
cout << "连续输错用户名或密码,退出系统!" << endl;
}
}
}
}
五、测试:
//TestRecord.cpp:
#include "Record.h"
#include <iostream>
using namespace std;
int main()
{
Record * record = new Record();
record->display_Record();
record->set_number(10001);
record->set_userName("liuzhen");
record->set_passWord("123456");
record->set_balance(2);
record->set_flag(1);
record->display_Record();
return 0;
}
//TestNode.cpp:
#include "Node.h"
#include <iostream>
using namespace std;
int main()
{
Node * node = new Node();
node->display_Node();
Record * r = new Record();
r->set_number(10001);
r->set_passWord("123");
r->set_userName("zhangsan");
r->set_balance(10000);
r->set_flag(1);
node->set_record(r);
node->display_Node();
return 0;
}
//TestLinklist.cpp:
#include "Linklist.h"
#include <iostream>
using namespace std;
int main()
{
Linklist * list = new Linklist();
list->display_Linklist();
cout << endl;
Record * r1 = new Record();
r1->set_number(10001);
r1->set_passWord("123");
r1->set_userName("zhangsan");
r1->set_balance(10000);
r1->set_flag(1);
Node * n1 = list->make_node(r1);
list->insert_node(n1);
list->display_Linklist();
cout << endl;
Record * r2 = new Record();
r2->set_number(10002);
r2->set_passWord("456");
r2->set_userName("lisi");
r2->set_balance(20000);
r2->set_flag(2);
Node * n2 = list->make_node(r2);
list->insert_node(n2);
list->display_Linklist();
cout << endl;
Node * n3 = list->find_node(10001); // 先判断是否查找到
if(n3 == NULL)
{
cout << "Not Found..." << endl;
}
else
{
n3->display_Node();
}
return 0;
}
//TestBank.cpp:
#include "Bank.h"
#include <iostream>
using namespace std;
void welcome();
void mainmenu(Bank * bank);
int main()
{
Bank * bank = new Bank();
cout << "开户操作:" << endl;
bank->open_account();
cout << "存款操作:" << endl;
bank->deposit();
cout << "取款操作:" << endl;
bank->withdraw();
cout << "查询操作:" << endl;
bank->find_account();
cout << "转账操作:" << endl;
bank->transfer_account();
cout << "销户操作:" << endl;
bank->close_account();
return 0;
}
心得体会:
感觉似乎在完成开户功能时,余下的任何代码都是水到渠成的,也在体会类封装与原来的面向过程时所体现的不同。明显的感觉类更加明确清晰,自己在做什么一目了然。而且即使是我做的主函数套用在同学的代码中依然没有任何错误,这很赏心悦目。