VatsalPandya47 / README.md

Hi there 👋, I'm Vatsal 🏢 Aspiring Software Engineer @University of Nebraska-Lincoln currently in Lincoln, NE 📫 How to reach me: vatsalpandya84@gmail.com
0 stars 0 forks source link

BankManagementSystem.c #1

Closed VatsalPandya47 closed 1 year ago

VatsalPandya47 commented 1 year ago

BankManagementSystem.c

BankManagementSystem.c is the main source code file for a simple yet effective Bank Management System implemented in the C programming language. This program serves as a foundational tool for managing basic banking operations and provides a command-line interface for users to interact with.

Features

Usage

  1. Clone the repository to your local machine.
  2. Navigate to the project directory.
  3. Compile the program using a C compiler (e.g., GCC):
    
    gcc BankManagementSystem.c -o BankManagementSystem
VatsalPandya47 commented 1 year ago

include

include

include

include

define MAX_ACCOUNTS 100

define PASSWORD_LENGTH 20

typedef struct { int account_number; char account_holder[50]; char password[PASSWORD_LENGTH]; float balance; } Account;

Account accounts[MAX_ACCOUNTS]; int num_accounts = 0;

// Function prototypes void createAccount(); void deposit(); void withdraw(); void viewBalance(); void printAllAccounts(); void changePassword(); bool isAccountExist(int account_number); bool authenticateAccount(int account_number, char* password);

int main() { int choice;

do {
    printf("\nBanking System Menu\n");
    printf("1. Create Account\n");
    printf("2. Deposit Money\n");
    printf("3. Withdraw Money\n");
    printf("4. View Balance\n");
    printf("5. View All Accounts\n");
    printf("6. Change Password\n");
    printf("7. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            createAccount();
            break;
        case 2:
            deposit();
            break;
        case 3:
            withdraw();
            break;
        case 4:
            viewBalance();
            break;
        case 5:
            printAllAccounts();
            break;
        case 6:
            changePassword();
            break;
        case 7:
            printf("Exiting the program. Goodbye!\n");
            exit(0);
        default:
            printf("Invalid choice. Please try again.\n");
    }
} while (1);

return 0;

}

void createAccount() { if (num_accounts >= MAX_ACCOUNTS) { printf("Maximum number of accounts reached. Cannot create more accounts.\n"); return; }

Account new_account;
printf("Enter the account number: ");
scanf("%d", &new_account.account_number);

// Check if account number already exists
if (isAccountExist(new_account.account_number)) {
    printf("An account with the same number already exists. Please enter a different account number.\n");
    return;
}

printf("Enter the account holder's name: ");
getchar(); // Consume the newline character left by previous scanf
fgets(new_account.account_holder, sizeof(new_account.account_holder), stdin);
new_account.account_holder[strcspn(new_account.account_holder, "\n")] = '\0'; // Remove trailing newline

printf("Set a password for the account: ");
getchar(); // Consume the newline character left by previous scanf
fgets(new_account.password, PASSWORD_LENGTH, stdin);
new_account.password[strcspn(new_account.password, "\n")] = '\0'; // Remove trailing newline

printf("Enter the initial balance: ");
scanf("%f", &new_account.balance);

accounts[num_accounts] = new_account;
num_accounts++;
printf("Account created successfully!\n");

}

void deposit() { int account_number; char password[PASSWORD_LENGTH]; float amount;

printf("Enter the account number: ");
scanf("%d", &account_number);

int account_index = -1;
for (int i = 0; i < num_accounts; i++) {
    if (accounts[i].account_number == account_number) {
        account_index = i;
        break;
    }
}

if (account_index == -1) {
    printf("Account not found. Please check the account number and try again.\n");
    return;
}

printf("Enter the account password: ");
getchar(); // Consume the newline character left by previous scanf
fgets(password, PASSWORD_LENGTH, stdin);
password[strcspn(password, "\n")] = '\0'; // Remove trailing newline

// Authenticate the account
if (!authenticateAccount(account_number, password)) {
    printf("Incorrect password. Access denied.\n");
    return;
}

printf("Enter the amount to deposit: ");
scanf("%f", &amount);

accounts[account_index].balance += amount;
printf("Deposit successful. New balance: %.2f\n", accounts[account_index].balance);

}

