Sunteți pe pagina 1din 3

/*

*
*
*
*
*/

employeetest.cpp
Employee class "unit test"
demonstrates exception handling: try | catch blocks
CSIS1600

#include
#include
#include
#include

<iostream>
<iomanip>
<fstream>
"Employee.cpp"

class EmployeeTest
{
public:
EmployeeTest();
void test_defaultContructor();
void test_paramConstructor();
void test_setters();
void test_fileIO();
private:
Employee* e1;
Employee* e2;
Employee* e3;
};//end EmployeeTest
EmployeeTest::EmployeeTest()
{
e1 = new Employee();
e2 = new Employee(1002, 12.5, 42);
e3 = new Employee(1003, 15.25, 40);
}
void EmployeeTest::test_defaultContructor()
{
cout << "Test default constructor..." << endl;
cout << e1->GetidNumber() << endl;
cout << e1->GetpayRate() << endl;
cout << e1->GetweeklyHours() << endl;
}//end testDefaultContructor()
void EmployeeTest::test_paramConstructor()
{
cout << "\nTest parametrized constructor..." << endl;
cout << e2->GetidNumber() << endl;
cout << e2->GetpayRate() << endl;
cout << e2->GetweeklyHours() << endl;
cout
cout
cout
}//end

<< e3->GetidNumber() << endl;


<< e3->GetpayRate() << endl;
<< e3->GetweeklyHours() << endl;
test_paramConstructor()

void EmployeeTest::test_setters()
{
cout << "\nTest set methods..." << endl;
e1->SetidNumber(1001);
e1->SetpayRate(10.0);
e1->SetweeklyHours(37);

cout << e1->GetidNumber() << endl;


cout << e1->GetpayRate() << endl;
cout << e1->GetweeklyHours() << endl;
}
void EmployeeTest::test_fileIO()
{
try
{
cout << "\nWrite employees to file..." << endl;
//open the file
ofstream outFile("employee.txt", ios::out);
if( !outFile ) //file not opened so throw exception
throw new string("employee.txt not opened...");
//write the records
e1->writeEmployee(outFile);
e2->writeEmployee(outFile);
e3->writeEmployee(outFile);
//close the file
outFile.close();
}
catch(string* msg)
{
cerr << "Exception: " << *msg << endl;
}
try
{
cout << "\nRead employees from file..." << endl;
//open the file
ifstream inFile("employee.txt", ios::in);
if( !inFile )
throw new string("employee.text not opened...");
//read the file
cout << setw(4) << "ID"
<< setw(11) << "Rate"
<< setw(10) << "Hours" << endl;
Employee e4, e5, e6;
e4.readEmployee(inFile);
cout << setw(5) << e4.GetidNumber() << " "
<< fixed << setprecision(2) << setw(9) << e4.GetpayRate() << " "
<< fixed << setprecision(2) << setw(9) << e4.GetweeklyHours() << endl;
e5.readEmployee(inFile);
cout << setw(5) << e5.GetidNumber() << " "
<< fixed << setprecision(2) << setw(9) << e5.GetpayRate() << " "
<< fixed << setprecision(2) << setw(9) << e5.GetweeklyHours() << endl;
e6.readEmployee(inFile);
cout << setw(5) << e6.GetidNumber() << " "
<< fixed << setprecision(2) << setw(9) << e6.GetpayRate() << " "
<< fixed << setprecision(2) << setw(9) << e6.GetweeklyHours() << endl;
//close the file
inFile.close();
}
catch(string* msg)
{
cerr << "Exception: " + *msg << endl;
}
}//end fileIO_test()

/*
*
*
*
*
*
*
*/

Participation P11 - File IO


add a test function for writing|reading an array of Employees
1. create an array of 5 Employees
2. write each Employee in the array to "EmployeeList.txt"
3. read "EmployeeList.txt" and add each Employee to a new array
4. display the new array of Employees

int main()
{
EmployeeTest emp;
emp.test_defaultContructor();
emp.test_paramConstructor();
emp.test_setters();
emp.test_fileIO();
return 0;
}

S-ar putea să vă placă și