/*********************************** ** Author: Steven Scott ** Filename: pex4.cpp ** Date: Oct 22, 2007 ** Purpose: Keep track of inventory ** levels for a company. ***********************************/ #include #include using namespace std; const int maxWidgets = 500; const int minWarning = static_cast(maxWidgets * 0.1); // Define functions void drawMenu(); bool getCmd(int &widgets, int &accepted, int &returned, int &sent, int &maxpending, int &pending); void listInv(int widgets); void recvShip(int &accepted, int &pending, int &sent, int &widgets, int &returned); void request(int &pending, int &sent, int &widgets, int &maxpending); void cmdLoop(int widgets); void quit(int accepted, int returned, int sent, int maxpending, int widgets); int getPosInt(string q); int main() { // Initialize variables with 0 values. int widgets = 0; int accepted = 0; int returned = 0; int sent = 0; int maxpending = 0; int pending = 0; // Draw the help menu drawMenu(); // Loop through the commands until getCmd returns false (Quit) while (getCmd(widgets, accepted, returned, sent, maxpending, pending)) cmdLoop(widgets); 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: widgets as int&, accepted as int&, ** returned as int&, sent as int&, maxpending as int&, ** pending as int&. ***********************************/ bool getCmd(int &widgets, int &accepted, int &returned, int &sent, int &maxpending, int &pending) { char cmd; cout << "Enter your choice: "; cin >> cmd; switch (tolower(cmd)) { case 'l': // Call list inv function listInv(widgets); break; case 'r': request(pending, sent, widgets, maxpending); // Call request function break; case 's': // Call shipment function recvShip(accepted, pending, sent, widgets, returned); break; case 'h': // Draw the menu drawMenu(); break; case 'q': quit (accepted, returned, sent, maxpending, widgets); // Call quit status function return false; break; default: cout << "Invalid command.\n"; break; } return true; } /*********************************** ** Function: listInv() ** Description: Prints inventory levels ** Return: void ** Arguments: widgets as int ***********************************/ void listInv(int widgets) { cout << "Number of widgets in inventory: " << widgets << endl; } /*********************************** ** Function: recvShip() ** Description: Recieves a shipment. ** Asking for amount, fills pending, ** returns unneeded. ** Return: void ** Arguments: accepted as int&, pending as int&, ** sent as int&, widgets as int&, returned as int&. ***********************************/ void recvShip(int &accepted, int &pending, int &sent, int &widgets, int &returned) { 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: pending as int&, send as int&, ** widgets as int&, maxpending as int&. ***********************************/ void request(int &pending, int &sent, int &widgets, int &maxpending) { 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: accepted as int, returned as int, ** sent as int, maxpending as int, widgets as int. ***********************************/ void quit(int accepted, int returned, int sent, int maxpending, int widgets) { 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::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: widgets as int. ***********************************/ void cmdLoop(int widgets) { if (widgets < minWarning) { cout << "\nWarning! The current inventory level is too low."; cout << "\n Order a new shipment ASAP.\n\n"; } }