Online Book Reader

Home Category

Professional C__ - Marc Gregoire [20]

By Root 1264 0
Database Class

The Database class uses the std::vector class from the standard library to store Employee objects.

Database.h

Because the database will take care of automatically assigning an employee number to a new employee, a constant defines where the numbering begins.

#include

#include

#include "Employee.h"

namespace Records {

const int kFirstEmployeeNumber = 1000;

Code snippet from EmployeeDB\Database.h

The database provides an easy way to add a new employee by providing a first and last name. For convenience, this method will return a reference to the new employee. External code can also get an employee reference by calling the getEmployee() method. Two versions of this method are declared. One allows retrieval by employee number. The other requires a first and last name.

class Database

{

public:

Database();

~Database();

Employee& addEmployee(std::string inFirstName,

std::string inLastName);

Employee& getEmployee(int inEmployeeNumber);

Employee& getEmployee(std::string inFirstName,

std::string inLastName);

Code snippet from EmployeeDB\Database.h

Because the database is the central repository for all employee records, it has methods that will output all employees, the employees who are currently hired, and the employees who are no longer hired.

void displayAll() const;

void displayCurrent() const;

void displayFormer() const;

Code snippet from EmployeeDB\Database.h

mEmployees contains the Employee objects. The mNextEmployeeNumber data member keeps track of what employee number will be assigned to a new employee.

protected:

std::vector mEmployees;

int mNextEmployeeNumber;

};

}

Code snippet from EmployeeDB\Database.h

Database.cpp

The Database constructor takes care of initializing the next employee number member to its starting value.

#include

#include

#include "Database.h"

using namespace std;

namespace Records {

Database::Database()

{

mNextEmployeeNumber = kFirstEmployeeNumber;

}

Database::~Database()

{

}

Code snippet from EmployeeDB\Database.cpp

The addEmployee() method creates a new Employee object, fills in its information and adds it to the vector. Note that the mNextEmployeeNumber data member is incremented after its use so that the next employee will get a new number.

Employee& Database::addEmployee(string inFirstName, string inLastName)

{

Employee theEmployee;

theEmployee.setFirstName(inFirstName);

theEmployee.setLastName(inLastName);

theEmployee.setEmployeeNumber(mNextEmployeeNumber++);

theEmployee.hire();

mEmployees.push_back(theEmployee);

return mEmployees[mEmployees.size()-1];

}

Code snippet from EmployeeDB\Database.cpp

Only one version of getEmployee() is shown. Both versions work in similar ways. The methods loop over all employees in mEmployees using an iterator 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. If your compiler does not support the auto feature, you should replace auto with vector::iterator.

Employee& Database::getEmployee(int inEmployeeNumber)

{

for (auto iter = mEmployees.begin();

iter != mEmployees.end(); ++iter) {

if (iter->getEmployeeNumber() == inEmployeeNumber)

return *iter;

}

cerr << "No employee with number " << inEmployeeNumber << endl;

throw exception();

}

Code snippet from EmployeeDB\Database.cpp

The display methods all use a similar algorithm. They loop through all employees and tell each employee to display itself to the console if the criterion for display matches. If your compiler does not support the auto feature, you should replace auto with vector::const_iterator because these are const member functions.

void Database::displayAll() const

{

for (auto iter = mEmployees.begin();

iter != mEmployees.end(); ++iter) {

iter->display();

}

}

void Database::displayCurrent() const

{

for (auto iter = mEmployees.begin();

iter != mEmployees.end(); ++iter) {

if (iter->getIsHired())

iter->display();

}

}

Return Main Page Previous Page Next Page

®Online Book Reader