C++银行管理系统

银行管理系统,主要可以实现存储,开户,销户,转账,查询,挂失,解挂等功能,本系统采用文件存储,一个文件保存银行工作者的姓名和密码,另一个保存存款人员的所有信息,这里面主要是用了友元类,实现一个类 对另一个类的私有成员的调用。不管怎莫说,这也是我第一个c++稍微大一点的程序代码,我还是比较满意,开始被文件读写给难住了,后来学习了一下,感觉比还好用!废话不多说,上代码

#ifndef __BANK_H
#define __BANK_H
#include "User.h"

#include <cstring>
#include <string>
#include <vector>

using namespace std;
const int upNum = 1000;

class Bank
{
public:
	Bank();//从文件中读数据到数组中
	~Bank();//写入文件
	void work();//业务驱动
	void openAccount();//开户
	void cancelAccount();//注销账户
	void save();//存款
	void withdraw();//取款
	void showAccount();//查询余额
	void transferAccounts();//转账
	void reportLoss();//挂失
	void cancelLoss();//解挂
	void updataPassword();//更改密码
	int getUser();//输入账号查询用户
private:
	int N;//用户数目
	User users[upNum];
};

int pass();
int chooseInMenu();
int inputPassword();

#endif


#pragma once
#include <string>
#include <iostream>

using namespace std;
class bank;

class User
{
public:
	User()
	{}
	void setUser(int acc, string nam, int pw, double bal, int sta);
	void showName();
	void showBalance(string prompt);
	bool passwordIsright();
	bool isNormalUser();
	friend class Bank;
private:
	int account;//账号
	int password;//密码
	string name;//用户名
	double balance;//账户余额
	int status;//0.正常 1.挂失 2 销户
};


#include "User.h"
#include "bank.h"

void User::setUser(int acc, string nam, int pw, double bal, int sta)
{
	this->account = acc;
	this->name = nam;
	this->password = pw;
	this->balance = bal;
	this->status = sta;
}

void User::showName()
{
	cout << "账户姓名:" << name << endl;
}

void User::showBalance(string prompt)
{
	cout << prompt << "余额:" << balance << endl;
}

bool User::passwordIsright()
{
	int i = 0;
	while (password / 10 > 10)
		i++;
	if (i > 0)
		return true;
	else
	{
		return false;
	}
}

bool User::isNormalUser()
{
	if (status == 0)
		return true;
	else
		return false;
}

#include "bank.h"


#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<ctype.h>
using namespace std;

Bank::Bank()
{
	ifstream infile("account.txt", ios::in);
	if (!infile)
	{
		cerr << "open error" << endl;

		exit(1);
	}
	int i = 0;
	int acc;
	string nam;
	int pw;
	double bal;
	int sta;
	while (infile >> acc >> nam >> pw >> bal >> sta)
	{
		users[i].setUser(acc, nam, pw, bal, sta);
		i++;
	}
	N = i;
	infile.close();
}

Bank::~Bank()
{
	ofstream outfile("account.txt", ios::out);
	if (!outfile)
	{
		cerr << "open error" << endl;
		exit(1);
	}

	int i;
	for (i = 0; i < N; i++)
	{
		outfile << users[i].account << " ";
		outfile << users[i].name << " ";
		outfile << users[i].password << " ";
		outfile << users[i].balance << " ";
		outfile << users[i].status << " ";
	}
	outfile.close();
}

void Bank::work()
{
	int iChoice;

	while (1)
	{
		iChoice = chooseInMenu();
		switch (iChoice)
		{
		case 1:openAccount();break;
		case 2:cancelAccount();break;
		case 3:save();break;
		case 4:withdraw();break;
		case 5:getUser();break;
		case 6:transferAccounts();break;
		case 7:reportLoss();break;
		case 8:cancelLoss();break;
		case 9:updataPassword();break;
		default:
			break;
		}
	}
}

void Bank::openAccount()
{
	if (N >= upNum)
	{
		cout << "用户已达上限" << endl;
		return;
	}
	int acc;
	string nam;
	int pw;
	double bal;
	int sta;

	cout << "正在开户" << endl;
	acc = 1001 + N;
	cout << "账号: " << acc << endl;
	cout << "户主姓名:";
	cin >> nam;
	int iPass1, iPass2;
	cout << "密码:";
	iPass1 = inputPassword();
	cout << "确认密码";
	iPass2 = inputPassword();
	if (iPass1 != iPass2)
	{
		cout << "密码输入不一致" << endl;
		return;
	}
	pw = iPass1;
	cout << "存入金额";
	cin >> bal;
	users[N].account = acc;
	users[N].password = pw;
	users[N].status = 0;
	users[N].balance = bal;
	cout << "开户成功" << endl;
	N++;
}

void Bank::cancelAccount()
{
	int i;
	char ch;

	while ((i = getUser()) == -1)
	{
		cout << "查无此账号,请重新输入:" << endl;
	}
	cout << "确认销户(y/n)" << endl;
	cin >> ch;
	if (ch == 'y')
	{
		cout << "取款1000元,销户成功" << endl;
		users[i].status = 2;
		users[i].balance = 0;

	}
	else {
		return;
	}
	

}

int Bank::getUser()
{
	int acc;
	int i;
	int flag = 0;

	cout << "请输入账号:";
	cin >> acc;
	for (i = 0; i < N; i++)
	{
		if (acc == users[i].account)
		{
			cout << "姓名:"<<users[i].name << endl;
			cout << "账户余额:"<<users[i].balance << endl;
			cout << "当前状态:"<<users[i].status << endl;
			flag = i;
		}
	}
	if (flag < i)
	{
		return flag;
	}
	else
		return -1;

}

