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