Bank program in C++
In this article, we will take a program to demonstrate the working of bank system, with its output and explanation.
Code:
#include<iostream>
#include<string.h>
using namespace std;
class bank_Name{
private:
int total_accounts;
struct account{
string name,address;
string id;
int mobile,money;
} account_array[100];
public:
bank_Name(){
total_accounts=0;
}
void select_service();
void create_account();
void display_all_accounts();
void update_account();
void search_account();
void transaction();
void delete_account();
};
void bank_Name::create_account(){
cout<< "Enter data of accountant " <<total_accounts + 1 <<endl;
cout<< "Enter name: ";
cin>>account_array[total_accounts].name;
cout<< "ID: ";
cin>>account_array[total_accounts].id;
cout<< "Address: ";
cin>>account_array[total_accounts].address;
cout<< "Contact: ";
cin>>account_array[total_accounts].mobile;
cout<< "Total Cash: ";
cin>>account_array[total_accounts].money;
total_accounts++;
}
void bank_Name::display_all_accounts(){
for(int i=0;i<total_accounts;i++){
cout<<"details of "<<i+1<<" account "<<endl;
cout<< "Name: " <<account_array[i].name <<endl;
cout<< "ID: " <<account_array[i].id<<endl;
cout<< "Address: " <<account_array[i].address<<endl;
cout<< "Contact: " <<account_array[i].mobile<<endl;
cout<< "Cash: " <<account_array[i].money<<endl;
}
}
void bank_Name::update_account() {
string id;
cout<< "Enter id of student whose data you want to update" <<endl;
cin>> id;
for (int i = 0; i<total_accounts; i++) {
if (id == account_array[i].id) {
cout<< "Previous data" <<endl;
cout<< "Data of person " <<i + 1 <<endl;
cout<< "Name: " <<account_array[i].name <<endl;
cout<< "ID: " <<account_array[i].id <<endl;
cout<< "Address: " <<account_array[i].address<<endl;
cout<< "Contact: " <<account_array[i].mobile<<endl;
cout<< "Cash: " <<account_array[i].money<<endl;
cout<< "\nEnter new data" <<endl;
cout<< "Enter name: ";
cin>>account_array[i].name;
cout<< "ID: ";
cin>>account_array[i].id;
cout<< "Address: ";
cin>>account_array[i].address;
cout<< "Contact: ";
cin>>account_array[i].mobile;
cout<< "Total Cash: ";
cin>>account_array[i].money;
break;
}
if (i == total_accounts - 1) {
cout<< "No such account found" <<endl;
}
}
}
void bank_Name::search_account() {
string id;
cout<< "Enter id of student whose data you want to search" <<endl;
cin>> id;
for (int i = 0; i<total_accounts; i++) {
if (id == account_array[i].id) {
cout<< "Name: " <<account_array[i].name <<endl;
cout<< "ID: " <<account_array[i].id <<endl;
cout<< "Address: " <<account_array[i].address<<endl;
cout<< "Contact: " <<account_array[i].mobile<<endl;
cout<< "Cash: " <<account_array[i].money<<endl;
break;
}
if (i == total_accounts - 1) {
cout<< "No such record found" <<endl;
}
}
}
void bank_Name::transaction() {
string id;
int money;
char ch;
cout<< "Enter id of student whose data you want to transaction" <<endl;
cin>> id;
for (int i = 0; i<total_accounts; i++) {
if (id == account_array[i].id) {
cout<< "Name: " <<account_array[i].name <<endl;
cout<< "Address: " <<account_array[i].address<<endl;
cout<< "Contact: " <<account_array[i].mobile<<endl;
cout<< "\nExisting Cash " <<account_array[i].money<<endl;
cout<< "Press 1 to deposit" <<endl;
cout<< "Press 2 to withdraw" <<endl;
cin>>ch;
switch (ch) {
case '1':
cout<< "How much cash you want to deposit?" <<endl;
cin>> money;
account_array[i].money += money;
cout<< "Your new cash is " <<account_array[i].money<<endl;
break;
case '2':
while (1) {
cout<< "How much cash you want to withdraw?" <<endl;
cin>> money;
if (money >account_array[i].money) {
cout<< "Your existing cash is just " <<account_array[i].money<<endl;
}
else {
account_array[i].money -= money;
cout<< "Your new cash is " <<account_array[i].money<<endl;
break;
}
}
break;
default:
cout<< "Invalid id" <<endl;
break;
}
break;
}
if (i == total_accounts - 1) {
cout<< "No such account found" <<endl;
}
}
}
void bank_Name::delete_account() {
char ch;
cout<< "Press 1 to remove a specific record" <<endl;
cout<< "Press 2 to remove all records" <<endl;
cin>>ch;
string id;
switch(ch) {
case '1':
cout<< "Enter id of student whose data you want to remove" <<endl;
cin>> id;
for (int i = 0; i<total_accounts; i++) {
if (id == account_array[i].id) {
for (int j = i; j <total_accounts; j++) {
account_array[j].name = account_array[j + 1].name;
account_array[j].id = account_array[j + 1].id;
account_array[j].address = account_array[j + 1].address;
account_array[j].mobile = account_array[j + 1].mobile;
account_array[j].money = account_array[j + 1].money;
}
total_accounts--;
cout<< "Your required data is deleted" <<endl;
break;
}
}
if (total_accounts == 0) {
cout<< "No such account found" <<endl;
}
break;
case '2':
total_accounts = 0;
cout<< "All accounts are deleted" <<endl;
break;
default:
cout<< "Invalid Input" <<endl;
break;
}
}
void bank_Name::select_service(){
char choose;
while(1){
cout<<"choose the option\n"<<endl;
cout<<"1 create a new bank account"<<endl;
cout<<"2 display all accounts list"<<endl;
cout<<"3 update the account already present in list"<<endl;
cout<<"4 check the details of the account"<<endl;
cout<<"5 make the transaction "<<endl;
cout<<"6 remove the account "<<endl;
cout<<"7 exit the program"<<endl;
cin>>choose;
switch(choose){
case '1':
bank_Name::create_account();
break;
case '2':
if(total_accounts==0){
cout<<"no accounts"<<endl;
}else{
bank_Name::display_all_accounts();
}
break;
case '3':
if(total_accounts==0){
cout<<"no accounts"<<endl;
}else{
bank_Name::update_account();
}
break;
case '4':
if(total_accounts==0){
cout<<"no accounts"<<endl;
}else{
bank_Name::search_account();
}
break;
case '5':
if(total_accounts==0){
cout<<"no accounts"<<endl;
}else{
bank_Name::transaction();
}
break;
case '6':
if(total_accounts==0){
cout<<"no accounts"<<endl;
}else{
bank_Name::delete_account();
}
break;
case '7':
exit(0);
break;
default:
cout<<"please select a valid option "<<endl;
break;
}
}
}
int main(){
bank_Name bank1;
bank1.select_service();
return 0;
}
Output:
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
1
Enter data of accountant 1
Enter name: sriram
ID: sriram_1
Address: ayodhya
Contact: 1234567890
Total Cash: 100
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
1
Enter data of accountant 2
Enter name: sita
ID: sita_2
Address: janakpur
Contact: 1234567891
Total Cash: 80
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
2
details of 1 account
Name: sriram
ID: sriram_1
Address: ayodhya
Contact: 1234567890
Cash: 100
details of 2 account
Name: sita
ID: sita_2
Address: janakpur
Contact: 1234567891
Cash: 80
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
3
Enter id of student whose data you want to update
sita_2
Previous data
Data of person 2
Name: sita
ID: sita_2
Address: janakpur
Contact: 1234567891
Cash: 80
Enter new data
Enter name: janaki
ID: janaki_2
Address: janakpur
Contact: 1234567891
Total Cash: 80
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
4
Enter id of student whose data you want to search
sriram_1
Name: sriram
ID: sriram_1
Address: ayodhya
Contact: 1234567890
Cash: 100
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
5
Enter id of student whose data you want to transaction
sriram_1
Name: sriram
Address: ayodhya
Contact: 1234567890
Existing Cash 100
Press 1 to deposit
Press 2 to withdraw
1
How much cash you want to deposit?
1000
Your new cash is 1100
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
4
Enter id of student whose data you want to search
sriram_1
Name: sriram
ID: sriram_1
Address: ayodhya
Contact: 1234567890
Cash: 1100
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
5
Enter id of student whose data you want to transaction
sriram_1
Name: sriram
Address: ayodhya
Contact: 1234567890
Existing Cash 1100
Press 1 to deposit
Press 2 to withdraw
2
How much cash you want to withdraw?
100
Your new cash is 1000
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
4
Enter id of student whose data you want to search
sriram_1
Name: sriram
ID: sriram_1
Address: ayodhya
Contact: 1234567890
Cash: 1000
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
6
Press 1 to remove a specific record
Press 2 to remove all records
2
All accounts are deleted
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
2
no accounts
choose the option
1 create a new bank account
2 display all accounts list
3 update the account already present in list
4 check the details of the account
5 make the transaction
6 remove the account
7 exit the program
7
Detailed explanation about the entire program:
It will give a deep explanation of every variable, function, and keyword in the program. So go through this until the end to get a good program vision.
Variables in the program:
total_accounts (private int):
It keeps track of the total number of bank accounts created.
account_array (private struct array):
An array of account struct objects that stores the details of each bank account, including name, address, ID, contact, and cash balance.
name (string):
It represents the name of a bank account holder.
address (string):
It represents the address of a bank account holder.
id (string):
It represents the unique identification number of a bank account.
mobile (int):
It represents the contact number of a bank account holder.
money (int):
It represents the cash balance of a bank account.
These variables are used throughout the code to store and manipulate data related to bank accounts. They play a crucial role in creating, updating, searching, and performing transactions on bank accounts.
Functions in the program:
Several functions are utilized in the following program. These functions are as follows:
1. bank_Name::create_account():
This function allows the user to create a new bank account. It prompts the user to enter details such as the account holder's name, ID, address, contact number, and initial cash balance. The function stores this information in the 'account_array ' struct at the next available index and increments the 'total_accounts ' variable.
2. bank_Name::display_all_accounts():
This function is used to display the details of all the created bank accounts. It iterates over the 'account_array' and prints the account holder's name, ID, address, contact number, and cash balance for each account.
3. bank_Name::update_account():
This function allows the user to update the information of a specific bank account. It prompts the user to enter the ID of the account they want to update. The function searches for a matching account in the 'account_array'. If found, it displays the account's previous details and prompts the user to enter the updated information, including name, ID, address, contact number, and cash balance.
4. bank_Name::search_account():
This function is used to search for a specific bank account based on its ID. The user is prompted to enter the ID of the account they want to search for. After that, the function iterates over the 'account_array' to find a matching account. If a match is found, it displays the account holder's name, ID, address, contact number, and cash balance. If no match is found, it displays a message indicating that no record was found.
5. bank_Name::transcation():
This function allows the user to perform transactions on a specific bank account. The user is prompted to enter the ID of the account they want to perform a transaction. The function searches for a matching account in the 'account_array'. If found, it displays the account holder's details and the existing cash balance. After that, the user is given the option to either deposit or withdraw money. Depending on the user's choice, the function either adds or subtracts the entered amount from the account's cash balance. If the user tries to withdraw more money than the account's balance, a message is displayed indicating insufficient funds.
6. bank_Name::delete_account():
This function allows the user to delete a specific bank account or remove all accounts. The user is prompted to choose between deleting a specific account or removing all accounts. If the user selects to delete a specific account, they are asked to enter the ID of the account they want to remove. After that, the function searches for a matching account in the 'account_array'. If found, it shifts the remaining accounts in the array to fill the gap left by the deleted account and decrements the 'total_accounts' variable. If the user selects to remove all accounts, the 'total_accounts' variable is set to 0, effectively deleting all accounts.
7. bank_Name::select_service():
This function acts as a menu for the user to select various banking services. It runs in a continuous loop until the user chooses to exit the program. The user is presented with different options, such as creating an account, displaying account details, updating accounts, searching accounts, making transactions, removing accounts, or exiting the program. The corresponding function is called to perform the desired banking operation based on the user's choice.
By understanding the purpose and functionality of each function, you can grasp the overall flow of the program and how different operations are performed in the banking system.