Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Design a Cash Register class using C++

Cash Register
Design a CashRegister class . The CashRegister class should perform the following:
1. Ask the user for the item and quantity being purchased.
2. Get the item’s cost from the InventoryItem object.
3. Add a 30″/0 profit to the cost to get the item ‘s unit price.
4. Multiply the unit price times the quantity being purchased to get the purchase
subtotal.
S. Compute a 6% sales tax on the subtotal to get the purchase total.
6. Display the purchase subtotal, tax, and total on the screen.
7. Subtract the quantity being purchased from the onHand variable of the
InventoryItem cl ass object.
Implement both classes in a complete program. Feel free to modify the
Inventoryltem class in any way necessary.
Input Validation: Do not accept a negative value for the quantity of items being
purchased.

Answer:

Cash Register


#include <iostream>
#include <cctype>
#include "CashRegister.h"
using namespace std;

const int NUM_ITEMS = 5;

int main()
{
   InventoryItem inventory[NUM_ITEMS] = { 
             InventoryItem("Adjustable Wrench", 7.0, 10),
             InventoryItem("Screwdriver", 3.5, 20),
             InventoryItem("Pliers", 9.0, 35),
             InventoryItem("Ratchet", 10.0, 10),
             InventoryItem("Socket Wrench", 9.75, 7) };
             
   CashRegister reg(inventory, NUM_ITEMS);
   char again;

   do
   {
      reg.makeSale();
      cout << "Do you want to purchase another item? "; cin >> again;
      while (toupper(again) != 'Y' && toupper(again) != 'N')
      {
         cout << "Enter Y or N please: "; cin >> again;
      }
   } while (toupper(again) != 'N');
}


 

Implementation file for the CashRegister class


#include <iostream>
#include <iomanip>
#include "CashRegister.h"
using namespace std;

//*****************************************************
// Constructor                                        *
// This constructor accepts an array of InventoryItem *
// objects and copies it to the internal array.       *
//*****************************************************

CashRegister::CashRegister(InventoryItem *i, int size)
{  
   // Create an array of inventory items.
   inventory = new InventoryItem [size];
   
   // Copy the array passed as i to the new
   // inventory item array.
   for(int count = 0; count < size; count++)
         inventory[count] = i[count];
}

//***************************************************
// The makeChoice function displays a list of items *
// and lets the user select one.                    *
//***************************************************

void CashRegister::makeChoice()
{
   cout << setw(5) << left << "#" << setw(20);
   cout << left <<"Item" << setw(12);
   cout << left << "qty on Hand\n";
   cout << "---------------------------------"
        << "-----------------------------\n";

   for (int index = 0; index < 5; index++)
   {
      cout << setw(5) << left << (index+1);
      cout << setw(20) << inventory[index].getDescription();
      cout << setw(12) << inventory[index].getUnits() << endl;
   }

   cout << "Which item above is being purchased? "; cin >> choice;
   while (choice < 0 || choice > 5)
   {
      cout << "That is not a valid choice.\n";
      cout << "Which item above is being purchased? "; cin >> choice;
   }
   choice--;
}

//***************************************************
// findUnitPrice member function                    *
//***************************************************

void CashRegister::findUnitPrice()
{
   unitPrice = inventory[choice].getCost() * 1.3;
}

//***************************************************
// findSalesTax member function                     *
//***************************************************

void CashRegister::findSalesTax()
{
   tax = subTotal * 0.06;
}

//***************************************************
// findTotal member function                        *
//***************************************************

void CashRegister::findTotal()
{
   total = subTotal + tax;
}

//***************************************************
// adjustUnits member function                      *
//***************************************************

bool CashRegister::adjustUnits()
{
   int quantity = inventory[choice].getUnits();
   if (quantity >= qty)
   {
	   inventory[choice].setUnits(quantity - qty);
	   return true;
   }
   else
      return false;
}

//***************************************************
// makeSale member function                         *
//***************************************************

void CashRegister::makeSale()
{
   // Display a list of items to choose from.
   makeChoice();
   
   // Get the number of units.
   cout << "How many units? "; cin >> qty;
   
   // Validate it.
   while (qty < 0)
   {
      cout << "Enter 0 or greater please: "; cin >> qty;
   }

   // Determine the unit price.
   findUnitPrice();
   
   // Calculate the subtotal.
   subTotal = qty * unitPrice;
   
   // Determine the sales tax.
   findSalesTax();
   
   // Determine the total.
   findTotal();
   
   // Adjust the units on hand.
   if (adjustUnits())
      displaySale();
   else
      cout << "There aren't enough units in stock.\n";
}

//***************************************************
// displaySale member function                      *
//***************************************************

void CashRegister::displaySale()
{
   cout << fixed << showpoint << setprecision(2);
   cout << "Subtotal:  $" << subTotal << endl;
   cout << "Sales Tax: $" << tax << endl;
   cout << "Total:     $" << total << endl;
}


 

Specification file for the CashRegister class


#ifndef CASHREGISTER_H
#define CASHREGISTER_H

#include "InventoryItem.h"

class CashRegister
{
private:
	// Private member variables
   InventoryItem *inventory;
   int choice;
   int qty;
   double unitPrice;
   double subTotal;
   double tax;
   double total;

   // Private member functions
   void findUnitPrice();
   void findSalesTax();
   void findTotal();
   bool adjustUnits();
   void makeChoice();
   
public:
   // Constructor
   CashRegister(InventoryItem *, int);
   
   // Other member functions
   void makeSale();
   void displaySale();
};

#endif



InventoryItem.h


#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <cstring>   // Needed for strlen and strcpy

// Constant for the description's default size
const int DEFAULT_SIZE = 51;

class InventoryItem
{
private:
   char *description;  // The item description
   double cost;        // The item cost
   int units;          // Number of units on hand
   
   // Private member function.
   void createDescription(int size, char *value)
      { // Allocate the default amount of memory for description.
        description = new char [size];
        
        // Store a value in the memory.
        strcpy(description, value); }

public:
   // Constructor #1
   InventoryItem()
      { // Store an empty string in the description
        // attribute.
        createDescription(DEFAULT_SIZE, "");
        
        // Initialize cost and units.
        cost = 0.0;
        units = 0; }

   // Constructor #2
   InventoryItem(char *desc)
      { // Allocate memory and store the description.
        createDescription(strlen(desc), desc); 
        
        // Initialize cost and units.
        cost = 0.0;
        units = 0; }

   // Constructor #3
   InventoryItem(char *desc, double c, int u)
      { // Allocate memory and store the description.
        createDescription(strlen(desc), desc); 

        // Assign values to cost and units.
        cost = c;
        units = u; }      
        
   // Destructor
   ~InventoryItem()
      { delete [] description; }

   // Mutator functions
   void setDescription(char *d) 
      { strcpy(description, d); }

   void setCost(double c)
      { cost = c; }
      
   void setUnits(int u)
      { units = u; }

   // Accessor functions
   const char *getDescription() const
      { return description; }
         
   double getCost() const
      { return cost; }

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


About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!