⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bank.cpp

📁 是个银行柜面系统软件
💻 CPP
字号:
#include "bank.h"
#include <cctype>

Bank::Bank() : current(-1)
{
}

int Bank::crr_acc() const
{
	return current;
}

char Bank::get_command()
{
	char ch;
	cout << "Your command:>";
	cin >> ch;
	ch = tolower(ch);
	while (ch != 'o' && ch != 'u' && ch != 'c' && ch != 'h' && ch != 'x') {
		cout << "Invalid command. Retry:" << endl;
		cout << "Your command:>";
		cin >> ch;
		ch = tolower(ch);
	}
	return ch;
}

void Bank::do_command(char ch)
{
	string ID;
	string passwd, confirm;
	bool existed, equal;
	Account tmp;

	switch (ch) {

	case 'u': // [u]se an existed one
		cout << "Enter your account ID: ";
		cin >> ID;

		do {
			existed = false;
			for (int i=0; i<accounts.size(); i++) {
				if (ID == accounts[i].ID()) {
					existed = true;
					current = i;
				}
			}
			if (!existed) {
				cout << "The account does not existed. Try a new one: "
					<< endl;
				cin >> ID;
			}
		} while (!existed);

		cout << "Enter your password: ";
		cin >> passwd;

		do {
			equal = true;
			if (passwd != accounts[current].passwd()) {
				equal = false;
				cout << "Password incorrect. Enter again: ";
				cin >> passwd;
			}
		} while (!equal);
		break;

	case 'h': // [h]elp
		command_help();
		current = -1;
		break;

	case 'x': // e[x]it
		break;

	case 'c': // [c]lose an account
		cout << "Enter your account ID: ";
		cin >> ID;

		do {
			existed = false;
			for (int i=0; i<accounts.size(); i++) {
				if (ID == accounts[i].ID()) {
					existed = true;
					current = i;
				}
			}
			if (!existed) {
				cout << "The account does not existed. Try a new one: "
					<< endl;
				cin >> ID;
			}
		} while (!existed);

		cout << "Enter your password: ";
		cin >> passwd;

		do {
			equal = true;
			if (passwd != accounts[current].passwd()) {
				equal = false;
				cout << "Password incorrect. Enter again: ";
				cin >> passwd;
			}
		} while (!equal);

		cout << "Account " << accounts[current].ID() << " closed." << endl;
		tmp = accounts[current];
		accounts[current] = accounts[accounts.size() - 1];
		accounts[accounts.size() - 1] = tmp;
		accounts.pop_back();
		current = -1;
		break;

	case 'o': // [o]pen an account
		cout << "Enter your account ID: ";
		cin >> ID;
		do {
			existed = false;
			for (int i=0; i<accounts.size(); i++) {
				if (ID == accounts[i].ID())
					existed = true;
			}
			if (existed) {
				cout << "Accout ID already existed. Enter a new one: ";
				cin >> ID;
			}
		} while (existed);

		do {
			equal = true;
			cout << "Enter your password: ";
			cin >> passwd;
			cout << "Confirm your password: ";
			cin >> confirm;
			if (passwd != confirm) {
				equal = false;
				cout << "The confirm password does not match your password. Retry:"
					<< endl;
			}
		} while (!equal);

		Account new_account(ID, passwd);
		accounts.push_back(new_account);
		current = accounts.size() - 1;
		break;
	}
}

char Bank::get_operation()
{
	char ch;
	cout << accounts[current].ID() << ":>";
	cin >> ch;
	ch = tolower(ch);
	while (ch != 'b' && ch != 't' && ch != 'w' && ch != 'p'
		&& ch != 'h' && ch != 'f') {
		cout << "Invalid command. Retry:" << endl;
		cout << accounts[current].ID() << ":>";
		cin >> ch;
		ch = tolower(ch);
	}
	return ch;
}

