Closed VatsalPandya47 closed 1 year ago
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; }
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
Account Creation: The program allows users to create new bank accounts with unique account numbers, which form the basis for all subsequent transactions.
Deposit and Withdrawal: Users can deposit funds into their accounts as well as withdraw money when needed, ensuring the flexibility to manage their finances.
Funds Transfer: The system enables seamless transfer of funds between different bank accounts, making it convenient for users to send money to others or manage their own accounts.
Account Details: Users can access comprehensive account details, including the account holder's name, account number, and current balance.
Transaction History: The program maintains a transaction history for each account, giving users the ability to review past financial activities.
Usage