Monday, October 9, 2017

Programming Challenge 13.17 - Cash Register

Example Files: CashRegister.h
                          InventoryItem.h
                          ShoppingBasket.h
                          CashRegister.cpp
                          CashRegisterDm.cpp                       

CashRegister.h


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

#ifndef CASH_REGISTER_H
#define CASH_REGISTER_H

#include "InventoryItem.h"

class CashRegister
{
    private:
        int    quantity;            // Item quantity to be bought
        double cost;                // Cost of the item
        double markup;                // Markup percentage (30%)
        double taxRate;            // Tax rate (6%)
        double unitPrice;            // The unit price of an item
        double salesTax;            // The sales tax (6%)
        double taxTotal;            // The tax total (sales tax + markup)
        double subTotal;            // The subtotal (markup + cost)
        double total;                // The grand-total

    public:
        CashRegister(double iMarkup = 0.30, double rate = 0.06)
        {
            markup = iMarkup;
            taxRate = rate;

            unitPrice = 0.0;
            salesTax = 0.0;
            subTotal = 0.0;
            taxTotal = 0.0;
            total = 0.0;
        }

        // Accessors
        void setCost(const Inventory item);
        bool isQuantity(Inventory item, int iQty);
        void updateRegister();
        void updateUnits(Inventory &item);
        void displayReceipt();

        // Mutators
        double getCost() const
        { return cost; }

        double getUnitPrice() const
        { return (getCost() * markup); }

        double getSalesTax() const
        { return (getCost() * taxRate); }

        double getTaxTotal() const
        { return (getCost() * taxRate) + getUnitPrice(); }

        double getSubTotal() const
        { return (getUnitPrice() + getCost()); }

        double getTotal() const
        { return (getSubTotal() + getSalesTax()); }

        int getQuantity() const
        { return quantity; }

        double recUnitPrice() const
        { return unitPrice; }

        double recSalesTax() const
        { return salesTax; }

        double recTaxTotal() const
        { return taxTotal; }

        double recSubTotal() const
        { return subTotal; }

        double recTotal() const
        { return total; }
};
#endif

InventoryItem.h


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

#ifndef INVENTORY_ITEM_H
#define INVENTORY_ITEM_H

#include <string>

class Inventory
{
    private:
        std::string description;        // The item name
              double    cost;                    // Cost of an item
              int       units;                // Number of units on hand
       
    public:
        Inventory()                            // Default constructor
        {
            description = " ";
            cost = 0.0;
            units = 0;
        }

        // Constructor accepting arguments for description, cost and units
        Inventory(std::string desc, double c, int u)
        {
            description = desc;
            cost = c;
            units = u;
        }

        void setDescription(std::string desc)
        { description = desc; }

        void setCost(double c)
        { cost = c; }

        void setUnits(int u)
        { units = u; }

        std::string getDescription() const
        { return description; }

        double getCost() const
        { return cost; }

        int getUnits()
        { return units; }
};
#endif

ShoppingBasket.h


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

#ifndef SHOPPING_BASKET_H
#define SHOPPING_BASKET_H

#include <string>

#include "InventoryItem.h"

class Basket
{
private:
    std::string description;        // Item description
    int numItems;                        // Number of items in the basket
    double cost;                        // Cost of items in the basket

public:
    Basket()                                // Default constructor
    {
        description = " ";
        numItems = 0;
        cost = 0.0;
    }

    Basket(int iQty)
    {
        numItems = iQty;
    }

    // Assigns values to description and cost
    void setItemInfo(const Inventory item)
    {
        description  = item.getDescription();
        cost         = item.getCost();
    }

    void setNumItems(int iQty)
    { numItems += iQty; }

    int getQuantity() const
    { return numItems; }

    std::string getDescription() const
    { return description; }

    double getCost() const
    { return cost; }
};
#endif

CashRegister.cpp


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

#include <iomanip>
#include <iostream>
#include <string>

#include "CashRegister.h"

/* **********************************************************
    CashRegister::isQuantity() accepts an Inventory object and
    an integer
    This function determines whether the value passed to it is
    valid. If it is, the value is assigned to the quantity
    member, and true is returned. If the number of available
    units is 0, the quantity member is assigned 0, a message
    is output to screen, and true is returned. If a quantity
    is entered that is greater than the number of available
    items, the item name and available quantity is output to
    screen, and false is returned.
   ********************************************************** */

