用C++写一个包含存取款,计算利息和信用卡功能银行管理系统
时间: 2024-03-12 18:48:50 浏览: 129
C++编写的银行管理系统
好的,下面是一个包含存取款、计算利息和信用卡功能的银行管理系统的C++代码示例:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Account {
public:
Account(string name, int accountNumber, double balance)
: m_name(name), m_accountNumber(accountNumber), m_balance(balance) {}
void deposit(double amount) { m_balance += amount; }
void withdraw(double amount) { m_balance -= amount; }
string getName() const { return m_name; }
int getAccountNumber() const { return m_accountNumber; }
double getBalance() const { return m_balance; }
protected:
string m_name;
int m_accountNumber;
double m_balance;
};
class SavingsAccount : public Account {
public:
SavingsAccount(string name, int accountNumber, double balance, double interestRate)
: Account(name, accountNumber, balance), m_interestRate(interestRate) {}
double calculateInterest() const { return m_balance * m_interestRate; }
protected:
double m_interestRate;
};
class CreditCard : public Account {
public:
CreditCard(string name, int accountNumber, double balance, double creditLimit, double interestRate)
: Account(name, accountNumber, balance), m_creditLimit(creditLimit), m_interestRate(interestRate) {}
void makePayment(double amount) {
if (amount > m_balance) {
cout << "Invalid payment amount. Cannot pay more than current balance." << endl;
} else {
m_balance -= amount;
}
}
void charge(double amount) {
if (m_balance + amount > m_creditLimit) {
cout << "Charge declined. Credit limit exceeded." << endl;
} else {
m_balance += amount;
}
}
double calculateInterest() const { return m_balance * m_interestRate; }
protected:
double m_creditLimit;
double m_interestRate;
};
class Bank {
public:
void addAccount(Account* account) { m_accounts.push_back(account); }
void removeAccount(int accountNumber) {
for (int i = 0; i < m_accounts.size(); i++) {
if (m_accounts[i]->getAccountNumber() == accountNumber) {
m_accounts.erase(m_accounts.begin() + i);
break;
}
}
}
Account* findAccount(int accountNumber) {
for (int i = 0; i < m_accounts.size(); i++) {
if (m_accounts[i]->getAccountNumber() == accountNumber) {
return m_accounts[i];
}
}
return nullptr;
}
private:
vector<Account*> m_accounts;
};
int main() {
Bank bank;
// Add some accounts
Account* aliceAccount = new SavingsAccount("Alice", 1234, 1000.0, 0.05);
bank.addAccount(aliceAccount);
Account* bobAccount = new CreditCard("Bob", 5678, 2000.0, 5000.0, 0.10);
bank.addAccount(bobAccount);
// Find an account by account number
Account* account = bank.findAccount(1234);
if (account) {
cout << account->getName() << "'s account balance is " << account->getBalance() << endl;
SavingsAccount* savingsAccount = dynamic_cast<SavingsAccount*>(account);
if (savingsAccount) {
cout << "The interest earned on Alice's savings account is " << savingsAccount->calculateInterest() << endl;
}
}
// Make a deposit
account->deposit(500.0);
cout << account->getName() << "'s account balance after deposit is " << account->getBalance() << endl;
// Make a withdrawal
account->withdraw(200.0);
cout << account->getName() << "'s account balance after withdrawal is " << account->getBalance() << endl;
// Charge the credit card
account = bank.findAccount(5678);
if (account) {
CreditCard* creditCard = dynamic_cast<CreditCard*>(account);
if (creditCard) {
creditCard->charge(1000.0);
cout << account->getName() << "'s credit card balance after charge is " << account->getBalance() << endl;
cout << "The interest charged on Bob's credit card is " << creditCard->calculateInterest() << endl;
}
}
// Make a payment on the credit card
account->makePayment(500.0);
cout << account->getName() << "'s credit card balance after payment is " << account->getBalance() << endl;
// Remove an account
bank.removeAccount(1234);
// Clean up memory
delete aliceAccount;
delete bobAccount;
return 0;
}
```
这个代码示例创建了 `Account` 类,作为其他具体账户类型的基类。它还创建了 `SavingsAccount` 类和 `CreditCard` 类,分别表示储蓄账户和信用卡账户。这些类都继承自 `Account` 类,并实现了计算利息、存取款和信用卡功能的方法。
在 `main()` 函数中,我们首先创建了一个储蓄账户和一个信用卡账户,并使用 `addAccount()` 方法将它们添加到银行中。然后,我们使用 `findAccount()` 方法查找一个特定账户,并显示其余额和利息。接着,我们对账户进行了存取款、信用卡充值和付款等操作,最后使用 `removeAccount()` 方法删除了一个账户。
值得注意的是,由于我们使用了指针来存储账户对象,所以需要在程序结束前手动释放内存。
阅读全文