fork(2) download
  1. <?php
  2.  
  3. class Managment
  4. {
  5. public $salary = 500;
  6. public $coffee = 20;
  7. public $pages = 200;
  8. public $rang;
  9. public $bodycount;
  10. public $isBoss = FALSE;
  11.  
  12. public function __construct($rang, $bodycount)
  13. {
  14. $this->rang = $rang;
  15. $this->bodycount = $bodycount;
  16. }
  17.  
  18. public function getStatistic()
  19. {
  20. $ko1 = 0; // коэффициент для умножения результатов по рангам
  21. switch($this->rang):
  22. case 1:
  23. $ko1 = 1;
  24. break;
  25. case 2:
  26. $ko1 = 1.25;
  27. break;
  28. case 3:
  29. $ko1 = 1.5;
  30. break;
  31. endswitch;
  32.  
  33. $ko2 = 0; //коэффициент для босса
  34. switch($this->isBoss):
  35. case FALSE:
  36. $ko2 = 1;
  37. break;
  38. case TRUE:
  39. $ko2 = 2;
  40. break;
  41. endswitch;
  42.  
  43. //считаю статистику по сотрудникам
  44. $statistic = [
  45. "salary"=> $this->salary*$this->bodycount*$ko1*$ko2,
  46. "coffee"=> $this->coffee*$this->bodycount*$ko2,
  47. "pages"=> $this->pages*$this->bodycount,
  48. ];
  49. //проверяю, босс ли, чтобы стереть его страницы
  50. if($this->isBoss){
  51. $statistic["pages"] = 0;
  52. }
  53. return $statistic;
  54. }
  55.  
  56. }
  57.  
  58. class Marketing
  59. {
  60. public $salary = 400;
  61. public $coffee = 15;
  62. public $pages = 150;
  63. public $rang;
  64. public $bodycount;
  65. public $isBoss = FALSE;
  66.  
  67. public function __construct($rang, $bodycount)
  68. {
  69. $this->rang = $rang;
  70. $this->bodycount = $bodycount;
  71. }
  72.  
  73. public function getStatistic()
  74. {
  75. $ko1 = 0;
  76. switch($this->rang):
  77. case 1:
  78. $ko1 = 1;
  79. break;
  80. case 2:
  81. $ko1 = 1.25;
  82. break;
  83. case 3:
  84. $ko1 = 1.5;
  85. break;
  86. endswitch;
  87.  
  88. $ko2 = 0;
  89. switch($this->isBoss):
  90. case FALSE:
  91. $ko2 = 1;
  92. break;
  93. case TRUE:
  94. $ko2 = 2;
  95. break;
  96. endswitch;
  97.  
  98.  
  99. $statistic = [
  100. "salary"=> $this->salary*$this->bodycount*$ko1*$ko2,
  101. "coffee"=> $this->coffee*$this->bodycount*$ko2,
  102. "pages"=> $this->pages*$this->bodycount,
  103. ];
  104.  
  105. if($this->isBoss){
  106. $statistic["pages"] = 0;
  107. }
  108. return $statistic;
  109. }
  110. }
  111.  
  112. class Engineer
  113. {
  114. public $salary = 200;
  115. public $coffee = 5;
  116. public $pages = 50;
  117. public $rang;
  118. public $bodycount;
  119. public $isBoss = FALSE;
  120.  
  121. public function __construct($rang, $bodycount)
  122. {
  123. $this->rang = $rang;
  124. $this->bodycount = $bodycount;
  125. }
  126.  
  127. public function getStatistic()
  128. {
  129. $ko1 = 0;
  130. switch($this->rang):
  131. case 1:
  132. $ko1 = 1;
  133. break;
  134. case 2:
  135. $ko1 = 1.25;
  136. break;
  137. case 3:
  138. $ko1 = 1.5;
  139. break;
  140. endswitch;
  141.  
  142. $ko2 = 0;
  143. switch($this->isBoss):
  144. case FALSE:
  145. $ko2 = 1;
  146. break;
  147. case TRUE:
  148. $ko2 = 2;
  149. break;
  150. endswitch;
  151.  
  152.  
  153. $statistic = [
  154. "salary"=> $this->salary*$this->bodycount*$ko1*$ko2,
  155. "coffee"=> $this->coffee*$this->bodycount*$ko2,
  156. "pages"=> $this->pages*$this->bodycount,
  157. ];
  158.  
  159. if($this->isBoss){
  160. $statistic["pages"] = 0;
  161. }
  162. return $statistic;
  163. }
  164. }
  165.  
  166. class Analysis
  167. {
  168. public $salary = 800;
  169. public $coffee = 50;
  170. public $pages = 5;
  171. public $rang;
  172. public $bodycount;
  173. public $isBoss = FALSE;
  174.  
  175. public function __construct($rang, $bodycount)
  176. {
  177. $this->rang = $rang;
  178. $this->bodycount = $bodycount;
  179. }
  180.  
  181. public function getStatistic()
  182. {
  183. $ko1 = 0;
  184. switch($this->rang):
  185. case 1:
  186. $ko1 = 1;
  187. break;
  188. case 2:
  189. $ko1 = 1.25;
  190. break;
  191. case 3:
  192. $ko1 = 1.5;
  193. break;
  194. endswitch;
  195.  
  196. $ko2 = 0;
  197. switch($this->isBoss):
  198. case FALSE:
  199. $ko2 = 1;
  200. break;
  201. case TRUE:
  202. $ko2 = 2;
  203. break;
  204. endswitch;
  205.  
  206.  
  207. $statistic = [
  208. "salary"=> $this->salary*$this->bodycount*$ko1*$ko2,
  209. "coffee"=> $this->coffee*$this->bodycount*$ko2,
  210. "pages"=> $this->pages*$this->bodycount,
  211. ];
  212.  
  213. if($this->isBoss){
  214. $statistic["pages"] = 0;
  215. }
  216. return $statistic;
  217. }
  218. }
  219.  
  220. function getStatsOfBuying() //функция получения полной статистики по отделу покупок
  221. {
  222. //список сотрудников, единственное, что нужно менять, если сокращаем, увеличиваем кол-во сотрудников
  223. $staff = [];
  224. $men = new Managment(1, 9);
  225. $staff[] = $men;
  226. $men = new Managment(2, 3);
  227. $staff[] = $men;
  228. $men = new Managment(3, 2);
  229. $staff[] = $men;
  230. $men = new Marketing(1, 2);
  231. $staff[] = $men;
  232. $men = new Managment(2, 1);
  233. $men->isBoss = TRUE;
  234. $staff[] = $men;
  235. //конец списка сотрудников
  236.  
  237. $totalSalary = $totalCoffee = $totalPages = $totalBodies = 0;
  238. foreach($staff as $man){
  239. $totalBodies += $man->bodycount;
  240. $totalSalary += $man->getStatistic()["salary"];
  241. $totalCoffee += $man->getStatistic()["coffee"];
  242. $totalPages += $man->getStatistic()["pages"];
  243. }
  244. $resultStats =
  245. [
  246. "bodies" => $totalBodies,
  247. "salary" => $totalSalary,
  248. "coffee" => $totalCoffee,
  249. "pages" => $totalPages,
  250. ];
  251. return $resultStats; //возвращаю массив со всей информацией
  252. }
  253.  
  254. function getStatsOfSelling()
  255. {
  256. $staff = [];
  257. $men = new Managment(1, 12);
  258. $staff[] = $men;
  259. $men = new Marketing(1, 6);
  260. $staff[] = $men;
  261. $men = new Analysis(1, 3);
  262. $staff[] = $men;
  263. $men = new Analysis(2, 2);
  264. $staff[] = $men;
  265. $men = new Marketing(2, 1);
  266. $men->isBoss = TRUE;
  267. $staff[] = $men;
  268.  
  269. $totalSalary = $totalCoffee = $totalPages = $totalBodies = 0;
  270. foreach($staff as $man){
  271. $totalBodies += $man->bodycount;
  272. $totalSalary += $man->getStatistic()["salary"];
  273. $totalCoffee += $man->getStatistic()["coffee"];
  274. $totalPages += $man->getStatistic()["pages"];
  275. }
  276. $resultStats =
  277. [
  278. "bodies" => $totalBodies,
  279. "salary" => $totalSalary,
  280. "coffee" => $totalCoffee,
  281. "pages" => $totalPages,
  282. ];
  283. return $resultStats;
  284. }
  285.  
  286. function getStatsOfAds()
  287. {
  288. $staff = [];
  289. $men = new Marketing(1, 15);
  290. $staff[] = $men;
  291. $men = new Marketing(1, 6);
  292. $staff[] = $men;
  293. $men = new Analysis(1, 3);
  294. $staff[] = $men;
  295. $men = new Analysis(2, 2);
  296. $staff[] = $men;
  297. $men = new Marketing(3, 1);
  298. $men->isBoss = TRUE;
  299. $staff[] = $men;
  300.  
  301. $totalSalary = $totalCoffee = $totalPages = $totalBodies = 0;
  302. foreach($staff as $man){
  303. $totalBodies += $man->bodycount;
  304. $totalSalary += $man->getStatistic()["salary"];
  305. $totalCoffee += $man->getStatistic()["coffee"];
  306. $totalPages += $man->getStatistic()["pages"];
  307. }
  308. $resultStats =
  309. [
  310. "bodies" => $totalBodies,
  311. "salary" => $totalSalary,
  312. "coffee" => $totalCoffee,
  313. "pages" => $totalPages,
  314. ];
  315. return $resultStats;
  316. }
  317.  
  318. function getStatsOfLogistics()
  319. {
  320. $staff = [];
  321. $men = new Managment(1, 13);
  322. $staff[] = $men;
  323. $men = new Managment(2, 5);
  324. $staff[] = $men;
  325. $men = new Engineer(1, 5);
  326. $staff[] = $men;
  327. $men = new Managment(1, 1);
  328. $men->isBoss = TRUE;
  329. $staff[] = $men;
  330.  
  331. $totalSalary = $totalCoffee = $totalPages = $totalBodies = 0;
  332. foreach($staff as $man){
  333. $totalBodies += $man->bodycount;
  334. $totalSalary += $man->getStatistic()["salary"];
  335. $totalCoffee += $man->getStatistic()["coffee"];
  336. $totalPages += $man->getStatistic()["pages"];
  337. }
  338. $resultStats =
  339. [
  340. "bodies" => $totalBodies,
  341. "salary" => $totalSalary,
  342. "coffee" => $totalCoffee,
  343. "pages" => $totalPages,
  344. ];
  345. return $resultStats;
  346. }
  347.  
  348. //функция аналог str_pad, только для кириллицы
  349. function mb_str_pad($str, $pad_len, $pad_str = ' ', $dir = STR_PAD_RIGHT, $encoding = NULL)
  350. {
  351. $encoding = $encoding === NULL ? mb_internal_encoding() : $encoding;
  352. $padBefore = $dir === STR_PAD_BOTH || $dir === STR_PAD_LEFT;
  353. $padAfter = $dir === STR_PAD_BOTH || $dir === STR_PAD_RIGHT;
  354. $pad_len -= mb_strlen($str, $encoding);
  355. $targetLen = $padBefore && $padAfter ? $pad_len / 2 : $pad_len;
  356. $strToRepeatLen = mb_strlen($pad_str, $encoding);
  357. $repeatTimes = ceil($targetLen / $strToRepeatLen);
  358. $repeatedString = str_repeat($pad_str, max(0, $repeatTimes));
  359. $before = $padBefore ? mb_substr($repeatedString, 0, floor($targetLen), $encoding) : '';
  360. $after = $padAfter ? mb_substr($repeatedString, 0, ceil($targetLen), $encoding) : '';
  361. return $before . $str . $after;
  362. }
  363.  
  364. //пишу функцию для вставки пробелов справа
  365. function padRight($string, $length)
  366. {
  367. $spaces = $length - mb_strlen($string);
  368. if($spaces>0)
  369. {
  370. $string=mb_str_pad($string, mb_strlen($string) + $spaces, " ", STR_PAD_RIGHT);
  371. }
  372. return $string;
  373. }
  374.  
  375. //пишу функцию для вставки пробелов слева
  376. function padLeft($string, $length)
  377. {
  378. $spaces = $length - mb_strlen($string);
  379. if($spaces>0)
  380. {
  381. $string=mb_str_pad($string, mb_strlen($string) + $spaces, " ", STR_PAD_LEFT);
  382. }
  383. return $string;
  384. }
  385.  
  386. //два типа колонок
  387. $col1 = 25;
  388. $col2 = 15;
  389.  
  390. //верхняя строка таблицы
  391. echo padRight("Департамент", $col1) .
  392. padLeft("сотр.", $col2) .
  393. padLeft("тугр.", $col2) .
  394. padLeft("кофе", $col2) .
  395. padLeft("стр.", $col2) .
  396. padLeft("тугр./стр.", $col2) .
  397. "\n";
  398.  
  399. echo str_repeat("-", 100)."\n";
  400.  
  401. $totalBodies = $totalSalary = $totalCoffee = $totalPages = $totalBalance = 0;
  402.  
  403. //делаю массив массивов статистики по всем департаментам
  404. $statistics = [
  405. "Закупок" => getStatsOfBuying(),
  406. "Продаж" => getStatsOfSelling(),
  407. "Рекламы" => getStatsOfAds(),
  408. "Логистики" => getStatsOfLogistics(),
  409. ];
  410.  
  411. //прохожусь по массиву массивов
  412. foreach($statistics as $department => $datum){
  413. $balance = round($datum["salary"] / $datum["pages"], 2);
  414. echo padRight("$department", $col1).
  415. padLeft($datum["bodies"], $col2) .
  416. padLeft($datum["salary"], $col2) .
  417. padLeft($datum["coffee"], $col2) .
  418. padLeft($datum["pages"], $col2) .
  419. padLeft($balance, $col2) .
  420. "\n";
  421. $totalBodies += $datum["bodies"];
  422. $totalSalary += $datum["salary"];
  423. $totalCoffee += $datum["coffee"];
  424. $totalPages += $datum["pages"];
  425. $totalBalance += $balance;
  426. }
  427.  
  428. echo "\n";
  429.  
  430. //колонка СРЕДНЕЕ
  431. echo padRight("Среднее", $col1) .
  432. padLeft($totalBodies/4, $col2) .
  433. padLeft($totalSalary/4, $col2) .
  434. padLeft($totalCoffee/4, $col2) .
  435. padLeft($totalPages/4, $col2) .
  436. padLeft($totalBalance/4, $col2) .
  437. "\n";
  438.  
  439. //колонка ВСЕГО
  440. echo padRight("Всего", $col1) .
  441. padLeft($totalBodies, $col2) .
  442. padLeft($totalSalary, $col2) .
  443. padLeft($totalCoffee, $col2) .
  444. padLeft($totalPages, $col2) .
  445. padLeft($totalBalance, $col2) .
  446. "\n";
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
Департамент                        сотр.          тугр.           кофе           стр.     тугр./стр.
----------------------------------------------------------------------------------------------------
Закупок                               17           9925            350           3100            3.2
Продаж                                24          13800            610           3325           4.15
Рекламы                               27          14000            595           3175           4.41
Логистики                             24          11625            425           3850           3.02

Среднее                               23        12337.5            495         3362.5          3.695
Всего                                 92          49350           1980          13450          14.78