Saturday, June 3, 2017

Programming Challenge 11.7 - Customer Accounts

Include-File: UtilityCls.h


/* Customer Accounts - This program uses a structure to store the following
    data about a customer account:

        * Name
        * Address
        * City, State, and ZIP
        * Telephone Number
        * Account Balance
        * Date of Last Payment

    The program uses an array of at least 10 structures. It lets the user
    enter data into the array, change the contents of any element, and
    display all the data stored in the array. The program has a menu-driven
    interface.

    Input Validation: When the data for a new account is entered, ensure
    that the user has entered data for all the fields. No negative account
    balances are allowed. */

#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() ||
                 accHolder[index].address[index] == ' ')
        {
            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 displays a list of customer names, and asks the user to
    enter a number displayed to the left of the names. It is
    used to display information about a specific account, as
    well as to change a specific account, stored in the array
    of structures.
    ********************************************************** */

int getAccID(AccountData accHolder[], const int ACCOUNTS)
{
    int accID = 0;

    for (int index = 0; index < ACCOUNTS; index++)
    {
        cout << "\tAccount # " << setw(3) << left << (index + 1)
              << accHolder[index].name << "\n";
    }

    cout << "\n\tEnter Account Number: ";
    cin >> accID;

    accID -= 1;        /* To keep input in sync with array index */

    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;

    accID = getAccID(accHolder, ACCOUNTS);
   
    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