bool CashRegister::isQuantity(Inventory item, int iQty)
{
    if (item.getUnits() > 0 && iQty <= item.getUnits())
    {
        quantity = iQty;
        return true;
    }
    else if (item.getUnits() == 0)
    {
        quantity = 0;
        std::cout << "\n" << item.getDescription() << " Out Of Stock\n";
        return true;
    }
    else
    {
        std::cout << "\nItem: " << item.getDescription() << "\n";
        std::cout << "Availabe quantity: " << item.getUnits() << "\n";
        return false;
    }

    return false;
}

/* **********************************************************
            CashRegister::setCost() accepts an Inventory object
    This function sets the cost for an item. The value to be
    assigned to the cost member is passed to it by the item
    object.
   ********************************************************** */

void CashRegister::setCost(const Inventory item)
{
    cost = item.getCost();
}

/* **********************************************************
    CashRegister::updateUnits() accepts a reference to an
    Inventory object
    This function sets the number of units on hand, stored in
    the item object member. The new value is calculated by
    subtracting the quantity of items bought from the units on
    hand stored in the item object.
   ********************************************************** */

void CashRegister::updateUnits(Inventory &item)
{
    item.setUnits(item.getUnits() - getQuantity());
}

/* **********************************************************
    CashRegister::updateRegister()
    This function keeps a running total of taxes and prices,
    while items are added to the basket. All values are
    multiplied by the quantity being purchased.
   ********************************************************** */

void CashRegister::updateRegister()
{
    unitPrice += getUnitPrice() * getQuantity();
    salesTax  += getSalesTax()  * getQuantity();
    subTotal  += getSubTotal()  * getQuantity();
    taxTotal  += getTaxTotal()  * getQuantity();
    total     += getTotal()     * getQuantity();
}

/* **********************************************************
   CashRegister::displayReceipt()
    This function outputs to screen the unit price, sales tax,
    tax-total, subtotal and total purchase price.
   ********************************************************** */

void CashRegister::displayReceipt()
{
    std::cout << std::fixed << std::showpoint << std::setprecision(2);

    std::cout << std::left << "Markup " << std::setw(26)
               << std::right << "$ "        << std::setw(6)
                 << recUnitPrice() << "\n";

    std::cout << std::left << "Sales-Tax " << std::setw(23)
                 << std::right << "$ " << std::setw(6)
                 << recSalesTax() << "\n";

    std::cout << std::left << "Tax-Total " << std::setw(23)
                 << std::right << "$ " << std::setw(6)
                 << recTaxTotal() << "\n\n";

    std::cout << std::left << "Sub-Total " << std::setw(23)
                 << std::right << "$ " << std::setw(6)
                 << recSubTotal() << "\n";

    std::cout << std::left << "Purchase Price " << std::setw(18)
                 << std::right << "$ " << std::setw(6)
                 << recTotal() << "\n";

    std::cout << "\nTHE BINFORD ITEM EMPORIUM \n"
               << "\t  THANKS YOU FOR YOUR PATRONAGE";
}

CashRegisterDm.cpp


/* CashRegisterDm.cpp - This program demonstrates the CashRegister class */

#include <iomanip>
#include <iostream>
#include <string>
#include <array>

#include "ShoppingBasket.h"
#include "CashRegister.h"
#include "InventoryItem.h"

const int NUM_ITEMS = 6;

void displayItems(std::array<Inventory, NUM_ITEMS>);
void shoppingBasket(std::array<Inventory, NUM_ITEMS> &,
                          std::array <Basket, NUM_ITEMS> &, CashRegister &);
void displayBasket(const std::array <Basket, NUM_ITEMS>);

