Bank system
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm> // For transform
#include <cctype> // For tolower
using namespace std;
const int MAX_ACCOUNTS = 10; // Maximum number of accounts
struct Account {
string name;
double balance;
int pin; // Add PIN for each account
double CashOnHand = 0.0; // Global cash on hand (initially set to $10,000)
};
void ClearC() {
// system("cls"); // Avoid using system calls; platform-specific
cout << "\033[2J\033[1;1H"; // ANSI escape code to clear console screen (works on most
terminals)
}
void displayMenu() {
cout << "\n--- Banking System Menu ---\n";
cout << "1. Create Account\n";
cout << "2. Deposit Money\n";
cout << "3. Withdraw Money\n";
cout << "4. Check Balance\n";
cout << "5. Change PIN\n";
cout << "6. Exit\n";
cout << "Choose an option: ";
}
bool accountExists(const Account accounts[], int accountCount, const string& name) {
string nameLower = name;
transform(nameLower.begin(), nameLower.end(), nameLower.begin(), ::tolower); // Convert
input name to lowercase
for (int i = 0; i < accountCount; i++) {
string existingNameLower = accounts[i].name;
transform(existingNameLower.begin(), existingNameLower.end(),
existingNameLower.begin(), ::tolower); // Convert stored name to lowercase
if (existingNameLower == nameLower) {
return true; // Account exists (case-insensitive)
}
}
return false; // Account does not exist
}
bool verifyPin(Account& account) {
int enteredPin;
cout << "Enter your PIN: ";
cin >> enteredPin;
return enteredPin == account.pin;
}
int main() {
char choice;
Account accounts[MAX_ACCOUNTS];
int accountCount = 0;
do {
ClearC();
displayMenu();
cin >> choice;
cin.ignore(); // To handle any leftover newline character
switch (choice) {
case '1': // Create Account
if (accountCount < MAX_ACCOUNTS) {
string name;
cout << "Enter account holder's name: ";
getline(cin, name);
cout << "Amount on hand: $";
cin >> accounts[accountCount].CashOnHand;
if (accountExists(accounts, accountCount, name)) {
cout << "You already have an account with this name!\n";
}
else {
accounts[accountCount].name = name;
accounts[accountCount].balance = 0.0; // Initial balance
cout << "Enter a 4-digit PIN: ";
cin >> accounts[accountCount].pin;
accountCount++;
cout << "Account created successfully!\n";
}
}
else {
cout << "Maximum account limit reached!\n";
}
break;
case '2': // Deposit Money
{
string name;
double amount;
cout << "Enter account holder's name: ";
getline(cin, name);
bool found = false;
for (int i = 0; i < accountCount; i++) {
if (accounts[i].name == name) {
cout << "Verifying PIN...\n";
if (verifyPin(accounts[i])) {
cout << "Cash on Hand: $" << fixed << setprecision(2) << CashOnHand <<
endl;
cout << "Enter amount to deposit: ";
cin >> amount;
if (amount > 0 && amount <= CashOnHand) { // Check if enough cash is
available
accounts[i].balance += amount;
CashOnHand -= amount; // Subtract from global cash on hand
cout << "Deposit successful!\n";
cout << "New balance: $" << fixed << setprecision(2) << accounts[i].balance
<< endl;
cout << "Remaining cash on hand: $" << fixed << setprecision(2) <<
CashOnHand << endl;
}
else if (amount > CashOnHand) {
cout << "Not enough cash on hand to deposit that amount!\n";
}
else {
cout << "Invalid amount!\n";
}
}
else {
cout << "Incorrect PIN.\n";
}
found = true;
break;
}
}
if (!found) {
cout << "Account not found!\n";
}
}
break;
case '3': // Withdraw Money
{
string name;
double amount;
cout << "Enter account holder's name: ";
getline(cin, name);
bool found = false;
for (int i = 0; i < accountCount; i++) {
if (accounts[i].name == name) {
cout << "Verifying PIN...\n";
if (verifyPin(accounts[i])) {
cout << "Balance: $" << fixed << setprecision(2) << accounts[i].balance <<
endl;
cout << "Enter amount to withdraw: ";
cin >> amount;
if (amount > 0 && amount <= accounts[i].balance) {
accounts[i].balance -= amount;
CashOnHand += amount;
cout << "Withdrawal successful!\n";
cout << "New balance: $" << fixed << setprecision(2) << accounts[i].balance
<< endl;
cout << "Cash on Hand: $" << fixed << setprecision(2) << CashOnHand <<
endl;
}
else {
cout << "Invalid amount or insufficient funds!\n";
}
}
else {
cout << "Incorrect PIN.\n";
}
found = true;
break;
}
}
if (!found) {
cout << "Account not found!\n";
}
}
break;
case '4': // Check Balance
{
string name;
cout << "Enter account holder's name: ";
getline(cin, name);
bool found = false;
for (int i = 0; i < accountCount; i++) {
if (accounts[i].name == name) {
cout << "Verifying PIN...\n";
if (verifyPin(accounts[i])) {
cout << "Current balance: $" << fixed << setprecision(2) << accounts[i].balance
<< endl;
}
else {
cout << "Incorrect PIN.\n";
}
found = true;
break;
}
}
if (!found) {
cout << "Account not found!\n";
}
}
break;
case '5': // Change PIN
{
string name;
cout << "Enter account holder's name: ";
getline(cin, name);
bool found = false;
for (int i = 0; i < accountCount; i++) {
if (accounts[i].name == name) {
cout << "Verifying PIN...\n";
if (verifyPin(accounts[i])) {
int newPin;
cout << "Enter new 4-digit PIN: ";
cin >> newPin;
accounts[i].pin = newPin;
cout << "PIN changed successfully!\n";
}
else {
cout << "Incorrect PIN.\n";
}
found = true;
break;
}
}
if (!found) {
cout << "Account not found!\n";
}
}
break;
case '6': // Exit
cout << "Exiting the banking system. Goodbye!\n";
return 0;
default:
cout << "Invalid choice! Please try again.\n";
}
cout << endl;
cout << "Try again? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
return 0;
}