home *** CD-ROM | disk | FTP | other *** search
/ Using Visual C++ 4 (Special Edition) / Using_Visual_C_4_Special_Edition_QUE_1996.iso / ch13 / expsum / expsum1 / expsum1.cpp
Encoding:
C/C++ Source or Header  |  1995-09-18  |  5.0 KB  |  178 lines

  1. // Get our needed include files
  2. #include <employee.h>
  3. #include <expense.h>
  4. #include <fstream.h>
  5. #include <iomanip.h>
  6.  
  7. // Definitions
  8. const int MIN_ALLOWABLE_EMPLOYEES = 1;
  9. const int MAX_ALLOWABLE_EMPLOYEES = 20;
  10. const int MIN_NUM_EXPENSES = 1;
  11. const int MAX_NUM_EXPENSES = 10;
  12. const float MIN_ALLOWABLE_EXPENSE = 1.0F;
  13. const float MAX_ALLOWABLE_EXPENSE = 1000.0F;
  14. const float MAX_ALLOWABLE_REIMBURSEMENT = 5000.0F;
  15.  
  16. // Error codes
  17. typedef enum {
  18.     EXP_NO_ERROR,
  19.     EXP_ERR_EXPENSE_TOO_SMALL,
  20.     EXP_ERR_EXPENSE_TOO_LARGE,
  21.     EXP_ERR_NO_EXPENSE_ITEMS,
  22.     EXP_ERR_TOO_MANY_EXPENSE_ITEMS,
  23.     EXP_ERR_TOTAL_EXPENSE_TOO_LARGE
  24. } ErrorCode;
  25.  
  26. float ProcessExpenseItem(ifstream& infile, ErrorCode& rc)
  27. {
  28.     Expense NewExpense;
  29.  
  30.     // Init the return code
  31.     rc = EXP_NO_ERROR;
  32.  
  33.     // Read in the expense record from the file
  34.     infile >> NewExpense;
  35.  
  36.     // Check for problems
  37.     if (NewExpense.GetExpenseAmount() < MIN_ALLOWABLE_EXPENSE) {
  38.         cout << "Error! Expense item under the minimum amount "
  39.              << "eligible for reimbursement.\n\n";
  40.         rc = EXP_ERR_EXPENSE_TOO_SMALL;
  41.         return 0.0F; 
  42.     }
  43.     if (NewExpense.GetExpenseAmount() > MAX_ALLOWABLE_EXPENSE) {
  44.         cout << "Error! Expense item greater than maximum amount "
  45.              << "eligible for reimbursement.\n\n";
  46.         rc = EXP_ERR_EXPENSE_TOO_LARGE;
  47.         return 0.0F; 
  48.     }
  49.  
  50.     // Display the expense
  51.     cout << "     " << setw(10) << NewExpense.GetDate();
  52.     cout << setw(35) << NewExpense.GetDescription();
  53.     cout.unsetf(ios::left);
  54.     cout << setw(12)<<NewExpense.GetExpenseAmount()<< "\n"<< setw(0);
  55.     cout.setf(ios::left);
  56.  
  57.     return NewExpense.GetExpenseAmount();
  58. }
  59.  
  60. float ProcessEmployee(ifstream& infile, ErrorCode& rc)
  61. {
  62.     Employee NewEmployee;
  63.     float    EmployeeTotal = 0.0F;
  64.     unsigned loop;
  65.  
  66.     // Init the return code
  67.     rc = EXP_NO_ERROR;
  68.  
  69.     // Read in the employee record from the file
  70.     infile >> NewEmployee;
  71.  
  72.     // Check for problems
  73.     if (NewEmployee.GetNumExpenses() < MIN_NUM_EXPENSES) {
  74.         cout << "Error! No expense items for this employee.\n\n";
  75.         rc = EXP_ERR_NO_EXPENSE_ITEMS;
  76.         return 0.0F; 
  77.     }
  78.     if (NewEmployee.GetNumExpenses() > MAX_NUM_EXPENSES) {
  79.         cout << "Error! Too many expense items for this employee.\n\n";
  80.         rc = EXP_ERR_TOO_MANY_EXPENSE_ITEMS;
  81.         return 0.0F; 
  82.     }
  83.  
  84.     // Display the employee record
  85.     cout << "Employee #" << NewEmployee.GetID() << "\n";
  86.     cout << NewEmployee.GetFirstName() << " ";
  87.     cout << NewEmployee.GetMiddleInitial() << ". ";
  88.     cout << NewEmployee.GetLastName() << "\n";
  89.     cout << NewEmployee.GetAddress() << "\n";
  90.     cout << NewEmployee.GetCity() << ", ";
  91.     cout << NewEmployee.GetState() << " ";
  92.     cout << NewEmployee.GetZip() << "\n\n";
  93.  
  94.     // Now get the expense records
  95.     cout << "     Expenses\n";
  96.     cout << "     ---------------------\n";
  97.     for (loop = 0; loop < NewEmployee.GetNumExpenses(); loop++) {
  98.         EmployeeTotal += ProcessExpenseItem(infile, rc);
  99.         if (rc != EXP_NO_ERROR)
  100.             return 0.0F;
  101.     }
  102.  
  103.     // Check for problems
  104.     if (EmployeeTotal > MAX_ALLOWABLE_REIMBURSEMENT) {
  105.         cout << "Error! Expense total greater than maximum "
  106.              << "amount eligible for reimbursement.\n\n";
  107.         rc = EXP_ERR_TOTAL_EXPENSE_TOO_LARGE;
  108.         return 0.0F; 
  109.     }
  110.  
  111.     // Display the employee total
  112.     cout << "     =========================================================\n";
  113.     cout << "     Employee Total:";
  114.     cout.unsetf(ios::left);
  115.     cout << setw(42) << EmployeeTotal << "\n\n\n";
  116.     cout.setf(ios::left);
  117.  
  118.     return EmployeeTotal;
  119. }
  120.  
  121. void main(int argc, char *argv[])
  122. {
  123.     // Check for right number of command-line arguments
  124.     if (argc != 2) {
  125.         cout << "USAGE: expsum1 <infile name>\n\n";
  126.         return;
  127.     }
  128.  
  129.     // Open the infile
  130.     ifstream infile(argv[1], ios::nocreate);
  131.     if (!infile) {
  132.         cout << "Expense file \"" << argv[1] << "\" not found.\n\n";
  133.         return;
  134.     }
  135.  
  136.     // Read in our number of employees
  137.     unsigned EmployeeCount;
  138.     float    FileTotal = 0.0F;
  139.     infile >> EmployeeCount;
  140.     if (EmployeeCount < MIN_ALLOWABLE_EMPLOYEES ||
  141.         EmployeeCount > MAX_ALLOWABLE_EMPLOYEES) {
  142.         cout << "Invalid number of employees specified in "
  143.              << "expense file.\n\n";
  144.         infile.close();
  145.     }
  146.  
  147.     // Set the stream display flags
  148.     cout.setf(ios::left | ios::showpoint | ios::fixed);
  149.     cout.precision(2);
  150.  
  151.     // Display our main heading
  152.     cout << "+====================================================================+\n";
  153.     cout << "|                     EXPENSE FILE PROCESSOR                         |\n";
  154.     cout << "+====================================================================+\n\n\n";
  155.  
  156.     // Main employee loop
  157.     ErrorCode rc;
  158.     do {
  159.         FileTotal += ProcessEmployee(infile, rc);
  160.         if (rc != EXP_NO_ERROR) {
  161.             infile.close();
  162.             return;
  163.         }
  164.     } while (--EmployeeCount);
  165.  
  166.     // Display the file total
  167.     cout <<"+=================================================================+\n";
  168.     cout << "| EXPENSE FILE TOTAL: ";
  169.     cout.unsetf(ios::left);
  170.     cout << setw(43) << FileTotal << " |\n";
  171.     cout.setf(ios::left);
  172.     cout <<"+=================================================================+\n";
  173.  
  174.     // Close the infile _ we're done!
  175.     infile.close();
  176. }
  177.  
  178.