/************************************* Author: Steven Scott** Filename: pex4.cpp** Date: Oct 22, 2007** Purpose: Keep track of inventory** levels for a company.***********************************/#include <iostream>#include <iomanip>using namespace std;// Define global variablesint widgets;int pending;int accepted;int returned;int sent;int maxpending;static int maxWidgets = 500;static int minWarning = static_cast<int>(maxWidgets * 0.1);// Define functionsvoid drawMenu();bool getCmd();void listInv();void recvShip();void request();void cmdLoop();void quit();int getPosInt(string q);int main() { // Initialize variables with 0 values. widgets = 0; accepted = 0; returned = 0; sent = 0; maxpending = 0; pending = 0; // Draw the help menu drawMenu(); // Loop through the commands until getCmd returns false (Quit) while (getCmd()) cmdLoop(); return 0;}/************************************* Function: drawMenu()** Description: Draws the help menu.** Return: void** Arguments: NULL***********************************/void drawMenu() { cout << string(38, '-') << endl; cout << "Choose from following commands:\n"; cout << "L --- list the current inventory level;\n"; cout << "R --- handle a request from the customer;\n"; cout << "S --- handle a shipment from the supplier;\n"; cout << "Q --- terminate the program;\n"; cout << "H --- display this help message.\n"; cout << string(38, '-') << endl;}/************************************* Function: getCmd()** Description: Takes users input,** calls whichever function they requested.** Return: true on all except quite** Arguments: NULL***********************************/bool getCmd() { char cmd; cout << "Enter your choice: "; cin >> cmd; switch (tolower(cmd)) { case 'l': // Call list inv function listInv(); break; case 'r': request(); // Call request function break; case 's': // Call shipment function recvShip(); break; case 'h': // Draw the menu drawMenu(); break; case 'q': quit (); // Call quit status function return false; break; default: cout << "Invalid command.\n"; break; } return true;}/************************************* Function: listInv()** Description: Prints inventory levels** Return: void** Arguments: NULL***********************************/void listInv() { cout << "Number of widgets in inventory: " << widgets << endl;}/************************************* Function: recvShip()** Description: Recieves a shipment.** Asking for amount, fills pending,** returns unneeded.** Return: void** Arguments: NULL***********************************/void recvShip() { int amt; amt = getPosInt("Enter the shipment amount: "); accepted += amt; if (pending > 0) { if (amt - pending >= 0) { sent += pending; amt -= pending; cout << pending << " widgets have been used to fulfil pending request(s)." << endl; pending = 0; } else { sent += amt; cout << amt << " widgets have been used to fulfil pending request(s)." << endl; pending -= amt; amt = 0; } } if (widgets + amt <= maxWidgets) { widgets += amt; cout << amt << " widgets have been stored.\n"; } else { int dec = amt - (maxWidgets - widgets); returned += dec; accepted -= dec; amt = amt - dec; widgets = maxWidgets; if (amt > 0) cout << amt << " widgets have been stored." << endl; cout << dec << " widgets have been sent back due to lack of space.\n"; }}/************************************* Function: request()** Description: Take and fill to ability** a customers request.** Return: void** Arguments: NULL***********************************/void request() { int amt; amt = getPosInt("Enter the request amount: "); if (amt <= widgets) { // Enough widgets widgets -= amt; sent += amt; cout << amt << " widgets of the request have been fulfilled.\n"; } else if (amt <= (widgets + (100 - pending))) { if (widgets > 0) { sent += widgets; cout << widgets << " widgets of the request have been fulfilled.\n"; amt -= widgets; widgets = 0; } pending += amt; if (pending > maxpending) maxpending = pending; cout << amt << " widgets of the request have been" << " entered into a pending list.\n"; } else { // Not enough widgets cout << "The request of " << amt << " widgets has been rejected.\n"; }}/************************************* Function: quit()** Description: Exit program. Print out** final values.** Return: void** Arguments: NULL***********************************/void quit() { cout << endl << "==== Statistics ====\n\n"; cout << "Widgets accepted from supplier:" << setw(8) << accepted << endl; cout << "Widgets returned to supplier :" << setw(8) << returned << endl; cout << "Widgets sent to customers :" << setw(8) << sent << endl; cout << "Maximum widgets pending :" << setw(8) << maxpending << endl; cout << "Number of widgets in inventory:" << setw(8) << widgets << "\n\n"; cout << "==== End of run ====" << endl;}/************************************* Function: getPosInt()** Description: Gets a users input,** only accepts positive integers.** Return: input as int** Arguments: question as string***********************************/int getPosInt(string q) { int amt = 0; while (cout << q) { cin >> amt; if (cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); amt = 0; } if (amt <= 0) cout << "The amount must be a positive number.\n"; else break; } return amt;}/************************************* Function: cmdLoop()** Description: Run every command loop,** prints out if the user is low on stock.** Return: void** Arguments: NULL***********************************/void cmdLoop() { if (widgets < minWarning) { cout << "\nWarning! The current inventory level is too low."; cout << "\n Order a new shipment ASAP.\n\n"; }}