fork download
  1. <?php
  2.  
  3.  
  4. abstract class Employee
  5. {
  6. public $rank;
  7. public $isChief = false;
  8. public $baseRate;
  9. public $coffeeConsumption;
  10. public $paperConsumption;
  11.  
  12. public function __construct($rank = 1)
  13. {
  14. $this->rank = $rank;
  15. }
  16. public function getSalary()
  17. {
  18. $salary = $this->baseRate;
  19. if ($this->rank == 2) {
  20. $salary *= 1.25;
  21. } elseif ($this->rank == 3) {
  22. $salary *= 1.5;
  23. }
  24. if ($this->isChief) {
  25. $salary *= 1.5;
  26. }
  27. return $salary;
  28. }
  29. public function getCoffeeConsumption(){
  30. if ($this->isChief) {
  31. return $this->coffeeConsumption * 2;
  32. } else {
  33. return $this->coffeeConsumption;
  34. }
  35. }
  36. public function getPaperConsumption()
  37. {
  38. if ($this->isChief) {
  39. return 0;
  40. } else {
  41. return $this->paperConsumption;
  42. }
  43. }
  44. }
  45.  
  46. class Manager extends Employee
  47. {
  48. public $baseRate = 500;
  49. public $coffeeConsumption = 20;
  50. public $paperConsumption = 200;
  51. }
  52.  
  53. class Marketing extends Employee
  54. {
  55. public $baseRate = 400;
  56. public $coffeeConsumption = 15;
  57. public $paperConsumption = 150;
  58. }
  59.  
  60. class Engineer extends Employee
  61. {
  62. public $baseRate = 200;
  63. public $coffeeConsumption = 5;
  64. public $paperConsumption = 50;
  65. }
  66.  
  67. class Analyst extends Employee
  68. {
  69. public $baseRate = 800;
  70. public $coffeeConsumption = 50;
  71. public $paperConsumption = 5;
  72. }
  73.  
  74. class Department
  75. {
  76. public $name;
  77. public $chief;
  78. public $employees;
  79.  
  80. public function __construct ($name)
  81. {
  82. $this->name = $name;
  83. }
  84.  
  85. public function setChief(Employee $chief)
  86. {
  87. $chief->isChief = true;
  88. $this->chief = $chief;
  89. }
  90. public function addEmployees($employees)
  91. {
  92. foreach ($employees as $employee) {
  93. $this->employees[] = $employee;
  94. }
  95. }
  96. public function getEmployeesCount()
  97. {
  98. return count($this->employees) + 1;
  99. }
  100. public function getEmployeesSalary()
  101. {
  102. $employeesSalary = 0;
  103. foreach ($this->employees as $employee) {
  104. $employeesSalary += $employee->getSalary();
  105. }
  106. $employeesSalary += $this->chief->getSalary();
  107. return round($employeesSalary);
  108. }
  109. public function getConsumptionOfCoffee()
  110. {
  111. $consumptionOfCoffee = 0;
  112. foreach ($this->employees as $employee) {
  113. $consumptionOfCoffee += $employee->getCoffeeConsumption();
  114. }
  115. $consumptionOfCoffee += $this->chief->getCoffeeConsumption();
  116. return $consumptionOfCoffee;
  117. }
  118. public function getPaperConsumption()
  119. {
  120. $paperConsumption = 0;
  121. foreach ($this->employees as $employee) {
  122. $paperConsumption += $employee->getPaperConsumption();
  123. }
  124. $paperConsumption += $this->chief->getPaperConsumption();
  125. return $paperConsumption;
  126. }
  127. public function getCostPerPage()
  128. {
  129. return round($this->getEmployeesSalary() / $this->getPaperConsumption(), 2);
  130. }
  131. }
  132.  
  133. //Функция генерация $count количества заданных сотрудников
  134. function generateEmployeesList ($name, $rank, $count, $employees)
  135. {
  136. for ($i = 0; $i < $count; ++$i) {
  137. $employees[] = new $name($rank);
  138. }
  139. return $employees;
  140. }
  141.  
  142. //Создаем департаменты
  143. $employees = array();
  144. $employees = generateEmployeesList('Manager', 1, 9, $employees);
  145. $employees = generateEmployeesList('Manager', 2, 3, $employees);
  146. $employees = generateEmployeesList('Manager', 3, 2, $employees);
  147. $employees = generateEmployeesList('Marketing', 1, 2, $employees);
  148. $chief = new Manager(2);
  149. $departmentOfProcurement = new Department('Закупок');
  150. $departmentOfProcurement->setChief($chief);
  151. $departmentOfProcurement->addEmployees($employees);
  152.  
  153. $employees = array();
  154. $employees = generateEmployeesList('Manager', 1, 12, $employees);
  155. $employees = generateEmployeesList('Marketing', 1, 6, $employees);
  156. $employees = generateEmployeesList('Analyst', 1, 3, $employees);
  157. $employees = generateEmployeesList('Analyst', 2, 2, $employees);
  158. $chief = new Marketing(2);
  159. $salesDepartment = new Department('Продаж');
  160. $salesDepartment->setChief($chief);
  161. $salesDepartment->addEmployees($employees);
  162.  
  163. $employees = array();
  164. $employees = generateEmployeesList('Marketing', 1, 15, $employees);
  165. $employees = generateEmployeesList('Marketing', 2, 10, $employees);
  166. $employees = generateEmployeesList('Manager', 1, 8, $employees);
  167. $employees = generateEmployeesList('Engineer', 1, 2, $employees);
  168. $chief = new Marketing(3);
  169. $departmentOfAdvertising = new Department('Рекламы');
  170. $departmentOfAdvertising->setChief($chief);
  171. $departmentOfAdvertising->addEmployees($employees);
  172.  
  173. $employees = array();
  174. $employees = generateEmployeesList('Manager', 1, 13, $employees);
  175. $employees = generateEmployeesList('Manager', 2, 5, $employees);
  176. $employees = generateEmployeesList('Engineer', 1, 5, $employees);
  177. $chief = new Manager(1);
  178. $logisticDepartment = new Department('Логистики');
  179. $logisticDepartment->setChief($chief);
  180. $logisticDepartment->addEmployees($employees);
  181.  
  182. $departments = array ($departmentOfProcurement, $salesDepartment, $departmentOfAdvertising, $logisticDepartment);
  183.  
  184. //Функции генерирующие отступ для строки слева и справа
  185. function padRight($string, $length)
  186. {
  187. $spaces = $length - mb_strlen($string);
  188. if ($spaces >= 0) {
  189. return $string . str_repeat(' ', $spaces);
  190. } else {
  191. return ' ' . mb_substr($string, 0, $length - 2) . '.';
  192. }
  193. }
  194. function padLeft($string, $length)
  195. {
  196. $spaces = $length - mb_strlen($string);
  197. if ($spaces >= 0) {
  198. return str_repeat(' ', $spaces) . $string;
  199. } else {
  200. return ' ' . mb_substr($string, 0, $length - 2) . '.';
  201. }
  202. }
  203.  
  204. $columnWidth = 12;
  205.  
  206. //Заголовок таблицы
  207. echo padRight('Департамент', $columnWidth) .
  208. padLeft('сотр.', $columnWidth) .
  209. padLeft('тугр.', $columnWidth) .
  210. padLeft('кофе', $columnWidth) .
  211. padLeft('стр.', $columnWidth) .
  212. padLeft('тугр./стр.', $columnWidth) . "\n";
  213. echo "-------------------------------------------------------------------------\n";
  214.  
  215. //Таблица
  216. $total = array();
  217. foreach ($departments as $department) {
  218. $employeesCount = $department->getEmployeesCount();
  219. $total['employeesCount'][] = $employeesCount;
  220. $employeesSalary = $department->getEmployeesSalary();
  221. $total['employeesSalary'][] = $employeesSalary;
  222. $coffeeConsumption = $department->getConsumptionOfCoffee();
  223. $total['coffeeConsumption'][] = $coffeeConsumption;
  224. $paperConsumption = $department->getPaperConsumption();
  225. $total['paperConsumption'][] = $paperConsumption;
  226. $costPerPage = $department->getCostPerPage();
  227. $total['costPerPage'][] = $costPerPage;
  228. echo padRight($department->name, $columnWidth) .
  229. padLeft($employeesCount, $columnWidth) .
  230. padLeft($employeesSalary, $columnWidth) .
  231. padLeft($coffeeConsumption, $columnWidth) .
  232. padLeft($paperConsumption, $columnWidth) .
  233. padLeft($costPerPage, $columnWidth) . "\n";
  234. }
  235.  
  236. echo padRight('Среднее', $columnWidth);
  237. foreach ($total as $value) {
  238. echo padLeft(array_sum($value) / count($departments), $columnWidth);
  239. }
  240. echo "\n";
  241. echo padRight('Всего', $columnWidth);
  242. foreach ($total as $value) {
  243. echo padLeft(array_sum($value), $columnWidth);
  244. }
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Департамент        сотр.       тугр.        кофе        стр.  тугр./стр.
-------------------------------------------------------------------------
Закупок               17        9613         350        3100         3.1
Продаж                24       13550         610        3325        4.08
Рекламы               36       16300         575        5450        2.99
Логистики             24       11375         425        3850        2.95
Среднее            25.25     12709.5         490     3931.25        3.28
Всего                101       50838        1960       15725       13.12