home *** CD-ROM | disk | FTP | other *** search
- // Get our needed include files
- #include <employee.h>
- #include <expense.h>
- #include <fstream.h>
- #include <iomanip.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;
-
- // Error codes
- typedef enum {
- EXP_NO_ERROR,
- EXP_ERR_EXPENSE_TOO_SMALL,
- EXP_ERR_EXPENSE_TOO_LARGE,
- EXP_ERR_NO_EXPENSE_ITEMS,
- EXP_ERR_TOO_MANY_EXPENSE_ITEMS,
- EXP_ERR_TOTAL_EXPENSE_TOO_LARGE
- } ErrorCode;
-
- float ProcessExpenseItem(ifstream& infile, ErrorCode& rc)
- {
- Expense NewExpense;
-
- // Init the return code
- rc = EXP_NO_ERROR;
-
- // Read in the expense record from the file
- infile >> NewExpense;
-
- // Check for problems
- if (NewExpense.GetExpenseAmount() < MIN_ALLOWABLE_EXPENSE) {
- cout << "Error! Expense item under the minimum amount "
- << "eligible for reimbursement.\n\n";
- rc = EXP_ERR_EXPENSE_TOO_SMALL;
- return 0.0F;
- }
- if (NewExpense.GetExpenseAmount() > MAX_ALLOWABLE_EXPENSE) {
- cout << "Error! Expense item greater than maximum amount "
- << "eligible for reimbursement.\n\n";
- rc = EXP_ERR_EXPENSE_TOO_LARGE;
- return 0.0F;
- }
-
- // 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, ErrorCode& rc)
- {
- Employee NewEmployee;
- float EmployeeTotal = 0.0F;
- unsigned loop;
-
- // Init the return code
- rc = EXP_NO_ERROR;
-
- // Read in the employee record from the file
- infile >> NewEmployee;
-
- // Check for problems
- if (NewEmployee.GetNumExpenses() < MIN_NUM_EXPENSES) {
- cout << "Error! No expense items for this employee.\n\n";
- rc = EXP_ERR_NO_EXPENSE_ITEMS;
- return 0.0F;
- }
- if (NewEmployee.GetNumExpenses() > MAX_NUM_EXPENSES) {
- cout << "Error! Too many expense items for this employee.\n\n";
- rc = EXP_ERR_TOO_MANY_EXPENSE_ITEMS;
- return 0.0F;
- }
-
- // 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, rc);
- if (rc != EXP_NO_ERROR)
- return 0.0F;
- }
-
- // Check for problems
- if (EmployeeTotal > MAX_ALLOWABLE_REIMBURSEMENT) {
- cout << "Error! Expense total greater than maximum "
- << "amount eligible for reimbursement.\n\n";
- rc = EXP_ERR_TOTAL_EXPENSE_TOO_LARGE;
- return 0.0F;
- }
-
- // 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: expsum1 <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;
- }
-
- // Read in our number of employees
- unsigned EmployeeCount;
- float FileTotal = 0.0F;
- infile >> EmployeeCount;
- if (EmployeeCount < MIN_ALLOWABLE_EMPLOYEES ||
- EmployeeCount > MAX_ALLOWABLE_EMPLOYEES) {
- cout << "Invalid number of employees specified in "
- << "expense file.\n\n";
- infile.close();
- }
-
- // 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
- ErrorCode rc;
- do {
- FileTotal += ProcessEmployee(infile, rc);
- if (rc != EXP_NO_ERROR) {
- infile.close();
- return;
- }
- } 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";
-
- // Close the infile _ we're done!
- infile.close();
- }
-
-