Monday, July 24, 2017

Programming Challenge 12.14 - Inventory Screen Report

Example File: PR_Item.dat


/* Inventory Screen Report - This program reads the data in the file created
    by the program in Programming Challenge 12.13. The program calculates and
    displays the following data:
   
        * The total wholesale value of the inventory
        * The total retail value of the inventory
        * The total quantity of all items in the inventory */

#include "Utility.h"

const int DESCR_SIZE = 25;
const int DATE_SIZE = 12;
const string ADMIN_NAME = "Administrator Rhodan";

struct Inventory
{
    int    numRecord;                       /* Holds the record number                */
    char   itemDescr[DESCR_SIZE];        /* Holds the item name                    */
    int    atHand;                            /* Quantity of items available        */
    double wholesaleCost;                /* Holds the wholesale cost            */
    double retailCost;                    /* Holds the retail cost                */
    char   dateAdded[DATE_SIZE];        /* Holds the date an item was added */

    /* Inventory Constructor */
    Inventory()
    {
        numRecord = 0;
        itemDescr[DESCR_SIZE] = ' ';
        atHand = 0;
        wholesaleCost = 0.0;
        retailCost = 0.0;
        dateAdded[DATE_SIZE] = ' ';
    }

    /* Inventory Destructor */
    ~Inventory()
    {
    }
};

struct ItemValue
{
    int    atHandTotal;        /* Holds the total amount of items available       */
    double wholesaleTotal;    /* Holds the total wholesale value of all items */
    double retailTotal;        /* Holds the total retail value of all items    */

    ItemValue()
    {
        atHandTotal = 0;
        wholesaleTotal = 0.0;
        retailTotal = 0.0;
    }   

    ~ItemValue()
    {
    }
};

int readFile(Inventory &, ItemValue &);
void calcValue(Inventory &, ItemValue &);
void showInventory(const Inventory &);
void showValue(const ItemValue &);

int main()
{
    ItemValue value;
    Inventory record;

    int fOpen = 0;

    cout << "\nCOSMIC WAREHOUSE COMPANY - TERRA HQ.\n\n"
          << "Welcome " << ADMIN_NAME << "!\n";

    fOpen = readFile(record, value);

    if (fOpen != -1)
    {
        cout << "\nI will now shut this program down, " << ADMIN_NAME << ".\n"
              << "The Cosmic Warehouse Company Item Record System "
              << "wishes you a successful day!";
    }


    pauseSystem();
    return 0;
}

/* **********************************************************
   Definition: readFile

    This function accepts two structures passed by reference
    as its arguments. The user is asked to enter the name of
    the file containing the item records. Upon success, the
    records are read in and processed. If an error occurs, the
    user is informed by a message, and the function exits.
   ********************************************************** */

int readFile(Inventory &items, ItemValue &value)
{
    string fileName = "";

    cout << "\nPlease enter the name of the file you wish me to retrieve\n"
          << "item information from, " << ADMIN_NAME << ": ";
    cin >> fileName;

    fstream readRec(fileName.c_str(), ios::in | ios::binary);

    if (!readRec.fail())
    {
        cout << "\nI successfully retrieved the data from " << fileName
              << ", " << ADMIN_NAME << "...\n"
              << "I will now process and display all available data ...\n\n";

        while (readRec.read(reinterpret_cast<char *>(&items), sizeof(items)))
        {
            calcValue(items, value);
            showInventory(items);
        }
        showValue(value);
    }
    else
    {
        cout << "\nI could not retrieve any item information from " << fileName << ".\n"
              << "\nSorry for the inconvenience, " << ADMIN_NAME << ".\n"
              << "A service technician will immediately resolve this problem.\n"
              << "The Cosmic Warehouse Company Item Record System will now shut down ...";
        return -1;
    }
    readRec.close();

    return 0;
}

/* **********************************************************
   Definition: calcValue

    This function accepts two structures passed by reference
    as its arguments. It calculates the total wholesale and
    retail values, as well as the total number of items. This
    data is stored in the appropriate member variables of the
    ItemValue structure.
   ********************************************************** */

void calcValue(Inventory &items, ItemValue &calcValue)
{
        calcValue.wholesaleTotal += items.wholesaleCost * items.atHand;
        calcValue.retailTotal     += items.retailCost * items.atHand;
        calcValue.atHandTotal     += items.atHand;
}

/* **********************************************************
   Definition: showInventory

    This function accepts a const structure variable passed to
    it by reference as its argument. It displays information
    about all items stored in the structure member variables.
   ********************************************************** */

void showInventory(const Inventory &items)
{

    cout << setw(19) << left << "Item Record Number:\t\t"
          << items.numRecord << "\n";
    cout << setw(19) << left << "Item Description:\t\t"
          << items.itemDescr << "\n";
    cout << setw(17) << left << "Quantity Available:\t\t"
          << items.atHand << "\n";
    cout << showpoint << fixed << setprecision(2);
    cout << setw(16) << left << "Wholesale Cost: "
          << setw(15) << right << "$ "
          << setw(9) << right << items.wholesaleCost << "\n";
    cout << setw(16) << left << "Retail    Cost: "
          << setw(15) << right << "$ "
         << setw(9) << right << items.retailCost << "\n";
    cout << "Date Added: " << setw(30) << right << items.dateAdded << "\n\n";
}

/* **********************************************************
   Definition: showValue

    This function accepts a const structure member variable
    passed by reference as its argument. It displays:

        * The total wholesale value of the items
        * The total retail value of the items
        * The total number of available items
   ********************************************************** */

void showValue(const ItemValue &value)
{
    cout << setw(16) << left << "Total Wholesale Value: "
          << setw(8) << right << "$ "
          << setw(4) << right << value.wholesaleTotal << "\n";
    cout << setw(16) << left << "Total Retail    Value: "
          << setw(8) << right << "$ "
          << setw(4) << right << value.retailTotal << "\n\n";
    cout << setw(19) << left << "Total Items Available: "
          << setw(12) << right << value.atHandTotal << "\n";
}

Example Output:





No comments:

Post a Comment