1. Introduction
In this blog post, we delve into creating a basic banking application in C++. This application will encompass functionalities such as creating bank accounts, depositing and withdrawing funds, transferring money between accounts, and displaying account details. Through this project, you will get acquainted with fundamental concepts of object-oriented programming in C++, including class design, encapsulation, and managing a collection of objects.
2. Program Steps
1. Class Design: Define a BankAccount class encapsulating attributes like account name, account number, and balance, along with methods to manipulate these attributes.
2. Creating Accounts: Implement a function to create new BankAccount objects and add them to a collection.
3. Deposit and Withdrawal Operations: Create methods in the BankAccount class for depositing and withdrawing funds.
4. Transfers Between Accounts: Write the logic for transferring funds from one account to another through a friend function.
5. Displaying Account Information: Write a function that allows users to view details of a specific account.
6. User Interaction: Develop a simple console-based interface for users to interact with the banking application, executing various operations based on user input.
3. Code Program
#include <iostream>
#include <vector>
#include <string>
#include <limits>
using namespace std;
class BankAccount {
private:
string accountName;
long accountNumber;
double balance;
public:
BankAccount(string accountName, long accountNumber, double initialBalance = 0.0)
: accountName(accountName), accountNumber(accountNumber), balance(initialBalance) {}
// Getter for account number, to facilitate transfers and account identification
long getAccountNumber() const { return accountNumber; }
// Deposit funds into the account
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "Amount deposited successfully. Current balance: $" << balance << endl;
} else {
cout << "Invalid amount. Please enter a positive number." << endl;
}
}
// Withdraw funds from the account
bool withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Amount withdrawn successfully. Remaining balance: $" << balance << endl;
return true;
} else {
cout << "Withdrawal failed. Please check the amount and try again." << endl;
return false;
}
}
// Display the account's details
void displayAccount() const {
cout << "Account Name: " << accountName << "\nAccount Number: " << accountNumber
<< "\nBalance: $" << balance << endl;
}
// Support for transferring money from this account to another
friend void transfer(BankAccount& from, BankAccount& to, double amount);
};
// Transfer function outside class definition
void transfer(BankAccount& from, BankAccount& to, double amount) {
if (from.withdraw(amount)) {
to.deposit(amount);
cout << "Transfer successful. $" << amount << " transferred from account "
<< from.getAccountNumber() << " to account " << to.getAccountNumber() << endl;
} else {
cout << "Transfer failed. Check the balance and try again." << endl;
}
}
vector<BankAccount> accounts;
void createAccount() {
string name;
long accountNumber;
double initialBalance;
cout << "Enter account name: ";
getline(cin, name);
cout << "Enter account number: ";
cin >> accountNumber;
cout << "Enter initial balance: ";
cin >> initialBalance;
accounts.emplace_back(name, accountNumber, initialBalance);
cout << "Account created successfully.\n";
}
int findAccountIndex(long accountNumber) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i].getAccountNumber() == accountNumber) return i;
}
return -1;
}
void performDeposit() {
long accountNumber;
double amount;
cout << "Enter account number: ";
cin >> accountNumber;
cout << "Enter amount to deposit: ";
cin >> amount;
int index = findAccountIndex(accountNumber);
if (index != -1) {
accounts[index].deposit(amount);
} else {
cout << "Account not found." << endl;
}
}
void performWithdrawal() {
long accountNumber;
double amount;
cout << "Enter account number: ";
cin >> accountNumber;
cout << "Enter amount to withdraw: ";
cin >> amount;
int index = findAccountIndex(accountNumber);
if (index != -1) {
accounts[index].withdraw(amount);
} else {
cout << "Account not found." << endl;
}
}
void performTransfer() {
long fromAccount, toAccount;
double amount;
cout << "Enter source account number: ";
cin >> fromAccount;
cout << "Enter target account number: ";
cin >> toAccount;
cout << "Enter amount to transfer: ";
cin >> amount;
int fromIndex = findAccountIndex(fromAccount);
int toIndex = findAccountIndex(toAccount);
if (fromIndex != -1 && toIndex != -1) {
transfer(accounts[fromIndex], accounts[toIndex], amount);
} else {
cout << "One or both accounts not found." << endl;
}
}
void performAccountDisplay() {
long accountNumber;
cout << "Enter account number to display: ";
cin >> accountNumber;
int index = findAccountIndex(accountNumber);
if (index != -1) {
accounts[index].displayAccount();
} else {
cout << "Account not found." << endl;
}
}
int main() {
int choice;
do {
cout << "\n1. Create Account\n";
cout << "2. Deposit\n";
cout << "3. Withdraw\n";
cout << "4. Transfer\n";
cout << "5. Display Account\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear the newline character from the buffer
switch (choice) {
case 1:
createAccount();
break;
case 2:
performDeposit();
break;
case 3:
performWithdrawal();
break;
case 4:
performTransfer();
break;
case 5:
performAccountDisplay();
break;
case 6:
cout << "Exiting application.\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 6);
return 0;
}
Output:
/tmp/cf1C6ykXyj.o 1. Create Account 2. Deposit 3. Withdraw 4. Transfer 5. Display Account 6. Exit Enter your choice: 1 Enter account name: Ramesh Enter account number: 123 Enter initial balance: 10000 Account created successfully. 1. Create Account 2. Deposit 3. Withdraw 4. Transfer 5. Display Account 6. Exit Enter your choice: 1 Enter account name: Umesh Enter account number: 112 Enter initial balance: 5000 Account created successfully. 1. Create Account 2. Deposit 3. Withdraw 4. Transfer 5. Display Account 6. Exit Enter your choice: 2 Enter account number: 123 Enter amount to deposit: 5000 Amount deposited successfully. Current balance: $15000 1. Create Account 2. Deposit 3. Withdraw 4. Transfer 5. Display Account 6. Exit Enter your choice: 3 Enter account number: 123 Enter amount to withdraw: 5000 Amount withdrawn successfully. Remaining balance: $10000 1. Create Account 2. Deposit 3. Withdraw 4. Transfer 5. Display Account 6. Exit Enter your choice: 4 Enter source account number: 123 Enter target account number: 112 Enter amount to transfer: 5000 Amount withdrawn successfully. Remaining balance: $5000 Amount deposited successfully. Current balance: $10000 Transfer successful. $5000 transferred from account 123 to account 112 1. Create Account 2. Deposit 3. Withdraw 4. Transfer 5. Display Account 6. Exit Enter your choice: 5 Enter account number to display: 123 Account Name: Ramesh Account Number: 123 Balance: $5000 1. Create Account 2. Deposit 3. Withdraw 4. Transfer 5. Display Account 6. Exit Enter your choice: 5 Enter account number to display: 112 Account Name: Umesh Account Number: 112 Balance: $10000 1. Create Account 2. Deposit 3. Withdraw 4. Transfer 5. Display Account 6. Exit Enter your choice: 6 Exiting application.
Explanation:
1. BankAccount Class: Serves as the core of the application, representing bank accounts with methods to deposit, withdraw, and display account details.
2. Creating Accounts: Users can create new bank accounts, which are stored in a vector of BankAccount objects for easy management and retrieval.
3. Deposits and Withdrawals: The application allows users to add or remove funds from their accounts, updating the balance accordingly.
4. Fund Transfers: The transfer function allows users to move funds between accounts, showcasing interaction between multiple objects.
5. Displaying Account Details: Users can view the details of their accounts, including the current balance, enhancing transparency and user experience.
6. User Interface: The application's console-based interface guides users through performing banking operations, demonstrating practical C++ input/output handling.
This C++ banking application project highlights the power and flexibility of object-oriented programming, providing a solid foundation for building more complex software systems.
Comments
Post a Comment
Leave Comment