TechTorch

Location:HOME > Technology > content

Technology

Creating a Basic Bank Management System in C: A Step-by-Step Guide for SEO

January 05, 2025Technology3463
Creating a Basic Bank Management System in C: A Step-by-Step Guide for

Creating a Basic Bank Management System in C: A Step-by-Step Guide for SEO

Developing a basic bank management system in C involves understanding several components such as account creation, balance inquiry, deposit, withdrawal, and account closure. This guide will walk you through a simple implementation that covers these functionalities.

Introduction

This tutorial provides a basic implementation of a bank management system in C. The system is designed to be straightforward and can be expanded with additional features such as file storage, user authentication, and input validation for a more robust system.

Components and Data Structures

Data Structures:

Account: A struct that holds account information including account number, name, and balance. Accounts Array: An array of Account structs is used to store multiple accounts.

Code Implementation

The following is a sample implementation:

#include stdio.h
#include stdlib.h
#include string.h
#define MAX_ACCOUNTS 100
typedef struct {
    int accountNumber;
    char name[100];
    float balance;
} Account;
Account accounts[MAX_ACCOUNTS];
int accountCount  0;
void createAccount() {
    if (accountCount  MAX_ACCOUNTS) {
        printf("Account limit reached.
");
        return;
    }
    Account newAccount;
      accountCount   1; // Simple account number generation
    printf("Enter the name: ");
    scanf("%s", );
      0.0; // Initial balance
    accounts[accountCount]  newAccount;
    accountCount  ;
    printf("Account created successfully.
");
}
void deposit() {
    int accountNumber;
    float amount;
    printf("Enter the account number: ");
    scanf("%d", accountNumber);
    if (accountNumber  1 || accountNumber  accountCount) {
        printf("Invalid account number.
");
        return;
    }
    printf("Enter the deposit amount: ");
    scanf("%f", amount);
    accounts[accountNumber - 1].balance   amount;
    printf("Success! Deposit completed.
");
}
void withdraw() {
    int accountNumber;
    float amount;
    printf("Enter the account number: ");
    scanf("%d", accountNumber);
    if (accountNumber  1 || accountNumber  accountCount) {
        printf("Invalid account number.
");
        return;
    }
    printf("Enter the withdrawal amount: ");
    scanf("%f", amount);
    if (amount  accounts[accountNumber - 1].balance) {
        printf("Insufficient funds.
");
    } else {
        accounts[accountNumber - 1].balance - amount;
        printf("Success! Withdrawal completed.
");
    }
}
void checkBalance() {
    int accountNumber;
    printf("Enter the account number: ");
    scanf("%d", accountNumber);
    if (accountNumber  1 || accountNumber  accountCount) {
        printf("Invalid account number.
");
    } else {
        printf("Balance: %.2f
", accounts[accountNumber - 1].balance);
    }
}
void closeAccount() {
    int accountNumber;
    printf("Enter the account number to close: ");
    scanf("%d", accountNumber);
    if (accountNumber  1 || accountNumber  accountCount) {
        printf("Invalid account number.
");
        return;
    }
    for (int i  accountNumber - 1; i  accountCount - 1; i  ) {
        accounts[i]  accounts[i   1]; // Shift accounts left
    }
    accountCount--;
    printf("Account closed successfully.
");
}
int main() {
    int choice;
    do {
        printf("--- Banking Management System ---
");
        printf("1. Create Account
");
        printf("2. Deposit
");
        printf("3. Withdraw
");
        printf("4. Check Balance
");
        printf("5. Close Account
");
        printf("6. Exit
");
        printf("Enter your choice: ");
        scanf("%d", choice);
        switch (choice) {
            case 1:
                createAccount();
                break;
            case 2:
                deposit();
                break;
            case 3:
                withdraw();
                break;
            case 4:
                checkBalance();
                break;
            case 5:
                closeAccount();
                break;
            case 6:
                printf("Exiting...
");
                break;
            default:
                printf("Invalid choice.
");
        }
    } while (choice ! 6);
    return 0;
}

Explanation of the Code

Data Structures: Account: Struct for holding account details (account number, name, and balance). accounts[]: Array of Accounts to store multiple accounts. Functions: createAccount: Creates a new account and initializes its details. deposit: Allows the user to deposit money into an account. withdraw: Allows the user to withdraw money from an account, checking for sufficient balance. checkBalance: Displays the balance of a specified account. closeAccount: Removes an account from the system. main: Main menu to present different operations until the user decides to exit.

How to Compile and Run

To compile and run this program:

Save the code in a file called bank_management_system.c. Open a terminal and navigate to the directory where the file is saved. Compile the code using: gcc bank_management_system.c -o bank_management_system Run the program: ./bank_management_system

This is a basic implementation and can be expanded with additional features like file storage, user authentication, and input validation for a more robust system.