You are on page 1of 8

mainCashReg.

cpp
#include <iostream>
#include <iomanip>
#include "cashRegister.h"
#include "inventoryItem.h"
using namespace std;
int main()
{
int numSelected;//int variable holds number selected
int qtySelected;//double variable holds number of items ordered
char again = 'Y';//char variable holds answer (y or n)
double subTotal;
const int NUM_ITEMS = 5; //
//array containing inventory items
InventoryItem inventory[] =
{
InventoryItem("Hammer", 6.95, 12),
InventoryItem("Wrench", 8.75, 20),
InventoryItem("Pliers", 3.75, 10),
InventoryItem("Ratchet", 7.95, 14),
InventoryItem("Screwdriver", 2.50, 22)
};
//format output
cout << fixed << showpoint << setprecision(2);
while (again == 'Y' || again == 'y')
{
//display current inventory
cout << "Listing of current inventory" << endl;
cout << " " << endl;
//table format
cout << setw(7) << "Item Number"
<< setw(17) << "Description"
<< setw(8) << "Cost" << setw(8)
<<setw(17) << "Units on Hand\n";
cout << "------------------------------------------------------------\n";
//for loop display allowing to display contents of array into table format
for (int i = 0; i < NUM_ITEMS; i++)
{
cout <<setw(7) << i;
cout << setw(17) << inventory[i].getDescription();
cout << setw(10) << "$" << inventory[i].getCost();
cout << setw(8) << inventory[i].getUnits() << endl;
}
cout << "" << endl;
cout << "" << endl;
//prompt user to input inventory number purchase
cout << "Please enter the item number of the inventory item you wish to
purchase: ";
cin >> numSelected;
cout << endl;
//validation loop
while((numSelected < 0) || (numSelected > 4))
{
cout << "Invalid Selection. Please enter an inventory item number 0-4: ";
cin >> numSelected;
}
//prompt for quantity of items wished to purchase
cout << "Please enter the quantity you wish to purchase: ";
cin >> qtySelected;
cout << endl;
//validation loop
while(qtySelected > inventory[numSelected].getUnits() || qtySelected < 1)
{
cout << "Invalid quantity. Please enter quantity less than or equal to " <<
inventory[numSelected].getUnits() << " units: ";
cin >> qtySelected;
}
CashRegister sale = CashRegister(inventory[numSelected], qtySelected);
cout << " " << endl;
//display subtotal, tax, and total
cout << "Total cost (30% markup on above listed cost included): " << endl;
cout << " " << endl;
//create a sale object for this transaction
//specify the item cost, but use the default
//tax rate of 6 percent
CashRegister sale2(CashRegister sale); //???
// display results
cout << "Subtotal:\t $" << sale.getSubtotal() << endl;
cout << "Sales tax:\t $" << sale.getSalesTax() << endl;
cout << "Total:\t\t $" << sale.getTotal() << endl;
//update inventory
sale.updateInventory();
//allow user to make another purchase
cout << "\nWould you like to make another purchase? ";
cin >> again;
}
return 0;
}

cashRegister.h
// This class has a private member function.
#ifndef CASHREGISTER_H
#define CASHREGISTER_H
#include "InventoryItem.h"
const double TAX_RATE = 0.06;
const double MARKUP = .30;
class CashRegister
{
private:
InventoryItem item;
int quantity;
public:
CashRegister(InventoryItem);
CashRegister(InventoryItem, int);
void setItem(InventoryItem);
void setQuantity(int);
void updateInventory();
int getQuantity() const;
double getCost() const;
double getUnitPrice() const;
double getSubtotal() const;
double getSalesTax() const;
double getTotal() const;
};
#endif

inventoryItem.h
//Specification file for the InventoryItem class.
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <cstring> //needed for strlen and strcpy
//constant for description's default size
const int DEFAULT_SIZE = 51;
//InventoryItem class declaration
class InventoryItem
{
private:
char *description; //item description
double cost; //item cost
int units; //Number of units on hand
int qtySold;
// Private member function.
void InventoryItem::createDescription(int size, char *value)
{
// Allocate the default amount of memory for description.
description = new char [size+1];
// 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; }
//constrcutor #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; }
void setQtySold(int qs)
{ qtySold = qs;}
//Accessor functions
const char *getDescription() const
{ return description; }
double getCost() const
{ return cost; }
int getUnits() const
{return units; }
void recordSale(int qs)
{
qtySold += qs;
units -= qs;
}
};
#endif

implementation.cpp
#include "cashRegister.h"
using namespace std;
CashRegister::CashRegister(InventoryItem i)
{
item = i;
}
CashRegister::CashRegister(InventoryItem i, int q)
{
item = i;
quantity = q;
}
void CashRegister::updateInventory()
{
item.recordSale(quantity);
}
void CashRegister::setItem(InventoryItem i)
{
item = i;
}
void CashRegister::setQuantity(int q)
{
quantity = q;
}
int CashRegister::getQuantity() const
{
return quantity;
}
double CashRegister::getCost() const
{
return item.getCost();
}
double CashRegister::getUnitPrice() const
{
double price;
price = getCost() + getCost() * MARKUP;
return price;
}
double CashRegister::getSubtotal() const
{
double subtotal;
subtotal = getUnitPrice() * quantity;
return subtotal;
}
double CashRegister::getSalesTax() const
{
double tax;
tax = getSubtotal() * TAX_RATE;
return tax;
}
double CashRegister::getTotal() const
{
double total;
total = getSubtotal() + getSalesTax();
return total;
}

You might also like