/**************************************************************************** ** COPYRIGHT (C): 1997 Cay S. Horstmann. All Rights Reserved. ** PROJECT: Practical OO Development with C++ and Java ** FILE: empltest.cpp ** PURPOSE: test empl.cpp ** VERSION 1.0 ** PROGRAMMERS: Cay Horstmann (CSH) ** RELEASE DATE: 3-15-97 (CSH) ** UPDATE HISTORY: ****************************************************************************/ /* Project: Console Application Add files empltest.cpp employee.cpp date.cpp Additional include directory: \PracticalOOBook\cpplib */ #include "setup.h" #include #include "date.h" #include "employee.h" int employee_comp(const Employee** e, const Employee** f) /* PURPOSE: compare two employee records for sorting RECEIVES: e, f - pointers to employees RETURNS: < 0 if e comes before f, 0 if they have the same id, >0 otherwise */ { return (**e).id() - (**f).id(); } /*.............................................................*/ double salary(const Employee& e, Date from, Date to) /* PURPOSE: Compute employee salary in a date range RECEIVES: e - the employee whose salary to compute from, to - the date range RETURNS: the total salary earned REMARKS: We assume 8 hours of work for every weekday in the pay period */ { double s = 0; double h = 0; for (Date d = from; d.compare(to) <= 0; d.advance(1)) { Date::Weekday w = d.weekday(); if (w != Date::SAT && w != Date::SUN) { h += 8; if (w == Date::FRI) { s += e.weekly_pay(h); h = 0; } } } if (h > 0) s += e.weekly_pay(h); return s; } /*.............................................................*/ int main() { #ifndef PRACTICALOO_MUST_USE_ALLOCATOR vector staff(5); #else vector > staff(5); #endif Employee* harry = new Employee("Harry Hacker", 149); Manager* carol = new Manager("Carol Smith"); harry->set_salary(40000); harry->raise_salary(0.08); carol->set_salary(60000); carol->raise_salary(0.03); staff[0] = new Employee("Joe User", 16); staff[1] = new Employee("Karl Schiller", 2536); staff[2] = new Employee("Jessica Chang", 31, 3, 1961); for (int i = 0; i < 3; i++) staff[i]->set_salary(35000); staff[2]->set_id(3); staff[3] = harry; staff[4] = carol; carol->add(harry); carol->add(staff[2]); qsort(&staff[0], 5, sizeof(staff[0]), (int (*) (const void*, const void *))employee_comp); cout.precision(2); cout.setf(ios::fixed | ios::showpoint); for (i = 0; i < staff.size(); i++) staff[i]->print(); for (i = 0; i < staff.size(); i++) cout << staff[i]->id() << " : " << staff[i]->weekly_pay(48) << endl; Date from(1, 1, 1994); Date to(31, 3, 1994); for (i = 0; i < staff.size(); i++) cout << staff[i]->id() << " : " << salary(*staff[i], from, to) << endl; for (i = 0; i < staff.size(); i++) delete staff[i]; return 0; }