home *** CD-ROM | disk | FTP | other *** search
- // Get the needed include files
- #include <fstream.h>
- #include <iomanip.h>
- #include "employee.h"
- #include "expense.h"
- #include "except.h"
-
- // Definitions
- const int MIN_ALLOWABLE_EMPLOYEES = 1;
- const int MAX_ALLOWABLE_EMPLOYEES = 20;
- const int MIN_NUM_EXPENSES = 1;
- const int MAX_NUM_EXPENSES = 10;
- const float MIN_ALLOWABLE_EXPENSE = 1.0F;
- const float MAX_ALLOWABLE_EXPENSE = 1000.0F;
- const float MAX_ALLOWABLE_REIMBURSEMENT = 5000.0F;
-
- float ProcessExpenseItem(ifstream& infile)
- {
- Expense NewExpense;
-
- // Read in the expense record from the file
- infile >> NewExpense;
-
- // Check for problems
- if (NewExpense.GetExpenseAmount() < MIN_ALLOWABLE_EXPENSE)
- throw ExpenseTooSmall();
- if (NewExpense.GetExpenseAmount() > MAX_ALLOWABLE_EXPENSE)
- throw ExpenseTooLarge();
-
- // Display the expense
- cout << " " << setw(10) << NewExpense.GetDate();
- cout << setw(35) << NewExpense.GetDescription();
- cout.unsetf(ios::left);
- cout <<setw(12)<< NewExpense.GetExpenseAmount()<< "\n"<< setw(0);
- cout.setf(ios::left);
-
- return NewExpense.GetExpenseAmount();
- }
-
- float ProcessEmployee(ifstream& infile)
- {
- Employee NewEmployee;
- float EmployeeTotal = 0.0F;
- unsigned loop;
-
- // Read in the employee record from the file
- infile >> NewEmployee;
-
- // Check for problems
- if (NewEmployee.GetNumExpenses() < MIN_NUM_EXPENSES)
- throw NoExpenseItems(NewEmployee.GetID());
- if (NewEmployee.GetNumExpenses() > MAX_NUM_EXPENSES)
- throw TooManyExpenseItems(NewEmployee.GetID());
-
- // Display the employee record
- cout << "Employee #" << NewEmployee.GetID() << "\n";
- cout << NewEmployee.GetFirstName() << " ";
- cout << NewEmployee.GetMiddleInitial() << ". ";
- cout << NewEmployee.GetLastName() << "\n";
- cout << NewEmployee.GetAddress() << "\n";
- cout << NewEmployee.GetCity() << ", ";
- cout << NewEmployee.GetState() << " ";
- cout << NewEmployee.GetZip() << "\n\n";
-
- // Now get the expense records
- cout << " Expenses\n";
- cout << " ---------------------\n";
- for (loop = 0; loop < NewEmployee.GetNumExpenses(); loop++)
- EmployeeTotal += ProcessExpenseItem(infile);
-
- // Check for problems
- if (EmployeeTotal > MAX_ALLOWABLE_REIMBURSEMENT)
- throw ExpenseTotalTooLarge();
-
- // Display the employee total
- cout << " =========================================================\n";
- cout << " Employee Total:";
- cout.unsetf(ios::left);
- cout << setw(42) << EmployeeTotal << "\n\n\n";
- cout.setf(ios::left);
-
- return EmployeeTotal;
- }
-
- void main(int argc, char *argv[])
- {
- // Check for right number of command-line arguments
- if (argc != 2) {
- cout << "USAGE: expsum2 <infile name>\n\n";
- return;
- }
-
- // Open the infile
- ifstream infile(argv[1], ios::nocreate);
- if (!infile) {
- cout << "Expense file \"" << argv[1] << "\" not found.\n\n";
- return;
- }
-
- // Main try block
- try {
-
- // Read in our number of employees
- unsigned EmployeeCount;
- float FileTotal = 0.0F;
- infile >> EmployeeCount;
- if (EmployeeCount < MIN_ALLOWABLE_EMPLOYEES ||
- EmployeeCount > MAX_ALLOWABLE_EMPLOYEES)
- throw ExpException(EXP_GENERIC_ERROR,
- "Invalid number of employees specified "
- "in expense file.");
-
- // Set the stream display flags
- cout.setf(ios::left | ios::showpoint | ios::fixed);
- cout.precision(2);
-
- // Display our main heading
- cout << "+====================================================================+\n";
- cout << "| EXPENSE FILE PROCESSOR |\n";
- cout << "+====================================================================+\n\n\n";
-
- // Main employee loop
- do {
- FileTotal += ProcessEmployee(infile);
- } while (--EmployeeCount);
-
- // Display the file total
- cout <<"+=================================================================+\n";
- cout << "| EXPENSE FILE TOTAL: ";
- cout.unsetf(ios::left);
- cout << setw(43) << FileTotal << " |\n";
- cout.setf(ios::left);
- cout <<"+=================================================================+\n";
- }
-
- // Handlers
- catch (EmployeeException& EmpExp) {
- cout << "\nError! ";
- cout << EmpExp.Why() << " (Employee " << EmpExp.GetEmployeeID() << ")\n";
- }
- catch (ExpException& Exp) {
- cout << "\nError! " << Exp.Why() << "\n";
- }
- catch (...) {
- cout << "Caught an unrecognized exception.\n";
- }
-
- // Close the infile รน _ we're done!
- infile.close();
- }
-