Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
18 kB
2
Indexable
Never
#include <iostream>
#include <fstream>
#include <cctype>
#include <iomanip>
#include <limits>
#include <ctime>
#include <string>
#include <cstdlib>

using namespace std;

// Forward declaration for User structure
struct User;

// Class for bank account
class bank_acc 
{
public:
    int depos;
    char valid_id[100];
    char name[100];
    char gender[10];
    char acctype[10];
    int account_number;
    string email;
    string id_num;
    string bday;
    string transactions;

public:
    void create_account();
    void modify_email();
    void show_account() const;
    void deposit(int); // --
    void withdrawal(int); // --
    void report() const; 
    void holder_list() const;
    int return_accnum() const;
    int return_depos() const;
    char return_acctype() const;
    void prev_transaction() const; // --
    void display_balance() const; //--
};

// Function declarations
void save_account();
void account_details(int);
void balance_transactions(int, int);
void modify_acc(int);
void delete_account(int);
void all_account();
void deposit_withdraw(int, int);
bool login(User users[], int size);
void logout(User users[], int size);

// User structure for login/logout system
struct User 
{
    string username;
    string password;
    bool loggedIn;
};

// Function to create a bank account
void bank_acc::create_account() 
{
    system("cls");
    srand(time(0));
    int accnum = rand() % 900000000 + 100000000;
    account_number = accnum;
    cout << "Create Account" << endl;
    cout << "Account Number: " << account_number << endl;
    cout << "Enter your name: ";
    cin.ignore();
    cin.getline(name, 100);
    cout << "Gender (F/M): ";
    cin.getline(gender, 10);
    cout << "Birth Date: ";
    getline(cin, bday);
    cout << "E-mail: ";
    getline(cin, email);
    cout << "Type of ID: ";
    cin.getline(valid_id, 100);
    cout << "ID number: ";
    getline(cin, id_num);
    cout << "Account Type (S/C): ";
    cin.getline(acctype, 10);
    cout << "Initial Deposit: ";
    cin >> depos;
    cin.ignore();
}

// Function to display account details
void bank_acc::show_account() const 
{
    cout << left;
    cout << setw(20) << "Account Number:" << account_number << endl;
    cout << setw(20) << "Name:" << name << endl;
    cout << setw(20) << "Account type:" << acctype << endl;
    cout << "\n************PERSONAL INFORMATION***************\n";
    cout << setw(20) << "Gender:" << gender << endl;
    cout << setw(20) << "Date of Birth:" << bday << endl;
    cout << setw(20) << "E-mail:" << email << endl;
    cout << setw(20) << "Account Balance:" << depos << endl;
}

// Function to return account number
int bank_acc::return_accnum() const 
{
    return account_number;
}

// Function to modify email
void bank_acc::modify_email() 
{
    cout << "New Email: ";
    cin.ignore();
    getline(cin, email);
    cout << "Successfully modified." << endl;
}

// Function for Money Deposit
void bank_acc::deposit(int amount)
{
    depos += amount;
    transactions += "Deposit: " + to_string(amount) + "\r\n";
    cout << "Amount deposited successfully.";
}

// Function for Money Withdrawal
void bank_acc::withdrawal(int amount)
{
    if (amount > depos) 
    {
        cout << "Insufficient balance.";
    } 
    else 
    {
        depos -= amount;
        transactions += "Withdrawal: " + to_string(amount) + "\r\n";
        cout << "Amount withdrawn successfully.";
    }    
}

// Function to view Previous Transactions
void bank_acc::prev_transaction() const
{
    cout << "TRANSACTION HISTORY:\n" << endl;
    cout << transactions << "\n" << endl;
}

// Function to write account details to file
void write_account() 
{
    bank_acc acc;
    ofstream outF;
    outF.open("bank_acc.txt", ios::app | ios::binary);
    acc.create_account();
    outF << acc.return_accnum() << endl;
    outF << acc.name << endl;
    outF << acc.gender << endl;
    outF << acc.bday << endl;
    outF << acc.email << endl;
    outF << acc.valid_id << endl;
    outF << acc.id_num << endl;
    outF << acc.acctype << endl;
    outF << acc.depos << endl;
    outF.close();
}

