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