Datasheet
Employee& Database::addEmployee(string inFirstName, string inLastName)
{
if (mNextSlot >= kMaxEmployees) {
cerr << “There is no more room to add the new employee!” << endl;
throw exception();
}
Employee& theEmployee = mEmployees[mNextSlot++];
theEmployee.setFirstName(inFirstName);
theEmployee.setLastName(inLastName);
theEmployee.setEmployeeNumber(mNextEmployeeNumber++);
theEmployee.hire();
return theEmployee;
}
The addEmployee() method fills in the next “blank” employee with actual information. An initial check
makes sure that the
mEmployees array is not full and throws an exception if it is. Note that after their
use, the
mNextSlot and mNextEmployeeNumber data members are incremented so that the next
employee will get a new slot and number.
Employee& Database::getEmployee(int inEmployeeNumber)
{
for (int i = 0; i < mNextSlot; i++) {
if (mEmployees[i].getEmployeeNumber() == inEmployeeNumber) {
return mEmployees[i];
}
}
cerr << “No employee with employee number “ << inEmployeeNumber << endl;
throw exception();
}
Employee& Database::getEmployee(string inFirstName, string inLastName)
{
for (int i = 0; i < mNextSlot; i++) {
if (mEmployees[i].getFirstName() == inFirstName &&
mEmployees[i].getLastName() == inLastName) {
return mEmployees[i];
}
}
cerr << “No match with name “ << inFirstName << “ “ << inLastName << endl;
throw exception();
}
Both versions of getEmployee() work in similar ways. The methods loop over all nonblank employees
in the
mEmployees array and check to see if each Employee is a match for the information passed to the
method. If no match is found, an error is output and an exception is thrown.
36
Chapter 1
04_574841 ch01.qxd 12/15/04 3:39 PM Page 36