// Function to display account details from file
void account_details(int n) {
    bank_acc acc;
    bool found = false;
    ifstream inFile;
    inFile.open("bank_acc.txt");
    if (!inFile) {
        cout << "File could not be opened!! Press any key...";
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin.get();
        return;
    }

    cout << "\n****************ACCOUNT DETAILS****************\n";
    while (inFile >> acc.account_number) {
        inFile.ignore();
        inFile.getline(acc.name, 100);
        inFile.getline(acc.gender, 10);
        getline(inFile, acc.bday);
        getline(inFile, acc.email);
        inFile.getline(acc.valid_id, 100);
        getline(inFile, acc.id_num);
        inFile.getline(acc.acctype, 10);
        inFile >> acc.depos;
        inFile.ignore();
        if (acc.return_accnum() == n) {
            acc.show_account();
            found = true;
        }
    }
    inFile.close();
    if (!found) {
        cout << "Account number does not exist" << endl;
    }
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
}


void delete_account(int n) {
    bank_acc acc;
    ifstream inFile;
    ofstream outFile;
    bool found = false;

    inFile.open("bank_acc.txt");
    outFile.open("temp.txt");

    if (!inFile || !outFile) {
        cout << "File could not be opened!! Press any key...";
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin.get();
        return;
    }

    while (inFile >> acc.account_number) {
        inFile.ignore();
        inFile.getline(acc.name, 100);
        inFile.getline(acc.gender, 10);
        getline(inFile, acc.bday);
        getline(inFile, acc.email);
        inFile.getline(acc.valid_id, 100);
        getline(inFile, acc.id_num);
        inFile.getline(acc.acctype, 10);
        inFile >> acc.depos;
        inFile.ignore();

    if (acc.return_accnum() != n) {
            outFile << acc.return_accnum() << endl;
            outFile << acc.name << endl;
            outFile << acc.gender << endl;
            outFile << acc.bday << endl;
            outFile << acc.email << endl;
            outFile << acc.valid_id << endl;
            outFile << acc.id_num << endl;
            outFile << acc.acctype << endl;
            outFile << acc.depos << endl;

        } else {
            found = true;
        }
    }

    inFile.close();
    outFile.close();
    remove("bank_acc.txt");
    rename("temp.txt", "bank_acc.txt");
    if (found) {
        cout << "Account deleted successfully." << endl;
    } else {
        cout << "Account number does not exist." << endl;
    }

    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();

}

// Function to display balance details and transaction history from file
void balance_transactions(int n, int option) 
{
    bank_acc acc;
    bool found = false;
    ifstream inF;
    inF.open("bank_acc.txt");
    if (!inF) 
	{
        cout << "This file does not exist. Please enter any key." << endl;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin.get();
        return;
    }
    cout << "\n****************BALANCE DETAILS****************\n";
    while (inF >> acc.account_number) 
	{
        inF.ignore();
        inF.getline(acc.name, 100);
        inF.getline(acc.gender, 10);
        getline(inF, acc.bday);
        getline(inF, acc.email);
        inF.getline(acc.valid_id, 100);
        getline(inF, acc.id_num);
        inF.getline(acc.acctype, 10);
        inF >> acc.depos;
        inF.ignore();
        getline(inF, acc.transactions);
        if (acc.return_accnum() == n) 
        {
            if (option == 1) 
            {
                acc.show_account();
            } 
            else if (option == 2) 
            {
                acc.prev_transaction();
            }
            found = true;
        }
    }
    inF.close();
    if (!found) 
	{
        cout << "Account number does not exist" << endl;
    }
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
}

