Tuesday, June 6, 2017

Programming Challenge 11.9 - Speaker's Bureau

Include File: UtilityCls.h


/* Speaker's Bureau - This program keeps track of a speaker's bureau. The
    program uses a structure to store the following data about a speaker:

        * Name
        * Telephone Number
        * Speaking Topic
        * Fee Required

    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
    user interface.

    Input Validation: When the data for a new speaker is entered, the program
    ensures that the user has entered data for all the fields. No negative
    amounts for a speaker's fee are accepted. */

#include "UtilityCls.h"

struct SpeakerInfo
{
    string name;                /* The speaker's name                 */
    string phoneNum;            /* The speaker's telephone number */
    string speakingTopic;    /* The speaking topic                 */
    double fee;                   /* The speaker's fee                     */   
};

enum menuSelection
{
    GET_SPEAKER_INFO = 1,
    CHANGE_SPEAKER_INFO = 2,
    DISPLAY_SPEAKER_INFO = 3,
    QUIT = 4
};

enum dataMode
{
    GET_INFO = 1,
    CHANGE_INFO = 2
};

void menu(SpeakerInfo [], const int);
int  menuItems();
void getSpeakerInfo(SpeakerInfo [], int, const int);
int  getSpeakerID(SpeakerInfo [], const int);
void displaySpeakerInfo(const SpeakerInfo [], const int);

int main()
{
    const int NUM_SPEAKERS = 10;

    SpeakerInfo speaker[NUM_SPEAKERS];

    menu(speaker, NUM_SPEAKERS);

   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\tSPEAKER'S CORNER - SPEAKER INFORMATION\n\n"
          << "\tMenu Selection\n\n"
          << "\t1. Enter Speaker Info\n"
          << "\t2. Change Speaker Info\n"
          << "\t3. Display Speaker Info\n"
          << "\t4. Quit\n\n"
          << "\tYour Selection: ";
    cin >> menuItem;

    while (menuItem < GET_SPEAKER_INFO || 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(SpeakerInfo speaker[], const int NUM_SPEAKERS)
{
    int selection = 0;
    int mode = 0;

    do
    {
        selection = menuItems();

        switch (selection)
        {
            case GET_SPEAKER_INFO:
            {
                clearScreen();
                cout << "\n\tSPEAKER'S CORNER - ENTER SPEAKER INFORMATION\n\n\n";

                mode = GET_INFO;
                getSpeakerInfo(speaker, mode, NUM_SPEAKERS);
            } break;

            case CHANGE_SPEAKER_INFO:
            {
                clearScreen();

                cout << "\n\tSPEAKER'S CORNER - CHANGE SPEAKER INFORMATION\n\n";
                mode = CHANGE_INFO;
                getSpeakerInfo(speaker, mode, NUM_SPEAKERS);
            } break;

            case DISPLAY_SPEAKER_INFO:
            {
                clearScreen();

                cout << "\n\tSPEAKER'S CORNER - DISPLAY SPEAKER INFORMATION\n\n";
                displaySpeakerInfo(speaker, NUM_SPEAKERS);
            } break;

            case QUIT:
            {
                cout << "\n\tNow exiting 'Speaker's Corner' ...\n"
                          "\tHave a nice day!\n\n";
            } break;
        }
    } while (selection != QUIT);
}

/* **********************************************************
   Definition: getSpeakerID

    This function accepts an array of structure as argument.
    It displays a list of the speaker names. The user is asked
    to enter the speaker's ID, to determine whose info he or
    she wishes to change. The speaker's ID is returend.
   ********************************************************** */

int getSpeakerID(SpeakerInfo speaker[], const int NUM_SPEAKERS)
{
    int index = 0;
    int speakerID = 0;

    for (int index = 0; index < NUM_SPEAKERS; index++)
    {
        cout << "\tSpeaker # " << (index + 1) << "\t"
              << speaker[index].name << "\n";
    }

    cout << "\n\tEnter speaker ID: ";
    cin >> speakerID;

    cout << "\n\n";

    return speakerID - 1;
}

/* **********************************************************
   Definition: getSpeakerInfo

    This function asks the user to enter information about ten
    speakers. This information is stored in the appropriate
    members in the array of structures.
   ********************************************************** */

void getSpeakerInfo(SpeakerInfo speaker[], int mode, const int NUM_SPEAKERS)
{
    int index = 0;
    int numSpeakers = 0;

    if (mode == CHANGE_INFO)
    {
        index = getSpeakerID(speaker, NUM_SPEAKERS);
        numSpeakers = 1;
    }
    else
    {
        index = 0;
        numSpeakers = NUM_SPEAKERS;
    }

    do
    {
        cout << setw(23) << left << "\tSpeaker's Name: ";
        cin.ignore();
        getline(cin, speaker[index].name);

        while (speaker[index].name.empty())
        {
            cout << setw(23) << left << "\tSpeaker's Name: ";
            getline(cin, speaker[index].name);
        }

        cout << setw(22) << left << "\tSpeaker's Telephone #";
        getline(cin, speaker[index].phoneNum);

        while (speaker[index].phoneNum.empty())
        {
            cout << setw(22) << left << "\tSpeaker's Telephone #";
            getline(cin, speaker[index].phoneNum);
        }

        cout << "\tSpeaking Topic:\n\t";
        getline(cin, speaker[index].speakingTopic);

        while (speaker[index].speakingTopic.empty())
        {
            cout << setw(22) << left << "\tSpeaking Topic: ";
            getline(cin, speaker[index].speakingTopic);
        }

        cout << setw(23) << left << "\n\tRequired Fee $";
        cin >> speaker[index].fee;

        while (speaker[index].fee <= 0.0)
        {
            cout << setw(23) << left << "\tRequired Fee $";
            cin >> speaker[index].fee;
        }
        cout << "\n";

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

/* **********************************************************
   Definition: displaySpeakerInfo

    This function displays information about all speakers in
    the array of structures.
   ********************************************************** */

void displaySpeakerInfo(const SpeakerInfo speaker[], const int NUM_SPEAKERS)
{
    for (int index = 0; index < NUM_SPEAKERS; index++)
    {
        cout << fixed << showpoint << setprecision(2);
        cout << setw(22) << left << "\n\tSpeaker's Name: "
              << speaker[index].name
              << setw(22) << left << "\n\tSpeaker's Telephone #"
              << speaker[index].phoneNum
              << setw(22) << left << "\n\tSpeaking Topic: "
              << speaker[index].speakingTopic
              << setw(22) << left << "\n\tRequired Fee: $"
              << speaker[index].fee << "\n";
    }
    cout << "\n\n";
}

Example Output:








No comments:

Post a Comment