fork download
  1. <?php
  2. header("Content-Type: text/plain; charset=utf-8");
  3.  
  4.  
  5. class Antycrisis
  6. {
  7. static function setAnticrisysProgram1($organisation) {
  8.  
  9. $askEngineer = function ($worker) {
  10. return (get_class($worker) == "Engineer");
  11. };
  12.  
  13. $askLeader = function($worker) {
  14. return ($worker->leader != 1);
  15. };
  16.  
  17. foreach ($organisation->departments as $dep) {
  18. $allEngineers = array_filter($dep->workers, $askEngineer);
  19. $targetAmount = ceil(count($allEngineers) * 0.4);
  20. $notLeaderEngineers = array_filter($allEngineers, $askLeader);
  21. $targetEngineers = array_slice($notLeaderEngineers, 0, $targetAmount);
  22. foreach ($targetEngineers as $name => $fireHim) {
  23. $dep->fireEmployee($fireHim);
  24. }
  25. }
  26. }
  27.  
  28. static function setAnticrisysProgram3($organisation) {
  29.  
  30. $askManager = function($worker) {
  31. return ((get_class($worker) == "Manager") and ($worker->rank != 3));
  32. };
  33.  
  34. foreach ($organisation->departments as $dep) {
  35.  
  36. $managers1_2 = array_filter($dep->workers, $askManager);
  37. $targetAmount = ceil(count($managers1_2) * 0.5);
  38. $targetManagers = array_slice($managers1_2, 0, $targetAmount);
  39.  
  40. foreach ($targetManagers as $luckyMan){
  41. $luckyMan->riseRank();
  42. }
  43.  
  44. }
  45. }
  46.  
  47. }
  48.  
  49.  
  50. class Organisation //содержит методы для подсчета общих и средних параметров по конторе
  51. {
  52. protected $name;
  53. public $departments = array();
  54.  
  55. public function __construct($name)
  56. {
  57. $this->name = $name;
  58. }
  59.  
  60.  
  61. public function countTotalWorkers()
  62. {
  63. $totalWorkers = 0;
  64. foreach($this->departments as $unit) {
  65. $totalWorkers += $unit->countDepWorkers();
  66. }
  67. return $totalWorkers;
  68. }
  69.  
  70. public function countAveregeWorkers()
  71. {
  72. return $this->countTotalWorkers() / count($this->departments);
  73. }
  74.  
  75. public function countTotalCoffe()
  76. {
  77. $companyCoffe = 0;
  78. foreach ($this->departments as $dep) {
  79. $companyCoffe += $dep->countDepCoffe();
  80. }
  81. return $companyCoffe;
  82. }
  83.  
  84. public function countTotalSalary()
  85. {
  86. $companySalary = 0;
  87. foreach ($this->departments as $dep) {
  88. $companySalary += $dep->countDepSalary();
  89. }
  90. return $companySalary;
  91. }
  92.  
  93. public function countTotalDocs()
  94. {
  95. $companyDocs = 0;
  96. foreach ($this->departments as $dep) {
  97. $companyDocs += $dep->countDepDocs();
  98. }
  99. return $companyDocs;
  100. }
  101.  
  102.  
  103. public function countAveregeCoffe()
  104. {
  105. return $this->countTotalCoffe() / count($this->departments);
  106. }
  107.  
  108. public function countAveregeSalary()
  109. {
  110. return $this->countTotalSalary() / count($this->departments);
  111. }
  112.  
  113. public function countAveregeDocs()
  114. {
  115. return $this->countTotalDocs() / count($this->departments);
  116. }
  117.  
  118. public function countTotalEfficiency()
  119. {
  120. $companyEfficiency = 0;
  121. foreach ($this->departments as $dep) {
  122. $companyEfficiency += $dep->countDepEfficiency();
  123. }
  124. return $companyEfficiency;
  125. }
  126.  
  127. public function countAveregeEfficiency()
  128. {
  129. return $this->countTotalEfficiency() / count($this->departments);
  130. }
  131.  
  132. }
  133.  
  134. class Department
  135. {
  136. protected $name;
  137. public $workers = array();
  138.  
  139. public function __construct($name)
  140. {
  141. $this->name = $name;
  142. }
  143.  
  144. public function fireEmployee($employee)
  145. {
  146. foreach ($this->workers as $num => $worker) {
  147.  
  148. //var_dump($num);
  149. //var_dump($worker);
  150. if ($employee === $worker) {
  151. unset($this->workers[$num]);
  152. }
  153. }
  154. }
  155.  
  156. public function addEmployee($employee)
  157. {
  158. if (is_array($employee)) {
  159. $this->workers = array_merge($this->workers, $employee);
  160. } else {
  161. $this->workers[] = $employee;
  162. }
  163. }
  164.  
  165. public function getName()
  166. {
  167. return $this->name;
  168. }
  169.  
  170. public function countDepWorkers()
  171. {
  172. return count($this->workers);
  173. }
  174.  
  175. public function countDepCoffe()
  176. {
  177. $totalDepCoffe = 0;
  178. foreach ($this->workers as $worker) {
  179. $totalDepCoffe += $worker->getCoffeUsage();
  180. }
  181. return $totalDepCoffe;
  182. }
  183.  
  184. public function countDepSalary()
  185. {
  186. $totalDepSalary = 0;
  187. foreach ($this->workers as $worker) {
  188. $totalDepSalary += $worker->getSalary();
  189. }
  190. return $totalDepSalary;
  191. }
  192.  
  193. public function countDepDocs()
  194. {
  195. $totalDepDocs = 0;
  196. foreach ($this->workers as $worker) {
  197. $totalDepDocs += $worker->getDocs();
  198. }
  199. return $totalDepDocs;
  200. }
  201.  
  202. function countDepEfficiency()
  203. {
  204. return $this->countDepSalary() / $this->countDepDocs();
  205. }
  206.  
  207. }
  208.  
  209. class Employee
  210. {
  211. public $rank;
  212. public $leader;
  213. protected $defaultSalary;
  214. protected $defaultCoffeUsage;
  215. protected $defaultDocs;
  216.  
  217. public function __construct($rank, $leader) {
  218. $this->rank = $rank;
  219. $this->leader = $leader;
  220. }
  221.  
  222. public function __destruct(){
  223. unset($this);
  224. }
  225.  
  226. public function riseRank()
  227. {
  228. if ($this->rank == 1 or $this->rank == 2) {
  229. $this->rank++;
  230. }
  231. }
  232.  
  233. public function getSalary()
  234. {
  235. if ($this->rank == 2) {
  236. $rankBonus = 1.25;
  237. } elseif ($this->rank == 3) {
  238. $rankBonus = 1.5;
  239. } else {
  240. $rankBonus = 1;
  241. }
  242.  
  243. if ($this->leader == true) {
  244. $leaderBonus = 1.5;
  245. } else {
  246. $leaderBonus = 1;
  247. }
  248. return $this->defaultSalary * $rankBonus * $leaderBonus;
  249. }
  250.  
  251. public function getCoffeUsage()
  252. {
  253. if ($this->leader) {
  254. return $this->defaultCoffeUsage * 2;
  255. } else {
  256. return $this->defaultCoffeUsage;
  257. }
  258. }
  259.  
  260. public function getDocs()
  261. {
  262. if ($this->leader) {
  263. return 0;
  264. } else {
  265. return $this->defaultDocs;
  266. }
  267. }
  268. }
  269.  
  270. class Manager extends Employee
  271. {
  272. protected $defaultSalary = 500;
  273. protected $defaultCoffeUsage = 20;
  274. protected $defaultDocs = 200;
  275. }
  276.  
  277. class MarketGuy extends Employee
  278. {
  279. protected $defaultSalary = 400;
  280. protected $defaultCoffeUsage = 15;
  281. protected $defaultDocs = 150;
  282. }
  283.  
  284. class Engineer extends Employee
  285. {
  286. protected $defaultSalary = 200;
  287. protected $defaultCoffeUsage = 5;
  288. protected $defaultDocs = 50;
  289. }
  290.  
  291. class Analyst extends Employee
  292. {
  293. protected $defaultSalary = 800;
  294. protected $defaultCoffeUsage = 50;
  295. protected $defaultDocs = 5;
  296. }
  297.  
  298.  
  299. class Factory
  300. {
  301. static function createEmployee($amount, $class, $rank, $leader)
  302. {
  303. $employees = array();
  304. for($i=1; $i<=$amount; $i++ ) {
  305. $employees[] = new $class($rank, $leader);
  306. }
  307. return $employees;
  308. }
  309.  
  310. }
  311.  
  312. //создаю организацию:
  313. $vector = new Organisation("Вектор");
  314.  
  315. //департаменты
  316. $purchase = new Department("Департамент закупок");
  317. //заполняю планктоном
  318. $purchase->addEmployee(Factory::createEmployee(9, "Manager", 1, false));
  319. $purchase->addEmployee(Factory::createEmployee(3, "Manager", 2, false));
  320. $purchase->addEmployee(Factory::createEmployee(2, "Manager", 3, false));
  321. $purchase->addEmployee(Factory::createEmployee(2, "MarketGuy", 1, false));
  322. $purchase->addEmployee(Factory::createEmployee(1, "Manager", 2, true));
  323. $vector->departments[] = $purchase; //добавляю созданный департамент в организацию
  324.  
  325. $sales = new Department("Департамент продаж");
  326. $sales->addEmployee(Factory::createEmployee(12, "Manager", 1, false));
  327. $sales->addEmployee(Factory::createEmployee(16, "MarketGuy", 1, false));
  328. $sales->addEmployee(Factory::createEmployee(3, "Analyst", 1, false));
  329. $sales->addEmployee(Factory::createEmployee(2, "Analyst", 2, false));
  330. $sales->addEmployee(Factory::createEmployee(1, "MarketGuy", 2, true));
  331. $vector->departments[] = $sales;
  332.  
  333. $advertising = new Department("Департамент рекламы");
  334. $advertising->addEmployee(Factory::createEmployee(15, "MarketGuy", 1, false));
  335. $advertising->addEmployee(Factory::createEmployee(10, "MarketGuy", 2, false));
  336. $advertising->addEmployee(Factory::createEmployee(8, "Manager", 1, false));
  337. $advertising->addEmployee(Factory::createEmployee(2, "Engineer", 1, false));
  338. $advertising->addEmployee(Factory::createEmployee(1, "MarketGuy", 3, true));
  339. $vector->departments[] = $advertising;
  340.  
  341. $logistics = new Department("Департамент логистики");
  342. $logistics->addEmployee(Factory::createEmployee(13, "Manager", 1, false));
  343. $logistics->addEmployee(Factory::createEmployee(5, "Manager", 2, false));
  344. $logistics->addEmployee(Factory::createEmployee(5, "Engineer", 1, false));
  345. $logistics->addEmployee(Factory::createEmployee(1, "Manager", 1, true));
  346. $vector->departments[] = $logistics;
  347.  
  348.  
  349.  
  350.  
  351.  
  352. function padLeft ($string, $length) {
  353. $strLength = mb_strlen($string);
  354. $num = $length - $strLength - 1;
  355. if ($num < 0) {
  356. $num = 0;
  357. }
  358. $spases = str_repeat(" ", $num);
  359. $string = "|" . $spases . $string;
  360. return $string;
  361. }
  362.  
  363. function padRight ($string, $length) {
  364. $strLength = mb_strlen($string);
  365. $num = $length - $strLength;
  366. if ($num < 0) {
  367. $num = 0;
  368. }
  369. $spases = str_repeat(" ", $num);
  370. $string .= $spases;
  371. return $string;
  372. }
  373.  
  374.  
  375. //echo "<pre>";
  376. function makeReport($vector){
  377.  
  378. $col1 = 23;
  379. $col2 = 13;
  380. $col3 = 13;
  381.  
  382. echo padRight("Департамент", $col1) .
  383. padLeft("сотрудников", $col2) .
  384. padLeft("тугрики", $col3) .
  385. padLeft("кофе", $col3) .
  386. padLeft("документы", $col2) .
  387. padLeft("тугр./документы", $col1) .
  388. "\n";
  389.  
  390. echo "--------------------------------------------------------------------------------\n";
  391.  
  392. foreach ($vector->departments as $dep) {
  393. echo padRight($dep->getName(), $col1) .
  394. padLeft($dep->countDepWorkers(), $col2) .
  395. padLeft($dep->countDepSalary(), $col3) .
  396. padLeft($dep->countDepCoffe(), $col3) .
  397. padLeft($dep->countDepDocs(), $col2) .
  398. padLeft($dep->countDepEfficiency(), $col1) .
  399. "\n";
  400. }
  401.  
  402. echo "--------------------------------------------------------------------------------\n";
  403.  
  404. echo padRight("Среднее", $col1) .
  405. padLeft($vector->countAveregeWorkers(), $col2) .
  406. padLeft($vector->countAveregeSalary(), $col3) .
  407. padLeft($vector->countAveregeCoffe(), $col3) .
  408. padLeft($vector->countAveregeDocs(), $col2) .
  409. padLeft($vector->countAveregeEfficiency(), $col1) .
  410. "\n" ;
  411.  
  412. echo padRight("Всего", $col1) .
  413. padLeft($vector->countTotalWorkers(), $col2) .
  414. padLeft($vector->countTotalSalary(), $col3) .
  415. padLeft($vector->countTotalCoffe(), $col3) .
  416. padLeft($vector->countTotalDocs(), $col2) .
  417. "|\n\n\n";
  418.  
  419. }
  420. echo "Оригинальный вектор:\n";
  421. makeReport($vector);
  422.  
  423. $vector1 = clone $vector;
  424. echo "Применение 1го антикризисного метода:\n\n";
  425. Antycrisis::setAnticrisysProgram1($vector1);
  426. makeReport($vector1);
  427.  
  428. $vector2 = clone $vector;
  429. echo "Применение 3го антикризисного метода:\n\n";
  430. Antycrisis::setAnticrisysProgram3($vector2);
  431. makeReport($vector2);
  432.  
  433.  
  434.  
  435.  