void Bank::do_operation(char ch)
{
	string prompt = accounts[current].ID();
	string prompt1 = prompt + ":Deposite" + ":>";
	string prompt2 = prompt + ":Draw" + ":>";
	switch (ch) {

	case 'b':
		cout << prompt << ":Balance" << ":>" << accounts[current].balance()
			 << endl;
		break;
	case 't': // deposi[t]e
		int sum;
		do {
			cout << prompt1 << "How much to pay into your account? ";
			cin >> sum;
			if (sum <= 0)
				cout << "Invalid input." << endl;
		} while (sum < 0);
		accounts[current].deposite(sum);
		cout << sum << " deposited." << endl;
		break;

	case 'p': // [p]rint operations
		cout << prompt << "'s operations:" << endl;
		accounts[current].print_operations();
		break;

	case 'f': // operations [f]inished
		cout << "Thanks for using the system, " << prompt << "." << endl;
		current = -1;
		break;

	case 'h': // [h]elp
		operation_help();
		break;

	case 'w': // dra[w]
		do {
			cout << prompt2 << "How much to draw from your account? ";
			cin >> sum;
			if (sum <= 0)
				cout << "Invalid input." << endl;
		} while (sum < 0);
		accounts[current].draw(sum);
		cout << sum << " drawn." << endl;
		if (accounts[current].balance() <= -5000) {
			cout << "You have overdrawn $" << abs(accounts[current].balance())
				 << ". Your account is closed." << endl;
			Account tmp = accounts[current];
			accounts[current] = accounts[accounts.size() - 1];
			accounts[accounts.size() - 1] = tmp;
			accounts.pop_back();
			current = -1;
		}
		if (accounts[current].balance() < 0)
			cout << "You have overdrawn. Please pay back soon."
				 << "The upper limit is $5,000.";
		break;
	}
}

void Bank::introduction()
{
	cout << "Welcome to the Bank Account System!" << endl;
	cout << "You can open/close/use an account, and do" << endl;
	cout << "some operations like paying into your account," << endl;
	cout << "drawing, and printing operations that you have made." << endl;
	cout << "[O]pen an account:         Enter 'O'" << endl;
	cout << "[U]se an existed account:  Enter 'U'" << endl;
	cout << "[C]lose an account:        Enter 'C'" << endl;
	cout << "E[x]it the system:         Enter 'X'\n" << endl;
	cout << "Anywhere, you can always enter 'H' for more detailed help.\n"
		      << endl;

}

void Bank::command_help()
{
	cout << " ** COMMAND HELP ** Commands listed below:" << endl;
	cout << " * [O]pen an account:         Enter 'O'" << endl;
	cout << " * [U]se an existed account:  Enter 'U'" << endl;
	cout << " * [C]lose an account:        Enter 'C'" << endl;
	cout << " * E[x]it the system:         Enter 'X'" << endl;
	cout << " * Your account ID should be unique. If you enter" << endl;
	cout << " * an ID that already existed when you open a new" << endl;
	cout << " * account. You are required to enter another." << endl;
}

void Bank::operation_help()
{
	cout << " ** OPERATION HELP **" << endl;
	cout << "The prompt shows the current accout. You can now enter" << endl;
	cout << "the operations." << endl;
	cout << "Operations listed below:" << endl;
	cout << " * Check [B]alance:      Enter 'B'" << endl;
	cout << " * Deposi[t]e:           Enter 'T'" << endl;
	cout << " * Dra[w]:               Enter 'W'" << endl;
	cout << " * [P]rint operations:   Enter 'P'" << endl;
	cout << " * [F]inish operations:  Enter 'F'" << endl;
	cout << " * You are allowed to overdraw, but the upper limit" << endl;
	cout << " * is $5,000. If you exceed that, your account will" << endl;
	cout << " * be closed." << endl;
}

void Bank::writeRecords(char* filePath)
{
	FILE* pFile=fopen(filePath,"w");

	if(pFile==NULL)
	{
		printf("Write files error!\n");
		return;
	}

	for(int i=0;i<accounts.size();i++)
	{
		accounts[i].write2File(pFile);
	}

	fclose(pFile);
}


void Bank::readRecords(char* filePath)
{
	FILE* pFile=fopen(filePath,"r");

	if(pFile==NULL)
	{
		return;
	}
	
	while(!feof(pFile))
	{
		Account act;
		act.readFromFile(pFile);
		accounts.push_back(act);
	}

	fclose(pFile);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -