银行账户管理程序
问题描述:
设计一个银行账户管理程序,账户的信息有账号(唯一)、姓名、余额、身份证号码、单位、电话号码、地址等,允许用户进行如下操作:开户、销户、存款、取款、转账、查询,一个用户可以有多个户头,账户的数值没有上限。
基本要求
程序运行时,可以由用户选择进行何种操作,开户操作要求输入用户信息后自动获取账号,用户销户后账号被回收,并且该账号可以继续分配给其它账户,不允许用户透支,根据姓名或账号可以进行用户的信息查询,所有的账户信息应存放到一个文件中,可以随时的访问和更新。
测试数据
程序应输入不少于10人的账户信息,应考虑到人员同名的情况。
实现提示
可定义一个账户类存放账户信息以及执行相应的操作,可以用一个链表类来管理账户。
选作内容
在上述程序的基础上,添加联名账户(一个联名账户有两个拥有者)的管理 。
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
#include<strstream>
#include<iomanip>
#include<conio.h>
using namespace std;
class Function;
class User//定义一个名为用户的类
{
public:
void get(User&);//显示信息
friend class Function;//友元化名为功能的类
ostream& print(ostream& os);
//friend ostream & operator<<(ostream & o, const User & u);
friend istream& operator>>(istream& i, User& u);
void PassWord();//设置密码的函数;
bool Login(User&);//登陆成功与否
void SaveFile(User&);
vector<string> StrFunction;
User() {}//构造函数
User(int A, string N, int F, string I, string Ad, string T) :Accout(A), Name(N), Fmoney(F), ID(I), Address(Ad), Telephone(T) {}
int Accout;
string Name;
int Fmoney;
string ID;
string Address;
string Telephone;
char Pword1[9];
char Pword2[9];
};
//用户信息
class Function
{
public:
void Saving(User&);//存钱功能
void Drawing(User&);//取钱功能
void Balance(User&);//查询余额功能
void Record(User&);//查询记录功能
void Delete(User&);//注销功能
void zhuanzhang(User&);//转账功能
private:
int SaveAndDrawMoney;
};
//存钱功能
void Function::Saving(User& u)
{
system("cls");
cout << "\n\n\n\t\t*********************************************\n" << endl;
cout << "\t\t请输入存款金额:";
string str1("\n\t\t现存RMB 钞+ ");
cin >> SaveAndDrawMoney;
u.Fmoney += SaveAndDrawMoney;
strstream ss;
string str2;
ss << u.Accout;
ss >> str2;
string frist = "Record" + str2 + ".txt";
const char* RecordFile = frist.c_str();
ofstream outfile(RecordFile, ios::out | ios::app);//将信息保存在文件里
outfile << str1 << SaveAndDrawMoney << endl;
outfile.close();
outfile.clear();
cout << "\n\n\t\t操作成功,单击任何键返回主菜单!";
_getch();
}
//取钱功能
void Function::Drawing(User& u)
{
system("cls");
cout << "\n\n\n\t\t*********************************************\n" << endl;
cout << "\t\t请输入取款金额:";
string str1("\n\t\t现取RMB 钞- ");
string str2;
int k = 0;
while (k <= 3)
{
cin >> SaveAndDrawMoney;
u.Fmoney -= SaveAndDrawMoney;
if (u.Fmoney >= 0)
{
system("cls");
cout << "\n\n\n\t\t正在出钞,请稍等!" << endl;
strstream ss;
ss << u.Accout;
ss >> str2;
string frist = "Record" + str2 + ".txt";
const char* RecordFile = frist.c_str();
ofstream outfile(RecordFile, ios::out | ios::app);//将信息保存在文件里
outfile << str1 << SaveAndDrawMoney << endl;
outfile.close();
outfile.clear();
break;
}
else
{
k++;
if (k >= 3)
{
system("cls");
cerr << "\n\n\n\t\t很抱歉,你重复输入错误多次" << endl
<< "\n\t\t正在退卡中……" << endl;
exit(0);
}
system("cls");
u.Fmoney += SaveAndDrawMoney;
cerr << "\n\n\n\t\t对不起,你输入的金额超出有效金额。" << endl
<< "\n\t\t 请重新输入: ";
}
}
cout << "\n\n\t\t操作成功,单击任何键返回主菜单!";
_getch();
}
//转账功能
void Function::zhuanzhang(User& u)
{
system("cls");
int others;
cout << "\n\n\n\t\t*********************************************\n" << endl;
cout << "\t\t请输入被转款人的卡号:";
cin >> others;
system("cls");
cout << "\n\n\n\t\t*********************************************\n" << endl;
cout << "\t\t请输入转账金额:";
string str1("\n\t\t转账RMB 钞- ");
string str2;
int k = 0;
//转账功能的实现
while (k <= 3)
{
cin >> SaveAndDrawMoney;
u.Fmoney -= SaveAndDrawMoney;
if (u.Fmoney >= 0)
{
system("cls");
cout << "\n\n\n\t\t正在转账,请稍等!" << endl;
strstream ss;
ss << u.Accout;
ss >> str2;
string frist = "Record" + str2 + ".txt";
const char* RecordFile = frist.c_str();//将string类型的字符串转化成const char类型的字符串
ofstream outfile(RecordFile, ios::out | ios::app);
outfile << str1 << SaveAndDrawMoney << endl;
outfile.close();
outfile.clear();
break;
}
else
{
k++;
if (k >= 3)
{
system("cls");
cerr << "\n\n\n\t\t很抱歉,你重复输入错误多次" << endl
<< "\n\t\t正在退卡中……" << endl;
exit(0);
}
system("cls");
u.Fmoney += SaveAndDrawMoney;
cerr << "\n\n\n\t\t对不起,你输入的金额超出有效金额。" << endl
<< "\n\t\t 请重新输入: ";
}
}
cout << "\n\n\t\t操作成功,单击任何键返回主菜单!";
_getch();
}
//注销功能
void Function::Delete(User& u) {
string str1 = "该卡已被注销";
string str2;
strstream ss;
ss << u.ID;
ss >> str2;
string frist = "Record" + str2 + ".txt";
const char* RecordFile = frist.c_str();
ofstream outfile(RecordFile, ios::out | ios::app);
outfile << str1 << SaveAndDrawMoney << endl;
outfile.close();
outfile.clear();
}
//查询余额功能
void Function::Balance(User& u)
{
system("cls");
cout << "\n\n\n\t\t*********************************************\n" << endl;
cout << "\t\t你当前的余额是:";
cout << "\t\t" << u.Fmoney << endl;
cout << "\n\t\t*********************************************\n" << endl;
cout << "\n\t\t操作成功,单击任何键返回主菜单!";
_getch();
}
//查询记录功能
void Function::Record(User& u)
{
string str1, str2;
system("cls");
cout << "\n\n\n\t\t*********************************************\n" << endl;
cout << "\t\t摘要币种钞汇存/取款金额" << endl;
strstream ss;
ss << u.Accout;
ss >> str2;
string frist = "Record" + str2 + ".txt";
const char* RecordFile = frist.c_str();
ifstream infile(RecordFile, ios::in);//将信息保存在文件里
if (!infile)
{
cout << "\n\t\t没有任何历史记录!" << endl;
_getch();
return;
}
while (!infile.eof())//判断文件是否运行到结尾
{
getline(infile, str1);
cout << "\n\t\t" << str1 << endl;
}
cout << "\n\t\t*********************************************\n" << endl;
cout << "\n\t\t操作成功,单击任何键返回主菜单!";
_getch();
}
//显示用户信息
ostream& User::print(ostream& os)
{
system("cls");
os << "\n\n\n\t\t*********************************************\n" << endl;
os << "\n\t\t用户卡号:" << Accout << endl;
os << "\n\t\t姓名: " << Name << endl;
os << "\n\t\t账户金额:" << Fmoney << endl;
os << "\n\t\t身份证号:" << ID << endl;
os << "\n\t\t地址: " << Address << endl;
os << "\n\t\t联系电话:" << Telephone << endl;
os << "\n\t\t密码: " << Pword1 << endl;
os << "\n\t\t*********************************************\n" << endl;
cout << "\n\t\t操作成功,单击任何键返回主菜单!";
_getch();
return os;
}
void User::SaveFile(User& u)
{
int filename = u.Accout;
string frist;
string last(".txt");
strstream ss;
ss << filename;
ss >> frist;
frist += last;
const char* UserID = frist.c_str();
ofstream outfile(UserID, ios::out | ios::trunc);
outfile << Accout << ' ' << Name << ' ' << Fmoney << ' ' << ID << ' ' << Address << ' ' << Telephone << ' ' << Pword1 << endl;
outfile.close();
}
//判断输入的密码是否正确
bool User::Login(User& u)
{
int acc;
string pwd;
cout << "\n\t\t登陆用户:" << endl;
cout << "\t\t卡号:";
cin >> acc;
cout << "\t\t密码:";
cin >> pwd;
int filename = acc;
string frist;
string last(".txt");
strstream ss;
ss << filename;
ss >> frist;
frist += last;
const char* UserID = frist.c_str();
ifstream infile(UserID, ios::in);
while (infile >> (*this))
{
if (u.Accout == acc)
{
string str;
str = Pword1;
if (str == pwd)
{
return 1;
}
}
}
return 0;
}
istream& operator>>(istream& i, User& u)
{
i >> u.Accout >> u.Name >> u.Fmoney >> u.ID >> u.Address >> u.Telephone >> u.Pword1;
return i;
}
//确认密码
void User::PassWord()
{
int num = 0;
while (num < 3)
{
cout << "\n\t\t请输入八位储蓄密码:";
for (int i = 0; i < 8; i++)
{
Pword1[i] = _getch(); cout << "*";
}
Pword1[8] = '\0';
cout << endl;
cout << "\n\t\t请再一次确认密码:";
for (int i = 0; i < 8; i++)
{
Pword2[i] = _getch(); cout << "*";
}
cout << endl;
Pword2[8] = '\0';
//判断两次的密码是否相同
if (strcmp(Pword1, Pword2) == 0)
{
cout << "\n\n\t\t密码确认成功,单击任何键进入主菜单";
_getch();
return;
}
else
{
num++;
cout << "\n\t\t你输入的密码错误,请重新输入!\n" << endl;
}
if (num == 3)
{
system("cls");
cerr << "\n\n\n\t\t对不起,你三次输入不正确\n" << endl
<< "\t\t感谢你的使用,再见! " << endl;
exit(0);
}
}
}
//输出用户的信息
void User::get(User& u)
{
system("cls");
cout << "\n\n\n\t\t请输入用户信息:\n" << endl;
cout << "\t\t*********************************************\n" << endl;
cout << "\n\t\t卡号:";
cin >> Accout;
cout << "\n\t\t姓名:";
cin >> Name;
cout << "\n\t\t账户金额:";
cin >> Fmoney;
cout << "\n\t\t身份证号:";
cin >> ID;
cout << "\n\t\t地址:";
cin >> Address;
cout << "\n\t\t联系电话:";
cin >> Telephone;
u.PassWord();
cout << "\n\t\t*********************************************\n" << endl;
}
//退出系统
void Sign()
{
system("cls");
cout << "\n\n\n\n\n\n\t\t\t谢谢您使用银行管理系统\t \n\n" << endl;
cout << "\t\t*********************************************\n" << endl;
cout << "\t\t 姓名: XXXXX\n\n"
<< "\t\t 班级: XXXXXXXXXX\n\n"
<< "\t\t 学号: XXXXXXXX\n\n" << endl;
cout << "\n\t\t 已经成功退出" << endl
<< "\n\t\t 欢迎你的光临!" << endl;
}
char custom(User& u)
{
char x;
int k = 0;
system("cls");
cout << "\n\n\n\t\t※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"
<< "\t\t※ ※\n"
<< "\t\t※ 欢迎使用储蓄管理系统 ※\n"
<< "\t\t※ ※\n"
<< "\t\t※ 请用户在使用前选择开户: ※\n"
<< "\t\t※ ※\n"
<< "\t\t※ 请选择: ※\n"
<< "\t\t※ 1:注册开户 ※\n"
<< "\t\t※ 2:用户登录 ※\n"
<< "\t\t※ 其他:退出 ※\n"
<< "\t\t※ ※\n"
<< "\t\t※ ※\n"
<< "\t\t※※※※※※※※※※※※※※※※※※※※※※※※※※※\n" << endl;
cout << "\n\t\t请选择操作方式:";
cin >> x;
//系统1
if (x == '1')
{
u.get(u);
}
//系统2
else if (x == '2')
{
while (k < 3)
{
if (u.Login(u)) //判断登陆是否成功
{
return 0;
}
else
{
system("cls");
cerr << "\n\n\n\t\t\t输入有误!\n" << endl
<< "\t\t\t请重新输入:" << endl;
k++;
}
while (k >= 3)
{
system("cls");
cerr << "\n\n\n\t\t对不起,你三次输入不正确\n" << endl
<< "\n\t\t感谢你的使用,再见!\n" << endl;
exit(0);
}
}
}
else
{
Sign(); exit(0);
}
return 0;
}
//菜单系统
char menu()
{
int k = 0;
char a[3], c, i;
system("cls");
cout << "\n\n\n\t\t 主菜单 \n\n";
cout << "\t\t*********************************************\n"
<< "\t\t* *\n"
<< "\t\t* 操作方式: *\n"
<< "\t\t* 1.存款 2.取款 *\n"
<< "\t\t* *\n"
<< "\t\t* 3.查询余额 4.查询历史记录 *\n"
<< "\t\t* *\n"
<< "\t\t* 5.用户信息 6.保存并退出 *\n"
<< "\t\t* *\n"
<< "\t\t* 7.注销账户 8.帮助 *\n"
<< "\t\t* *\n"
<< "\t\t* 9.转账 *\n"
<< "\t\t*********************************************\n" << endl;
while (k < 3)
{
cout << "\n\t\t请选择操作方式: ";
cin >> a;
//判断输入是否合法
if (strlen(a) == 1)
{
for (i = '1'; i <= '9'; i++)
{
c = i;
if (c == a[0]) return c;
}
}
else
{
system("cls");
cerr << "\n\n\n\t\t\t输入有误!\n" << endl
<< "\t\t\t请重新输入:" << endl;
k++;
}
while (k >= 3)
{
system("cls");
cerr << "\n\n\n\t\t对不起,你三次输入不正确\n" << endl
<< "\n\t\t感谢你的使用,再见!\n" << endl;
exit(0);
}
}
return c;
}
//相关须知
void help() {
system("cls");
cout << " *** 该系统实现了开户、销户、存钱、取钱等一系列操作 ***" << endl << endl;
cout << " *** 全体设计成员衷心希望你能够在使用期间有着良好体验 ***" << endl << endl;
cout << " *** 如果遇到什么问题,请拨打相关工作人员的电话:17704125165 ***" << endl << endl;
cout << " *** 合肥工业大学2022级计科五班 计算机与信息学院 卢冠辰 高培骏 张卫 ***" << endl << endl;
cout << " *** 该银行账目管理系统大作业项目参考了以下资料 ***" << endl << endl;
cout << " ***b站部分讲解视频、优快云上的若干C++项目指南以及leetcode和牛客的相关讨论***" << endl << endl;
system("pause");
}
//功能选择
char Choose(User& u, Function& F)
{
lu_houhui:
char c = menu();
while (c != '6')
{
switch (c)
{
case '1':F.Saving(u); c = menu(); break;
case '2':F.Drawing(u); c = menu(); break;
case '3':F.Balance(u); c = menu(); break;
case '4':F.Record(u); c = menu(); break;
case '5':u.print(cout); c = menu(); break;
case '7':lu_shiwu:
char lu_res;
system("cls");
cout << "\t\t*********************************************\n"
<< "\t\t* *\n"
<< "\t\t* 您真的想好了吗 *\n"
<< "\t\t* 注销账户后,该银行卡将会失效 *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* 1.后悔了? *\n"
<< "\t\t* *\n"
<< "\t\t* 2.决定好了? *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t*********************************************\n" << endl;
cin >> lu_res;
system("cls");
//让用户确认是否要注销
if (lu_res == '1') { goto lu_houhui; }
else if (lu_res == '2') {
u.ID = INT_MAX;
u.Accout = INT_MAX;
u.Name = " ";
//注销账户
F.Delete(u);
cout << "\t\t*********************************************\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* 注销成功! *\n"
<< "\t\t* *\n"
<< "\t\t* 正在退卡中...... *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t*********************************************\n" << endl;
system("pause");
goto lu_xiaohu;
}
else {
cout << "\t\t*********************************************\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* 输入格式有误,请重新输入! *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t* *\n"
<< "\t\t*********************************************\n" << endl;
system("pause");
goto lu_shiwu;
}
case '8':help(); c = menu(); break;
case '9':F.zhuanzhang(u); c = menu(); break;
}
}
//销户成功
lu_xiaohu:
u.SaveFile(u);
Sign();
return 0;
}
int main()
{
User u;
Function F;
custom(u);
Choose(u, F);
system("pause");
return 0;
}