fork 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 $trail = [];
  18. private $padder;
  19.  
  20. private $prettyData = [];
  21. private $printableResult;
  22.  
  23. public function __construct($departments)
  24. {
  25. $this->setPadder();
  26. $this->setHeadTrials();
  27.  
  28. $inputArr= [];
  29. foreach($departments as $department) {
  30. $input["col1"] = $department->getDepName();
  31. $input["col2"] = $department->getEmployeesNum();
  32. $input["col3"] = $department->getTotalSalary();
  33. $input["col4"] = $department->getTotalCoffee();
  34. $input["col5"] = $department->getTotalPages();
  35. $input["col6"] = $department->getSalaryPagesRatio();
  36.  
  37. array_push($inputArr, $input);
  38. }
  39. //var_dump($inputArr);
  40. $enum = range(1, count($inputArr));
  41. for($i = 0; $i < count($inputArr); $i++) {
  42. $inputArr[$i] = array_merge(["№" => $enum[$i]], $inputArr[$i]);
  43. $this->fillPrettyData($inputArr[$i]);
  44. }
  45.  
  46. $this->setPrintableHeader();
  47. foreach($this->prettyData as $row) {
  48. $this->setPrintableResult($row);
  49. }
  50. }
  51.  
  52. private function setPrintableHeader()
  53. {
  54. foreach(array_values($this->head) as $header) {
  55. $this->printableResult .= (string)($header);
  56. $this->printableResult .= " ";
  57. }
  58. $this->printableResult .= ("\n" . str_repeat("-", $this->padder) . "\n");
  59. }
  60.  
  61. private function setHeadTrials()
  62. {
  63. foreach($this->head as $col=>$str) {
  64. $this->trail[$col] = mb_strlen($str);
  65. }
  66. }
  67.  
  68. private function setPadder()
  69. {
  70. $func = function($value) {
  71. return mb_strlen((string)$value);
  72. };
  73. $this->padder = array_sum(array_map($func, array_values($this->head))) +
  74. count(array_values($this->head)) - 1;
  75. }
  76.  
  77. private function fillPrettyData($arr)
  78. {
  79. $func = function($value) {
  80. return mb_strlen((string)$value);
  81. };
  82. $dataLens = [];
  83. $temp = array_combine(array_values($this->trail),
  84. array_map($func, array_values($arr)));
  85.  
  86. //var_dump(array_values($arr));
  87. foreach($temp as $len1=>$len2) {
  88. $len = abs($len1 - $len2);
  89. $data = str_repeat(" ", $len);
  90. array_push($dataLens, $data);
  91. }
  92. $prettyChunk = array_combine(array_values($arr),
  93. $dataLens);
  94.  
  95. array_push($this->prettyData, $prettyChunk);
  96. }
  97.  
  98. private function setPrintableResult($arr)
  99. {
  100. foreach($arr as $data=>$padding) {
  101. $res = (string)$data . (string)$padding;
  102. $this->printableResult .= $res;
  103. $this->printableResult .= " ";
  104. }
  105. $this->printableResult .= ("\n" . str_repeat("-", $this->padder) . "\n");
  106. }
  107.  
  108. public function __toString()
  109. {
  110. return $this->printableResult;
  111. }
  112. }
  113.  
  114.  
  115. class Employee
  116. {
  117. private $salary;
  118. private $rank;
  119. private $coffeeAmt;
  120. private $pages;
  121. private $isBoss;
  122.  
  123. public function __construct($salary, $rank, $coffeeAmt, $pages, $boss=false)
  124. {
  125. $this->salary = $salary;
  126. $this->rank = $rank;
  127. $this->coffeeAmt = $coffeeAmt;
  128. $this->pages = $pages;
  129. $this->isBoss = $boss;
  130. }
  131.  
  132. public function getSalary()
  133. {
  134. if ($this->rank === 1) {
  135. $salary = $this->salary;
  136. } elseif($this->rank === 2) {
  137. $salary = $this->salary * 1.25;
  138. } elseif($this->rank === 3) {
  139. $salary = $this->salary * 1.5;
  140. }
  141.  
  142. if($this->isBoss) {
  143. $salary += $salary * 0.5;
  144. }
  145.  
  146. return $salary;
  147. }
  148.  
  149. public function getCoffee()
  150. {
  151. if($this->isBoss) {
  152. return $this->coffeeAmt * 2;
  153. }
  154. return $this->coffeeAmt;
  155. }
  156.  
  157. public function getPages()
  158. {
  159. if($this->isBoss) {
  160. return 0;
  161. }
  162. return $this->pages;
  163. }
  164.  
  165. public function isBoss()
  166. {
  167. return $this->isBoss;
  168. }
  169.  
  170. public function getRank()
  171. {
  172. return $this->rank;
  173. }
  174.  
  175. public function setRank($rank)
  176. {
  177. $this->rank = $rank;
  178. }
  179. }
  180.  
  181.  
  182. class Manager extends Employee
  183. {
  184.  
  185. }
  186.  
  187.  
  188. class Analyst extends Employee
  189. {
  190.  
  191. }
  192.  
  193.  
  194. class Engineer extends Employee
  195. {
  196.  
  197. }
  198.  
  199.  
  200. class Marketer extends Employee
  201. {
  202.  
  203. }
  204.  
  205.  
  206. class Department
  207. {
  208. private $totalSalary = [];
  209. private $totalCoffeeAmount = [];
  210. private $totalWorkDone = [];
  211. private $employees = [];
  212. private $depName;
  213. private $employeeSalarys = [
  214. "Analyst" => 800,
  215. "Engineer" => 200,
  216. "Marketer" => 400,
  217. "Manager" => 500
  218. ];
  219. private $employeeCoffees = [
  220. "Analyst" => 50,
  221. "Engineer" => 5,
  222. "Marketer" => 15,
  223. "Manager" => 20
  224. ];
  225. private $employeePages = [
  226. "Analyst" => 5,
  227. "Engineer" => 50,
  228. "Marketer" => 150,
  229. "Manager" => 200
  230. ];
  231.  
  232. public function __construct($depName)
  233. {
  234. $this->depName = $depName;
  235. }
  236.  
  237. public function addEmployee($cls, $rank, $amt, $boss=false)
  238. {
  239. for($i = 0; $i < $amt; $i++) {
  240. $emp = new $cls($this->employeeSalarys[$cls],
  241. $rank,
  242. $this->employeeCoffees[$cls],
  243. $this->employeePages[$cls],
  244. $boss);
  245. array_push($this->employees, $emp);
  246. }
  247. }
  248.  
  249. public function fireEmployee($emp)
  250. {
  251. if (($key = array_search($emp, $this->employees)) !== false) {
  252. unset($this->employees[$key]);
  253. }
  254. }
  255.  
  256. public function IncreaseRank($emp)
  257. {
  258. if (in_array($emp, $this->employees)) {
  259. $emp->setRank($emp->getRank() + 1);
  260. }
  261. }
  262.  
  263. public function DecreaseRank($emp)
  264. {
  265. if (in_array($emp, $this->employees)) {
  266. $emp->setRank($emp->getRank() - 1);
  267. }
  268. }
  269.  
  270. public function changeBoss($cls, $rank)
  271. {
  272. for($i = 0; $i < count($this->employees); $i++) {
  273. if($this->employees[$i]->isBoss()) {
  274. unset($this->employees[$i]);
  275. }
  276. }
  277. $this->addEmployee($cls, $rank, 1, $boss=true);
  278. }
  279.  
  280. public function changeBaseSalary($cls, $salary)
  281. {
  282. $this->employeeSalarys[$cls] = $salary;
  283. }
  284.  
  285. public function changeBaseCoffee($cls, $amt)
  286. {
  287. $this->employeeCoffees[$cls] = $amt;
  288. }
  289.  
  290. public function changeBasePages($cls, $num)
  291. {
  292. $this->employeePages[$cls] = $num;
  293. }
  294.  
  295. public function getTotalSalary()
  296. {
  297. foreach($this->employees as $employee) {
  298. array_push($this->totalSalary, $employee->getSalary());
  299. }
  300. return array_sum($this->totalSalary);
  301. }
  302.  
  303. public function getTotalPages()
  304. {
  305. foreach($this->employees as $employee) {
  306. array_push($this->totalWorkDone, $employee->getPages());
  307. }
  308. return array_sum($this->totalWorkDone);
  309. }
  310.  
  311. public function getTotalCoffee()
  312. {
  313. foreach($this->employees as $employee) {
  314. array_push($this->totalCoffeeAmount, $employee->getCoffee());
  315. }
  316. return array_sum($this->totalCoffeeAmount);
  317. }
  318.  
  319. public function getEmployeesNum()
  320. {
  321. return count($this->employees);
  322. }
  323.  
  324. public function getSalaryPagesRatio()
  325. {
  326. return number_format(array_sum($this->totalSalary) /
  327. array_sum($this->totalWorkDone), 3);
  328. }
  329.  
  330. public function getDepName()
  331. {
  332. return $this->depName;
  333. }
  334.  
  335. public function __clone()
  336. {
  337. $this->totalSalary = [];
  338. $this->totalCoffeeAmount = [];
  339. $this->totalWorkDone = [];
  340. }
  341. }
  342.  
  343.  
  344. function testing()
  345. {
  346. /*Syntax of employee creation $dep->addEmployee("Class", rank, amount)*/
  347.  
  348. $purchaseDep = new Department("закупок");
  349. $purchaseDep->addEmployee("Manager", 1, 9);
  350. $purchaseDep->addEmployee("Manager", 2, 3);
  351. $purchaseDep->addEmployee("Manager", 3, 2);
  352. $purchaseDep->addEmployee("Marketer", 1, 2);
  353. $purchaseDep->addEmployee("Manager", 2, 1, $boss=true);
  354.  
  355. $salesDep = new Department("продаж");
  356. $salesDep->addEmployee("Manager", 1, 12);
  357. $salesDep->addEmployee("Marketer", 1, 6);
  358. $salesDep->addEmployee("Analyst", 1, 3);
  359. $salesDep->addEmployee("Analyst", 2, 2);
  360. $salesDep->addEmployee("Marketer", 2, 1, $boss=true);
  361.  
  362. $adsDep = new Department("рекламы");
  363. $adsDep->addEmployee("Marketer", 1, 15);
  364. $adsDep->addEmployee("Marketer", 2, 10);
  365. $adsDep->addEmployee("Manager", 1, 8);
  366. $adsDep->addEmployee("Engineer", 1, 2);
  367. $adsDep->addEmployee("Marketer", 3, 1, $boss=true);
  368.  
  369. $logisticsDep = new Department("логистики");
  370. $logisticsDep->addEmployee("Manager", 1, 13);
  371. $logisticsDep->addEmployee("Manager", 2, 5);
  372. $logisticsDep->addEmployee("Engineer", 1, 5);
  373. $logisticsDep->addEmployee("Manager", 1, 1, $boss=true);
  374.  
  375. $grid = new PrettyPrinter([$purchaseDep, $salesDep, $adsDep, $logisticsDep]);
  376. echo $grid;
  377. }
  378.  
  379. testing();
  380.  
Success #stdin #stdout 0.02s 24448KB
stdin
Standard input is empty
stdout
№ департамент Σрабов Σтугриков Σкофе Σстраниц тугриков/страниц 
--------------------------------------------------------------
1 закупок     17     9612.5    350   3100     3.101            
--------------------------------------------------------------
2 продаж      24     13550     610   3325     4.075            
--------------------------------------------------------------
3 рекламы     36     16300     575   5450     2.991            
--------------------------------------------------------------
4 логистики   24     11375     425   3850     2.955            
--------------------------------------------------------------