****Please do not use pre-compiled headers***** ?This program will use two class
ID: 3713930 • Letter: #
Question
****Please do not use pre-compiled headers*****
?This program will use two classes; one of the classes is provided in the assignment description for week 7 in the course site. Write a class name CashRegister, with the class declaration in a file called CashRegister.h and the implementation in a file called CashRegister.cpp. This class will interact with the InventoryItem class that has been provided. The program should display a list of items that are available to purchase.
The program will ask the user for the item and quantity being purchased. It will then get the cost of the item from the InventoryItem object. It will add 30% profit to the cost of the item to get the item’s unit price. It will then multiply the unit price times the quantity being purchased to get the purchase subtotal. The program will then compute a 6% sales tax on the subtotal to get the purchase total. It should then display the purchase subtotal, tax and total on the screen. The program will then subtract the quantity being purchased from the onHand variable of the Inventory Item class object. InventoryItem will need to be modified to handle this.
Validation: Do not accept a negative value for the quantity of items being purchased.
The inventory files are provided below:
InventoryItem.cpp
InventoryItem.h
This is my main, CashRegister.cpp and Cash Register.h files I have tried so far but get many errors I don't quite understand.
Main.cpp
#include <iostream>
#include <iomanip>
#include "CashRegister.h"
#include "InventoryItem.h"
using namespace std;
void displayMenu(InventoryItem[], int, int&, int&);
void getOrder(int&, int&, InventoryItem[], int size);
void displayTotal(CashRegister);
bool buyAgain();
int main()
{
const int SIZE = 5;
int menuNumber, numUnits;
InventoryItem tools[SIZE] = {
InventoryItem("Adjustable Wrench", 15.00, 10),
InventoryItem("Screwdriver", 5.00, 20),
InventoryItem("Pliers", 7.00, 35),
InventoryItem("Ratchet", 10.00, 10),
InventoryItem("Socket Wrench", 12.00, 7) };
displayMenu(tools, SIZE, menuNumber, numUnits);
cout << endl;
system("pause");
return 0;
}
// this function displays the menu with quanities of items and then calls the getOrder function
void displayMenu(InventoryItem array[], int size, int &menuNumber, int &numUnits)
{
for (int i = 0; i < 80; i++)
cout << char(196);
cout << left << setw(5) << "#" << setw(20) << "Item" << setw(10) << "qty on Hand" << endl;
for (int i = 0; i < 80; i++)
cout << char(196);
for (int i = 0; i < size; i++)
{
cout << setw(5) << (i + 1) << setw(20) << array[i].getDescription() << setw(10) << array[i].getUnits() << endl;
}
getOrder(menuNumber, numUnits, array, size);
}
// this function prompts the user which item they would like to buy and how many. it will validate the
// item and quantity for proper entries
void getOrder(int &menu, int &units, InventoryItem array[], int size)
{
bool checksGood = false, // over all test
testOne = false, // test if units is above 0
testTwo = false; // test if units is less than or equal to the quantity left in stock
cout << "Which item above is being purchased? ";
cin >> menu;
while (menu < 1 || menu > 5)
{
cout << "Invalid item selection." << endl;
cout << "Which item above is being purchased? ";
cin >> menu;
}
cout << "How many units? ";
cin >> units;
while (!checksGood)
{
while (units < 0)
{
cout << "Units cannot be less than 0." << endl;
cout << "How many units would you like to buy? ";
cin >> units;
if (units > array[(menu - 1)].getUnits())
testTwo = false;
}
testOne = true;
// if units is greater than how many are left in stock
while (units > array[(menu - 1)].getUnits())
{
cout << "ERROR: We only have " << array[(menu - 1)].getUnits() << " in stock." << endl;
cout << "How many units would you like to buy? ";
cin >> units;
if (units < 0)
testOne = false;
}
testTwo = true;
// if both tests come back true, pass the function
if (testOne == true && testTwo == true)
checksGood = true;
}
CashRegister price(array[(menu - 1)].getCost(), units);
displayTotal(price);
array[(menu - 1)].setUnitsSold(units);
if (buyAgain())
displayMenu(array, size, menu, units);
}
// this function displays the subtotal tax and total
void displayTotal(CashRegister totals)
{
cout << setprecision(2) << fixed << showpoint;
cout << "Subtotal: $" << totals.getSubtotal() << endl
<< "Sales Tax: $" << totals.getSalesTax() << endl
<< "Total: $" << totals.getPurchaseTotal() << endl;
}
// this function prompts the user if they would like to make another purchase
bool buyAgain()
{
bool again = false;
char let = '0';
if (!again)
{
cout << "Do you want to purchase another item (Y or N)? ";
cin >> let;
}
if (toupper(let) == 'Y')
again = true;
cout << endl;
return again;
}
CashRegister.h
#ifndef CASHREGISTER_H
#define CASHREGISTER_H
#include "InventoryItem.h"
class CashRegister
{
private:
double cost;
int units;
double unitPrice;
double subtotal;
double salesTax;
double purchaseTotal;
public:
// constructor
CashRegister(double, int);
// setters
void setUnitPrice();
void setSubtotal();
void setSalesTax();
void setPurchaseTotal();
// getters
double getUnitPrice() const;
double getSubtotal() const;
double getSalesTax() const;
double getPurchaseTotal() const;
};
#endif
CashRegister.cpp
#include "CashRegister.h"
#include "InventoryItem.h"
// constructor
CashRegister::CashRegister(double c, int u)
{
cost = c;
units = u;
setUnitPrice();
setSubtotal();
setSalesTax();
setPurchaseTotal();
}
// this setter function assigns the unit price to the cost with 30% profit
void CashRegister::setUnitPrice()
{
unitPrice = (cost * 0.30) + cost;
}
// this setter function assigns the subtotal to the unit price times the quantity
void CashRegister::setSubtotal()
{
subtotal = unitPrice * units;
}
// this setter function sets the sales tax to the subtotal times 6%
void CashRegister::setSalesTax()
{
salesTax = subtotal * .06;
}
// this setter function sets the purchase total to the subtotal plus the sales tax
void CashRegister::setPurchaseTotal()
{
purchaseTotal = subtotal + salesTax;
}
// getter functions return their respective attributes
double CashRegister::getUnitPrice() const
{
return unitPrice;
}
double CashRegister::getSubtotal() const
{
return subtotal;
}
double CashRegister::getSalesTax() const
{
return salesTax;
}
double CashRegister::getPurchaseTotal() const
{
return purchaseTotal;
}
Explanation / Answer
File: CashRegister.h
// CashRegister class declararion
#ifndef CASHREGISTER_H
#define CASHREGISTER_H
// header file section
#include "InventoryItem.h"
// constants declaration
const double PROFIT_RATE = 0.30;
const double TAX_RATE = 0.06;
class CashRegister
{
private:
// member variables
InventoryItem *item;
int quantity;
public:
// constructors
CashRegister();
CashRegister(InventoryItem *it, int qty);
// member functions
double getUnitPrice() const;
double getSubtotal() const;
double getSalesTax() const;
double getTotal() const;
void updateInventory();
};
#endif
File: CashRegister.cpp
// CashRegister class implementation
#include "cashRegister.h"
// default constructor implementation
CashRegister::CashRegister()
{
item = NULL;
quantity = 0;
}
// parameterized constructor implementation
CashRegister::CashRegister(InventoryItem *it, int qty)
{
item = it;
quantity = qty;
}
// updateInventory function implementation
void CashRegister::updateInventory()
{
item->setUnits(item->getUnits() - quantity);
}
// getUnitPrice function implementation
double CashRegister::getUnitPrice() const
{
return (item->getCost() + item->getCost() * PROFIT_RATE);
}
// getSubtotal function implementation
double CashRegister::getSubtotal() const
{
return getUnitPrice() * quantity;
}
// getSalesTax function implementation
double CashRegister::getSalesTax() const
{
return getSubtotal() * TAX_RATE;
}
// getTotal function implementation
double CashRegister::getTotal() const
{
return getSubtotal() + getSalesTax();
}
File: Driver.cpp
// Header files section
#include <iostream>
#include <iomanip>
#include "cashRegister.h"
#include "inventoryItem.h"
using namespace std;
// start main function
int main()
{
// constant declaration
const int MAX_ITEMS = 5;
// veriables declaration
int itemToBePurchased;
int numberOfUnitsToBePurchased;
char repeat = 'Y';
// list of items
InventoryItem inventory[MAX_ITEMS] = {
InventoryItem("Adjustable Wrench", 7.0, 10),
InventoryItem("Srewdriver", 2.50, 20),
InventoryItem("Pliers", 3.75, 35),
InventoryItem("Ratchet", 7.95, 10),
InventoryItem("Socket Wrench", 8.25, 7) };
cout << fixed << showpoint << setprecision(2) << left;
// repeat the loop until the user wants to exit
do
{
// display the list of items
cout << left << setw(5)<< "#"
<< setw(20) << "Item"
<< right<< setw(10) << "Cost($)"
<< setw(20) << "Quantity on hand" << endl;
cout << "-------------------------------------------------------" << endl;
for (int i = 0; i < MAX_ITEMS; i++)
{
cout << left << setw(5) << (i + 1);
cout << setw(20) << inventory[i].getDescription();
cout << right << setw(9) << inventory[i].getCost();
cout << setw(12) << inventory[i].getUnits() << endl;
}
// prompt the user to enter item number to be purchased
cout << " Enter item number to be purchased: ";
cin >> itemToBePurchased;
// validate the item number
while (itemToBePurchased < 1 || itemToBePurchased > MAX_ITEMS)
{
cout << "Enter a valid item number 1-" << MAX_ITEMS << ": ";
cin >> itemToBePurchased;
}
// verify the requested item is available or not
if (inventory[itemToBePurchased - 1].getUnits() > 0)
{
cout << "Enter number of units to be purchased: ";
cin >> numberOfUnitsToBePurchased;
// verify the requested number of items are available
while (numberOfUnitsToBePurchased < 1 || numberOfUnitsToBePurchased> inventory[itemToBePurchased - 1].getUnits())
{
cout << "Enter a valid units 1-" << inventory[itemToBePurchased - 1].getUnits() << ": ";
cin >> numberOfUnitsToBePurchased;
}
// create a CashRegister for the item
CashRegister cr = CashRegister(&inventory[itemToBePurchased - 1], numberOfUnitsToBePurchased);
// display the results of cost
cout << " Subtotal: $" << cr.getSubtotal() << endl;
cout << "Sales Tax: $" << cr.getSalesTax() << endl;
cout << "Total: $" << cr.getTotal() << endl;
// update the inventory
cr.updateInventory();
}
else
{
cout << "No quantity on hand for " << inventory[itemToBePurchased - 1].getDescription() << endl;
}
// prompt the user for the repetition choice
cout << " Do you want to purchase another item? (Y/N): ";
cin >> repeat;
cout << endl;
} while (repeat == 'Y' || repeat == 'y');
// display the list of items
cout << "Final inventory:" << endl;
cout << left << setw(5) << "#"
<< setw(20) << "Item"
<< right << setw(10)<< "Cost($)"
<< setw(20) << "Quantity on hand" << endl;
cout << "-------------------------------------------------------" << endl;
for (int i = 0; i < MAX_ITEMS; i++)
{
cout << left << setw(5)<< (i + 1);
cout << setw(20) << inventory[i].getDescription();
cout << right << setw(9) << inventory[i].getCost();
cout << setw(12) << inventory[i].getUnits() << endl;
}
cout << endl;
system("pause");
return 0;
} // end of main function
File: InventoryItem.h
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <string> // 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);
public:
// Constructor #1
InventoryItem();
// Constructor #2
InventoryItem(char *desc);
// Constructor #3
InventoryItem(char *desc, double c, int u);
// Destructor
~InventoryItem();
// Mutator functions
void setDescription(char *d);
void setCost(double c);
void setUnits(int u);
// Accessor functions
const char *getDescription() const;
double getCost() const;
int getUnits() const;
};
#endif
File: InventoryItem.cpp
#include "InventoryItem.h"
// 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_s(description, size + 1, value);
}
// Constructor #1
InventoryItem::InventoryItem()
{
// Store an empty string in the description
// attribute.
createDescription(DEFAULT_SIZE, "");
// Initialize cost and units.
cost = 0.0;
units = 0;
}
// Constructor #2
InventoryItem::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::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::~InventoryItem()
{
delete[] description;
}
// Mutator functions
void InventoryItem::setDescription(char *d)
{
//strcpy(description, d);
if (strlen(description) != strlen(d))
delete[] description;
createDescription(strlen(d), d);
}
void InventoryItem::setCost(double c)
{
cost = c;
}
void InventoryItem::setUnits(int u)
{
units = u;
}
// Accessor functions
const char *InventoryItem::getDescription() const
{
return description;
}
double InventoryItem::getCost() const
{
return cost;
}
int InventoryItem::getUnits() const
{
return units;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.