fork(1) download
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6. class PrettyPrinter
  7. {
  8. private $head = [
  9. "col0" => "№",
  10. "col1" => "департамент",
  11. "col2" => "Σрабов",
  12. "col3" => "Σтугриков",
  13. "col4" => "Σкофе",
  14. "col5" => "Σстраниц",
  15. "col6" => "тугриков/страниц"
  16. ];
  17. private $foot = [
  18. "col0" => " ",
  19. "col1" => "Итого",
  20. "col2" => NULL,
  21. "col3" => NULL,
  22. "col4" => NULL,
  23. "col5" => NULL,
  24. "col6" => NULL
  25. ];
  26. private $trail = [];
  27. private $padder;
  28.  
  29. private $prettyData = [];
  30. private $printableResult;
  31.  
  32. public function __construct($departments)
  33. {
  34. $this->setPadder();
  35. $this->setHeadTrials();
  36. $inputArr= [];
  37. foreach($departments as $department) {
  38. $input["col1"] = $department->getDepName();
  39. $input["col2"] = $department->getEmployeesNum();
  40. $input["col3"] = $department->getTotalSalary();
  41. $input["col4"] = $department->getTotalCoffee();
  42. $input["col5"] = $department->getTotalPages();
  43. $input["col6"] = number_format($department->getSalaryPagesRatio(), 3);
  44.  
  45. array_push($inputArr, $input);
  46. }
  47. $enum = range(1, count($inputArr));
  48. for($i = 0; $i < count($inputArr); $i++) {
  49. $inputArr[$i] = array_merge(["№" => $enum[$i]], $inputArr[$i]);
  50. $this->fillPrettyData($inputArr[$i]);
  51. }
  52.  
  53. $this->setPrintableHeader();
  54. $this->initFooter($inputArr);
  55. $this->setPrintableFoot();
  56. foreach($this->prettyData as $row) {
  57. $this->setPrintableResult($row);
  58. }
  59. }
  60.  
  61. private function setPrintableHeader()
  62. {
  63. foreach(array_values($this->head) as $header) {
  64. $this->printableResult .= (string)($header);
  65. $this->printableResult .= " ";
  66. }
  67. $this->printableResult .= ("\n" . str_repeat("-", $this->padder) . "\n");
  68. }
  69.  
  70. private function setHeadTrials()
  71. {
  72. foreach($this->head as $col=>$str) {
  73. $this->trail[$col] = mb_strlen($str);
  74. }
  75. }
  76.  
  77. private function setPadder()
  78. {
  79. $func = function($value) {
  80. return mb_strlen((string)$value);
  81. };
  82. $this->padder = array_sum(array_map($func, array_values($this->head))) +
  83. count(array_values($this->head)) - 1;
  84. }
  85.  
  86. private function fillPrettyData($arr)
  87. {
  88. $func = function($value) {
  89. return mb_strlen((string)$value);
  90. };
  91. $dataLens = [];
  92. $temp = array_combine(array_values($this->trail),
  93. array_map($func, array_values($arr)));
  94.  
  95. foreach($temp as $len1=>$len2) {
  96. $len = abs($len1 - $len2);
  97. $data = str_repeat(" ", $len);
  98. array_push($dataLens, $data);
  99. }
  100. $prettyChunk = array_combine(array_values($arr),
  101. $dataLens);
  102.  
  103. array_push($this->prettyData, $prettyChunk);
  104. }
  105.  
  106. private function setPrintableResult($arr)
  107. {
  108. foreach($arr as $data=>$padding) {
  109. $res = (string)$data . (string)$padding;
  110. $this->printableResult .= $res;
  111. $this->printableResult .= " ";
  112. }
  113. $this->printableResult .= ("\n" . str_repeat("-", $this->padder) . "\n");
  114. }
  115.  
  116. public function __toString()
  117. {
  118. return $this->printableResult;
  119. }
  120.  
  121. private function initFooter($arr)
  122. {
  123. $allWorkers = 0;
  124. $allCoffee = 0;
  125. $allSalary = 0;
  126. $allPages = 0;
  127. $allAvg = [];
  128.  
  129. foreach($arr as $subarr) {
  130. $cur_vals = array_values($subarr);
  131. $allWorkers += $cur_vals[2];
  132. $allSalary += $cur_vals[3];
  133. $allCoffee += $cur_vals[4];
  134. $allPages += $cur_vals[5];
  135. array_push($allAvg, $cur_vals[6]);
  136. }
  137.  
  138. $this->foot["col2"] = $allWorkers;
  139. $this->foot["col3"] = $allSalary;
  140. $this->foot["col4"] = $allCoffee;
  141. $this->foot["col5"] = $allPages;
  142. $this->foot["col6"] = array_sum($allAvg) / count($allAvg);
  143. }
  144.  
  145. private function setPrintableFoot()
  146. {
  147. $this->fillPrettyData($this->foot);
  148. }
  149. }
  150.  
  151.  
  152. class Employee
  153. {
  154. private $salary;
  155. private $rank;
  156. private $coffeeAmt;
  157. private $pages;
  158. private $isBoss;
  159.  
  160. public function __construct($salary, $rank, $coffeeAmt, $pages, $boss=false)
  161. {
  162. $this->salary = $salary;
  163. $this->rank = $rank;
  164. $this->coffeeAmt = $coffeeAmt;
  165. $this->pages = $pages;
  166. $this->isBoss = $boss;
  167. }
  168.  
  169. public function getSalary()
  170. {
  171. if ($this->rank === 1) {
  172. $salary = $this->salary;
  173. } elseif($this->rank === 2) {
  174. $salary = $this->salary * 1.25;
  175. } elseif($this->rank === 3) {
  176. $salary = $this->salary * 1.5;
  177. }
  178.  
  179. if($this->isBoss) {
  180. $salary += $salary * 0.5;
  181. }
  182.  
  183. return $salary;
  184. }
  185.  
  186. public function getCoffee()
  187. {
  188. if($this->isBoss) {
  189. return $this->coffeeAmt * 2;
  190. }
  191. return $this->coffeeAmt;
  192. }
  193.  
  194. public function getPages()
  195. {
  196. if($this->isBoss) {
  197. return 0;
  198. }
  199. return $this->pages;
  200. }
  201.  
  202. public function isBoss()
  203. {
  204. return $this->isBoss;
  205. }
  206.  
  207. public function getRank()
  208. {
  209. return $this->rank;
  210. }
  211.  
  212. public function setRank($rank)
  213. {
  214. $this->rank = $rank;
  215. }
  216.  
  217. public function setSalary($salary)
  218. {
  219. $this->salary = $salary;
  220. }
  221.  
  222. public function setCoffee($amt)
  223. {
  224. $this->coffeeAmt = $amt;
  225. }
  226.  
  227. public function setPages($amt)
  228. {
  229. $this->pages = $amt;
  230. }
  231. }
  232.  
  233.  
  234. class Manager extends Employee
  235. {
  236. }
  237.  
  238.  
  239. class Analyst extends Employee
  240. {
  241. }
  242.  
  243.  
  244. class Engineer extends Employee
  245. {
  246. }
  247.  
  248.  
  249. class Marketer extends Employee
  250. {
  251. }
  252.  
  253.  
  254. class Department
  255. {
  256. private $totalSalary;
  257. private $totalCoffeeAmount;
  258. private $totalWorkDone;
  259. private $employees = [];
  260. private $depName;
  261.  
  262. public function __construct($depName)
  263. {
  264. $this->depName = $depName;
  265. }
  266.  
  267. public function addEmployee($emp)
  268. {
  269. array_push($this->employees, $emp);
  270. }
  271.  
  272. public function fireEmployee($emp)
  273. {
  274. if (($key = array_search($emp, $this->employees)) !== false) {
  275. unset($this->employees[$key]);
  276. }
  277. }
  278.  
  279. public function getTotalSalary()
  280. {
  281. foreach($this->employees as $employee) {
  282. // var_dump($employee->getSalary());
  283. $this->totalSalary += $employee->getSalary();
  284. }
  285. return $this->totalSalary;
  286. }
  287.  
  288. public function getTotalPages()
  289. {
  290. foreach($this->employees as $employee) {
  291. $this->totalWorkDone += $employee->getPages();
  292. }
  293. return $this->totalWorkDone;
  294. }
  295.  
  296. public function getTotalCoffee()
  297. {
  298. foreach($this->employees as $employee) {
  299. $this->totalCoffeeAmount += $employee->getCoffee();
  300. }
  301. return $this->totalCoffeeAmount;
  302. }
  303.  
  304. public function getEmployeesNum()
  305. {
  306. return count($this->employees);
  307. }
  308.  
  309. public function getSalaryPagesRatio()
  310. {
  311. return $this->totalSalary / $this->totalWorkDone;
  312. }
  313.  
  314. public function getDepName()
  315. {
  316. return $this->depName;
  317. }
  318.  
  319. public function getEmployees()
  320. {
  321. return $this->employees;
  322. }
  323.  
  324. public function __clone()
  325. {
  326. $clonedEmps = [];
  327. foreach($this->employees as $emp) {
  328. $clonedEmps[] = clone $emp;
  329. }
  330. $this->employees = $clonedEmps;
  331. $this->totalSalary = 0;
  332. $this->totalCoffeeAmount = 0;
  333. $this->totalWorkDone = 0;
  334. }
  335. }
  336.  
  337.  
  338. function createEmployee($dep, $cls, $rank, $amt, $boss=false)
  339. {
  340. $empSalarys = ["Manager" => 500, "Marketer" => 400, "Analyst" => 800, "Engineer" => 200];
  341. $empCoffee = ["Manager" => 20, "Marketer" => 15, "Analyst" => 50, "Engineer" => 5];
  342. $empPages = ["Manager" => 200, "Marketer" => 150, "Analyst" => 5, "Engineer" => 50];
  343.  
  344. for($i = 0; $i < $amt; $i++) {
  345. $emp = new $cls($empSalarys[$cls], $rank, $empCoffee[$cls], $empPages[$cls], $boss);
  346. $dep->addEmployee($emp);
  347. }
  348. }
  349.  
  350.  
  351. function cmpRanks($emp1, $emp2)
  352. {
  353. if($emp1->getRank() === $emp2->getRank()) {
  354. return 0;
  355. }
  356. return ($emp1->getRank() < $emp2->getRank()) ? -1 : 1;
  357. }
  358.  
  359. function tryModel1($dep)
  360. {
  361. $engineers = [];
  362. foreach($dep->getEmployees() as $emp) {
  363. if(get_class($emp) === "Engineer") {
  364. array_push($engineers, $emp);
  365. } elseif($emp->isBoss() === true) {
  366. continue;
  367. } else {
  368. $randomEmp = array_rand($dep->getEmployees());
  369. }
  370. }
  371. usort($engineers, "cmpRanks");
  372. for($i = 0; $i < round(count($engineers) * 0.4); $i++) {
  373. if($engineers[$i]->isBoss() !== true) {
  374. $dep->fireEmployee($engineers[$i]);
  375. } else {
  376. $dep->fireEmployee($randomEmp);
  377. }
  378. }
  379. }
  380.  
  381. function tryModel2($dep)
  382. {
  383. $checkBoss = false;
  384. foreach($dep->getEmployees() as $emp) {
  385. if(get_class($emp) === "Analyst") {
  386. $checkBoss = true;
  387. $emp->setSalary(1100);
  388. $emp->setCoffee(75);
  389. }
  390. }
  391. if($checkBoss) {
  392. foreach($dep->getEmployees() as $emp) {
  393. if($emp->isBoss() && get_class($emp) !== "Analyst") {
  394. $dep->fireEmployee($emp);
  395. $newBoss = new Analyst(1100, 3, 75, 5, $boss=true);
  396. $dep->addEmployee($newBoss);
  397. }
  398. }
  399. }
  400. }
  401.  
  402. function tryModel3($dep)
  403. {
  404. $depEmployees = $dep->getEmployees();
  405. $firstRankMan = [];
  406. $secondRankMan = [];
  407. foreach($depEmployees as $emp) {
  408. if(get_class($emp) === "Manager" && $emp->getRank() === 1 && $emp->isBoss() === false) {
  409. array_push($firstRankMan, $emp);
  410. } elseif(get_class($emp) === "Manager" && $emp->getRank() === 2 && $emp->isBoss() === false) {
  411. array_push($secondRankMan, $emp);
  412. }
  413. }
  414. $firstRankLim = round(count($firstRankMan) * 0.5);
  415. $secondRankLim = round(count($secondRankMan) * 0.5);
  416. for($i = 0; $i < $firstRankLim; $i++) {
  417. $key = array_search($firstRankMan[$i], $depEmployees);
  418. $depEmployees[$key]->setRank(2);
  419. }
  420. for($i = 0; $i < $secondRankLim; $i++) {
  421. $key = array_search($secondRankMan[$i], $depEmployees);
  422. $depEmployees[$key]->setRank(3);
  423. }
  424. }
  425.  
  426.  
  427. $purchaseDep = new Department("закупок");
  428. createEmployee($purchaseDep, "Manager", 1, 9);
  429. createEmployee($purchaseDep, "Manager", 2, 3);
  430. createEmployee($purchaseDep, "Manager", 3, 2);
  431. createEmployee($purchaseDep, "Marketer", 1, 2);
  432. createEmployee($purchaseDep, "Manager", 2, 1, $boss=true);
  433.  
  434. echo "Посчитаем-ка расходы на зряплату {$purchaseDep->getTotalSalary()}\n";
  435. echo "Забыл, сколько там вышло? {$purchaseDep->getTotalSalary()}\n";
  436. echo "Посчитаем в третий раз чтобы точно знать {$purchaseDep->getTotalSalary()}\n";
  437.  
  438.  
  439.  
  440. // $salesDep = new Department("продаж");
  441. // createEmployee($salesDep, "Manager", 1, 12);
  442. // createEmployee($salesDep, "Marketer", 1, 6);
  443. // createEmployee($salesDep, "Analyst", 1, 3);
  444. // createEmployee($salesDep, "Analyst", 2, 2);
  445. // createEmployee($salesDep, "Marketer", 2, 1, $boss=true);
  446.  
  447. // $adsDep = new Department("рекламы");
  448. // createEmployee($adsDep, "Marketer", 1, 15);
  449. // createEmployee($adsDep, "Marketer", 2, 10);
  450. // createEmployee($adsDep, "Manager", 1, 8);
  451. // createEmployee($adsDep, "Engineer", 1, 2);
  452. // createEmployee($adsDep, "Marketer", 3, 1, $boss=true);
  453.  
  454. // $logisticsDep = new Department("логистики");
  455. // createEmployee($logisticsDep, "Manager", 1, 13);
  456. // createEmployee($logisticsDep, "Manager", 2, 5);
  457. // createEmployee($logisticsDep, "Engineer", 1, 5);
  458. // createEmployee($logisticsDep, "Manager", 1, 1, $boss=true);
  459.  
  460. // $allDeps = [$purchaseDep, $salesDep, $adsDep, $logisticsDep];
  461. // $clonedDeps1 = [];
  462. // $clonedDeps2 = [];
  463. // $clonedDeps3 = [];
  464.  
  465. // foreach($allDeps as $department) {
  466. // $clonedDeps1[] = clone $department;
  467. // $clonedDeps2[] = clone $department;
  468. // $clonedDeps3[] = clone $department;
  469. // }
  470.  
  471. // function tryCrisisModel($clonedDeps, $choice)
  472. // {
  473. // if($choice === 1) {
  474. // array_walk($clonedDeps, "tryModel1");
  475. // } elseif($choice === 2) {
  476. // array_walk($clonedDeps, "tryModel2");
  477. // } elseif($choice === 3) {
  478. // array_walk($clonedDeps, "tryModel3");
  479. // } else {
  480. // exit("No such model number.\n");
  481. // }
  482. // $result = new PrettyPrinter($clonedDeps);
  483. // echo "\n\n";
  484. // echo "Применение кризисной модели #{$choice}\n\n";
  485. // echo $result;
  486. // }
  487.  
  488. // $baseData = new PrettyPrinter($allDeps);
  489. // echo "Начальные данные.\n\n";
  490. // echo $baseData;
  491.  
  492. // tryCrisisModel($clonedDeps1, 1);
  493. // tryCrisisModel($clonedDeps2, 2);
  494. // tryCrisisModel($clonedDeps3, 3);
  495.  
  496. //?>
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
Посчитаем-ка расходы на зряплату 9612.5
Забыл, сколько там вышло? 19225
Посчитаем в третий раз чтобы точно знать 28837.5