Success #stdin #stdout 0.03s 24448KB
stdin
Standard input is empty
stdout
Оригинальный вектор:
Департамент            | сотрудников|     тугрики|        кофе|   документы|       тугр./документы
--------------------------------------------------------------------------------
Департамент закупок    |          17|      9612.5|         350|        3100|       3.1008064516129
Департамент продаж     |          34|       17550|         760|        4825|       3.6373056994819
Департамент рекламы    |          36|       16300|         575|        5450|       2.9908256880734
Департамент логистики  |          24|       11375|         425|        3850|       2.9545454545455
--------------------------------------------------------------------------------
Среднее                |       27.75|   13709.375|       527.5|     4306.25|       3.1708708234284
Всего                  |         111|     54837.5|        2110|       17225|


Применение 1го антикризисного метода:

Департамент            | сотрудников|     тугрики|        кофе|   документы|       тугр./документы
--------------------------------------------------------------------------------
Департамент закупок    |          17|      9612.5|         350|        3100|       3.1008064516129
Департамент продаж     |          34|       17550|         760|        4825|       3.6373056994819
Департамент рекламы    |          35|       16100|         570|        5400|       2.9814814814815
Департамент логистики  |          22|       10975|         415|        3750|       2.9266666666667
--------------------------------------------------------------------------------
Среднее                |          27|   13559.375|      523.75|     4268.75|       3.1615650748107
Всего                  |         108|     54237.5|        2095|       17075|


Применение 3го антикризисного метода:

Департамент            | сотрудников|     тугрики|        кофе|   документы|       тугр./документы
--------------------------------------------------------------------------------
Департамент закупок    |          17|     10487.5|         350|        3100|        3.383064516129
Департамент продаж     |          34|       18300|         760|        4825|       3.7927461139896
Департамент рекламы    |          35|       16600|         570|        5400|       3.0740740740741
Департамент логистики  |          22|       12225|         415|        3750|                  3.26
--------------------------------------------------------------------------------
Среднее                |          27|   14403.125|      523.75|     4268.75|       3.3774711760482
Всего                  |         108|     57612.5|        2095|       17075|