fork download
  1. <?php
  2.  
  3. class Company
  4. {
  5. private $departments = [];
  6.  
  7. public function addDepartment(Department $department) : self
  8. {
  9. $this->departments[] = $department;
  10.  
  11. return $this;
  12. }
  13.  
  14. public function getCountEmployee() : int
  15. {
  16. $data = 0;
  17. foreach ($this->departments as $department) {
  18. $data += $department->getCountEmployee();
  19. }
  20. return $data;
  21. }
  22.  
  23. public function getExpenses() : array
  24. {
  25. $rate = 0;
  26. $coffee = 0;
  27.  
  28. foreach ($this->departments as $department) {
  29. $rate += $department->getExpenses()['Зарплата'];
  30. $coffee += $department->getExpenses()['Кофе'];
  31. }
  32.  
  33. return [$rate, $coffee];
  34. }
  35.  
  36. public function getAverageExpenses() : array
  37. {
  38. $rate = 0;
  39. $coffee = 0;
  40.  
  41. foreach ($this->departments as $department) {
  42. $rate += $department->getExpenses()['Зарплата'];
  43. $coffee += $department->getExpenses()['Кофе'];
  44. }
  45.  
  46. return [
  47. $rate / count($this->getDepartments()),
  48. $coffee / count($this->getDepartments())
  49. ];
  50. }
  51.  
  52. public function getReports() : float
  53. {
  54. $reports = 0;
  55. foreach ($this->departments as $department) {
  56. $reports += $department->getReports();
  57. }
  58. return $reports;
  59. }
  60.  
  61. public function getAverageReports() : float
  62. {
  63. $reports = 0;
  64. foreach ($this->departments as $department) {
  65. $reports += $department->getReports();
  66. }
  67. return $reports / count($this->getDepartments());
  68. }
  69.  
  70. public function getAverageConsumptionMoneyPerPage() : float
  71. {
  72. $moneyConsumption = 0;
  73. foreach ($this->departments as $department) {
  74. $moneyConsumption += $department->getAverageConsumptionMoneyPerPage();
  75. }
  76. return $moneyConsumption / count($this->getDepartments());
  77. }
  78.  
  79. public function getConsumptionMoneyPerPage() : float
  80. {
  81. $moneyConsumption = 0;
  82. foreach ($this->departments as $department) {
  83. $moneyConsumption += $department->getAverageConsumptionMoneyPerPage();
  84. }
  85. return $moneyConsumption;
  86. }
  87.  
  88. public function getDepartments() : array
  89. {
  90. return $this->departments;
  91. }
  92.  
  93. public function getAverageCountEmployers() : float
  94. {
  95. $countEmployers = 0;
  96. foreach ($this->departments as $department) {
  97. $countEmployers += $department->getCountEmployee();
  98. }
  99. return $countEmployers / count($this->getDepartments());
  100. }
  101.  
  102. public function setAntiCrisisMeasuresFirst() : self
  103. {
  104. foreach ($this->departments as $department) {
  105. $department->setAntiCrisisMeasuresFirst();
  106. }
  107.  
  108. return $this;
  109. }
  110.  
  111. public function setAntiCrisisMeasuresSecond() : self
  112. {
  113. foreach ($this->departments as $department) {
  114. $department->setAntiCrisisMeasuresSecond();
  115. }
  116.  
  117. return $this;
  118. }
  119.  
  120. public function setAntiCrisisMeasuresThird() : self
  121. {
  122. foreach ($this->departments as $department) {
  123. $department->setAntiCrisisMeasuresThird();
  124. }
  125.  
  126. return $this;
  127. }
  128. }
  129.  
  130. class Department
  131. {
  132. protected $name;
  133. protected $employees = [];
  134.  
  135. public function __construct(string $name)
  136. {
  137. $this->name = $name;
  138. }
  139.  
  140. public function addEmployee(Employee $employee, int $count = 1) : self
  141. {
  142. $employee = serialize($employee);
  143. for ($i = 0; $i < $count; $i++) {
  144. $this->employees[] = unserialize($employee);
  145. }
  146.  
  147. return $this;
  148. }
  149.  
  150. public function getCountEmployee() : int
  151. {
  152. return count($this->employees);
  153. }
  154.  
  155. public function getExpenses() : array
  156. {
  157. $money = 0;
  158. $coffee = 0;
  159.  
  160. foreach ($this->employees as $employee) {
  161. $money += $employee->getJob()->getRate();
  162. $coffee += $employee->getJob()->getCoffee();
  163. }
  164.  
  165. return [
  166. 'Зарплата' => $money,
  167. 'Кофе' => $coffee
  168. ];
  169. }
  170.  
  171. public function getReports() : float
  172. {
  173. $reports = 0;
  174.  
  175. foreach ($this->employees as $employee) {
  176. $reports += $employee->getJob()->getReport();
  177. }
  178.  
  179. return $reports;
  180. }
  181.  
  182. public function getName() : string
  183. {
  184. return $this->name;
  185. }
  186.  
  187. public function getAverageConsumptionMoneyPerPage() : float
  188. {
  189. return round($this->getExpenses()['Зарплата'] / $this->getReports(), 2);
  190. }
  191.  
  192. public function dismissEmployee(int $id) : self
  193. {
  194. unset($this->employees[$id]);
  195.  
  196. return $this;
  197. }
  198.  
  199. public function getEmployeesByJob(Job $job) : array
  200. {
  201. $data = [];
  202. foreach ($this->employees as $employee) {
  203. if ($employee->getJob() instanceof $job) {
  204. $data[] = $employee;
  205. }
  206. }
  207. return $data;
  208. }
  209.  
  210. public function getLeaders() : array
  211. {
  212. $data = [];
  213. foreach ($this->employees as $employee) {
  214. if ($employee->isLeader()) {
  215. $data[] = $employee;
  216. }
  217. }
  218. return $data;
  219. }
  220.  
  221. public function getEmployeersByJobAndRank(Job $job, int $rank) : array
  222. {
  223. $data = [];
  224. foreach ($this->employees as $id => $employee) {
  225. if ($employee->getJob() instanceof $job) {
  226. if ($employee->getRank() === $rank) {
  227. $data[] = $employee;
  228. }
  229. }
  230. }
  231. return $data;
  232. }
  233.  
  234. public function setAntiCrisisMeasuresFirst()
  235. {
  236. $reduction = ceil(count($this->getEmployeesByJob(new Engineer())) * 0.4);
  237. $minRank = 1;
  238. $iteration = 1;
  239.  
  240. $skippableEmployees = [];
  241.  
  242. while($reduction !== 0) {
  243. foreach ($this->employees as $id => $employee) {
  244. if ($employee->getJob() instanceof Engineer) {
  245. if ($employee->getRank() === $minRank) {
  246. if ($employee->isLeader()) {
  247. if (! in_array($id, $skippableEmployees)) {
  248. $skippableEmployees[] = $id;
  249. }
  250. }
  251.  
  252. if (! in_array($id, $skippableEmployees)) {
  253. if ($reduction > 0) {
  254. $this->dismissEmployee($id);
  255. $reduction -= 1;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. if ($iteration % 2 === 0) {
  262. $minRank++;
  263. if ($minRank > 3) {
  264. break;
  265. }
  266. }
  267. $iteration++;
  268. }
  269. }
  270.  
  271. public function setAntiCrisisMeasuresSecond()
  272. {
  273. $maxRank = 0;
  274.  
  275. foreach ($this->employees as $id => $employee) {
  276. if ($employee->isLeader()) {
  277. if (! is_a($employee->getJob(), Analyst::class)) {
  278.  
  279. foreach ($this->employees as $search) {
  280. if ($search->getJob() instanceof Analyst && $search->getRank() > $maxRank) {
  281. $maxRank = $search->getRank();
  282. }
  283. }
  284.  
  285. if($maxRank !== 0) {
  286. foreach ($this->employees as $search) {
  287. if ($search->getJob() instanceof Analyst && $search->getRank() === $maxRank) {
  288. $job = get_class($search->getJob());
  289. $search->setJob(new $job(), $maxRank, true);
  290. }
  291. }
  292. $job = get_class($employee->getJob());
  293. $employee->setJob(new $job(), $maxRank);
  294. }
  295.  
  296. }
  297. }
  298. if ($employee->getJob() instanceof Analyst) {
  299. $employee->getJob()->setRate(1100);
  300. $employee->getJob()->setCoffee(75);
  301.  
  302. $employee->updateJob($employee->getRank(), $employee->isLeader());
  303. }
  304. }
  305. }
  306.  
  307. public function setAntiCrisisMeasuresThird()
  308. {
  309. $employeeFirstRank = $this->getEmployeersByJobAndRank(new Manager(), 1);
  310. $employeeSecondRank = $this->getEmployeersByJobAndRank(new Manager(), 2);
  311.  
  312. $countFirstRank = count($employeeFirstRank) * 0.5;
  313. $countSecondRank = count($employeeSecondRank) * 0.5;
  314.  
  315. $upgradedFirstRank = 0;
  316. $upgradedSecondRank = 0;
  317.  
  318. foreach ($this->employees as $id => $employee) {
  319. if ($employee->getJob() instanceof Manager) {
  320. if ($employee->getRank() === 1) {
  321. if($upgradedFirstRank < $countFirstRank) {
  322. $employee->setJob(new Manager(), 2);
  323. $upgradedFirstRank++;
  324. }
  325. } elseif($employee->getRank() === 2) {
  326. if($upgradedSecondRank < $countSecondRank) {
  327. $employee->setJob(new Manager(), 3);
  328. $upgradedSecondRank++;
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }
  335.  
  336. abstract class Job
  337. {
  338. public function setRate(int $rate) : self
  339. {
  340. $this->rate = $rate;
  341.  
  342. return $this;
  343. }
  344.  
  345. public function getRate() : int
  346. {
  347. return $this->rate;
  348. }
  349.  
  350. public function setCoffee(int $coffee) : self
  351. {
  352. $this->coffee = $coffee;
  353.  
  354. return $this;
  355. }
  356.  
  357. public function getCoffee() : int
  358. {
  359. return $this->coffee;
  360. }
  361.  
  362. public function setReport(int $report) : self
  363. {
  364. $this->report = $report;
  365.  
  366. return $this;
  367. }
  368.  
  369. public function getReport() : int
  370. {
  371. return $this->report;
  372. }
  373. }
  374.  
  375. class Employee
  376. {
  377. protected $rank;
  378. protected $isLeader = false;
  379. protected $job;
  380.  
  381. public function __construct(Job $job, int $rank, bool $isLeader = false)
  382. {
  383. $this->rank = $rank;
  384. $this->isLeader = $isLeader;
  385. $this->job = $job;
  386.  
  387. $this->updateJob($rank, $isLeader);
  388. }
  389.  
  390. public function updateJob(int $rank, bool $isLeader = false)
  391. {
  392. switch ($rank) {
  393. case 1:
  394. break;
  395. case 2:
  396. $this->job->setRate($this->job->getRate() * 1.25);
  397. break;
  398. case 3:
  399. $this->job->setRate($this->job->getRate() * 1.5);
  400. break;
  401. }
  402.  
  403. if ($isLeader === true) {
  404. $this->job->setRate($this->job->getRate() * 1.5);
  405. $this->job->setCoffee($this->job->getCoffee() * 2);
  406. $this->job->setReport(0);
  407. }
  408. }
  409.  
  410. public function getRank() : int
  411. {
  412. return $this->rank;
  413. }
  414.  
  415. public function isLeader() : bool
  416. {
  417. return $this->isLeader;
  418. }
  419.  
  420. public function getJob() : Job
  421. {
  422. return $this->job;
  423. }
  424.  
  425. public function setJob(Job $job, int $rank, bool $isLeader = false) : self
  426. {
  427. $this->job = $job;
  428. $this->rank = $rank;
  429. $this->isLeader = $isLeader;
  430.  
  431. $this->updateJob($rank, $isLeader);
  432.  
  433. return $this;
  434. }
  435. }
  436.  
  437. class Manager extends Job
  438. {
  439. protected $rate = 500;
  440. protected $coffee = 20;
  441. protected $report = 200;
  442. }
  443.  
  444. class Marketer extends Job
  445. {
  446. protected $rate = 400;
  447. protected $coffee = 15;
  448. protected $report = 150;
  449. }
  450.  
  451. class Engineer extends Job
  452. {
  453. protected $rate = 200;
  454. protected $coffee = 5;
  455. protected $report = 50;
  456. }
  457.  
  458. class Analyst extends Job
  459. {
  460. protected $rate = 800;
  461. protected $coffee = 50;
  462. protected $report = 5;
  463. }
  464.  
  465. function padRight(string $string, int $length): string
  466. {
  467. return $string . str_repeat(' ', $length - mb_strlen($string));
  468. }
  469.  
  470. function padLeft(string $string, int $length): string
  471. {
  472. return str_repeat(' ', $length - mb_strlen($string)) . $string;
  473. }
  474.  
  475. function write($vector) {
  476. foreach ($vector->getDepartments() as $department) {
  477. echo padRight($department->getName(), 11)
  478. .padLeft($department->getCountEmployee(), 11)
  479. .padLeft($department->getExpenses()['Зарплата'], 11)
  480. .padLeft($department->getExpenses()['Кофе'], 11)
  481. .padLeft($department->getReports(), 11)
  482. .padLeft($department->getAverageConsumptionMoneyPerPage(), 11) . PHP_EOL;
  483. }
  484. echo padRight('Среднее', 11)
  485. .padLeft($vector->getAverageCountEmployers(), 11)
  486. .padLeft($vector->getAverageExpenses()[0], 11)
  487. .padLeft($vector->getAverageExpenses()[1], 11)
  488. .padLeft($vector->getAverageReports(), 11)
  489. .padLeft($vector->getAverageConsumptionMoneyPerPage(), 11) . PHP_EOL;
  490. echo padRight('Всего', 11)
  491. .padLeft($vector->getCountEmployee(), 11)
  492. .padLeft($vector->getExpenses()[0], 11)
  493. .padLeft($vector->getExpenses()[1], 11)
  494. .padLeft($vector->getReports(), 11)
  495. .padLeft($vector->getConsumptionMoneyPerPage(), 11) . PHP_EOL;
  496. }
  497.  
  498. $vector = new Company();
  499.  
  500. $purchasing = (new Department('закупок'))
  501. ->addEmployee(new Employee(new Manager(), 3), 9)
  502. ->addEmployee(new Employee(new Manager(), 2), 3)
  503. ->addEmployee(new Employee(new Manager(), 3), 2)
  504. ->addEmployee(new Employee(new Marketer(), 1), 2)
  505. ->addEmployee(new Employee(new Manager(), 2, true));
  506.  
  507. $sells = (new Department('продаж'))
  508. ->addEmployee(new Employee(new Manager(), 1), 12)
  509. ->addEmployee(new Employee(new Marketer(), 1), 6)
  510. ->addEmployee(new Employee(new Analyst(), 1), 3)
  511. ->addEmployee(new Employee(new Analyst(), 2), 2)
  512. ->addEmployee(new Employee(new Manager(), 2, true));
  513.  
  514. $ad = (new Department('рекламы'))
  515. ->addEmployee(new Employee(new Marketer(), 1), 15)
  516. ->addEmployee(new Employee(new Marketer(), 2), 10)
  517. ->addEmployee(new Employee(new Manager(), 1), 8)
  518. ->addEmployee(new Employee(new Engineer(), 1), 2)
  519. ->addEmployee(new Employee(new Marketer(), 3, true));
  520.  
  521. $logistics = (new Department('логистики'))
  522. ->addEmployee(new Employee(new Manager(), 1), 13)
  523. ->addEmployee(new Employee(new Manager(), 2), 5)
  524. ->addEmployee(new Employee(new Engineer(), 1), 5)
  525. ->addEmployee(new Employee(new Manager(), 1, true));
  526.  
  527. $vector->addDepartment($purchasing);
  528. $vector->addDepartment($sells);
  529. $vector->addDepartment($ad);
  530. $vector->addDepartment($logistics);
  531.  
  532. echo padLeft("Департамент", 11)
  533. .padLeft("сотр.", 11)
  534. .padLeft("тугр.", 11)
  535. .padLeft("кофе", 11)
  536. .padLeft("стр.", 11)
  537. .padLeft("тугр./стр.", 15) . PHP_EOL;
  538.  
  539. echo '---------------------------------------------------------------------' . PHP_EOL;
  540. echo "=== DEFAULT ===" . PHP_EOL;
  541. write($vector);
  542.  
  543. $vector = serialize($vector);
  544.  
  545. $antiCrisisVectorFirst = unserialize($vector);
  546. $antiCrisisVectorSecond = unserialize($vector);
  547. $antiCrisisVectorThird = unserialize($vector);
  548.  
  549. $antiCrisisVectorFirst->setAntiCrisisMeasuresFirst();
  550. $antiCrisisVectorSecond->setAntiCrisisMeasuresSecond();
  551. $antiCrisisVectorThird->setAntiCrisisMeasuresThird();
  552.  
  553. echo "=== ANTI-CRISIS FIRST ===" . PHP_EOL;
  554. write($antiCrisisVectorFirst);
  555.  
  556. echo "=== ANTI-CRISIS SECOND ===" . PHP_EOL;
  557. write($antiCrisisVectorSecond);
  558.  
  559. echo "=== ANTI-CRISIS THIRD ===" . PHP_EOL;
  560. write($antiCrisisVectorThird);
Success #stdin #stdout 0.02s 24856KB
stdin
Standard input is empty
stdout
Департамент      сотр.      тугр.       кофе       стр.     тугр./стр.
---------------------------------------------------------------------
=== DEFAULT ===
закупок             17      11862        350       3100       3.83
продаж              24      13737        620       3325       4.13
рекламы             36      16300        575       5450       2.99
логистики           24      11375        425       3850       2.95
Среднее          25.25    13318.5      492.5    3931.25      3.475
Всего              101      53274       1970      15725       13.9
=== ANTI-CRISIS FIRST ===
закупок             17      11862        350       3100       3.83
продаж              24      13737        620       3325       4.13
рекламы             35      16100        570       5400       2.98
логистики           22      10975        415       3750       2.93
Среднее           24.5    13168.5     488.75    3893.75     3.4675
Всего               98      52674       1955      15575      13.87
=== ANTI-CRISIS SECOND ===
закупок             17      11862        350       3100       3.83
продаж              24      15325        775       3515       4.36
рекламы             36      16300        575       5450       2.99
логистики           24      11375        425       3850       2.95
Среднее          25.25    13715.5     531.25    3978.75     3.5325
Всего              101      54862       2125      15915      14.13
=== ANTI-CRISIS THIRD ===
закупок             17      12112        350       3100       3.91
продаж              24      14300        600       3525       4.06
рекламы             36      16800        575       5450       3.08
логистики           24      12625        425       3850       3.28
Среднее          25.25   13959.25      487.5    3981.25     3.5825
Всего              101      55837       1950      15925      14.33