Please write this in C++ with Classes. Please write the code in computer, with c
ID: 3868815 • Letter: P
Question
Please write this in C++ with Classes. Please write the code in computer, with comments and organization. (Not so advanced because it is for a class of data structures). Please implement the InventoryItem.h in this code.
17. Cásh Register Design a CashRegister class that can be used with the InventoryItem class discussed in this chapter. 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% 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 sub- total. 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 class object. Implement both classes in a complete program. Feel free to modify the InventoryItem class in any way necessarvExplanation / Answer
Below is your code: -
InventoryItem.h
#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;
}
};
CashRegister.h
#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);
int getQuantity() const;
double getCost() const;
double getUnitPrice() const;
double getSubtotal() const;
double getSalesTax() const;
double getTotal() const;
};
#endif
CashRegister.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::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;
}
main.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 ";
cout << "------------------------------------------------------------ ";
//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: $" << sale.getSubtotal() << endl;
cout << "Sales tax: $" << sale.getSalesTax() << endl;
cout << "Total: $" << sale.getTotal() << endl;
inventory[numSelected].setUnits(inventory[numSelected].getUnits() - qtySelected);
//allow user to make another purchase
cout << " Would you like to make another purchase? ";
cin >> again;
}
return 0;
}
Sample Run: -
Listing of current inventory
Item Number Description Cost Units on Hand
------------------------------------------------------------
0 Hammer $6.95 12
1 Wrench $8.75 20
2 Pliers $3.75 10
3 Ratchet $7.95 14
4 Screwdriver $2.50 22
Please enter the item number of the inventory item you wish to purchase: 0
Please enter the quantity you wish to purchase: 10
Total cost (30% markup on above listed cost included):
Subtotal: $90.35
Sales tax: $5.42
Total: $95.77
Would you like to make another purchase? Y
Listing of current inventory
Item Number Description Cost Units on Hand
------------------------------------------------------------
0 Hammer $6.95 2
1 Wrench $8.75 20
2 Pliers $3.75 10
3 Ratchet $7.95 14
4 Screwdriver $2.50 22
Please enter the item number of the inventory item you wish to purchase: 2
Please enter the quantity you wish to purchase: 11
Invalid quantity. Please enter quantity less than or equal to 10 units: 10
Total cost (30% markup on above listed cost included):
Subtotal: $48.75
Sales tax: $2.92
Total: $51.67
Would you like to make another purchase? Y
Listing of current inventory
Item Number Description Cost Units on Hand
------------------------------------------------------------
0 Hammer $6.95 2
1 Wrench $8.75 20
2 Pliers $3.75 0
3 Ratchet $7.95 14
4 Screwdriver $2.50 22
Please enter the item number of the inventory item you wish to purchase: 4
Please enter the quantity you wish to purchase: 20
Total cost (30% markup on above listed cost included):
Subtotal: $65.00
Sales tax: $3.90
Total: $68.90
Would you like to make another purchase? Y
Listing of current inventory
Item Number Description Cost Units on Hand
------------------------------------------------------------
0 Hammer $6.95 2
1 Wrench $8.75 20
2 Pliers $3.75 0
3 Ratchet $7.95 14
4 Screwdriver $2.50 2
Please enter the item number of the inventory item you wish to purchase: 3
Please enter the quantity you wish to purchase: 3
Total cost (30% markup on above listed cost included):
Subtotal: $31.01
Sales tax: $1.86
Total: $32.87
Would you like to make another purchase? N
--------------------------------
Process exited after 39.77 seconds with return value 0
Press any key to continue . . .