int main()
{
    CashRegister sales;

    std::array<Inventory, NUM_ITEMS> items { Inventory("Hammer", 2.95, 3),
                                                          Inventory("Chisel", 3.75, 3),
                                                          Inventory("Wrench", 5.25, 3),
                                                          Inventory("Pliers", 6.95, 3),
                                                          Inventory("Crimper", 24.97, 13),
                                                          Inventory("Jab Saw", 7.97, 51) };
   
    std::array <Basket, NUM_ITEMS> content{ };

    char repeat = ' ';

    std::cout << "\tWELCOME TO THE BINFORD ITEM EMPORIUM\n\n";

    do
    {
        shoppingBasket(items, content, sales);

        std::cout << "\nDo you wish to buy another item? ";
        std::cin >> repeat;
        std::cout << "\n";

        while (toupper(repeat) != 'Y' && toupper(repeat) != 'N')
        {
            std::cout << "\nDo you wish to buy another item? ";
            std::cin >> repeat;
        }

        if (toupper(repeat) == 'N')
        {
            std::cout << "     BINFORD ITEM EMPORIUM INVOICE\n\n";

            displayBasket(content);
            sales.displayReceipt();
           
        }
    } while (toupper(repeat) != 'N');

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

/* **********************************************************
    displayItems() accepts a pointer to an Inventory object
    This function displays the item names, units on hand and
    item cost.
    ********************************************************** */

void displayItems(std::array<Inventory, NUM_ITEMS> item)
{
    std::cout << std::fixed << std::showpoint << std::setprecision(2);
    std::cout << "Item ID\t\t" << "Description\t\t" << "On Hand\t\t" << "Cost\n\n";

    for (int i = 0; i < NUM_ITEMS; i++)
    {
        if (item[i].getUnits() > 0)
        {
            std::cout << (i + 1) << "\t\t"
                       << item[i].getDescription() << "\t\t\t"
                         << item[i].getUnits() << "\t\t"
                         << item[i].getCost() << "\n";
        }
        else
        {
            std::cout << (i + 1) << "\t\t"
                       << item[i].getDescription() << "\t\t\t"
                         << "Out of stock\t"
                         << item[i].getCost() << "\n";
        }
    }
}

/* **********************************************************
   shoppingBasket() accepts a reference to an array of item
    objects, a reference parameter to an array of
    basket objects and a reference to a sales object
   
    The customer is asked to enter the item ID and quantity he
    or she wishes to buy. This information is then processed
    by member functions of the sales and basket classes.
   ********************************************************** */

void shoppingBasket(std::array<Inventory, NUM_ITEMS> &item, std::array <Basket, NUM_ITEMS> &basket,
                          CashRegister &sales)
{
    int iQty = 0;
    int iID = 0;

    displayItems(item);

    std::cout << "\nWhich item do you wish to buy? ";
    std::cin >> iID;

    while (iID <= 0 || iID > NUM_ITEMS)
    {
        std::cout << "\nWhich item do you wish to buy? (1 through " << NUM_ITEMS << ") ";
        std::cin >> iID;
    }

    std::cout << "How many items do you wish to buy? ";
    std::cin >> iQty;

    while (sales.isQuantity(item[iID - 1], iQty) == false)
    {
        std::cout << "How many items do you wish to buy? ";
        std::cin >> iQty;
    }   

    sales.setCost(item[iID-1]);
    sales.updateUnits(item[iID - 1]);
    sales.updateRegister();

    basket[iID - 1].setItemInfo(item[iID-1]);
    basket[iID - 1].setNumItems(sales.getQuantity());
}

/* **********************************************************
   displayBasket() accepts an array of basket objects
    This function outputs to screen the item name, quantity
    and cost of the items currently in the basket.
   ********************************************************** */

void displayBasket(const std::array <Basket, NUM_ITEMS> content)
{
    std::cout << std::fixed << std::showpoint << std::setprecision(2);

    for (int i = 0; i < NUM_ITEMS; i++)
    {
        if (content[i].getQuantity() > 0)
        {
            std::cout << "ITEM NAME: " << std::setw(28) << content[i].getDescription() << "\n"
                         << "QUANTITY:  " << std::setw(28) << content[i].getQuantity() << "\n"
                         << "COST: "        << std::setw(26)
                         << "$"               << std::setw(7) << content[i].getCost() << "\n\n";
        }   
    }
    std::cout << "---------------------------------------\n\n";
}

Example Output:





1 comment:

  1. My C++ Playground: Programming Challenge 13.17 - Cash Register >>>>> Download Now

    >>>>> Download Full

    My C++ Playground: Programming Challenge 13.17 - Cash Register >>>>> Download LINK

    >>>>> Download Now

    My C++ Playground: Programming Challenge 13.17 - Cash Register >>>>> Download Full

    >>>>> Download LINK xE

    ReplyDelete