In: Computer Science
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
// Class Bank definition
class Bank
{
public:
// Data member to store data
string name;
string accountNumber;
double balance;
// Pointer to point to next node
Bank *next;
};// End of class
// Declares a start pointer and assigns null to it
Bank *start = NULL;
// Function to accept data
void accept(Bank *ptr)
{
cout<<"\n Enter the name: ";
cin>>ptr->name;
cout<<"\n Enter the account number: ";
cin>>ptr->accountNumber;
cout<<"\n Enter initial balance: ";
cin>>ptr->balance;
}// End of function
// Function to display information
void displayAccounts()
{
// Creates a temporary pointer
Bank *ptr;
// Checks if start is null then list is empty
if(start == NULL)
{
cout<<"\nList is empty:\n";
return;
}// End of if condition
else
{
// Points to starting account
ptr = start;
cout<<"\n Accounts Information \n";
// Loops till end of the list
while(ptr != NULL)
{
cout<<"\n Name: "<<ptr->name<<"\t Account
Number: "<<ptr->accountNumber
<<"\t Balance: "<<ptr->balance;
// Move to next account
ptr=ptr->next ;
}// End of while loop
}// End of else
}// End of function
// Function to display parameter node information
void displayAccount(Bank *ptr)
{
cout<<"\n Name: "<<ptr->name<<"\t Account
Number: "<<ptr->accountNumber<<"\t Balance:
"<<ptr->balance;
}// End of function
// Function to search an account
int Search(string accNo)
{
int position = -1, counter = 0;
// Creates a temporary pointers
Bank *ptr;
// Checks if start is null then list is empty
if(start==NULL)
{
cout<<"\nThe List is Empty:\n";
exit(0);
}// End of if condition
// Otherwise
else
{
// Points to starting account
ptr = start;
// Loops till end of the list
while(ptr != NULL)
{
// Checks if current account number is equals to parameter account
number
if(ptr->accountNumber.compare(accNo) == 0)
{
cout<<"\n Account details \n";
// Calls the function to display found account information
displayAccount(ptr);
// Assigns the found position
position = counter;
// Returns the position
return counter;
}// End of if condition
// Move to next account
ptr=ptr->next ;
// Increase the record counter
counter++;
}// End of while loop
}// End of else
// Checks if found position is -1 account not found
if(position == -1)
cout<<"\n Account not available.";
// returns the position
return position;
}// End of function
// Function to add an account number
void addAccount()
{
// Creates a temporary pointer
Bank *temp = new Bank();
// Checks if temp is null then list is full
if(temp==NULL)
{
cout<<"\nOut of Memory Space:\n";
return;
}// End of if condition
// Calls the function accept data for node temp
accept(temp);
// Assigns null to temp next
temp->next = NULL;
// Checks if the start is NULL then it is the first node to
insert
if(start == NULL)
{
// temp address is assigned to start
start = temp;
}// End of if condition
// Otherwise
else
{
// start address is assigned to temp next
temp->next = start;
// temp address is assigned to start
start = temp;
}// End of else
}// End of function
// Function to delete a node at specified position
void deleteAccount()
{
int position;
string accNo;
// Creates a temporary pointer
Bank *temp,*ptr;
// Checks if start is null then list is empty
if(start==NULL)
{
cout<<"\nThe List is Empty:\n";
exit(0);
}// End of if condition
// Otherwise
else
{
// Accepts the account number
cout<<"\nEnter the account number: \t";
cin>>accNo;
// Calls the function to search the account number
// Stores the returned found position
position = Search(accNo);
// Checks if found position is -1 account not found
if(position == -1)
{
cout<<"\n No such account number: "<<accNo;
return;
}// End of if condition
// Checks if the position is zero
if(position == 0)
{
// Temporary pointer points start
ptr = start;
// Start pointing to next node
start = start->next;
cout<<"\nThe deleted account is: ";
// Calls the function to display the deleted account
information
displayAccount(ptr);
// Deletes the pointer
free(ptr);
}// End of if condition
// Otherwise
else
{
// Temporary pointer points start
ptr = start;
// Loops till record found position
for(int c = 0; c < position; c++)
{
// temporary pointer pointing to previous node ptr
temp = ptr;
// Move to next account
ptr = ptr->next;
// Checks if ptr is null node not found
if(ptr == NULL)
{
cout<<"\nAccount not Found:\n";
return;
}// End of if condition
}// End of for loop
temp->next = ptr->next ;
cout<<"\n The deleted account:";
// Calls the function to display deleted account information
displayAccount(ptr);
// Deletes the account
free(ptr);
}// End of inner else
}// End of outer else
}// End of function
// Function to perform deposit operation
void deposit(string accNo)
{
int c, position = -1;
double amount;
// Creates a temporary pointers
Bank *temp,*ptr;
// Checks if start is null then list is empty
if(start==NULL)
{
cout<<"\nThe List is Empty:\n";
exit(0);
}// End of if condition
// Otherwise
else
{
// Temporary pointer points start
ptr = start;
c = 0;
// Loops till end of the list
while(ptr != NULL)
{
// Temporary pointer pointing to previous node ptr
temp = ptr;
// Compare the current node account number with the parameter accNo
for equal
if(ptr->accountNumber.compare(accNo) == 0)
{
// Stores the found index position
position = c;
break;
}// End of if condition
// Move to next account
ptr = ptr->next;
// Increase the index counter by one
c++;
}// End of while loop
// Checks if the position is -1 not found
if(position == -1)
cout<<"\n Account not found:";
// Otherwise checks if the position is zero then starting
node
else if(position == 0)
{
ptr = start;
// Accepts the deposit amount
cout<<"\n Enter the deposit amount: ";
cin>>amount;
// Checks if deposit amount is negative display error
message
if(amount < 0)
{
cout<<"\n Invalid amount.";
return;
}// End of if condition
// Otherwise valid amount
else
// Increases the balance by amount
ptr->balance += amount;
}// End of else if condition
// Otherwise
else
{
// Temporary pointer points start
ptr = start;
// Loops up to entered position
for(c = 0; c < position; c++)
{
// Temporary pointer pointing to previous node ptr
temp = ptr;
// Move to next account
ptr = ptr->next;
// Checks if ptr is null node not found
if(ptr == NULL)
{
cout<<"\nAccount not found:\n";
return;
}// End of if condition
}// End of for loop
// Accept the deposit amount
cout<<"\n Enter the deposit amount: ";
cin>>amount;
// Checks if deposit amount is negative display error
message
if(amount < 0)
{
cout<<"\n Invalid amount.";
return;
}// End of if condition
// Otherwise valid amount
else
// Adds the amount to balance
ptr->balance += amount;
}// End of else
}// End of outer else
}// End of function
// Function to perform withdrawal operation
void withdraw(string accNo)
{
int c, position = -1;
double amount;
// Creates a temporary pointers
Bank *temp,*ptr;
// Checks if start is null then list is empty
if(start==NULL)
{
cout<<"\nThe List is Empty:\n";
exit(0);
}// End of if condition
// Otherwise
else
{
// Temporary pointer points start
ptr = start;
c = 0;
// Loops till end of the list
while(ptr != NULL)
{
// Temporary pointer pointing to previous node ptr
temp = ptr;
// Compare the current node account number with the parameter accNo
for equal
if(ptr->accountNumber.compare(accNo) == 0)
{
// Stores the found index position
position = c;
break;
}// End of if condition
// Move to next account
ptr = ptr->next;
// Increase the index counter by one
c++;
}// End of while loop
// Checks if the position is -1 not found
if(position == -1)
cout<<"\n Account not found:";
// Otherwise checks if the position is zero then starting
node
else if(position == 0)
{
// Points to the starting account
ptr = start;
// Accepts the withdrawal amount
cout<<"\n Enter the withdrawal amount: ";
cin>>amount;
// Checks if withdrawal amount is negative display the error
message
if(amount < 0)
{
cout<<"\n Invalid amount.";
return;
}// End of if condition
// Otherwise checks withdrawal amount is greater than the
current balance
// display error message
else if(amount > ptr->balance)
{
cout<<"\n Insufficient balance.";
return;
}// End of else if condition
// Otherwise valid withdrawal amount
else
// Subtracts the amount from current balance
ptr->balance -= amount;
}// End of else if condition
// Otherwise
else
{
// Temporary pointer points start
ptr = start;
// Loops up to entered position
for(c = 0; c < position; c++)
{
// temporary pointer pointing to previous node ptr
temp = ptr;
// Move to next node
ptr = ptr->next;
// Checks if ptr is null node not found
if(ptr == NULL)
{
cout<<"\nAccount not found:\n";
return;
}// End of if condition
}// End of for loop
// Accepts the withdrawal amount
cout<<"\n Enter the withdrawal amount: ";
cin>>amount;
// Checks if withdrawal amount is negative display the error
message
if(amount < 0)
{
cout<<"\n Invalid amount.";
return;
}// End of if condition
// Otherwise checks withdrawal amount is greater than the
current balance
// display error message
else if(amount > ptr->balance)
{
cout<<"\n Insufficient balance.";
return;
}// End of else if condition
// Otherwise valid withdrawal amount
else
// Subtracts the amount from current balance
ptr->balance -= amount;
}// End of else
}// End of outer else
}// End of function
// Function to display user choice and return user choice
int menu()
{
// To store user choice
int choice;
// Displays menu
cout<<"\n ************ Bank Account ************ \n";
cout<<"\n MENU \n";
cout<<"---------------------------------------\n";
cout<<"\n 1. Add account.";
cout<<"\n 2. Delete account.";
cout<<"\n 3. Search account.";
cout<<"\n 4. Display all accounts.";
cout<<"\n 5. Deposit.";
cout<<"\n 6. Withdrawal.";
cout<<"\n 7. Quit";
cout<<"\n--------------------------------------\n";
// Accepts user choice
cout<<"Enter your choice:\t";
cin>>choice;
// returns user choice
return choice;
}// End of function
// main function definition
int main()
{
string accNo;
// Loops till user choice is not 7
while(1)
{
// Calls the function to display menu.
// Calls the function as per user choice returned by the
function
switch(menu())
{
case 1:
addAccount();
break;
case 2:
deleteAccount();
break;
case 3:
cout<<"\n Enter an account number to search: ";
cin>>accNo;
Search(accNo);
break;
case 4:
displayAccounts();
break;
case 5:
cout<<"\n Enter an account number to deposit: ";
cin>>accNo;
deposit(accNo);
break;
case 6:
cout<<"\n Enter an account number to withdrawal: ";
cin>>accNo;
withdraw(accNo);
break;
case 7:
exit(0);
default:
cout<<"\n Wrong Choice:\n";
}//end of switch - case
}// End of while loop
return 0;
}//end of main function
Sample Output:
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 4
List is empty:
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 1
Enter the name: Mohan
Enter the account number: 111
Enter initial balance: 2000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 4
Accounts Information
Name: Mohan Account Number: 111 Balance: 2000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 1
Enter the name: Pyari
Enter the account number: 222
Enter initial balance: 78000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 4
Accounts Information
Name: Pyari Account Number: 222 Balance: 78000
Name: Mohan Account Number: 111 Balance: 2000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 1
Enter the name: Ram
Enter the account number: 12000
Enter initial balance: 3000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 3
Enter an account number to search: 555
Account not available.
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 3
Enter an account number to search: 111
Account details
Name: Mohan Account Number: 111 Balance: 2000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 5
Enter an account number to deposit: 444
Account not found:
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 5
Enter an account number to deposit: 222
Enter the deposit amount: 4000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 4
Accounts Information
Name: Ram Account Number: 12000 Balance: 3000
Name: Pyari Account Number: 222 Balance: 82000
Name: Mohan Account Number: 111 Balance: 2000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 5
Enter an account number to deposit: 111
Enter the deposit amount: -10000
Invalid amount.
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 5
Enter an account number to deposit: 111
Enter the deposit amount: 10000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 4
Accounts Information
Name: Ram Account Number: 12000 Balance: 3000
Name: Pyari Account Number: 222 Balance: 82000
Name: Mohan Account Number: 111 Balance: 12000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 6
Enter an account number to withdrawal: 12000
Enter the withdrawal amount: -10
Invalid amount.
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 6
Enter an account number to withdrawal: 12000
Enter the withdrawal amount: 6000
Insufficient balance.
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 6
Enter an account number to withdrawal: 12000
Enter the withdrawal amount: 500
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 4
Accounts Information
Name: Ram Account Number: 12000 Balance: 2500
Name: Pyari Account Number: 222 Balance: 82000
Name: Mohan Account Number: 111 Balance: 12000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 2
Enter the account number: 999
Account not available.
No such account number: 999
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 2
Enter the account number: 12000
Account details
Name: Ram Account Number: 12000 Balance: 2500
Position: 0
The deleted account is:
Name: Ram Account Number: 12000 Balance: 2500
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 4
Accounts Information
Name: Pyari Account Number: 222 Balance: 82000
Name: Mohan Account Number: 111 Balance: 12000
************ Bank Account ************
MENU
---------------------------------------
1. Add account.
2. Delete account.
3. Search account.
4. Display all accounts.
5. Deposit.
6. Withdrawal.
7. Quit
--------------------------------------
Enter your choice: 7