void Bank::save()
{
	int i;
	char ch;
	int bal;

	while ((i = getUser()) == -1)
	{
		cout << "查无此账号,请重新输入:" << endl;
	}
	cout << "请输入要存的金额:";
	cin >> bal;
	users[i].balance += bal;
	cout << "存钱成功" << endl;
}

void Bank::withdraw()
{
	int i;
	char ch;
	int bal;

	while ((i = getUser()) == -1)
	{
		cout << "查无此账号,请重新输入:" << endl;
	}
	cout << "请输入要取的金额:";
	cin >> bal;
	if (users[i].balance < bal)
	{
		cout << "余额不足" << endl;
		return;
	}
	else
	{
		users[i].balance -= bal;
	}

	cout << "取钱成功" << endl;
}

void Bank::transferAccounts()
{
	int i;
	char ch;
	int bal;

	while ((i = getUser()) == -1)
	{
		cout << "查无此账号,请重新输入:" << endl;
	}
	cout << "请输入要转的金额:";
	cin >> bal;
	users[i].balance += bal;
	cout << "转账成功" << endl;
}

void Bank::reportLoss()
{
	int i;
	int pass;
	char ch;
	int bal;

	while ((i = getUser()) == -1)
	{
		cout << "查无此账号,请重新输入:" << endl;
	}
	cout << "请输入密码" << endl;
	cin >> pass;
	if (users[i].password == pass)
	{
		cout << "挂失成功" << endl;
		users[i].status = 1;
	}
	else {
		cout << "密码输入错误" << endl;

	}
}

void Bank::cancelLoss()
{
	int pass;
	char ch;
	int bal;

	while ((i = getUser()) == -1)
	{
		cout << "查无此账号,请重新输入:" << endl;
	}
	cout << "请输入密码" << endl;
	cin >> pass;
	if (users[i].password == pass)
	{
		cout << "解挂成功" << endl;
		users[i].status = 0;
	}
	else {
		cout << "密码输入错误" << endl;

	}
}

void Bank::updataPassword()
{
	int i;
	char ch;
	int bal;

	while ((i = getUser()) == -1)
	{
		cout << "查无此账号,请重新输入:" << endl;
	}

	int iPass1, iPass2;
	cout << "新密码:";
	iPass1 = inputPassword();
	cout << "确认密码";
	iPass2 = inputPassword();
	if (iPass1 != iPass2)
	{
		cout << "密码输入不一致" << endl;
		return;
	}
	else
	{
		users[i].password = iPass1;
		cout << "密码输入成功" << endl;
	}
	
}

#include<iostream>	
#include<fstream>
#include<conio.h>
#include<cstdlib>
#include<cstring>
#include<ctype.h>

#include"bank.h"
using namespace std;

int pass()//验证用户密码,正确返回1,错误返回0
{
	char sNameInfile[20];
	char sPassInfile[20];
	ifstream infile("password.txt", ios::in);
	if (!infile)
	{
		cout << "password file cannot open!" << endl;
		system("pause");
		exit(1);
	}
	infile >> sNameInfile >> sPassInfile;
	infile.close();

	char sName[20];
	char sPass[20];
	char ch;
	int iTry = 3;
	int right = 0;

	do
	{
		cout << "请输入业务员用户名:";
		cin >> sName;
		cout << "请输入密码:";
		int i = 0;
		while ((ch = getch()) != '\r')
		{
			sPass[i++] = ch;
			putchar('*');
		}
		sPass[i] = '\0';
		fflush(stdin);
		cout << endl;
		if (strcmp(sPass, sPassInfile) == 0 && strcmp(sName, sNameInfile) == 0)
		{
			right = 1;
			break;
		}
		else
		{
			iTry--;
			if (iTry < 0)
				cout << "对不起,您已输入超过三次	" << endl;

		}

	} while (iTry);
	return right;
}

int chooseInMenu()
{
	int i;
	while (1)
	{
		cout << endl;
		cout << "+------------------------------------+" << endl;
		cout << "+       1 开户  2 销户  3 存款        +" << endl;
		cout << "+       4 取款  5 查询  6 转账        +" << endl;
		cout << "+       7 挂失  8 解挂  9 改密        +" << endl;
		cout << "+                      0 退出        +" << endl;
		cout << "+------------------------------------+" << endl;
		cout << "请输入操作指令:";
		cin >> i;
		if (i >= 0 && i <= 9)
			break;
		else
			cout << "请重新输入" << endl;

	}
	return i;
}

int inputPassword()
{
	char ch;
	int iPass = 0;
	int i;
	while (1)
	{
		for (i = 0; i < 6; i++)
		{
			ch = getch();
			putchar('*');
			if (isdigit(ch))
				iPass = iPass * 10 + (ch - '0');
			else
			{
				iPass = 0;
				break;
			}
		}
		fflush(stdin);
		cout << endl;
		if (iPass == 0)
		{
			cout << "密码要求全为数字" << endl;
			cout << "请重新输入" << endl;
		}
		else
			break;
	}
	return iPass;
}

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

int main()
{
	cout << "+-------------------------------------------+" << endl;
	cout << "               欢迎光临csdn银行              " << endl;
	cout << "+-------------------------------------------+" << endl;

	Bank b;
	if (pass())
	{
		Bank b;
		b.work();
	}
}


  • 18
    点赞
  • 171
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值