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