Monday, August 21, 2017

Programming Challenge 13.6 - Inventory Class

Example Files: InventoryClass.h
                          InventoryClass.cpp
                          InventoryClassDemo.cpp

IventoryClass.h


/* InventoryClass.h - Specification file for the InventoryClass class. */

#ifndef INVENTORY_CLASS_H
#define INVENTORY_CLASS_H

class Inventory
{
    private:
        int itemNumber;      // The item number
        int quantity;             // Number of items on hand
        double cost;             // Wholesale per-unit cost of the item
        double totalCost;     // Total cost of the item

    public:
        // Default constructor
        Inventory()
        {
            itemNumber = 0;
            quantity = 0;
            cost = 0.0;
            totalCost = 0.0;
        }

        // Constructor accepting arguments for itemID, quantity and cost
        Inventory(int iID, int iQty, double iCost)
        {
            itemNumber = iID;
            quantity = iQty;
            cost = iCost;
        }

        // Mutators
        bool setItemNumber(int iID);
        bool setQuantity(int iQty);
        bool setCost(double iCost);
        void setTotalCost(double iCost);

        // Accessors
        int getItemNumber() const
        { return itemNumber; }

        int getQuantity() const
        { return quantity; }

        double getCost() const
        { return cost; }

        double getTotalCost() const
        { return totalCost; }   
};
#endif

InventoryClass.cpp


/* InventoryClass.cpp - Implementation file for the InventoryClass class. */

#include "InventoryClass.h"
#include <string>
#include <iostream>

/* **********************************************************
                Inventory::setItemNumber
    Sets the value of the member variable itemNumber.
   ********************************************************** */

bool Inventory::setItemNumber(int iID)
{
    if (iID > 0)
    {
        itemNumber = iID;
        return true;
    }
    else
    {
        std::cout << "\nInvalid item ID.\n\n";
    }

    return false;
}

/* **********************************************************
                Inventory::setQuantity
    Sets the value of the member variable quantity.
   ********************************************************** */

bool Inventory::setQuantity(int iQty)
{
    if (iQty > 0)
    {
        quantity = iQty;
        return true;
    }
    else
    {
        std::cout << "\nItem quantity cannot be 0 or negative.\n\n";
    }

    return false;
}

/* **********************************************************
                Inventory::setCost
    Sets the value of the member variable cost.
   ********************************************************** */

bool Inventory::setCost(double iCost)
{
    if (iCost > 0)
    {
        cost = iCost;
        return true;
    }
    else
    {
        std::cout << "\nWholesale cost cannot be 0 or negative.\n";
    }

    return false;
}

/* **********************************************************
                Inventory::setItemNumber
    Sets the value of the member variable totalCost. The total
    cost is calculated as quantity times cost.
   ********************************************************** */

void Inventory::setTotalCost(double iCost)
{
    totalCost = (quantity * iCost);
}

InventoryClassDemo.cpp


/* InventoryClass.cpp - This program demonstrates the InventoryClass class. */

#include "InventoryClass.h"
#include <iomanip>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::setw;
using std::setprecision;
using std::showpoint;
using std::fixed;

void getItemInfo(Inventory *, const int);
void displayItems(const Inventory *, const int);

int main()
{
    const int NUM_ITEMS = 2;
    Inventory items[NUM_ITEMS];

    cout << "CYBER CITY BATTLE-CHIP WAREHOUSE\n\n";

    getItemInfo(items, NUM_ITEMS);

    for (int i = 0; i < NUM_ITEMS; i++)
    {
        items[i].setTotalCost(items[i].getCost());
    }

    displayItems(items, NUM_ITEMS);

    cin.get();
    cin.ignore();
    return 0;
}

/* **********************************************************
    getItemInfo (accepts an array of RetailItem objects and
    an integer value holding the number of items as arguments)
    It asks the user to enter an item ID, the quantity on hand
    and the cost of an item.
   ********************************************************** */

void getItemInfo(Inventory *items, const int NUM_ITEMS)
{
    int    itemID = 0;
    int    itemQuantity = 0;
    double itemCost = 0.0;

    for (int i = 0; i < NUM_ITEMS; i++)
    {
        cout << "Enter Item Number: " << setw(5) << "\n";
        cin >> itemID;
       
        while (!items[i].setItemNumber(itemID))
        {
            cout << "Enter Item Number: " << setw(5) << "\n";
            cin >> itemID;
        }

        cout << "Enter Quantity: " << setw(4) << "\n";
        cin >> itemQuantity;

        while (!items[i].setQuantity(itemQuantity))
        {
            cout << "Enter Quantity: " << setw(5) << "\n";
            cin >> itemQuantity;
        }

       cout << "Enter Wholesale Cost: " << setw(4) << "\n";
        cin >> itemCost;

        while (!items[i].setCost(itemCost))
        {
            cout << "Enter Wholesale Cost: " << setw(5) << "\n";
            cin >> itemCost;
        }
        cout << "\n";
    }
}

/* **********************************************************
    displayItems (accepts an array of RetailItem objects and
    an integer value holding the number of items as arguments)
    It displays information about the items.
   ********************************************************** */

void displayItems(const Inventory *items, const int NUM_ITEMS)
{
    cout << "\nCYBER CITY BATTLE-CHIP WAREHOUSE\n\n"
         << setw(5)  << "Item ID"
          << setw(19) << "Units on Hand" << setw(16)
          << setw(13) << "Cost" << setw(13)
          << setw(20) << "Total Cost\n";
    cout << "==========================================================\n";

    for (int i = 0; i < NUM_ITEMS; i++)
    {
        cout << setw(7)  << items[i].getItemNumber()
              << setw(19) << items[i].getQuantity();
        cout << showpoint << fixed << setprecision(2);
        cout << setw(13) << items[i].getCost()
              << setw(19) << items[i].getTotalCost() << "\n";
    }
}

Example Output:





No comments:

Post a Comment