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

  1. // Get the needed include files
  2. #include <fstream.h>
  3. #include <iomanip.h>
  4. #include "employee.h"
  5. #include "expense.h"
  6. #include "except.h"
  7.  
  8. // Definitions
  9. const int MIN_ALLOWABLE_EMPLOYEES = 1;
  10. const int MAX_ALLOWABLE_EMPLOYEES = 20;
  11. const int MIN_NUM_EXPENSES = 1;
  12. const int MAX_NUM_EXPENSES = 10;
  13. const float MIN_ALLOWABLE_EXPENSE = 1.0F;
  14. const float MAX_ALLOWABLE_EXPENSE = 1000.0F;
  15. const float MAX_ALLOWABLE_REIMBURSEMENT = 5000.0F;
  16.  
  17. float ProcessExpenseItem(ifstream& infile)
  18. {
  19.     Expense NewExpense;
  20.  
  21.     // Read in the expense record from the file
  22.     infile >> NewExpense;
  23.  
  24.     // Check for problems
  25.     if (NewExpense.GetExpenseAmount() < MIN_ALLOWABLE_EXPENSE)
  26.         throw ExpenseTooSmall();
  27.     if (NewExpense.GetExpenseAmount() > MAX_ALLOWABLE_EXPENSE)
  28.         throw ExpenseTooLarge();
  29.  
  30.     // Display the expense
  31.     cout << "     " << setw(10) << NewExpense.GetDate();
  32.     cout << setw(35) << NewExpense.GetDescription();
  33.     cout.unsetf(ios::left);
  34.     cout <<setw(12)<< NewExpense.GetExpenseAmount()<< "\n"<< setw(0);
  35.     cout.setf(ios::left);
  36.  
  37.     return NewExpense.GetExpenseAmount();
  38. }
  39.  
  40. float ProcessEmployee(ifstream& infile)
  41. {
  42.     Employee NewEmployee;
  43.     float    EmployeeTotal = 0.0F;
  44.     unsigned loop;
  45.  
  46.     // Read in the employee record from the file
  47.     infile >> NewEmployee;
  48.  
  49.     // Check for problems
  50.     if (NewEmployee.GetNumExpenses() < MIN_NUM_EXPENSES)
  51.         throw NoExpenseItems(NewEmployee.GetID());
  52.     if (NewEmployee.GetNumExpenses() > MAX_NUM_EXPENSES)
  53.         throw TooManyExpenseItems(NewEmployee.GetID());
  54.  
  55.     // Display the employee record
  56.     cout << "Employee #" << NewEmployee.GetID() << "\n";
  57.     cout << NewEmployee.GetFirstName() << " ";
  58.     cout << NewEmployee.GetMiddleInitial() << ". ";
  59.     cout << NewEmployee.GetLastName() << "\n";
  60.     cout << NewEmployee.GetAddress() << "\n";
  61.     cout << NewEmployee.GetCity() << ", ";
  62.     cout << NewEmployee.GetState() << " ";
  63.     cout << NewEmployee.GetZip() << "\n\n";
  64.  
  65.     // Now get the expense records
  66.     cout << "     Expenses\n";
  67.     cout << "     ---------------------\n";
  68.     for (loop = 0; loop < NewEmployee.GetNumExpenses(); loop++)
  69.         EmployeeTotal += ProcessExpenseItem(infile);
  70.  
  71.     // Check for problems
  72.     if (EmployeeTotal > MAX_ALLOWABLE_REIMBURSEMENT)
  73.         throw ExpenseTotalTooLarge();
  74.  
  75.     // Display the employee total
  76.     cout << "     =========================================================\n";
  77.     cout << "     Employee Total:";
  78.     cout.unsetf(ios::left);
  79.     cout << setw(42) << EmployeeTotal << "\n\n\n";
  80.     cout.setf(ios::left);
  81.  
  82.     return EmployeeTotal;
  83. }
  84.  
  85. void main(int argc, char *argv[])
  86. {
  87.     // Check for right number of command-line arguments
  88.     if (argc != 2) {
  89.         cout << "USAGE: expsum2 <infile name>\n\n";
  90.         return;
  91.     }
  92.  
  93.     // Open the infile
  94.     ifstream infile(argv[1], ios::nocreate);
  95.     if (!infile) {
  96.         cout << "Expense file \"" << argv[1] << "\" not found.\n\n";
  97.         return;
  98.     }
  99.  
  100.     // Main try block
  101.     try {
  102.  
  103.         // Read in our number of employees
  104.         unsigned EmployeeCount;
  105.         float    FileTotal = 0.0F;
  106.         infile >> EmployeeCount;
  107.         if (EmployeeCount < MIN_ALLOWABLE_EMPLOYEES || 
  108.             EmployeeCount > MAX_ALLOWABLE_EMPLOYEES)
  109.             throw ExpException(EXP_GENERIC_ERROR,
  110.                                "Invalid number of employees specified "
  111.                                "in expense file.");
  112.  
  113.         // Set the stream display flags
  114.         cout.setf(ios::left | ios::showpoint | ios::fixed);
  115.         cout.precision(2);
  116.  
  117.         // Display our main heading
  118.         cout << "+====================================================================+\n";
  119.         cout << "|                     EXPENSE FILE PROCESSOR                         |\n";
  120.         cout << "+====================================================================+\n\n\n";
  121.  
  122.         // Main employee loop
  123.         do {
  124.             FileTotal += ProcessEmployee(infile);
  125.         } while (--EmployeeCount);
  126.  
  127.         // Display the file total
  128.         cout <<"+=================================================================+\n";
  129.         cout << "| EXPENSE FILE TOTAL: ";
  130.         cout.unsetf(ios::left);
  131.         cout << setw(43) << FileTotal << " |\n";
  132.         cout.setf(ios::left);
  133.         cout <<"+=================================================================+\n";
  134.     }
  135.  
  136.     // Handlers
  137.     catch (EmployeeException& EmpExp) {
  138.         cout << "\nError! ";
  139.         cout << EmpExp.Why() << " (Employee " << EmpExp.GetEmployeeID() << ")\n";
  140.     }
  141.     catch (ExpException& Exp) {
  142.         cout << "\nError! " << Exp.Why() << "\n";
  143.     }
  144.     catch (...) {
  145.         cout << "Caught an unrecognized exception.\n";
  146.     }
  147.  
  148.     // Close the infile รน  _ we're done!
  149.     infile.close();
  150. }
  151.