模拟银行帐户管理程序

2022/8/31 14:54:27

本文主要是介绍模拟银行帐户管理程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

UML类图:

代码:

#include <iostream>
#include <iomanip>
using namespace std;

// 银行账户类
class Account {
public:
	Account() :balance(0) {}
	Account(double balance) :balance(balance) {}
	/*向当前账户取钱,并且保证账户不会被透支。如果提取金额大于账户金额,函数将
	保持 balance 不变,并打印信息“Debit amount exceeded account balance”*/
	virtual bool debit(double amount);
	virtual bool credit(double amount); // 向当前账户加钱
	virtual double getBalance(); // 返回当前账户 balance 的值
	virtual ~Account() {}
private:
	double balance; // 账户余额
};

bool Account::debit(double amount) {
	if (amount > balance) {
		cout << "Debit amount exceeded account balance." << endl;
		return false;
	} else {
		this->balance = this->balance - amount;
		return true;
	}
}

bool Account::credit(double amount) {
	if (amount < 0) {
		return false;
	} else {
		this->balance = this->balance + amount;
		return true;
	}
}

double Account::getBalance() {
	return this->balance;
}

// 存款账户类
class SavingsAccount :public Account {
public:
	/*构造函数,接受初始余额值和初始利率值*/
	SavingsAccount(double balance, double rate) :Account(balance), interestRate(rate / 100) {}
	double getInterestRate() {
		return this->interestRate;
	}
	/*返回代表账户的利息的一个double值,这个值是 balance 和 interestRate 的乘积*/
	double calculateInterest() {
		return this->getBalance() * this->interestRate;
	}
	~SavingsAccount() {}
private:
	double interestRate; // 账户的比率(百分比)
};

// 支票账户类
class CheckAccount :public Account {
public:
	/*构造函数,接受初始余额值和交易费用值*/
	CheckAccount(double balance, double fee) :Account(balance), fee(fee) {}
	double getFee() {
		return this->fee;
	}
	/*重新定义成员函数 debit,当每笔交易完成时,从 balance 中减去每笔交易的费用。*/
	bool debit(double amount) {
		if (amount > this->getBalance()) {
			cout << "Debit amount exceeded account balance." << endl;
			return false;
		} else if (this->fee + amount > this->getBalance()) {
			cout << "Transaction fee exceeded account balance while debiting." << endl;
			return false;
		} else {
			Account::debit(amount + this->fee);
			return true;
		}
	}
	/*重新定义成员函数 credit,当每笔交易完成时,从 balance 中减去每笔交易的费用。*/
	bool credit(double amount) {
		if (this->fee > amount + this->getBalance()) {
			cout << "Transaction fee exceeded account balance while crediting." << endl;
			return false;
		} else {
			Account::credit(amount);
			Account::debit(this->fee);
			return true;
		}
	}
	~CheckAccount() {};
private:
	double fee; // 每笔交易的费用
};

int main() {
	Account* accounts[3];
	accounts[0] = new SavingsAccount(100, 3); // 余额100元,利息3%
	accounts[1] = new CheckAccount(100, 5); // 余额100元,交易费5元
	accounts[2] = new CheckAccount(50, 5); // 余额50元,交易费5元

	for (int i = 0; i < 3; i++) {
		cout << "第" << i + 1 << "次循环的结果:" << endl;
		accounts[i]->debit(200);
		accounts[i]->debit(40);
		accounts[i]->credit(50);
		accounts[i]->debit(49);
		accounts[i]->debit(43);
		accounts[i]->credit(1);
		SavingsAccount* derivedPtr = dynamic_cast<SavingsAccount*>(accounts[i]);
		if (derivedPtr != NULL) { //如果类型兼容,转换成功
			derivedPtr->credit(derivedPtr->calculateInterest());
		}
		cout << "账户的余额为:" << fixed << setprecision(2) << accounts[i]->getBalance() << endl;
	}

	delete accounts[0];
	delete accounts[1];
	delete accounts[2];

	return 0;
}


这篇关于模拟银行帐户管理程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程