Sunday, June 4, 2017

Programming Challenge 11.8 - Search Function For Customer Accounts

Include File:   UtilityCls.h
Example File: nameDB.txt


/* Search Function For Customer Accounts - This program is a modification
    of Programming Challenge 11.7. It adds a function that allows the user
    to search the structure array for a particular customer's account. It
    accepts part of the customer's name as an argument and then searches
    for an account with a name that matches it. All accounts that match are
    displayed. If no account matches, a message saying so is displayed. */

#include "UtilityCls.h"

struct AccountData
{
    int    accNumber;                /* The account number for new accounts             */   
    string name;                    /* The account holder's name                         */
    string address;                /* The account holder's address                     */
    string city;                    /* The city the account holder lives in         */
    string state;                    /* The state the account holder lives in         */
    string zipCode;                /* The states ZIP-code                                 */
    string telephoneNumber;        /* The account holder's telephone number         */
    double accountBalance;        /* The account balance                                 */
    string dateLastPayment;        /* The date of his or her last payment             */
};

enum menuSelection
{
    ENTER_DATA = 1,
    CHANGE_DATA = 2,
    VIEW_DATA = 3,
    QUIT = 4
};

enum dataMode
{
    GET_ACC_DATA = 1,
    CHANGE_ACC_DATA = 2
};

void menu(AccountData [], const int);
void getAccData(AccountData [], int, const int);
int  getAccID(AccountData [], const int);
bool validateInput(AccountData [], int, const int);
bool findData(AccountData[], int);
void displayData(AccountData [], const int);

int main()
{
    const int ACCOUNTS = 10;

    AccountData accData[ACCOUNTS];

    menu(accData, ACCOUNTS);

    pauseSystem();
    return 0;
}

/* **********************************************************
    Definition: menuItems

    This function presents a menu-screen to the user from
    which he or she can select one of the available options.
    ********************************************************** */

int menuItems()
{
    int menuItem = 0;

    cout << "\n\t\tASHIKAGA BANK - ACCOUNT MANAGER\n\n"
          << "\tMenu Selection\n\n"
          << "\t1. Enter Account Data\n"
          << "\t2. Change Account Data\n"
          << "\t3. View Account Data\n"
          << "\t4. Quit\n\n"
          << "\tYour Selection: ";
    cin >> menuItem;

    while (menuItem < ENTER_DATA || menuItem > QUIT)
    {
        cout << "\n\tMenu item " << menuItem << " does not exist.\n\n"
              << "\tYour selection: ";
        cin >> menuItem;
    }

    return menuItem;
}

/* **********************************************************
   Definition: menu

    This function represents the main-menu, from which all the
    other functions in this program are called.
   ********************************************************** */

void menu(AccountData accHolder[], const int ACCOUNTS)
{
    int selection = 0;
    int mode = 0;

    do
    {
        selection = menuItems();

        switch (selection)
        {
            case ENTER_DATA:
            {
                clearScreen();

                cout << "\n\tASHIKAGA BANK - CUSTOMER ACCOUNT DATA SYSTEM\n\n";           
                mode = GET_ACC_DATA;
                getAccData(accHolder, mode, ACCOUNTS);
            } break;

            case CHANGE_DATA:
            {
                clearScreen();

                cout << "\n\tASHIKAGA BANK - CHANGE CUSTOMER ACCOUNT DATA\n\n";
                mode = CHANGE_ACC_DATA;
                getAccData(accHolder, mode, ACCOUNTS);
            } break;

            case VIEW_DATA:
            {
                clearScreen();

                cout << "\n\tASHIKAGA BANK - DISPLAY ACCOUNT INFORMATION\n\n";
                displayData(accHolder, ACCOUNTS);
            } break;

            case QUIT:
            {
                cout << "\n\tASHIKAGA BANK - CUSTOMER ACCOUNT DATA SYSTEM LOGOUT\n\t"
                      << "Remember Policy: Customer First!\n\n";
            } break;
        }
    } while (selection != QUIT);
}

/* **********************************************************
    Definition: getAccData

    This function accepts an array of structures as argument.
    The mode passed to the function determines what the user
    is asked for to input:
   
        * Account data (this includes entering an account-ID.)
        * Change data for an existing account (this excludes
          the account-ID member).

    In this way the function fulfills double-duty. It allows
    both entering a new set of data, as well as to change data
    for a specific account.
    ********************************************************** */

