In C++. Write a program to keep track of a hardware store inventory in order to
ID: 3692860 • Letter: I
Question
In C++.
Write a program to keep track of a hardware store inventory in order to become familiar with the vector type and discover how to sort records using recursive functions.
Write a program to keep track of a hardware store inventory. The store sells various items. For each item in the store, the following information is kept: item ID, item name, number of pieces ordered, number of pieces currently in the store, number of pieces sold, manufacturers price for the item, and the stores selling price. At the end of each week, the store manager would like to see a report in the following form: The total inventory is the total selling value of all of the items currently in the store. The total number of items is the sum of the number of pieces of all of the items in the store. Your program must be menu driven, giving the user various choices, such as checking whether an item is in the store, selling an item, and printing the report. After inputting the data, sort it according to the items names. Also, after an item is sold, update the appropriate counts. Initially, the number of pieces (of an item) in the store is the same as the number of pieces ordered, and the number of pieces of an item sold is zero. Input to the program is a file consisting of data in the following form: itemID itemName pOrdered manufPrice sellingPrice Use seven parallel vectors to store the information. The program must contain at least the following functions: one to input data into the vectors, one to display the menu, one to sell an item, and one to print the report for the manager
Explanation / Answer
/*
C++ Code
Program: Written to keep track of a hardware store's inventory.
For each item in the store, the following information is kept: item ID, item name,
number of pieces ordered, number of pieces sold, manufacturers' price for the item,
and the store's selling price. At the end of each week, the store manager requests a report.
*********************************************************************************************
*/
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
const int invSize = 2;
void getInventory(ifstream& infile, vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,vector<double> sellingPrice); //
void displayMenu(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,vector<double> sellingPrice); //
void sellItem(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,vector<double> sellingPrice); //
void itemSearch(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice); //
void printReport(vector<string> itemID, vector<string> itemName, vector<int> pOrdered,vector<int> pInStore, vector<int> pSold, vector<double> manufPrice,vector<double> sellingPrice); //
int main() //
{
//Information from existing inventory file. "invSize" represents the size of the arrays. - John Oswald
//vector<int> itemID;
vector<int> itemQuantity;
vector<string> itemID;
vector<string> itemName;
vector<int> pOrdered;
vector<int> pInStore;
vector<int> pSold;
vector<double> manufPrice;
vector<double> sellingPrice;
//int itemNumber;
ifstream infile;
infile.open("inventory.txt");
if (!infile)
{
cout << "Input file (inventory.txt) does not exsit." << endl;
return 1;
}
getInventory(infile, itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
displayMenu(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
infile.close();
return 0;
}
void getInventory(ifstream& infile, vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice) //
{
unsigned int i;
string line;
for (i = 0; i < itemID.size(); i++)
{
infile >> itemID[i]
>> itemName[i]
>> pOrdered[i]
>> manufPrice[i]
>> sellingPrice[i];
}
pInStore[i] = pOrdered[i];
pSold[0] = 0;
}
void displayMenu(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice) //
{
char menuAnswer;
cout << "Type 'C' to check whether or not an item is in stock." << endl;
cout << "Type 'S' to sell an item to a customer." << endl;
cout << "Type 'R' to print an inventory report." << endl;
cin >> menuAnswer;
if (menuAnswer = 'C' || 'c')
itemSearch(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
if (menuAnswer = 'S' || 's')
sellItem(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
if (menuAnswer = 'R' || 'r')
printReport(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
}
void sellItem(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice) //
{
int amtBought;
double cash;
char verifyItem;
int i;
int itemNumber;
for (i = 0; i < 5; i++)
{
cout << "What itemID is being purchased? ";
cin >> itemNumber;
i = itemNumber;
if (i != -1)
{
cout << "You are selling " << itemName[i] << ", which costs $" << sellingPrice[i] << ". Is this correct? (Y/N)" << endl;
cout << endl;
cin >> verifyItem;
if (verifyItem == 'N' || verifyItem == 'n')
{
cout << "What item number is being purchased? " << endl;
cin >> itemNumber;
}
cout << "How many items are being purchased? " << endl;
cin >> amtBought;
if (amtBought < 1)
cout << "None of this item is being purchased!" << endl;
else
{
if (pInStore[i] < amtBought)
cout << "There are not enough of this item to sell! Up to " << pInStore[i] << " can be sold." << endl;
else
{
cout << "The total price of this transaction is " << amtBought * sellingPrice[i] << endl;
cout << "How much money did the customer give? " << endl;
cin >> cash;
if (cash < (amtBought * sellingPrice[i]))
{
cout << "The customer did not pay enough." << endl;
continue;
}
else
{
pInStore[i] = pInStore[i] - amtBought;
cout << "Please give the customer $" << cash - (amtBought * sellingPrice[i]) << " in change." << endl;
cout << "Have a nice day!" << endl;
}
}
}
}
}
}
void itemSearch(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice) //
{
int number;
cout << "Which item would you like to check?" << endl;
cout << endl;
cin >> number;
//for (number = 0; number < 5; number++)
if (pInStore[number] > 0)
cout << "There are " << pInStore[number] << "of that item available." << endl;
}
void printReport(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice)
{
unsigned int i;
int totalItems = 0;
double totalInventory = 0;
cout << " Friendly Hardware Store" << endl << endl;
cout << "itemID ItemName pOrdered pInStore pSold manufPrice sellingPrice" << endl;
cout << fixed << showpoint;
cout << setprecision(2);
// start loop at zero because the first element in a
// vector object is at location 0 (P. 576).
for (i = 0; i < itemID.size(); i++)
{
cout << left;
cout << setw(7) << itemID.at(i);
cout << setw(15) << itemName.at(i);
cout << right;
cout << setw(8) << pOrdered.at(i);
cout << setw(9) << pInStore.at(i);
cout << setw(6) << pSold.at(i);
cout << setw(11) << manufPrice.at(i);
cout << setw(13) << sellingPrice.at(i) << endl;
totalInventory += pInStore.at(i) * sellingPrice.at(i);
totalItems += pInStore.at(i);
}
cout << endl;
cout << "Total Inventory: $" << totalInventory << endl;
cout << "Total number of items in the store: " << totalItems << endl;
return;
}