// Function to write deposit and withdrawal details
void deposit_withdraw(int n, int option)
{
    int amt;
    bool found = false;
    bank_acc acc;
    ifstream inF;
    ofstream outF;
    inF.open("bank_acc.txt");
    outF.open("dep_with.txt");
    if (!inF || !outF)
    {
        cout << "This file does not exist. Please enter any key." << endl;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');;
        cin.get();
        return;
    }
    while (inF >> acc.account_number) 
    {
        inF.ignore();  
        inF.getline(acc.name, 100);
        inF.getline(acc.gender, 10);
        getline(inF, acc.bday);
        getline(inF, acc.email);
        inF.getline(acc.valid_id, 100);
        getline(inF, acc.id_num);
        inF.getline(acc.acctype, 10);
        inF >> acc.depos;
        inF.ignore();  
        getline(inF, acc.transactions);
        if (acc.return_accnum() == n) 
        {
            if (option == 1) 
            {
                cout << "Enter amount to deposit: ";
                cin >> amt;
                acc.deposit(amt);
            } 
            else if (option == 2) 
            {
                cout << "Enter amount to withdraw: ";
                cin >> amt;
                acc.withdrawal(amt);
            }
            found = true;
        }
        outF << acc.return_accnum() << endl;
        outF << acc.name << endl;
        outF << acc.gender << endl;
        outF << acc.bday << endl;
        outF << acc.email << endl;
        outF << acc.valid_id << endl;
        outF << acc.id_num << endl;
        outF << acc.acctype << endl;
        outF << acc.depos << endl;
        outF << acc.transactions << endl;
    }
    
    inF.close();
    outF.close();
    remove("bank_acc.txt");
    rename("dep_with.txt", "bank_acc.txt");
    if (!found) 
    {
        cout << "Account number does not exist" << endl;
    }
    cin.ignore(numeric_limits<streamsize>::max(), '\n');;
    cin.get();
}

// Function to modify account details
void modify_acc(int n) 
{
    bool found = false;
    bank_acc acc;
    ifstream inF;
    ofstream outF;
    inF.open("bank_acc.txt");
    outF.open("temp.txt");
    if (!inF || !outF) 
	{
        cout << "File could not be opened!! Press any key...";
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin.get();
        return;
    }
    while (inF >> acc.account_number) 
	{
        inF.ignore();
        inF.getline(acc.name, 100);
        inF.getline(acc.gender, 10);
        getline(inF, acc.bday);
        getline(inF, acc.email);
        inF.getline(acc.valid_id, 100);
        getline(inF, acc.id_num);
        inF.getline(acc.acctype, 10);
        inF >> acc.depos;
        inF.ignore();
        if (acc.return_accnum() == n) 
		{
            acc.show_account();
            cout << "===Modify Email===" << endl;
            acc.modify_email();
            found = true;
        }
        outF << acc.return_accnum() << endl;
        outF << acc.name << endl;
        outF << acc.gender << endl;
        outF << acc.bday << endl;
        outF << acc.email << endl;
        outF << acc.valid_id << endl;
        outF << acc.id_num << endl;
        outF << acc.acctype << endl;
        outF << acc.depos << endl;
    }
    inF.close();
    outF.close();
    remove("bank_acc.txt");
    rename("temp.txt", "bank_acc.txt");
    if (!found) 
	{
        cout << "Record Not Found.." << endl;
    }
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
}

// Function for user login
bool login(User users[], int size) 
{
    string inputUsername, inputPassword;
    cout << "===============================================\n";
    cout << "***********************************************\n";
    cout << "                     LOG IN                    \n";
    cout << "***********************************************\n";
    cout << "===============================================\n";
    cout << "Enter username: ";
    cin >> inputUsername;
    cout << "Enter password: ";
    cin >> inputPassword;

    for (int i = 0; i < size; i++) 
	{
        if (inputUsername == users[i].username && inputPassword == users[i].password) 
		{
            users[i].loggedIn = true;
            cout << "\nLogin successful!\n" << endl;
            return true;
        }
    }
    cout << "\nInvalid username or password.\n" << endl;
    return false;
}