void withdraw() { int account_number; char password[PASSWORD_LENGTH]; float amount;

printf("Enter the account number: ");
scanf("%d", &account_number);

int account_index = -1;
for (int i = 0; i < num_accounts; i++) {
    if (accounts[i].account_number == account_number) {
        account_index = i;
        break;
    }
}

if (account_index == -1) {
    printf("Account not found. Please check the account number and try again.\n");
    return;
}

printf("Enter the account password: ");
getchar(); // Consume the newline character left by previous scanf
fgets(password, PASSWORD_LENGTH, stdin);
password[strcspn(password, "\n")] = '\0'; // Remove trailing newline

// Authenticate the account
if (!authenticateAccount(account_number, password)) {
    printf("Incorrect password. Access denied.\n");
    return;
}

printf("Enter the amount to withdraw: ");
scanf("%f", &amount);

if (amount > accounts[account_index].balance) {
    printf("Insufficient balance. Cannot withdraw.\n");
    return;
}

accounts[account_index].balance -= amount;
printf("Withdrawal successful. New balance: %.2f\n", accounts[account_index].balance);

}

void viewBalance() { int account_number; char password[PASSWORD_LENGTH];

printf("Enter the account number: ");
scanf("%d", &account_number);

int account_index = -1;
for (int i = 0; i < num_accounts; i++) {
    if (accounts[i].account_number == account_number) {
        account_index = i;
        break;
    }
}

if (account_index == -1) {
    printf("Account not found. Please check the account number and try again.\n");
    return;
}

printf("Enter the account password: ");
getchar(); // Consume the newline character left by previous scanf
fgets(password, PASSWORD_LENGTH, stdin);
password[strcspn(password, "\n")] = '\0'; // Remove trailing newline

// Authenticate the account
if (!authenticateAccount(account_number, password)) {
    printf("Incorrect password. Access denied.\n");
    return;
}

printf("Account Number: %d\n", accounts[account_index].account_number);
printf("Account Holder: %s\n", accounts[account_index].account_holder);
printf("Balance: %.2f\n", accounts[account_index].balance);

}

void printAllAccounts() { if (num_accounts == 0) { printf("No accounts found.\n"); return; }

char password[PASSWORD_LENGTH];

printf("Enter your password to view all accounts: ");
getchar(); // Consume the newline character left by previous scanf
fgets(password, PASSWORD_LENGTH, stdin);
password[strcspn(password, "\n")] = '\0'; // Remove trailing newline

// Authenticate the user
if (!authenticateAccount(0, password)) {
    printf("Incorrect password. Access denied.\n");
    return;
}

printf("\nAll Accounts:\n");
for (int i = 0; i < num_accounts; i++) {
    printf("Account Number: %d\n", accounts[i].account_number);
    printf("Account Holder: %s\n", accounts[i].account_holder);
    printf("Balance: %.2f\n\n", accounts[i].balance);
}

}

void changePassword() { int account_number; char current_password[PASSWORD_LENGTH]; char new_password[PASSWORD_LENGTH];

printf("Enter the account number: ");
scanf("%d", &account_number);

int account_index = -1;
for (int i = 0; i < num_accounts; i++) {
    if (accounts[i].account_number == account_number) {
        account_index = i;
        break;
    }
}

if (account_index == -1) {
    printf("Account not found. Please check the account number and try again.\n");
    return;
}

printf("Enter the current password: ");
getchar(); // Consume the newline character left by previous scanf
fgets(current_password, PASSWORD_LENGTH, stdin);
current_password[strcspn(current_password, "\n")] = '\0'; // Remove trailing newline

// Authenticate the account
if (!authenticateAccount(account_number, current_password)) {
    printf("Incorrect password. Access denied.\n");
    return;
}

printf("Enter the new password: ");
getchar(); // Consume the newline character left by previous scanf
fgets(new_password, PASSWORD_LENGTH, stdin);
new_password[strcspn(new_password, "\n")] = '\0'; // Remove trailing newline

strcpy(accounts[account_index].password, new_password);
printf("Password changed successfully!\n");

}

bool isAccountExist(int account_number) { for (int i = 0; i < num_accounts; i++) { if (accounts[i].account_number == account_number) { return true; } } return false; }

bool authenticateAccount(int account_number, char* password) { for (int i = 0; i < num_accounts; i++) { if (accounts[i].account_number == account_number && strcmp(accounts[i].password, password) == 0) { return true; } } return false; }