fork download
  1. <?
  2. abstract class Employee
  3. {
  4. const RANK_FIRST = 1;
  5. const RANK_SECOND = 2;
  6. const RANK_THIRD = 3;
  7. const PROF_MANAGER = 1;
  8. const PROF_MARKETER = 2;
  9. const PROF_ENGINEER = 3;
  10. const PROF_ANALYST = 4;
  11.  
  12. protected $isLeader = false;
  13. protected $defaultSalary;
  14. protected $coffeeConsumption;
  15. protected $pagesCreated;
  16.  
  17. public function __construct($rank)
  18. {
  19. $this->rank = $rank;
  20. }
  21.  
  22. public function createEmployee($profession, $rank)
  23. {
  24. switch ($profession) {
  25. case Employee::PROF_MANAGER:
  26. $employee = new Manager($rank);
  27. break;
  28. case Employee::PROF_MARKETER:
  29. $employee = new Marketer($rank);
  30. break;
  31. case Employee::PROF_ENGINEER:
  32. $employee = new Engineer($rank);
  33. break;
  34. case Employee::PROF_ANALYST:
  35. $employee = new Analyst($rank);
  36. break;
  37. default:
  38. try {
  39. $employee = NULL;
  40. throw new Exception("<b>Error:</b> Profession {$profession} is not an Employee derivated class.");
  41. }
  42. catch (Exception $e) {
  43. echo $e->getMessage();
  44. }
  45. break;
  46. }
  47. return $employee;
  48. }
  49.  
  50. public function getSalary()
  51. {
  52. $salaryRate = 1;
  53. if ($this->rank == Employee::RANK_SECOND) {
  54. $salaryRate = 1.25;
  55. } elseif ($this->rank == Employee::RANK_THIRD) {
  56. $salaryRate = 1.5;
  57. }
  58. if ($this->isLeader) {
  59. $salaryRate *= 1.5;
  60. }
  61. return $this->defaultSalary * $salaryRate;
  62. }
  63.  
  64. public function getCoffeeConsumption()
  65. {
  66. $coffeeConsumption = $this->coffeeConsumption;
  67. if ($this->isLeader) {
  68. $coffeeConsumption = $this->coffeeConsumption * 2;
  69. }
  70. return $coffeeConsumption;
  71. }
  72.  
  73. public function getPagesCreated()
  74. {
  75. $pagesCreated = $this->pagesCreated;
  76. if ($this->isLeader) {
  77. $pagesCreated = 0;
  78. }
  79. return $pagesCreated;
  80. }
  81.  
  82. public function setIsLeader($isLeader)
  83. {
  84. $this->isLeader = $isLeader;
  85. return $this;
  86. }
  87. }
  88.  
  89. class Manager extends Employee
  90. {
  91. protected $defaultSalary = 500;
  92. protected $coffeeConsumption = 20;
  93. protected $pagesCreated = 200;
  94. }
  95.  
  96. class Marketer extends Employee
  97. {
  98. protected $defaultSalary = 400;
  99. protected $coffeeConsumption = 15;
  100. protected $pagesCreated = 150;
  101. }
  102.  
  103. class Engineer extends Employee
  104. {
  105. protected $defaultSalary = 200;
  106. protected $coffeeConsumption = 5;
  107. protected $pagesCreated = 50;
  108. }
  109.  
  110. class Analyst extends Employee
  111. {
  112. protected $defaultSalary = 800;
  113. protected $coffeeConsumption = 50;
  114. protected $pagesCreated = 5;
  115. }
  116.  
  117. class Department
  118. {
  119. protected $name;
  120. protected $employees = array();
  121. protected $leader;
  122.  
  123. public function __construct($name)
  124. {
  125. $this->name = $name;
  126. }
  127.  
  128. public function addEmployees($profession, $rank, $count = 1)
  129. {
  130. for ($i = 0; $i < $count; $i++) {
  131. $this->employees[] = Employee::createEmployee($profession, $rank);
  132. }
  133. return $this;
  134. }
  135.  
  136. public function setLeader(Employee $person)
  137. {
  138. $person->setIsLeader(true);
  139. $this->leader = $person;
  140. }
  141.  
  142. public function getTotalSalary()
  143. {
  144. $totalSalary = 0;
  145. foreach ($this->employees as $employee) {
  146. $totalSalary += $employee->getSalary();
  147. }
  148. return $totalSalary;
  149. }
  150.  
  151. public function getTotalPages()
  152. {
  153. $totalPages = 0;
  154. foreach ($this->employees as $employee) {
  155. $totalPages += $employee->getPagesCreated();
  156. }
  157. return $totalPages;
  158. }
  159.  
  160. public function getTotalCoffeeConsumption()
  161. {
  162. $totalCoffee = 0;
  163. foreach ($this->employees as $employee) {
  164. $totalCoffee += $employee->getCoffeeConsumption();
  165. }
  166. return $totalCoffee;
  167. }
  168.  
  169. public function getEmployeesCount()
  170. {
  171. return count($this->employees);
  172. }
  173.  
  174. public function getCurrencyPerPage()
  175. {
  176. $currencyPerPage = $this->getTotalSalary() / $this->getTotalPages();
  177. return $currencyPerPage;
  178. }
  179. public function getDepartmentSummary()
  180. {
  181. $format = "%-15s %15d %15d %15d %15d %15.2f\n";
  182. printf($format, $this->name, $this->getEmployeesCount(), $this->getTotalSalary(), $this->getTotalCoffeeConsumption(), $this->getTotalPages(), $this->getCurrencyPerPage());
  183. }
  184. }
  185.  
  186. $salesDepartment = new Department("Продаж");
  187. $salesDepartment->addEmployees(Employee::PROF_MANAGER, Employee::RANK_FIRST, 12)->addEmployees(Employee::PROF_MARKETER, Employee::RANK_FIRST, 6)->addEmployees(Employee::PROF_ANALYST, Employee::RANK_FIRST, 3)->addEmployees(Employee::PROF_ANALYST, Employee::RANK_SECOND, 2);
  188. $salesDepartment->setLeader(new Manager(Employee::RANK_SECOND));
  189.  
  190. $purchasesDepartment = new Department("Закупок");
  191. $purchasesDepartment->addEmployees(Employee::PROF_MANAGER, Employee::RANK_FIRST, 9)->addEmployees(Employee::PROF_MANAGER, Employee::RANK_SECOND, 3)->addEmployees(Employee::PROF_MANAGER, Employee::RANK_THIRD, 2)->addEmployees(Employee::PROF_MARKETER, Employee::RANK_FIRST, 2);
  192. $purchasesDepartment->setLeader(new Marketer(Employee::RANK_SECOND));
  193.  
  194. $advertisementDepartment = new Department("Рекламы");
  195. $advertisementDepartment->addEmployees(Employee::PROF_MARKETER, Employee::RANK_FIRST, 15)->addEmployees(Employee::PROF_MARKETER, Employee::RANK_SECOND, 10)->addEmployees(Employee::PROF_MANAGER, Employee::RANK_FIRST, 8)->addEmployees(Employee::PROF_ENGINEER, Employee::RANK_FIRST, 2);
  196. $advertisementDepartment->setLeader(new Marketer(Employee::RANK_THIRD));
  197.  
  198. $logisticsDepartment = new Department("Логистики");
  199. $logisticsDepartment->addEmployees(Employee::PROF_MANAGER, Employee::RANK_FIRST, 13)->addEmployees(Employee::PROF_MANAGER, Employee::RANK_SECOND, 5)->addEmployees(Employee::PROF_ENGINEER, Employee::RANK_FIRST, 5);
  200. $logisticsDepartment->setLeader(new Manager(Employee::RANK_FIRST));
  201.  
  202. $departmentsArray = array(
  203. 'salesDepartment',
  204. 'purchasesDepartment',
  205. 'advertisementDepartment',
  206. 'logisticsDepartment'
  207. );
  208. $countofDepartments = count($departmentsArray);
  209. $totalEmployees = 0;
  210. $totalSalary = 0;
  211. $totalCoffee = 0;
  212. $totalPages = 0;
  213. $currencyPerPage = 0;
  214. printf("%-15s %16s %19s %19s %19s %22s\n", 'Департамент', 'cотр.', 'тугр.', 'кофе', 'стр.', 'тугр./стр.');
  215. printf("%'-90s\n", '');
  216. foreach ($departmentsArray as $department) {
  217. $$department->getDepartmentSummary();
  218. $totalEmployees += $$department->getEmployeesCount();
  219. $totalSalary += $$department->getTotalSalary();
  220. $totalCoffee += $$department->getTotalCoffeeConsumption();
  221. $totalPages += $$department->getTotalPages();
  222. $currencyPerPage += $$department->getCurrencyPerPage();
  223. }
  224. #$avgEmployees = $totalEmployees/$countofDepartments;
  225. #$avgSalary = $totalSalary/$countofDepartments;
  226. #$avgCoffee = $totalCoffee/$countofDepartments;
  227. #$avgPages = $totalPages/$countofDepartments;
  228. #$avgCurrencyPerPage = $currencyPerPage/$countofDepartments;
  229. $parametersArray = array('totalEmployees'=>'avgEmployees', 'totalSalary'=>'avgSalary', 'totalCoffee'=>'avgCoffee', 'totalPages'=>'avgPages', 'currencyPerPage'=>'avgCurrencyPerPage');
  230. foreach($parametersArray as $totalVariable => $avgVariable){
  231. $$avgVariable = $$totalVariable / $countofDepartments;
  232. }
  233. printf("%-15s %15d %15d %15d %15d %15.2f\n", 'Среднее', $avgEmployees, $avgSalary, $avgCoffee, $avgPages, $avgCurrencyPerPage);
  234. printf("%-15s %15d %15d %15d %15d %15.2f\n", 'Всего', $totalEmployees, $totalSalary, $totalCoffee, $totalPages, $currencyPerPage);
  235. #Employee::createEmployee('Driver', Employee::RANK_FIRST);
  236. ?>
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Департамент         cотр.           тугр.            кофе             стр.      тугр./стр.
------------------------------------------------------------------------------------------
Продаж                 23           12800             580            3325            3.85
Закупок               16            8675             310            3100            2.80
Рекламы               35           15400             545            5450            2.83
Логистики              23           10625             385            3850            2.76
Среднее               24           11875             455            3931            3.06
Всего                   97           47500            1820           15725           12.23