模拟银行帐户管理程序
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; }
这篇关于模拟银行帐户管理程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?
- 2024-05-09企业src漏洞挖掘-有意思的命令执行