fork download
  1. <?php
  2.  
  3.  
  4. abstract class Employee
  5. {
  6. public $rank;
  7. public $boss;
  8.  
  9. public function __construct($rank, $boss)
  10. {
  11. $this->boss = $boss;
  12. $this->rank = $rank;
  13. }
  14.  
  15. public function getSalary()
  16. {
  17. if ($this->rank == 1){
  18. $factor = 1;
  19. }elseif($this->rank == 2){
  20. $factor = 1.25;
  21. }elseif($this->rank == 3){
  22. $factor = 1.5;
  23. }
  24. if ($this->boss == true)
  25. {
  26. $bossFactor = 1.5;
  27. }else{
  28. $bossFactor = 1;
  29. }
  30. $salary = $this->getRate() * $factor * $bossFactor;
  31. return $salary;
  32. }
  33.  
  34. public function getCoffe()
  35. {
  36. if ($this->boss == true)
  37. {
  38. $bossFactor = 2;
  39. }else{
  40. $bossFactor = 1;
  41. }
  42. $coffe = $this->getBaseCoffe() * $bossFactor;
  43. return $coffe;
  44. }
  45.  
  46. public function getPages()
  47. {
  48. if ($this->boss == true)
  49. {
  50. $bossFactor = 0;
  51. }else{
  52. $bossFactor = 1;
  53. }
  54. $pages = $this->getBasePages() * $bossFactor;
  55. return $pages;
  56. }
  57.  
  58. abstract protected function getRate();
  59. abstract protected function getBaseCoffe();
  60. abstract protected function getBasePages();
  61. }
  62.  
  63. class Manager extends Employee
  64. {
  65. public $rate = 500;
  66. public $coffe = 20;
  67. public $pages = 200;
  68.  
  69. public function getRate()
  70. {
  71. return $this->rate;
  72. }
  73. public function getBaseCoffe()
  74. {
  75. return $this->coffe;
  76. }
  77. public function getBasePages()
  78. {
  79. return $this->pages;
  80. }
  81. }
  82.  
  83. class Marketer extends Employee
  84. {
  85. public $rate = 400;
  86. public $coffe = 15;
  87. public $pages = 150;
  88.  
  89. public function getRate()
  90. {
  91. return $this->rate;
  92. }
  93. public function getBaseCoffe()
  94. {
  95. return $this->coffe;
  96. }
  97. public function getBasePages()
  98. {
  99. return $this->pages;
  100. }
  101. }
  102.  
  103. class Engineer extends Employee
  104. {
  105. public $rate = 200;
  106. public $coffe = 5;
  107. public $pages = 50;
  108.  
  109. public function getRate()
  110. {
  111. return $this->rate;
  112. }
  113. public function getBaseCoffe()
  114. {
  115. return $this->coffe;
  116. }
  117. public function getBasePages()
  118. {
  119. return $this->pages;
  120. }
  121. }
  122.  
  123. class Analyst extends Employee
  124. {
  125. public $rate = 800;
  126. public $coffe = 50;
  127. public $pages = 5;
  128.  
  129. public function getRate()
  130. {
  131. return $this->rate;
  132. }
  133. public function getBaseCoffe()
  134. {
  135. return $this->coffe;
  136. }
  137. public function getBasePages()
  138. {
  139. return $this->pages;
  140. }
  141. }
  142.  
  143. class Department
  144. {
  145. public $departmentName;
  146. public $employees;
  147.  
  148. public function __construct($name)
  149. {
  150. $this->departmentName = $name;
  151. }
  152.  
  153. public function addEmployee($employee)
  154. {
  155. if(class_exists($employee[0]))
  156. {
  157. $position = preg_split('//u', mb_strtolower($employee[0]), -1, PREG_SPLIT_NO_EMPTY);
  158. $position[0] = mb_strtoupper($position[0]);
  159. $position = implode("", $position);
  160. $q = array_pop($employee);
  161. if ($employee[1] > 0 && $employee[1] < 4)
  162. {
  163. if (is_bool($employee[2]))
  164. {
  165. for($i=0; $i<$q; $i++)
  166. {
  167. $this->employees[] = (new $employee[0]($employee[1], $employee[2]));
  168. }
  169. }else{
  170. throw new Exception("Неправильно указано руководящее положение: {$employee[2]}");
  171. }
  172. }else{
  173. throw new Exception("Неправильно указан ранг: {$employee[1]}");
  174. }
  175. }else{
  176. throw new Exception("Нет такой профессии: {$employee[0]}");
  177. }
  178. }
  179.  
  180. public function fireEmployee($employee)
  181. {
  182. $employeeKey = array_search($employee, $this->employees);
  183. unset ($this->employees[$employeeKey]);
  184. }
  185.  
  186. public function findEmployee($position, $boss, $rank)
  187. {
  188. $i = 0;
  189. foreach($this->employees as $employee)
  190. {
  191. if(get_class($employee)==$position && $employee->boss == $boss && $employee->rank == $rank)
  192. {
  193. return $employee;
  194. }
  195. }
  196. }
  197.  
  198. public function countEmployees($position)
  199. {
  200. $number = array(
  201. 1 => 0,
  202. 2 => 0,
  203. 3 => 0
  204. );
  205. foreach($this->employees as $employee)
  206. {
  207. if(get_class($employee)==$position)
  208. {
  209. $number[$employee->rank] += 1;
  210. }
  211. }
  212. foreach($number as $rank => $quantity)
  213. {
  214. if($quantity == 0)
  215. {
  216. unset($number[$rank]);
  217. }
  218. }
  219. return $number;
  220. }
  221.  
  222. public function getEmployees()
  223. {
  224. return $this->employees;
  225. }
  226.  
  227. public function getDepartmentSalary()
  228. {
  229. $departmentSalary = 0;
  230. foreach($this->employees as $employee)
  231. {
  232. $departmentSalary += $employee->getSalary();
  233. }
  234. return $departmentSalary;
  235. }
  236.  
  237. public function getDepartmentCoffe()
  238. {
  239. $departmentCoffe = 0;
  240. foreach($this->employees as $employee)
  241. {
  242. $departmentCoffe += $employee->getCoffe();
  243. }
  244. return $departmentCoffe;
  245. }
  246.  
  247. public function getDepartmentPages()
  248. {
  249. $departmentPages = 0;
  250. foreach($this->employees as $employee)
  251. {
  252. $departmentPages += $employee->getPages();
  253. }
  254. return $departmentPages;
  255. }
  256.  
  257. public function getSalaryPerPages()
  258. {
  259. return round($this->getDepartmentSalary() / $this->getDepartmentPages(), 2);
  260. }
  261.  
  262. public function getPersonelAmount()
  263. {
  264. return count($this->employees);
  265. }
  266. }
  267.  
  268. class Company
  269. {
  270. public $departments;
  271.  
  272. public function __clone()
  273. {
  274. $clones = array();
  275.  
  276. foreach($this->departments as $department) {
  277. $department = clone $department;
  278. $clones[] = $department;
  279. }
  280. $this->departments = $clones;
  281. }
  282.  
  283. public function addDepartment($department)
  284. {
  285. $this->departments[] = $department;
  286. }
  287.  
  288. public function getDepartments()
  289. {
  290. return $this->departments;
  291. }
  292.  
  293. public function getTotalSalary()
  294. {
  295. $salary = 0;
  296. foreach($this->departments as $department)
  297. {
  298. $salary += $department->getDepartmentSalary();
  299. }
  300. return $salary;
  301. }
  302.  
  303. public function getTotalCoffe()
  304. {
  305. $coffe = 0;
  306. foreach($this->departments as $department)
  307. {
  308. $coffe += $department->getDepartmentCoffe();
  309. }
  310. return $coffe;
  311. }
  312.  
  313. public function getTotalPages()
  314. {
  315. $pages = 0;
  316. foreach($this->departments as $department)
  317. {
  318. $pages += $department->getDepartmentPages();
  319. }
  320. return $pages;
  321. }
  322.  
  323. public function getTotalSalaryPerPages()
  324. {
  325. $spp = 0;
  326. foreach($this->departments as $department)
  327. {
  328. $spp += $department->getSalaryPerPages();
  329. }
  330. return $spp;
  331. }
  332.  
  333. public function getTotalPersonelAmount()
  334. {
  335. $quantity = 0;
  336. foreach($this->departments as $department)
  337. {
  338. $quantity += $department->getPersonelAmount();
  339. }
  340. return $quantity;
  341. }
  342.  
  343. public function departmentCount()
  344. {
  345. return count($this->departments);
  346. }
  347. }
  348.  
  349. function padRight($text, $col)
  350. {
  351. return $text.str_repeat(' ',$col-mb_strlen($text));
  352. }
  353. function padLeft($text, $col)
  354. {
  355. return str_repeat(' ',$col-mb_strlen($text)).$text;
  356. }
  357.  
  358. $departments[0]=new Department("Закупок");
  359. $departments[1]=new Department("Продаж");
  360. $departments[2]=new Department("Рекламы");
  361. $departments[3]=new Department("Логистики");
  362.  
  363. $departments[0]->addEmployee(['Manager', 1, false, 9]);
  364. $departments[0]->addEmployee(['Manager', 2, false, 3]);
  365. $departments[0]->addEmployee(['Manager', 3, false, 2]);
  366. $departments[0]->addEmployee(['Marketer', 1, false, 2]);
  367. $departments[0]->addEmployee(['Manager', 2, true, 1]);
  368.  
  369. $departments[1]->addEmployee(['Manager', 1, false, 12]);
  370. $departments[1]->addEmployee(['Marketer', 1, false, 6]);
  371. $departments[1]->addEmployee(['Analyst', 1, false, 3]);
  372. $departments[1]->addEmployee(['Analyst', 2, false, 2]);
  373. $departments[1]->addEmployee(['Marketer', 2, true, 1]);
  374.  
  375. $departments[2]->addEmployee(['Marketer', 1, false, 15]);
  376. $departments[2]->addEmployee(['Marketer', 2, false, 10]);
  377. $departments[2]->addEmployee(['Manager', 1, false, 8]);
  378. $departments[2]->addEmployee(['Engineer', 1, false, 2]);
  379. $departments[2]->addEmployee(['Marketer', 3, true, 1]);
  380.  
  381. $departments[3]->addEmployee(['Manager', 1, false, 13]);
  382. $departments[3]->addEmployee(['Manager', 2, false, 5]);
  383. $departments[3]->addEmployee(['Engineer', 1, false, 5]);
  384. $departments[3]->addEmployee(['Manager', 1, true, 1]);
  385.  
  386. $company = new Company;
  387. foreach ($departments as $department)
  388. {
  389. $company->addDepartment($department);
  390. }
  391.  
  392. function printData($company, $headline)
  393. {
  394. $col1 = 20;
  395. $col2 = 8;
  396. $col3 = 12;
  397. $col4 = 12;
  398. $col5 = 12;
  399. $col6 = 12;
  400. $col7 = 50;
  401. if($headline == 0){
  402. echo padRight("Департамент", $col1) .
  403. padLeft("сотр.", $col2) .
  404. padLeft("тугр.", $col3) .
  405. padLeft("кофе", $col4) .
  406. padLeft("стр.", $col5) .
  407. padLeft("тугр./стр.", $col6) . "\n" .
  408. str_repeat("==", 40) . "\n";
  409. }else{
  410. echo padLeft ('Антикризисная мера #'."{$headline}", $col7)."\n";
  411. echo str_repeat("==", 40) . "\n";
  412. }
  413. foreach($company->getDepartments() as $department)
  414. {
  415. echo padRight($department->departmentName, $col1) .
  416. padLeft($department->getPersonelAmount(), $col2) .
  417. padLeft($department->getDepartmentSalary(), $col3) .
  418. padLeft($department->getDepartmentCoffe(), $col4) .
  419. padLeft($department->getDepartmentPages(), $col5) .
  420. padLeft($department->getSalaryPerPages(), $col6) . "\n" ;
  421.  
  422. }echo str_repeat("==", 40) . "\n";
  423.  
  424. echo padRight('Среднее', $col1) .
  425. padLeft($company->getTotalPersonelAmount()/$company->departmentCount(), $col2) .
  426. padLeft($company->getTotalSalary()/$company->departmentCount(), $col3) .
  427. padLeft($company->getTotalCoffe()/$company->departmentCount(), $col4) .
  428. padLeft($company->getTotalPages()/$company->departmentCount(), $col5) .
  429. padLeft($company->getTotalSalaryPerPages()/$company->departmentCount(), $col6) . "\n" ;
  430.  
  431. echo padRight('Всего', $col1) .
  432. padLeft($company->getTotalPersonelAmount(), $col2) .
  433. padLeft($company->getTotalSalary(), $col3) .
  434. padLeft($company->getTotalCoffe(), $col4) .
  435. padLeft($company->getTotalPages(), $col5) .
  436. padLeft($company->getTotalSalaryPerPages(), $col6)."\n\n";
  437. }
  438.  
  439.  
  440.  
  441. class AnticrysisCommittee
  442. {
  443.  
  444.  
  445. static public function firstSolution($company)
  446. {
  447. foreach($company->getDepartments() as $department)
  448. {
  449. $engineerQuantity = $department->countEmployees("Engineer");
  450. foreach($engineerQuantity as $rank => $quantity)
  451. {
  452. $firedEngineers = ceil($quantity*0.4);
  453. for($i = 0; $i < $firedEngineers; $i++)
  454. {
  455. $department->fireEmployee($department->findEmployee("Engineer", false, $rank));
  456. }
  457. }
  458. }
  459. printData($company, 1);
  460. }
  461.  
  462. static public function secondSolution($company)
  463. {
  464. foreach($company->getDepartments() as $department)
  465. {
  466. $i = 0;
  467. $analystQuantity = $department->countEmployees("Analyst");
  468. if ($analystQuantity != NULL)
  469. {
  470. foreach($department->getEmployees() as $employee)
  471. {
  472. if(get_class($employee)=="Analyst")
  473. {
  474. $employee->rate = 1100;
  475. $employee->coffe = 75;
  476.  
  477. if(max(array_flip($analystQuantity)) == $employee->rank && $employee->boss == false && $i<1)
  478. {
  479. $employee->boss = true;
  480. $i+=1;
  481. }
  482. if(max(array_flip($analystQuantity)) > $employee->rank && $employee->boss == true)
  483. {
  484. $employee->boss = false;
  485. }
  486. }
  487. elseif($employee->boss == true)
  488. {
  489. $employee->boss = false;
  490. }
  491. }
  492. }
  493. }
  494. printData($company,2);
  495. }
  496.  
  497. static public function thirdSolution($company)
  498. {
  499. foreach($company->getDepartments() as $department)
  500. {
  501. foreach($department->getEmployees() as $employee)
  502. {
  503. if(get_class($employee)=="Manager" && $employee->rank < 3)
  504. {
  505. $employee->rank = $employee->rank + 1;
  506.  
  507. }
  508. }
  509. }
  510. printData($company, 3);
  511. }
  512. }
  513.  
  514. $company1 = clone $company;
  515. $company2 = clone $company;
  516. $company3 = clone $company;
  517.  
  518. printData($company, 0);
  519.  
  520. AnticrysisCommittee::firstSolution($company1);
  521. AnticrysisCommittee::secondSolution($company2);
  522. AnticrysisCommittee::thirdSolution($company3);
  523.  
  524.  
