Question
#include #include #include #include using namespace std; int determineNumRecords(string fname); //function to populate the inventory from file bool readInventory(string fname, string *pluPtr, string *namePtr, int
#include #include #include #include using namespace std; int determineNumRecords(string fname); //function to populate the inventory from file bool readInventory(string fname, string *pluPtr, string *namePtr, int *typePtr, double *pricePtr, int *inventoryPtr, int numRecords); //this function is to does the check out double checkout(string *pluPtr, double *pricePtr, int *inventoryPtr, int numRecords); //function to search the products int searchProduct(string varPLU, string *pluPtr, int numRecords); //Method to diaply the inventory void display(string message, string *pluPtr, string *namePtr, int *typePtr, double *pricePtr, int *inventoryPtr, int numRecords); //The main int main() { //declare the local variables string inputFile; int numOfRecords; string *pluPtr, *namePtr; int *typePtr, *inventoryPtr; double *pricePtr; double totalVal; //take the file name input from user cout > inputFile; //determine the number of records numOfRecords = determineNumRecords(inputFile); //if file open failed if(numOfRecords == -1) cout > userCho; switch(userCho) { case 1: totalVal = checkout(pluPtr, pricePtr, inventoryPtr, numOfRecords); cout
return 0; } //function to determine number of records in the file int determineNumRecords(string fname) { ifstream infile(fname.c_str()); if(infile.fail()) return -1; else { int numRec = 0; string fileLine; while(getline(infile, fileLine)) numRec++; infile.close(); return numRec; } } //function to populate the inventory from file 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 itr = 0; itr > pluPtr[itr] >> namePtr[itr] >> typePtr[itr] >> pricePtr[itr] >> inventoryPtr[itr]; } return true; } } //this function is to does the check out double checkout(string *pluPtr, double *pricePtr, int *inventoryPtr, int numRecords) { string varPLU = ""; int prodQnty; double totalVal = 0;
while(true) { cout > varPLU;
if(varPLU == "0") break;
cout > prodQnty;
while(prodQnty > prodQnty; }
int prodIndex = searchProduct(varPLU, pluPtr, numRecords); if(prodIndex == -1) cout
} cout
Actual Problem:
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 0 1.54 27 A008 Mangos 0 1.69 19 Your program should: Read the file to determine the number of records in the file Dynamically allocate the necessary parallel arrays, with the appropriate size, and populate the arrays with the items from the file. There should be one array for each item in the product record. Then the program should print the inventory as read from the file, and loops over displaying a menu for the user to choose from: 1 Checkout 2 Print current inventory 3 Quit
Checkout If the user chooses checkout, the user can purchase multiple products in each checkout. The user is asked to enter the PLU code, then the quantity (weight or # of units, depending on the sales type) for each product. The program keeps up with the total cost ($). The user can enter 0 for a PLU to indicate the end of checkout. The program then displays the total price, and displays the menu again. The program should do input validation (quantity must be > 0) and ask the user to reenter if the input is invalid. Your program should make sure the quantity bought is not more than the inventory level. For example, if the user wants to buy 10 apples, but only 5 apples are in the inventory, the program will sell only 5 apples. The inventory level should be automatically updated with each purchase.
Print current inventory If the user chooses this option, the program should print the inventory, updated with all the purchases made so far.
Quit If the user chooses this option, the program exits the menu loop and automatically print the updated inventory, which is the current inventory. Then the program terminates.
1. Additional Requirements Make sure you meet all the requirements to avoid losing points
a) Functions You are required to implement your program with these functions. You may implement more functions to make your program more modular. readInventory: This function reads from a file and populates parallel arrays. It takes as arguments the file name, and the pointers to the parallel arrays, as well as the number of records. It returns true if file open is successful, false otherwise. The parameters in the parameter list must be in this order: File name Pointer to PLU array Pointer to product name array Pointer to product sales type array Pointer to price array Pointer to inventory array Number of records
You need to follow the above conventions (function name, parameter list, return type) to pass the unit test. Note: The function should close the file when done.
checkout: This function does the checkout. It takes as arguments the pointers to the parallel arrays, and the number of records in the inventory. It reads the PLU and quantity from the user, updates the data in the arrays to reflect the user's purchase, does input validation on the purchased quantity (> 0), asks the user to reenter as long as the quantity is
You need to follow the above conventions (function name, parameter list, return type) to pass the unit test.
b) Dynamic memory management Your program should release all the dynamic memory when it terminates.
c) Outline of main
Prompt the user for the file name. Determine the number of records in the file Dynamically allocate the parallel arrays of the right size, to hold the product data items Call readInventory to populate the arrays. You may assume the data records in the file are correct (no need to do input validation) Display menu and get users choice. Ask the user to reenter as long as the choice is invalid (assume the user only types an integer) While (users choice is not Quit) Call checkout if user chooses Checkout Print current inventory if user chooses Print current inventory Display menu and get users choice. Ask the user to reenter as long as the choice is invalid (assume the user only types an integer) End while Print current inventory
d) Style Make sure you follow the style requirements, especially regarding the comment header for functions, to avoid losing points.
2. Grading criteria
- Source code inspection (grader) *Style: 5 points (refer to the Homework Notes for the style requirements) *Request dynamic memory allocation for the arrays: 15 points *Release dynamic memory at the end: 5 points
- Code compilation and execution (zylabs) *Output test-1 - Print inventory read from file and print menu Output starts like output-1: 10 points. Use testInput1.txt, downloadable from zylabs *Output test-2 - Read users choice with input validation - Output contains output-2: 15 points. Use testInput2.txt, downloadable from zylabs *Output test-3 - Print updated inventory when the user chooses quit Output ends like output-3: 20 points. Use testInput3.txt, downloadable from zylabs *Unit test-1 - readInventory: 15 points *Unit test-2 - checkout: 15 points
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started