Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Above is \"1337-S18-HW2-Input\". HW2 (Graded out of 100) Write a program that re

ID: 3879340 • Letter: A

Question

Above is "1337-S18-HW2-Input".

HW2 (Graded out of 100) Write a program that reads grocery product records from a file, then store the information in parallel arrays. Each grocery product record consists of the following 5 items: PLU code: Product Look Up Code. Unique for each product, stored as a string. Product Name, stored as a string Product Sales Type: 0 per unit, 1-per pound Price per Pound or price per unit, Current Inventory Level (pounds or units). Below is an example of file content. In this example, there are four records, and each line corresponds to a record. A001 Apples 1 0.90 21 A002 Peaches 1 0.82 11 A006 Avocados 01.54 27 A008 Mangos 0 1.69 19

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


int determineNumRecords(string fname);
bool readInventory(string fname, string *pluPtr, string *namePtr, int *typePtr, double *pricePtr, int *inventoryPtr, int numRecords);
double checkout(string *pluPtr, double *pricePtr, int *inventoryPtr, int numRecords);
int searchProduct(string plu, string *pluPtr, int numRecords);
void display(string message, string *pluPtr, string *namePtr, int *typePtr, double *pricePtr, int *inventoryPtr, int numRecords);

int main()
{
string filename;
int numRecords;
string *pluPtr, *namePtr;
int *typePtr, *inventoryPtr;
double *pricePtr;
double total;
  
cout << "Enter the input file name: ";
cin >> filename;
  
numRecords = determineNumRecords(filename);
if(numRecords == -1)
cout << "ERROR: Unable to open input file " << filename << endl;
else
{
pluPtr = new string[numRecords];
namePtr = new string[numRecords];
typePtr = new int[numRecords];
inventoryPtr = new int[numRecords];
pricePtr = new double[numRecords];
  
readInventory(filename, pluPtr, namePtr, typePtr, pricePtr, inventoryPtr, numRecords);
  
cout << "There are " << numRecords << " records in the file" << endl;
display("Inventory read from file", pluPtr, namePtr, typePtr, pricePtr, inventoryPtr, numRecords);
int choice = 0;
while(choice != 3)
{
cout << "1. Checkout" << endl;
cout << "2. Print current inventory" << endl;
cout << "3. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
total = checkout(pluPtr, pricePtr, inventoryPtr, numRecords);
cout << "Total is: $" << total << endl;
break;
case 2:
display("Current Inventory", pluPtr, namePtr, typePtr, pricePtr, inventoryPtr, numRecords);
break;
case 3:
break;
default:
cout << "Invalid choice!!" << endl;
}
}
}
  
return 0;
}


int determineNumRecords(string fname)
{
ifstream infile(fname.c_str());
if(infile.fail())
return -1;
else
{
int n = 0;
string line;
while(getline(infile, line))
n++;
infile.close();
return n;
}
}
bool readInventory(string fname, string *pluPtr, string *namePtr, int *typePtr, double *pricePtr, int *inventoryPtr, int numRecords)
{
ifstream infile(fname.c_str());
if(infile.fail())
return false;
else
{
for(int i = 0; i < numRecords; i++)
{
infile >> pluPtr[i] >> namePtr[i] >> typePtr[i] >> pricePtr[i] >> inventoryPtr[i];
}
return true;
}
}

double checkout(string *pluPtr, double *pricePtr, int *inventoryPtr, int numRecords)
{
string plu = "";
int qty;
double total = 0;
  
while(true)
{
cout << "Enter PLU (0 if done): ";
cin >> plu;
  
if(plu == "0")
break;
  
cout << "Enter quantity: ";
cin >> qty;
  
while(qty <= 0)
{
cout << "Quantity must be positive. Reenter: ";
cin >> qty;
}
  
int index = searchProduct(plu, pluPtr, numRecords);
if(index == -1)
cout << "PLU not found" << endl;
else
{
if(inventoryPtr[index] < qty)
cout << "Not enough quantity in inventory" << endl;
else
{
inventoryPtr[index] -= qty;
total += pricePtr[index] * qty;
}
}
cout << endl;
  
}
cout << endl;
return total;
}
int searchProduct(string plu, string *pluPtr, int numRecords)
{
for(int i = 0 ; i < numRecords; i++)
{
if(pluPtr[i] == plu)
return i;
}
return -1;
}
void display(string message, string *pluPtr, string *namePtr, int *typePtr, double *pricePtr, int *inventoryPtr, int numRecords)
{
cout << message << endl;
for(int i = 0; i < numRecords; i++)
{
cout << left << "PLU: " << pluPtr[i] << ". ";
cout << setw(25) << namePtr[i] << ". ";
cout << "type: " << typePtr[i] << ". ";
cout << right << "unit price: " << setw(6) << pricePtr[i] << ". ";
cout << "inventory: " << inventoryPtr[i] << endl;
}
  
cout << endl << endl;
}

sample input file: product.txt
==============
A001 Apples 1 0.9 21
A002 Peches 1 0.82 11
A006 Avocados 0 1.54 27
A009 Strawberries_Case 0 12.5 8

output
======
Enter the input file name: product.txt
There are 4 records in the file
Inventory read from file
PLU: A001. Apples . type: 1. unit price: 0.9. inventory: 21
PLU: A002. Peches . type: 1. unit price: 0.82. inventory: 11
PLU: A006. Avocados . type: 0. unit price: 1.54. inventory: 27
PLU: A009. Strawberries_Case . type: 0. unit price: 12.5. inventory: 8


1. Checkout
2. Print current inventory
3. Quit
Enter your choice: 1
Enter PLU (0 if done): 1111
Enter quantity: 5
PLU not found

Enter PLU (0 if done): A006
Enter quantity: 50
Not enough quantity in inventory

Enter PLU (0 if done): A006
Enter quantity: 10

Enter PLU (0 if done): A001
Enter quantity: 5

Enter PLU (0 if done): 0
Total is: $19.9

1. Checkout
2. Print current inventory
3. Quit
Enter your choice: 2
Current Inventory
PLU: A001. Apples . type: 1. unit price: 0.9. inventory: 16
PLU: A002. Peches . type: 1. unit price: 0.82. inventory: 11
PLU: A006. Avocados . type: 0. unit price: 1.54. inventory: 17
PLU: A009. Strawberries_Case . type: 0. unit price: 12.5. inventory: 8


1. Checkout
2. Print current inventory
3. Quit
Enter your choice: 2
Current Inventory
PLU: A001. Apples . type: 1. unit price: 0.9. inventory: 16
PLU: A002. Peches . type: 1. unit price: 0.82. inventory: 11
PLU: A006. Avocados . type: 0. unit price: 1.54. inventory: 17
PLU: A009. Strawberries_Case . type: 0. unit price: 12.5. inventory: 8


1. Checkout
2. Print current inventory
3. Quit
Enter your choice: 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote