Refactor Complex C++ Banking System for Maintainability and Performance
Refactor the provided C++ code of a banking system that handles multiple account types, transactions, and interest calculations. The goal is to improve code quality, maintainability, and performance without changing the system's behavior.
Challenge prompt
You are given a legacy C++ banking system that supports multiple account types (Savings, Checking) and processes deposits, withdrawals, and monthly interest calculations. The current code is monolithic, has duplicated logic, and lacks clear abstractions. Refactor the code to improve modularity, use appropriate object-oriented design patterns, remove code duplication, and optimize performance while preserving the behavior. Ensure your refactored code compiles and produces the same output as the original. Provide clear class and method structures and improve naming conventions for better readability.
Guidance
- • Identify duplicated code and consolidate it into reusable methods or base classes.
- • Use inheritance and polymorphism to separate account-specific behaviors.
- • Aim for clear separation of concerns: transaction handling, interest calculation, and account management.
- • Ensure your refactoring maintains the same output and performance characteristics.
Hints
- • Consider creating a base Account class and deriving SavingsAccount and CheckingAccount classes.
- • Use virtual functions for interest calculation and transaction operations.
- • Encapsulate transaction logic in a separate method to reduce code repetition.
Starter code
#include <iostream>
#include <vector>
using namespace std;
class BankingSystem {
public:
struct Account {
int id;
double balance;
string type; // "checking" or "savings"
};
vector<Account> accounts;
void deposit(int id, double amount) {
for (auto &acc : accounts) {
if (acc.id == id) {
if (acc.type == "checking") {
acc.balance += amount;
} else if (acc.type == "savings") {
acc.balance += amount;
}
}
}
}
void withdraw(int id, double amount) {
for (auto &acc : accounts) {
if (acc.id == id) {
if (acc.type == "checking") {
if (acc.balance >= amount) {
acc.balance -= amount;
}
} else if (acc.type == "savings") {
if (acc.balance >= amount) {
acc.balance -= amount;
}
}
}
}
}
void applyMonthlyInterest() {
for (auto &acc : accounts) {
if (acc.type == "savings") {
acc.balance *= 1.01; // 1% interest
} else if (acc.type == "checking") {
acc.balance *= 1.001; // 0.1% interest
}
}
}
void printBalances() {
for (const auto &acc : accounts) {
cout << "Account " << acc.id << " (" << acc.type << "): $" << acc.balance << endl;
}
}
};
int main() {
BankingSystem system;
system.accounts.push_back({1, 1000.0, "checking"});
system.accounts.push_back({2, 2000.0, "savings"});
system.deposit(1, 500);
system.withdraw(2, 100);
system.applyMonthlyInterest();
system.printBalances();
return 0;
}Expected output
Account 1 (checking): $1501.5 Account 2 (savings): $1919
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.