void getAccData(AccountData accHolder[], int mode, const int ACCOUNTS)
{
    int index = 0;
    int accNum = 0;
    int numAcc = ACCOUNTS;
    bool found = false;
    bool valid = false;

    if (mode == CHANGE_ACC_DATA)
    {
        accNum = getAccID(accHolder, ACCOUNTS);

        index = accNum;       
        numAcc = 1;               
    }

    do
    {
        /* This if-statement is only executed when the user has selected
           the option to enter data from the menu. */
        if (mode == GET_ACC_DATA)
        {
            cout << setw(29) << left << "\n\tEnter New Account ID: ";
            cin >> accHolder[index].accNumber;

            valid = validateInput(accHolder, index, ACCOUNTS);

            while (found = findData(accHolder, index))
            {
                cout << setw(29) << left << "\n\tEnter New Account ID: ";
                cin >> accHolder[index].accNumber;
            }
        }

        cout << setw(30) << left << "\n\tEnter Name: ";
        cin.ignore();
        getline(cin, accHolder[index].name);
       
        while (accHolder[index].name.empty() ||
                accHolder[index].name[index] == ' ')
        {
            cout << setw(28) << left << "\tEnter Name:";
            getline(cin, accHolder[index].name);
        }

        cout << setw(29) << left << "\tEnter Address:";
        getline(cin, accHolder[index].address);

        while (accHolder[index].address.empty())
        {
            cout << setw(29) << left << "\tEnter Address:";
            getline(cin, accHolder[index].address);
        }

        cout << setw(29) << left << "\tEnter Name of City:";
        getline(cin, accHolder[index].city);

        while (accHolder[index].city.empty() ||
                   accHolder[index].city[index] == ' ')
        {
            cout << setw(29) << left << "\tEnter Name of City: ";
            getline(cin, accHolder[index].city);
        }

        cout << setw(29) << left << "\tEnter Name of State:";
        getline(cin, accHolder[index].state);

        while (accHolder[index].state.empty())
        {
            cout << setw(29) << left << "\tEnter Name of State:";
            getline(cin, accHolder[index].state);
        }

        cout << setw(29) << left << "\tEnter ZIP-Code:";
        getline(cin, accHolder[index].zipCode);

        while (accHolder[index].zipCode.empty()  ||
                 accHolder[index].zipCode.length() != 8)
        {
            cout << setw(29) << left << "\tEnter ZIP-Code";
            getline(cin, accHolder[index].zipCode);
        }

        cout << setw(29) << left << "\tEnter Telephone Number:";
        getline(cin, accHolder[index].telephoneNumber);

        while (accHolder[index].telephoneNumber.empty())
        {
            cout << setw(29) << left << "\tEnter Telephone Number: ";
            getline(cin, accHolder[index].telephoneNumber);
        }

        cout << setw(29) << left << "\tEnter Account Balance: ";
        cin >> accHolder[index].accountBalance;

        while (accHolder[index].accountBalance <= 0)
        {
            cout << setw(29) << left << "\tEnter Account Balance: ";
            cin >> accHolder[index].accountBalance;
        }

        cout << setw(29) << left << "\tEnter Date of Last Payment: ";
        cin.ignore();
        getline(cin, accHolder[index].dateLastPayment);

       
        while (accHolder[index].dateLastPayment.empty() ||
                accHolder[index].dateLastPayment.length() < 10)
        {
            cout << setw(29) << left << "\tEnter Date of Last Payment: ";
            getline(cin, accHolder[index].dateLastPayment);
        }

        index += 1;
    } while (index < numAcc);
}

/* **********************************************************
   Definition: findData

    This function accepts an array of structures, as well as
    an array index as arguments. It determines, whether an
    account ID already exists in the database. The result is
    returned.
   ********************************************************** */

bool findData(AccountData accHolder[], int accID)
{
    bool found = false;

    for (int index = 0; index < accID; index++)
    {
        if (accHolder[index].accNumber == accHolder[accID].accNumber)
        {
            cout << "\n\tAccount-ID taken.\n";
            found = true;
        }
    }

    return found;
}

/* **********************************************************
    Definition: validateInput

    This function accepts an array of structures, as well as
    an array index as arguments. It verifies, that the account
    number is not lower than or equal to 0.
    ********************************************************** */

bool validateInput(AccountData accHolder[], int accID, const int ACCOUNTS)
{
    bool valid = false;

    while (accHolder[accID].accNumber <= 0)
    {
        cout << setw(29) << left << "\n\tEnter New Account ID: ";
        cin >> accHolder[accID].accNumber;
    }

    return valid;
}

/* **********************************************************
    Definition: getAccID

    This function accpets an array of structures as argument.
    It asks the user to enter a full or partial name. If the
    name or names are found, they are displayed, and the user
    is asked to enter the ID of the account he or she wishes
    to view or change data for. If no match is found, the user
    is informed by a message.
    ********************************************************** */

int getAccID(AccountData accHolder[], const int ACCOUNTS)
{
    int     index = 0;
    int     accID = 0;
    string searchName = " ";
    bool     found = false;

    cout << "\n\tEnter a name (partial or full) to search for: ";
    cin.ignore();
    getline(cin, searchName);

    for (index = 0; index < ACCOUNTS; index++)
    {
        if (accHolder[index].name.find(searchName) != string::npos)
        {       
            cout << "\n\tAccount Name: " << (index + 1)
                  << " " << accHolder[index].name << "\n";
            found = true;
        }
    }

    if (found == true)
    {
        cout << "\n\tEnter Account ID: ";
        cin >> accID;

        accID -= 1;        /* To keep input in sync with array indices */
    }
    else
    {
        cout << "\n\tThis name was not found in database!\n";
        accID = -1;
    }
   
    return accID;
}

/* **********************************************************
   definition: displayData

    This function accepts an array of structures as argument.
    It displays information about a specific customer, stored
    in the array.
   ********************************************************** */

void displayData(AccountData accHolder[], const int ACCOUNTS)
{
    int accID = 0;
    int index = 0;

    /* This is going to hold the name-search */
    accID = getAccID(accHolder, ACCOUNTS);
   
    if (accID != -1)
    {
        cout << fixed << showpoint << setprecision(2);

        cout << setw(24) << left << "\n\tAccount ID #"
                << accHolder[accID].accNumber
              << setw(24) << left << "\n\tAccount Holder: "
              << accHolder[accID].name
              << setw(24) << left << "\n\tAddress: "
              << accHolder[accID].address
              << setw(24) << left << "\n\tCity: "
              << accHolder[accID].city
              << setw(24) << left << "\n\tState: "
              << accHolder[accID].state
              << setw(24) << left << "\n\tZIP-Code: "
              << accHolder[accID].zipCode
              << setw(24) << left << "\n\tTelephone #"
              << accHolder[accID].telephoneNumber
              << setw(24) << left << "\n\tAccount Balance JPY: "
              << accHolder[accID].accountBalance
              << setw(24) << left << "\n\tDate of Last Payment: "
              << accHolder[accID].dateLastPayment << "\n\n";
    }
}

Example Output:






No comments:

Post a Comment