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. abstract class PersonelDepartment
  144. {
  145. public function addEmployee($employee)
  146. {
  147. if(class_exists($employee[0]))
  148. {
  149. $position = preg_split('//u', mb_strtolower($employee[0]), -1, PREG_SPLIT_NO_EMPTY);
  150. $position[0] = mb_strtoupper($position[0]);
  151. $position = implode("", $position);
  152. $q = array_pop($employee);
  153. if ($employee[1] > 0 && $employee[1] < 4)
  154. {
  155. if (is_bool($employee[2]))
  156. {
  157. for($i=0; $i<$q; $i++)
  158. {
  159. $this->employees[] = (new $employee[0]($employee[1], $employee[2]));
  160. }
  161. }else{
  162. throw new Exception("Неправильно указано руководящее положение: {$employee[2]}");
  163. }
  164. }else{
  165. throw new Exception("Неправильно указан ранг: {$employee[1]}");
  166. }
  167. }else{
  168. throw new Exception("Нет такой профессии: {$employee[0]}");
  169. }
  170. }
  171.  
  172. public function fireEmployee($employee)
  173. {
  174. $employeeKey = array_search($employee, $this->employees);
  175. unset ($this->employees[$employeeKey]);
  176. }
  177.  
  178. public function countEmployees($position)
  179. {
  180. $number = array(
  181. 1 => 0,
  182. 2 => 0,
  183. 3 => 0
  184. );
  185. foreach($this->employees as $employee)
  186. {
  187. if(get_class($employee)==$position)
  188. {
  189. $number[$employee->rank] += 1;
  190. }
  191. }
  192. foreach($number as $rank => $quantity)
  193. {
  194. if($quantity == 0)
  195. {
  196. unset($number[$rank]);
  197. }
  198. }
  199. return $number;
  200. }
  201.  
  202. public function findEmployee($position, $boss, $rank)
  203. {
  204. foreach($this->employees as $employee)
  205. {
  206. if(get_class($employee)==$position && $employee->boss == $boss && $employee->rank == $rank)
  207. {
  208. return $employee;
  209. }
  210. }
  211. }
  212.  
  213. public function getEmployees()
  214. {
  215. return $this->employees;
  216. }
  217.  
  218. public function getPersonelAmount()
  219. {
  220. return count($this->employees);
  221. }
  222.  
  223. public function getBoss()
  224. {
  225. foreach ($this->employees as $employee)
  226. {
  227. if($employee->boss == true)
  228. {
  229. return $employee;
  230. }
  231. }
  232. }
  233.  
  234. public function changeBoss($boss, $promotionApplicant)
  235. {
  236. $boss->boss = false;
  237. $promotionApplicant->boss = true;
  238. }
  239. }
  240.  
  241. class Department extends PersonelDepartment
  242. {
  243. public $departmentName;
  244. public $employees;
  245.  
  246. public function __construct($name)
  247. {
  248. $this->departmentName = $name;
  249. }
  250.  
  251. public function __clone()
  252. {
  253. $clones = array();
  254.  
  255. foreach($this->employees as $employees) {
  256. $employees = clone $employees;
  257. $clones[] = $employees;
  258. }
  259. $this->employees = $clones;
  260. }
  261.  
  262. public function getDepartmentSalary()
  263. {
  264. $departmentSalary = 0;
  265. foreach($this->employees as $employee)
  266. {
  267. $departmentSalary += $employee->getSalary();
  268. }
  269. return $departmentSalary;
  270. }
  271.  
  272. public function getDepartmentCoffe()
  273. {
  274. $departmentCoffe = 0;
  275. foreach($this->employees as $employee)
  276. {
  277. $departmentCoffe += $employee->getCoffe();
  278. }
  279. return $departmentCoffe;
  280. }
  281.  
  282. public function getDepartmentPages()
  283. {
  284. $departmentPages = 0;
  285. foreach($this->employees as $employee)
  286. {
  287. $departmentPages += $employee->getPages();
  288. }
  289. return $departmentPages;
  290. }
  291.  
  292. public function getSalaryPerPages()
  293. {
  294. return round($this->getDepartmentSalary() / $this->getDepartmentPages(), 2);
  295. }
  296. }
  297.  
  298. class Company
  299. {
  300. public $departments;
  301.  
  302. public function __clone()
  303. {
  304. $clones = array();
  305.  
  306. foreach($this->departments as $department) {
  307. $department = clone $department;
  308. $clones[] = $department;
  309. }
  310. $this->departments = $clones;
  311. }
  312.  
  313. public function addDepartment($department)
  314. {
  315. $this->departments[] = $department;
  316. }
  317.  
  318. public function getDepartments()
  319. {
  320. return $this->departments;
  321. }
  322.  
  323. public function getTotalSalary()
  324. {
  325. $salary = 0;
  326. foreach($this->departments as $department)
  327. {
  328. $salary += $department->getDepartmentSalary();
  329. }
  330. return $salary;
  331. }
  332.  
  333. public function getTotalCoffe()
  334. {
  335. $coffe = 0;
  336. foreach($this->departments as $department)
  337. {
  338. $coffe += $department->getDepartmentCoffe();
  339. }
  340. return $coffe;
  341. }
  342.  
  343. public function getTotalPages()
  344. {
  345. $pages = 0;
  346. foreach($this->departments as $department)
  347. {
  348. $pages += $department->getDepartmentPages();
  349. }
  350. return $pages;
  351. }
  352.  
  353. public function getTotalSalaryPerPages()
  354. {
  355. $spp = 0;
  356. foreach($this->departments as $department)
  357. {
  358. $spp += $department->getSalaryPerPages();
  359. }
  360. return $spp;
  361. }
  362.  
  363. public function getTotalPersonelAmount()
  364. {
  365. $quantity = 0;
  366. foreach($this->departments as $department)
  367. {
  368. $quantity += $department->getPersonelAmount();
  369. }
  370. return $quantity;
  371. }
  372.  
  373. public function departmentCount()
  374. {
  375. return count($this->departments);
  376. }
  377. }
  378.  
  379. function padRight($text, $col)
  380. {
  381. return $text.str_repeat(' ',$col-mb_strlen($text));
  382. }
  383. function padLeft($text, $col)
  384. {
  385. return str_repeat(' ',$col-mb_strlen($text)).$text;
  386. }
  387.  
  388. $departments[0]=new Department("Закупок");
  389. $departments[1]=new Department("Продаж");
  390. $departments[2]=new Department("Рекламы");
  391. $departments[3]=new Department("Логистики");
  392.  
  393. $departments[0]->addEmployee(['Manager', 1, false, 9]);
  394. $departments[0]->addEmployee(['Manager', 2, false, 3]);
  395. $departments[0]->addEmployee(['Manager', 3, false, 2]);
  396. $departments[0]->addEmployee(['Marketer', 1, false, 2]);
  397. $departments[0]->addEmployee(['Manager', 2, true, 1]);
  398.  
  399. $departments[1]->addEmployee(['Manager', 1, false, 12]);
  400. $departments[1]->addEmployee(['Marketer', 1, false, 6]);
  401. $departments[1]->addEmployee(['Analyst', 1, false, 3]);
  402. $departments[1]->addEmployee(['Analyst', 2, false, 2]);
  403. $departments[1]->addEmployee(['Marketer', 2, true, 1]);
  404.  
  405. $departments[2]->addEmployee(['Marketer', 1, false, 15]);
  406. $departments[2]->addEmployee(['Marketer', 2, false, 10]);
  407. $departments[2]->addEmployee(['Manager', 1, false, 8]);
  408. $departments[2]->addEmployee(['Engineer', 1, false, 2]);
  409. $departments[2]->addEmployee(['Marketer', 3, true, 1]);
  410.  
  411. $departments[3]->addEmployee(['Manager', 1, false, 13]);
  412. $departments[3]->addEmployee(['Manager', 2, false, 5]);
  413. $departments[3]->addEmployee(['Engineer', 1, false, 5]);
  414. $departments[3]->addEmployee(['Manager', 1, true, 1]);
  415.  
  416. $company = new Company;
  417. foreach ($departments as $department)
  418. {
  419. $company->addDepartment($department);
  420. }
  421.  
  422. function printData($company, $headline)
  423. {
  424. $col1 = 20;
  425. $col2 = 8;
  426. $col3 = 12;
  427. $col4 = 12;
  428. $col5 = 12;
  429. $col6 = 12;
  430. $col7 = 50;
  431. if($headline == 0){
  432. echo padRight("Департамент", $col1) .
  433. padLeft("сотр.", $col2) .
  434. padLeft("тугр.", $col3) .
  435. padLeft("кофе", $col4) .
  436. padLeft("стр.", $col5) .
  437. padLeft("тугр./стр.", $col6) . "\n" .
  438. str_repeat("==", 40) . "\n";
  439. }else{
  440. echo padLeft ('Антикризисная мера #'."{$headline}", $col7)."\n";
  441. echo str_repeat("==", 40) . "\n";
  442. }
  443. foreach($company->getDepartments() as $department)
  444. {
  445. echo padRight($department->departmentName, $col1) .
  446. padLeft($department->getPersonelAmount(), $col2) .
  447. padLeft($department->getDepartmentSalary(), $col3) .
  448. padLeft($department->getDepartmentCoffe(), $col4) .
  449. padLeft($department->getDepartmentPages(), $col5) .
  450. padLeft($department->getSalaryPerPages(), $col6) . "\n" ;
  451.  
  452. }echo str_repeat("==", 40) . "\n";
  453.  
  454. echo padRight('Среднее', $col1) .
  455. padLeft($company->getTotalPersonelAmount()/$company->departmentCount(), $col2) .
  456. padLeft($company->getTotalSalary()/$company->departmentCount(), $col3) .
  457. padLeft($company->getTotalCoffe()/$company->departmentCount(), $col4) .
  458. padLeft($company->getTotalPages()/$company->departmentCount(), $col5) .
  459. padLeft($company->getTotalSalaryPerPages()/$company->departmentCount(), $col6) . "\n" ;
  460.  
  461. echo padRight('Всего', $col1) .
  462. padLeft($company->getTotalPersonelAmount(), $col2) .
  463. padLeft($company->getTotalSalary(), $col3) .
  464. padLeft($company->getTotalCoffe(), $col4) .
  465. padLeft($company->getTotalPages(), $col5) .
  466. padLeft($company->getTotalSalaryPerPages(), $col6)."\n\n";
  467. }
  468.  
  469.  
  470.  
  471. class AnticrysisCommittee
  472. {
  473.  
  474.  
  475. static public function firstSolution($company)
  476. {
  477. foreach($company->getDepartments() as $department)
  478. {
  479. $engineerQuantity = $department->countEmployees("Engineer");
  480. foreach($engineerQuantity as $rank => $quantity)
  481. {
  482. $firedEngineers = ceil($quantity*0.4);
  483. for($i = 0; $i < $firedEngineers; $i++)
  484. {
  485. $department->fireEmployee($department->findEmployee("Engineer", false, $rank));
  486. }
  487. }
  488. }
  489. printData($company, 1);
  490. }
  491.  
  492. static public function secondSolution($company)
  493. {
  494. foreach($company->getDepartments() as $department)
  495. {
  496. $analystQuantity = $department->countEmployees("Analyst");
  497. if ($analystQuantity != NULL)
  498. {
  499. $maxAnalystRank = max(array_flip($analystQuantity));
  500. $boss = $department->getBoss();
  501. if (get_class($boss) != "Analyst" OR $boss->rank < $maxAnalystRank)
  502. {
  503. $promotionApplicant = $department->findEmployee("Analyst", false, $maxAnalystRank);
  504. $department->changeBoss($boss, $promotionApplicant);
  505. }
  506. foreach($department->getEmployees() as $employee)
  507. {
  508. if(get_class($employee)=="Analyst")
  509. {
  510. $employee->rate = 1100;
  511. $employee->coffe = 75;
  512. }
  513. }
  514. }
  515. }
  516. printData($company,2);
  517. }
  518.  
  519. static public function thirdSolution($company)
  520. {
  521. foreach($company->getDepartments() as $department)
  522. {
  523. foreach($department->getEmployees() as $employee)
  524. {
  525. if(get_class($employee)=="Manager" && $employee->rank < 3)
  526. {
  527. $employee->rank = $employee->rank + 1;
  528.  
  529. }
  530. }
  531. }
  532. printData($company, 3);
  533. }
  534. }
  535.  
  536.  
  537. $company1 = clone $company;
  538. $company2 = clone $company;
  539. $company3 = clone $company;
  540.  
  541. printData($company, 0);
  542.  
  543. AnticrysisCommittee::firstSolution($company1);
  544. AnticrysisCommittee::secondSolution($company2);
  545. AnticrysisCommittee::thirdSolution($company3);
  546.  
Success #stdin #stdout 0.03s 52432KB
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       15050         610        3325        4.53
Рекламы                   36       17300         575        5450        3.17
Логистики                 24     13812.5         425        3850        3.59
================================================================================
Среднее                25.25   14365.625         490     3931.25       3.735
Всего                    101     57462.5        1960       15725       14.94