0% found this document useful (0 votes)
163 views

Cbe Bank System

The document contains code for a bank account management system using linked lists in C++. It includes functions to create and add accounts, find accounts by number, deposit and withdraw funds, delete accounts, print accounts, and get total balances and number of accounts. The main function tests these functions by allowing a manager user to add accounts, view accounts, search accounts, view averages, delete accounts, and login as a regular user.

Uploaded by

elias ferhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views

Cbe Bank System

The document contains code for a bank account management system using linked lists in C++. It includes functions to create and add accounts, find accounts by number, deposit and withdraw funds, delete accounts, print accounts, and get total balances and number of accounts. The main function tests these functions by allowing a manager user to add accounts, view accounts, search accounts, view averages, delete accounts, and login as a regular user.

Uploaded by

elias ferhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

#include <iostream>

#include<string>

using namespace std;

// Structure to represent a customer account

struct Account {

int accountNumber;

string name;

double balance;

Account *next;

};

// Function to create a new account

Account* createAccount(int accountNumber, string name, double balance) {

Account *newAccount = new Account;

newAccount->accountNumber = accountNumber;

newAccount->name = name;

newAccount->balance = balance;

newAccount->next = NULL;

return newAccount;

// Function to add a new account to the linked list

Account* addAccount(Account *head, int accountNumber, string name, double balance) {

Account *newAccount = createAccount(accountNumber, name, balance);

if (head == NULL) {

head = newAccount;

} else {

Account *current = head;


while (current->next != NULL) {

current = current->next;

current->next = newAccount;

return head;

// Function to search for an account by account number

Account* findAccount(Account *head, int accountNumber) {

Account *current = head;

while (current != NULL) {

if (current->accountNumber == accountNumber) {

return current;

current = current->next;

return NULL;

// Function to deposit money into an account

void deposit(Account *account, double amount) {

account->balance += amount;

//cout<<account->balance;

// Function to withdraw money from an account

bool withdraw(Account *account, double amount) {

if (account->balance >= amount) {


account->balance -= amount;

return true;

} else {

// return false;

cout<<"your balance is insufficient, you can't withdraw this amount from this account:"<<endl;

// Function to delete an account from the linked list

Account* deleteAccount(Account *head, int accountNumber) {

Account *current = head;

Account *previous = NULL;

while (current != NULL) {

if (current->accountNumber == accountNumber) {

if (previous == NULL) {

head = current->next;

} else {

previous->next = current->next;

delete current;

break;

previous = current;

current = current->next;

return head;

// Function to print all accounts in the linked list


void printAccounts(Account *head) {

Account *current = head;

while (current != NULL) {

cout << "Account Number: "<< current->accountNumber << endl;

cout << "Name: " << current->name << endl;

cout << "Balance: " << current->balance << endl << endl;

current = current->next;

//function to print searched customer

void prints(Account *head){

Account *current = head;

cout << "Account Number: "<< current->accountNumber << endl;

cout << "Name: " << current->name << endl;

cout << "Balance: " << current->balance << endl << endl;

// Function to get the total number of accounts in the linked list

int getNumAccounts(Account *head) {

int numAccounts = 0;

Account *current = head;

while (current != NULL) {

numAccounts++;

current = current->next;

return numAccounts;

}
// Function to get the total balance of all accounts in the linked list

double getTotalBalance(Account *head) {

double totalBalance = 0;

Account *current = head;

while (current != NULL) {

totalBalance += current->balance;

current = current->next;

return totalBalance;

// Function to get the average balance of all accounts in the linked list

double getAverageBalance(Account *head) {

int numAccounts = getNumAccounts(head);

if (numAccounts == 0) {

return 0;

double totalBalance = getTotalBalance(head);

return totalBalance / numAccounts;

//Function to transfer money from account to account

void transferMoney(Account *head, int accountFrom, int accountTo, double amount) {

// Find the accounts

Account *accountFromPtr = NULL;

Account *accountToPtr = NULL;

Account *current = head;

while (current != NULL) {

if (current->accountNumber == accountFrom) {
accountFromPtr = current;

if (current->accountNumber == accountTo) {

accountToPtr = current;

current = current->next;

// Check if both accounts were found

if (accountFromPtr == NULL || accountToPtr == NULL) {

cout << "Error: One or both accounts not found." << endl;

return;

// Check if accountFrom has enough balance

if (accountFromPtr->balance < amount) {

cout << "Error: Insufficient funds in account " << accountFrom << "." << endl;

return;

// Transfer the money

accountFromPtr->balance -= amount;

accountToPtr->balance += amount;

// Print a confirmation message

cout << "Transferred " << amount << " from account " << accountFrom << " to account " << accountTo
<< "." << endl;

}
//Function to update personal information

void updateCustomer(Account *head,int accountNumber, string name, double amount) {

// Find the customer

Account *customerPtr = NULL;

Account *current = head;

while (current != NULL) {

if (current->accountNumber == accountNumber) {

customerPtr = current;

break;

current = current->next;

// Check if the customer was found

if (customerPtr == NULL) {

cout << "Error: Customer not found." << endl;

return;

// Update the personal information

customerPtr->name= name;

customerPtr->balance= amount;

// Print a confirmation message

// cout << "Updated personal information for customer " << name << "." << endl;

// Function to delete all accounts from the linked list

void deleteAllAccounts(Account *&head) {


Account *current = head;

while (current != NULL) {

Account *temp = current;

current = current->next;

delete temp;

head = NULL;

// Main function to test the linked list implementation

int main() {

cout<<"\n\t\t\t CBE BANK SYSTEM ";

cout<<"\n\t\t\t*********************\n\n\n\n\n";

cout<<" HINT: to login as a MANAGER username=ADMIN and password=1234\n\n to login as a


USER username=USER and password=4321\n\n";

Account *head = NULL;


struct Node {

string username;

int password;

bool isManager;

Node* next;

};

// Function to set up the login interface

// Prompt the user to enter their username and password

string username;

int password;

cout << "Please enter your username: ";

cin >> username;

cout << "Please enter your password: ";

cin >> password;

Node* had = new Node{"admin", 1234, true, NULL};

// Traverse the linked list to find the matching node

Node* current = had;

if (current->username != username && current->password != password) {

cout << "Incorrect username or password. Please try again." << endl;}

else{

while (current != NULL) {


if (current->username == username && current->password == password) {

// If the node matches, check if the user is a manager

if (current->isManager) {

int n;

do{

cout << "Welcome, manager " << username << "!" << endl;

cout<<"\n\t\t\t\t * MAIN MENU *";

cout<<"\n\t\t\t\t*************************\n";

cout<<"\n\t\t 1. to add a new account: "<<endl;

cout<<"\n\t\t 2. to show added customers: "<<endl;

cout<<"\n\t\t 3. to search a specific account: "<<endl;

cout<<"\n\t\t 4. to display average balance in our bank: "<<endl;

cout<<"\n\t\t 5. to delete a specific account: "<<endl;

cout<<"\n\t\t 6. How many customers are there? : "<<endl;

cout<<"\n\t\t 7. How much money stored in our bank? : "<<endl;

cout<<"\n\t\t 8. Do you want to update the account?: "<<endl;

cout<<"\n\t\t 9. Do you want to delete all accounts?: "<<endl;

cout<<"\n\t\t10. Do you want to login as a user?: "<<endl;

cin>>n;

switch(n){

case 1:

int accountNumber;

string name;

double balance;
int n;

cout<<"how many customers you want to add? ";

cin>>n;

for(int i=0;i<n;i++){

cout<<"enter Account number"<<endl;

cin>>accountNumber;

cout<<"enter name of customers"<<endl;

cin>>name;

cout<<"enter initial balance"<<endl;

cin>>balance;

head = addAccount(head, accountNumber, name, balance);

cout<<"SUCCESSFULLY ADDED! "<<endl;

break;

case 2:{

cout<<"our bank cuatomers:"<<endl;

printAccounts(head);

break;

case 3:

{int accno;

cout<<" enter account number you want to find: ";

cin>>accno;
Account *foundAccount = findAccount(head, accno); // calling the findAccount function

if (foundAccount != NULL) {

// do something with the found account (e.g. print its details)

prints(foundAccount);

} else {

cout<<" the account was not found";

break;}

case 4:

cout << "Average Balance: "<< getAverageBalance(head) << endl;

break;

case 5: {

cout<<"Enter account number to delete a specific account: ";

int a;

cin>>a;

Account *foundAccount = findAccount(head, a); // calling the findAccount function

if (foundAccount != NULL) {

head=deleteAccount(foundAccount, a);

cout<<"updated customers: "<<endl;

printAccounts(head);
break; }

case 6:{

cout<<"numbers of customers in our bank: "<<getNumAccounts(head)<<endl;

break;

case 7:{

cout<<"Total revenues of our bank: "<<getTotalBalance(head)<<endl;

break;

case 8:

int a, b;

string c;

cout<<"enter account number you want to Update : ";

cin>>a;

cout<<"\nNew name: ";

cin>>c;

cout<<"\nNew balance: ";

cin>>b;

updateCustomer(head, a, c, b);

break;

case 9:

deleteAllAccounts(head);

cout<<"deleted all accounts!!! ";

break;
default:

break;

}}while(n !=10);}

// Call manager function here

// current=current->next;

string username;

int password;

cout << "Please enter your username: ";

cin >> username;

cout << "Please enter your password: ";

cin >> password;

// Node* had = new Node{"admin", "admin", true, NULL};

Node* had = new Node{"user", 4321 , true, NULL};

// had->next->next = new Node{"user2", "password2", false, NULL};

// Traverse the linked list to find the matching node

Node* current = had;

if (current->username != username && current->password != password) {

cout << "Incorrect username or password. Please try again." << endl;}

else{

while (current != NULL) {

if (current->username == username && current->password == password)

{
cout << "Welcome, user "<< username<< "!" << endl;

int x;

cout<<"\n\t\t 1. to Deposits money: "<<endl;

cout<<"\n\t\t 2. to Withdraw money: "<<endl;

cout<<"\n\t\t 3. to Transfer money: "<<endl;

cin>>x;

while(x !=4){

switch(x){

case 1:

cout<<"Enter accno to add deposit: ";

int a;

cin>>a;

Account *foundAccount = findAccount(head, a); // calling the findAccount function

if (foundAccount != NULL) {

cout<<"enter deposit money: ";

int b;

cin>>b;

deposit(foundAccount, b);

prints(foundAccount);

// do something with the found account (e.g. print its details)

}
break; } case 2:

cout<<"Enter your accno to withdraw: ";

int a;

cin>>a;

Account *foundAccount = findAccount(head, a); // calling the findAccount function

if (foundAccount != NULL) {

cout<<"enter the money to withdraw";

int b;

cin>>b;

cout<<withdraw(foundAccount, b);

prints(foundAccount);

break;

case 3:

int a, b, c;

cout<<"enter senders account number: ";

cin>>a;

cout<<"\nenter recievers account number: ";

cin>>b;
cout<<"\nenter transfered money: ";

cin>>c;

transferMoney(head, a, b, c);

cout << "Updated account balances:" << endl;

printAccounts(head);

default:{

break;}

}}

current = current->next;

}}

return 0;

You might also like