Online Book Reader

Home Category

Professional C__ - Marc Gregoire [21]

By Root 1278 0

}

Code snippet from EmployeeDB\Database.cpp

DatabaseTest.cpp

A simple test for the basic functionality of the database follows:

#include

#include "Database.h"

using namespace std;

using namespace Records;

int main()

{

Database myDB;

Employee& emp1 = myDB.addEmployee("Greg", "Wallis");

emp1.fire();

Employee& emp2 = myDB.addEmployee("Scott", "Kleper");

emp2.setSalary(100000);

Employee& emp3 = myDB.addEmployee("Nick", "Solter");

emp3.setSalary(10000);

emp3.promote();

cout << "all employees: " << endl;

cout << endl;

myDB.displayAll();

cout << endl;

cout << "current employees: " << endl;

cout << endl;

myDB.displayCurrent();

cout << endl;

cout << "former employees: " << endl;

cout << endl;

myDB.displayFormer();

return 0;

}

Code snippet from EmployeeDB\DatabaseTest.cpp

The User Interface

The final part of the program is a menu-based user interface that makes it easy for users to work with the employee database.

UserInterface.cpp

The main() function is a loop that displays the menu, performs the selected action, then does it all again. For most actions, separate functions are defined. For simpler actions, like displaying employees, the actual code is put in the appropriate case.

#include

#include

#include "Database.h"

using namespace std;

using namespace Records;

int displayMenu();

void doHire(Database& inDB);

void doFire(Database& inDB);

void doPromote(Database& inDB);

void doDemote(Database& inDB);

int main()

{

Database employeeDB;

bool done = false;

while (!done) {

int selection = displayMenu();

switch (selection) {

case 1:

doHire(employeeDB);

break;

case 2:

doFire(employeeDB);

break;

case 3:

doPromote(employeeDB);

break;

case 4:

employeeDB.displayAll();

break;

case 5:

employeeDB.displayCurrent();

break;

case 6:

employeeDB.displayFormer();

break;

case 0:

done = true;

break;

default:

cerr << "Unknown command." << endl;

}

}

return 0;

}

Code snippet from EmployeeDB\UserInterface.cpp

The displayMenu() function outputs the menu and gets input from the user. One important note is that this code assumes that the user will “play nice” and type a number when a number is requested. When you read about I/O in Chapter 15, you will learn how to protect against bad input.

int displayMenu()

{

int selection;

cout << endl;

cout << "Employee Database" << endl;

cout << "-----------------" << endl;

cout << "1) Hire a new employee" << endl;

cout << "2) Fire an employee" << endl;

cout << "3) Promote an employee" << endl;

cout << "4) List all employees" << endl;

cout << "5) List all current employees" << endl;

cout << "6) List all previous employees" << endl;

cout << "0) Quit" << endl;

cout << endl;

cout << "---> ";

cin >> selection;

return selection;

}

Code snippet from EmployeeDB\UserInterface.cpp

The doHire() function gets the new employee’s name from the user and tells the database to add the employee. It handles errors somewhat gracefully by outputting a message and continuing.

void doHire(Database& inDB)

{

string firstName;

string lastName;

cout << "First name? ";

cin >> firstName;

cout << "Last name? ";

cin >> lastName;

try {

inDB.addEmployee(firstName, lastName);

} catch (const std::exception&) {

cerr << "Unable to add new employee!" << endl;

}

}

Code snippet from EmployeeDB\UserInterface.cpp

doFire() and doPromote() both ask the database for an employee by their employee number and then use the public methods of the Employee object to make changes.

void doFire(Database& inDB)

{

int employeeNumber;

cout << "Employee number? ";

cin >> employeeNumber;

try {

Employee& emp = inDB.getEmployee(employeeNumber);

emp.fire();

cout << "Employee " << employeeNumber << " terminated." << endl;

} catch (const std::exception&) {

cerr << "Unable to terminate employee!" << endl;

}

}

void doPromote(Database& inDB)

{

int employeeNumber;

int raiseAmount;

cout << "Employee number? ";

cin >> employeeNumber;

cout << "How much of a raise? ";

cin >> raiseAmount;

try {

Employee& emp = inDB.getEmployee(employeeNumber);

Return Main Page Previous Page Next Page

®Online Book Reader