Success #stdin #stdout 0.03s 52472KB
stdin
Standard input is empty
stdout
Департамент            сотр.       тугр.        кофе        стр.  тугр./стр.
================================================================================
Закупок                   17      9612.5         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.375         490     3931.25        3.28
Всего                    101     50837.5        1960       15725       13.12

                             Антикризисная мера #1
================================================================================
Закупок                   17      9612.5         350        3100         3.1
Продаж                    24       13550         610        3325        4.08
Рекламы                   35       16100         570        5400        2.98
Логистики                 22       10975         415        3750        2.93
================================================================================
Среднее                 24.5   12559.375      486.25     3893.75      3.2725
Всего                     98     50237.5        1945       15575       13.09

                             Антикризисная мера #2
================================================================================
Закупок                   17      9612.5         350        3100         3.1
Продаж                    24     15637.5         795        3470        4.51
Рекламы                   36       16300         575        5450        2.99
Логистики                 24       11375         425        3850        2.95
================================================================================
Среднее                25.25    13231.25      536.25      3967.5      3.3875
Всего                    101       52925        2145       15870       13.55

                             Антикризисная мера #3
================================================================================
Закупок                   17       11300         350        3100        3.65
Продаж                    24     17137.5         795        3470        4.94
Рекламы                   36       17300         575        5450        3.17
Логистики                 24     13812.5         425        3850        3.59
================================================================================
Среднее                25.25     14887.5      536.25      3967.5      3.8375
Всего                    101       59550        2145       15870       15.35