All pastes #790045 Raw Edit

Damnit

public cpp v1 · immutable
·published 2007-11-20 01:15 UTC
rendered paste body
/************************************* 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;const int maxWidgets = 500;const int minWarning = static_cast<int>(maxWidgets * 0.1);const int pendingLimit = 100;/******************************************* Define named constants for characters******************************************/const char cList = 'L';const char cReq = 'R';const char cShip = 'S';const char cQuit = 'Q';const char cHelp = 'H';/************************************************* Ok, now tell me if you recommend this now?** Define functions for later use.***********************************************//************************************* Function: drawMenu()** Description: Draws the help menu.** Return: void** Arguments: NULL***********************************/void drawMenu();/************************************* Function: getCmdAndParse()   (Happy?)** 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 getCmdAndParse(int &widgets, int &accepted, int &returned,            int &sent, int &maxpending, int &pending);/************************************* Function: listInv()** Description: Prints inventory levels** Return: void** Arguments: widgets as int***********************************/void listInv(int widgets);/************************************* 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);/************************************* 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);/************************************* 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);/************************************* 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);/************************************* Function: getPosInt()** Description: Gets a users input,** only accepts positive integers.** Return: input as int** Arguments: question as string***********************************/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 (getCmdAndParse(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 << cList << " --- list the current inventory level;\n";  cout << cReq << " --- handle a request from the customer;\n";  cout << cShip << " --- handle a shipment from the supplier;\n";  cout << cQuit << " --- terminate the program;\n";  cout << cHelp << " --- display this help message.\n";  cout << string(38, '-') << endl;}/************************************* Function: getCmdAndParse()    (Happy?)** 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 getCmdAndParse(int &widgets, int &accepted, int &returned,            int &sent, int &maxpending, int &pending) {  char cmd;  cout << "Enter your choice: ";  cin >> cmd;  switch (toupper(cmd)) {   case cList:     // Call list inv function     listInv(widgets);   break;   case cReq:     request(pending, sent, widgets, maxpending);     // Call request function   break;   case cShip:     // Call shipment function     recvShip(accepted, pending, sent, widgets, returned);   break;   case cHelp:     // Draw the menu     drawMenu();   break;   case cQuit:     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 + (pendingLimit - 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<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: 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";  }}