// Function for user logout
void logout(User users[], int size) 
{
    char confirm;
    cout << "Are you sure you want to log out? (Y/N): ";
    cin >> confirm;
    cout << "\n";

    if (confirm == 'y' || confirm == 'Y') 
	{
        for (int i = 0; i < size; i++) {
            if (users[i].loggedIn) 
			{
                users[i].loggedIn = false;
                cout << "Logout successful! Returning to homepage...\n" << endl;
                return;
            }
        }
    } 
	else 
	{
        cout << "\nLogout canceled.\n" << endl;
    }
}

int main() 
{
    int num;
    User users[2] = {{"teller", "tellermmm", false}, {"manager", "managermmm", false}}; // Menu options for teller and manager should be different
    int choice;

    do {
        bool isLoggedIn = false;
        for (int i = 0; i < 2; i++) 
		{
            if (users[i].loggedIn) 
			{
                isLoggedIn = true;
                break;
            }
        }

        if (!isLoggedIn) 
		{
            cout << "===============================================\n";
            cout << "***********************************************\n";
            cout << "              WELCOME TO THE BANK              \n";
            cout << "***********************************************\n";
            cout << "===============================================\n";
            cout << "1. Login\n";
            cout << "2. Exit\n";
            cout << "\n";
            cout << "Enter your choice: ";
            cin >> choice;
            cout << " \n";

            switch (choice) 
			{
                case 1:
                    login(users, 2);
                    break;
                case 2:
                    cout << "Exiting..." << endl;
                    return 0;
                    break;
                default:
                    cout << "Invalid choice. Please try again." << endl;
                    break;
            }
        } 
		else 
		{
            cout << "===============================================\n";
            cout << "***********************************************\n";
            cout << "                 MAIN MENU                     \n";
            cout << "***********************************************\n";
            cout << "===============================================\n";
            cout << "1. Create Account" << endl;
            cout << "2. Show Account Details" << endl;
            cout << "3. Modify Account" << endl;
            cout << "4. Deposit" << endl;
            cout << "5. Withdraw" << endl;
            cout << "6. Check Balance" << endl;
            cout << "7. Previous Transactions" << endl;
            cout << "8. Logout" << endl;
            cout << " " << endl;
            cout << "Enter your choice: ";
            cin >> choice;
            cout << " " << endl;

            switch (choice) 
			{
			    case 1:
			        write_account();
			        break;
                case 2:
                    cout << "\n****************ACCOUNT NUMBER*****************\n";
                    cout << "Enter account number: ";
                    cin >> num;
                    account_details(num);
                    break;
                case 3:
                    cout << "\n****************ACCOUNT NUMBER*****************\n";
                    cout << "Enter account number: ";
                    cin >> num;
                    modify_acc(num);
                    break;
                case 4: // Deposit
                    cout << "\n****************ACCOUNT NUMBER*****************\n";
                    cout << "Enter account number: ";
                    cin >> num;    
                    deposit_withdraw(num, 1);
                    break;
                case 5: // Withdrawal
                    cout << "\n****************ACCOUNT NUMBER*****************\n";
                    cout << "Enter account number: ";
                    cin >> num;    
                    deposit_withdraw(num, 2);
                    break;
                case 6: // Balance Inquiry
                    cout << "\n****************ACCOUNT NUMBER*****************\n";
                    cout << "Enter account number: ";
                    cin >> num;  
                    balance_transactions(num, 1);
                    break;
                case 7: //  Previous Transactions
                    cout << "\n****************ACCOUNT NUMBER*****************\n";
                    cout << "Enter account number: ";
                    cin >> num; 
                    balance_transactions(num, 2);
                    break;
                case 8:
                    logout(users, 2);
                    break;
                default:
                    cout << "Invalid choice. Please try again." << endl;
                    break;
            }
        }
    } while (choice != 2 || choice != 8);

    return 0;
}
Leave a Comment