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 riseRank()
  223. {
  224. if ($this->rank == 1 or $this->rank == 2) {
  225. $this->rank++;
  226. }
  227. }
  228.  
  229. public function getSalary()
  230. {
  231. if ($this->rank == 2) {
  232. $rankBonus = 1.25;
  233. } elseif ($this->rank == 3) {
  234. $rankBonus = 1.5;
  235. } else {
  236. $rankBonus = 1;
  237. }
  238.  
  239. if ($this->leader == true) {
  240. $leaderBonus = 1.5;
  241. } else {
  242. $leaderBonus = 1;
  243. }
  244. return $this->defaultSalary * $rankBonus * $leaderBonus;
  245. }
  246.  
  247. public function getCoffeUsage()
  248. {
  249. if ($this->leader) {
  250. return $this->defaultCoffeUsage * 2;
  251. } else {
  252. return $this->defaultCoffeUsage;
  253. }
  254. }
  255.  
  256. public function getDocs()
  257. {
  258. if ($this->leader) {
  259. return 0;
  260. } else {
  261. return $this->defaultDocs;
  262. }
  263. }
  264. }
  265.  
  266. class Manager extends Employee
  267. {
  268. protected $defaultSalary = 500;
  269. protected $defaultCoffeUsage = 20;
  270. protected $defaultDocs = 200;
  271. }
  272.  
  273. class MarketGuy extends Employee
  274. {
  275. protected $defaultSalary = 400;
  276. protected $defaultCoffeUsage = 15;
  277. protected $defaultDocs = 150;
  278. }
  279.  
  280. class Engineer extends Employee
  281. {
  282. protected $defaultSalary = 200;
  283. protected $defaultCoffeUsage = 5;
  284. protected $defaultDocs = 50;
  285. }
  286.  
  287. class Analyst extends Employee
  288. {
  289. protected $defaultSalary = 800;
  290. protected $defaultCoffeUsage = 50;
  291. protected $defaultDocs = 5;
  292. }
  293.  
  294.  
  295. class Factory
  296. {
  297. static function createEmployee($amount, $class, $rank, $leader)
  298. {
  299. $employees = array();
  300. for($i=1; $i<=$amount; $i++ ) {
  301. $employees[] = new $class($rank, $leader);
  302. }
  303. return $employees;
  304. }
  305.  
  306. }
  307.  
  308. //создаю организацию:
  309. $vector = new Organisation("Вектор");
  310.  
  311. //департаменты
  312. $purchase = new Department("Департамент закупок");
  313. //заполняю планктоном
  314. $purchase->addEmployee(Factory::createEmployee(9, "Manager", 1, false));
  315. $purchase->addEmployee(Factory::createEmployee(3, "Manager", 2, false));
  316. $purchase->addEmployee(Factory::createEmployee(2, "Manager", 3, false));
  317. $purchase->addEmployee(Factory::createEmployee(2, "MarketGuy", 1, false));
  318. $purchase->addEmployee(Factory::createEmployee(1, "Manager", 2, true));
  319. $vector->departments[] = $purchase; //добавляю созданный департамент в организацию
  320.  
  321. $sales = new Department("Департамент продаж");
  322. $sales->addEmployee(Factory::createEmployee(12, "Manager", 1, false));
  323. $sales->addEmployee(Factory::createEmployee(16, "MarketGuy", 1, false));
  324. $sales->addEmployee(Factory::createEmployee(3, "Analyst", 1, false));
  325. $sales->addEmployee(Factory::createEmployee(2, "Analyst", 2, false));
  326. $sales->addEmployee(Factory::createEmployee(1, "MarketGuy", 2, true));
  327. $vector->departments[] = $sales;
  328.  
  329. $advertising = new Department("Департамент рекламы");
  330. $advertising->addEmployee(Factory::createEmployee(15, "MarketGuy", 1, false));
  331. $advertising->addEmployee(Factory::createEmployee(10, "MarketGuy", 2, false));
  332. $advertising->addEmployee(Factory::createEmployee(8, "Manager", 1, false));
  333. $advertising->addEmployee(Factory::createEmployee(2, "Engineer", 1, false));
  334. $advertising->addEmployee(Factory::createEmployee(1, "MarketGuy", 3, true));
  335. $vector->departments[] = $advertising;
  336.  
  337. $logistics = new Department("Департамент логистики");
  338. $logistics->addEmployee(Factory::createEmployee(13, "Manager", 1, false));
  339. $logistics->addEmployee(Factory::createEmployee(5, "Manager", 2, false));
  340. $logistics->addEmployee(Factory::createEmployee(5, "Engineer", 1, false));
  341. $logistics->addEmployee(Factory::createEmployee(1, "Manager", 1, true));
  342. $vector->departments[] = $logistics;
  343.  
  344.  
  345.  
  346.  
  347.  
  348. function padLeft ($string, $length) {
  349. $strLength = mb_strlen($string);
  350. $num = $length - $strLength - 1;
  351. if ($num < 0) {
  352. $num = 0;
  353. }
  354. $spases = str_repeat(" ", $num);
  355. $string = "|" . $spases . $string;
  356. return $string;
  357. }
  358.  
  359. function padRight ($string, $length) {
  360. $strLength = mb_strlen($string);
  361. $num = $length - $strLength;
  362. if ($num < 0) {
  363. $num = 0;
  364. }
  365. $spases = str_repeat(" ", $num);
  366. $string .= $spases;
  367. return $string;
  368. }
  369.  
  370.  
  371. //echo "<pre>";
  372. function makeReport($vector){
  373.  
  374. $col1 = 23;
  375. $col2 = 13;
  376. $col3 = 13;
  377.  
  378. echo padRight("Департамент", $col1) .
  379. padLeft("сотрудников", $col2) .
  380. padLeft("тугрики", $col3) .
  381. padLeft("кофе", $col3) .
  382. padLeft("документы", $col2) .
  383. padLeft("тугр./документы", $col1) .
  384. "\n";
  385.  
  386. echo "--------------------------------------------------------------------------------\n";
  387.  
  388. foreach ($vector->departments as $dep) {
  389. echo padRight($dep->getName(), $col1) .
  390. padLeft($dep->countDepWorkers(), $col2) .
  391. padLeft($dep->countDepSalary(), $col3) .
  392. padLeft($dep->countDepCoffe(), $col3) .
  393. padLeft($dep->countDepDocs(), $col2) .
  394. padLeft($dep->countDepEfficiency(), $col1) .
  395. "\n";
  396. }
  397.  
  398. echo "--------------------------------------------------------------------------------\n";
  399.  
  400. echo padRight("Среднее", $col1) .
  401. padLeft($vector->countAveregeWorkers(), $col2) .
  402. padLeft($vector->countAveregeSalary(), $col3) .
  403. padLeft($vector->countAveregeCoffe(), $col3) .
  404. padLeft($vector->countAveregeDocs(), $col2) .
  405. padLeft($vector->countAveregeEfficiency(), $col1) .
  406. "\n" ;
  407.  
  408. echo padRight("Всего", $col1) .
  409. padLeft($vector->countTotalWorkers(), $col2) .
  410. padLeft($vector->countTotalSalary(), $col3) .
  411. padLeft($vector->countTotalCoffe(), $col3) .
  412. padLeft($vector->countTotalDocs(), $col2) .
  413. "|\n\n\n";
  414.  
  415. }
  416.  
  417. makeReport($vector);
  418.  
  419.  
  420. echo "Применение 1го антикризисного метода:\n\n";
  421. Antycrisis::setAnticrisysProgram1($vector);
  422.  
  423. //echo "Применение 3го антикризисного метода:\n\n";
  424. //Antycrisis::setAnticrisysProgram3($vector);
  425.  
  426.  
  427.  
  428.  
  429. makeReport($vector);
Success #stdin #stdout 0.02s 52472KB
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|