fork download
  1. <?php
  2.  
  3. // your code goes here<?php
  4.  
  5.  
  6. class Worker
  7. {
  8. public $occupation; // профессия
  9. public $ifChief;
  10. public $rank; // ранг
  11.  
  12. public $coffeeAmount;
  13. public $salary;
  14. public $reportPages;
  15.  
  16. public function __construct($occupation, $ifChief, $rank)
  17. {
  18. $this->occupation = $occupation;
  19. $this->ifChief = $ifChief;
  20. $this->rank = $rank;
  21. }
  22.  
  23. public function getSalary()
  24. {
  25. $salary = 0;
  26.  
  27. switch ($this->occupation) { // зависимость зарплаты от профессии
  28. case 'me':
  29. $salary += 500;
  30. break;
  31. case 'ma':
  32. $salary += 400;
  33. break;
  34. case 'en':
  35. $salary += 200;
  36. break;
  37. case 'an':
  38. $salary += 800;
  39. break;
  40. }
  41.  
  42. switch ($this->rank) { // зависимость зарплаты от ранга
  43. case 2:
  44. $salary *= 1.25;
  45. break;
  46. case 3:
  47. $salary *= 1.5;
  48. break;
  49. default:
  50. break;
  51. }
  52.  
  53. if ($this->ifChief) {
  54. $salary *= 1.5;
  55. }
  56.  
  57. return $salary;
  58. }
  59.  
  60. public function getCoffeeAmount()
  61. {
  62. $coffeeAmount = 0;
  63.  
  64. switch ($this->occupation) {
  65. case 'me':
  66. $coffeeAmount += 20;
  67. break;
  68. case 'ma':
  69. $coffeeAmount += 15;
  70. break;
  71. case 'en':
  72. $coffeeAmount += 5;
  73. break;
  74. case 'an':
  75. $coffeeAmount += 50;
  76. break;
  77. }
  78.  
  79. if ($this->ifChief) {
  80. $coffeeAmount *= 2;
  81. }
  82.  
  83. return $coffeeAmount;
  84. }
  85.  
  86. public function getReportPages()
  87. {
  88. $reportPages = 0;
  89.  
  90. switch ($this->occupation) {
  91. case 'me':
  92. $reportPages += 200;
  93. break;
  94. case 'ma':
  95. $reportPages += 150;
  96. break;
  97. case 'en':
  98. $reportPages += 50;
  99. break;
  100. case 'an':
  101. $reportPages += 5;
  102. break;
  103. }
  104.  
  105. if ($this->ifChief) {
  106. $reportPages = 0;
  107. }
  108.  
  109. return $reportPages;
  110. }
  111. }
  112.  
  113. function fillArrayWithWorkers($array, $number, $occupation, $ifChief, $rank)
  114. {
  115. $last = count($array);
  116. for ($i = 0; $i < $number; $i++) {
  117. $array[$last+$i] = new Worker($occupation, $ifChief, $rank);
  118. }
  119. return $array;
  120. }
  121.  
  122. $procurementDept = array(); // департамент закупок
  123. $procurementDept = fillArrayWithWorkers($procurementDept, 9, 'me', false, 1);
  124. $procurementDept = fillArrayWithWorkers($procurementDept, 3, 'me', false, 2);
  125. $procurementDept = fillArrayWithWorkers($procurementDept, 2, 'me', false, 3);
  126. $procurementDept = fillArrayWithWorkers($procurementDept, 2, 'ma', false, 1);
  127. $procurementDept = fillArrayWithWorkers($procurementDept, 1, 'me', true, 2);
  128.  
  129. $sailsDept = array();
  130. $sailsDept = fillArrayWithWorkers($sailsDept, 12, 'me', false, 1);
  131. $sailsDept = fillArrayWithWorkers($sailsDept, 6, 'ma', false, 1);
  132. $sailsDept = fillArrayWithWorkers($sailsDept, 3, 'an', false, 1);
  133. $sailsDept = fillArrayWithWorkers($sailsDept, 2, 'an', false, 2);
  134. $sailsDept = fillArrayWithWorkers($sailsDept, 1, 'ma', true, 2);
  135.  
  136. $adsDept = array();
  137. $adsDept = fillArrayWithWorkers($adsDept, 15, 'ma', false, 1);
  138. $adsDept = fillArrayWithWorkers($adsDept, 10, 'ma', false, 2);
  139. $adsDept = fillArrayWithWorkers($adsDept, 8, 'me', false, 1);
  140. $adsDept = fillArrayWithWorkers($adsDept, 2, 'en', false, 1);
  141. $adsDept = fillArrayWithWorkers($adsDept, 1, 'ma', true, 3);
  142.  
  143. $logisticDept = array();
  144. $logisticDept = fillArrayWithWorkers($logisticDept, 13, 'me', false, 1);
  145. $logisticDept = fillArrayWithWorkers($logisticDept, 5, 'me', false, 2);
  146. $logisticDept = fillArrayWithWorkers($logisticDept, 5, 'en', false, 1);
  147. $logisticDept = fillArrayWithWorkers($logisticDept, 1, 'me', true, 1);
  148.  
  149.  
  150. function getDeptSalary ($dept)
  151. {
  152. $deptSalary = 0;
  153.  
  154. foreach ($dept as $worker) {
  155. $deptSalary += $worker->getSalary();
  156. }
  157. return $deptSalary;
  158. }
  159. $procurementDeptSalary = getDeptSalary($procurementDept);
  160. $sailsDeptSalary = getDeptSalary($sailsDept);
  161. $adsDeptSalary = getDeptSalary($adsDept);
  162. $logisticDeptSalary = getDeptSalary($logisticDept);
  163. $totalSalary = $procurementDeptSalary + $sailsDeptSalary +
  164. $adsDeptSalary + $logisticDeptSalary;
  165. $averageSalary = $totalSalary / 4;
  166.  
  167. function getDeptCoffeeAmount ($dept)
  168. {
  169. $deptCoffee = 0;
  170. foreach ($dept as $worker) {
  171. $deptCoffee += $worker->getCoffeeAmount();
  172. }
  173. return $deptCoffee;
  174. }
  175. $procurementDeptCoffeeAmount = getDeptCoffeeAmount($procurementDept);
  176. $sailsDeptCoffeeAmount = getDeptCoffeeAmount($sailsDept);
  177. $adsDeptCoffeeAmount = getDeptCoffeeAmount($adsDept);
  178. $logisticDeptCoffeeAmount = getDeptCoffeeAmount($logisticDept);
  179. $totalCoffeeAmount = $procurementDeptCoffeeAmount + $sailsDeptCoffeeAmount +
  180. $adsDeptCoffeeAmount + $logisticDeptCoffeeAmount;
  181. $averageCoffeeAmount = $totalCoffeeAmount / 4;
  182.  
  183. function getDeptReportPages ($dept)
  184. {
  185. $deptReportPages = 0;
  186. foreach ($dept as $worker) {
  187. $deptReportPages += $worker->getReportPages();
  188. }
  189. return $deptReportPages;
  190. }
  191. $procurementDeptRP = getDeptReportPages($procurementDept);
  192. $sailsDeptRP = getDeptReportPages($sailsDept);
  193. $adsDeptRP = getDeptReportPages($adsDept);
  194. $logisticDeptRP = getDeptReportPages($logisticDept);
  195. $totalReportPages = $procurementDeptRP + $sailsDeptRP + $adsDeptRP + $logisticDeptRP;
  196. $averageReportPages = $totalReportPages / 4;
  197.  
  198. function getSalaryPerPage ($dept)
  199. {
  200. $salary = getDeptSalary($dept);
  201. $reportPages = getDeptReportPages($dept);
  202. $salaryPerPage = $salary / $reportPages;
  203. $salaryPerPage = round($salaryPerPage, 2);
  204.  
  205. return $salaryPerPage;
  206. }
  207. $procurementDeptSPP = getSalaryPerPage($procurementDept);
  208. $sailsDeptSPP = getSalaryPerPage($sailsDept);
  209. $adsDeptSPP = getSalaryPerPage($adsDept);
  210. $logisticDeptSPP = getSalaryPerPage($logisticDept);
  211. $totalSalaryPerPage = $procurementDeptSPP + $sailsDeptSPP + $adsDeptSPP + $logisticDeptSPP;
  212. $averageSalaryPerPage = $totalSalaryPerPage / 4;
  213.  
  214. $totalWorkers = count($procurementDept) + count($sailsDept) +
  215. count($adsDept) + count($logisticDept);
  216. $averageWorkers = $totalWorkers / 4;
  217.  
  218. // Функции для генерирования таблиц
  219. function padLeft($string, $length)
  220. {
  221. $strLength = mb_strlen($string);
  222. $spaces = 0;
  223.  
  224. if ($strLength < $length) {
  225. $spaces = $length - $strLength;
  226. }
  227.  
  228. for ($i = 0; $i < $spaces; $i++) {
  229. echo " ";
  230. }
  231.  
  232. echo $string;
  233. }
  234. function padRight($string, $length)
  235. {
  236. $strLength = mb_strlen($string);
  237. $spaces = 0;
  238.  
  239. if ($strLength < $length) {
  240. $spaces = $length - $strLength;
  241. }
  242.  
  243. echo $string;
  244.  
  245. for ($i = 0; $i < $spaces; $i++) {
  246. echo " ";
  247. }
  248. }
  249. // Ширина колонок
  250. $col1 = 15;
  251. $col2 = 10;
  252.  
  253. // заголовок таблицы
  254. echo padRight("Департамент", $col1) .
  255. padLeft("сотр.", $col2) .
  256. padLeft("тугр.", $col2) .
  257. padLeft("кофе", $col2) .
  258. padLeft("стр.", $col2) .
  259. padLeft("тугр./стр.", $col1) . "\n----------------------------------------------------------------------\n";
  260.  
  261. // таблица
  262. function printDeptStats ($deptName, $dept)
  263. {
  264. $col1 = 15;
  265. $col2 = 10;
  266.  
  267. echo padRight($deptName, $col1) .
  268. padLeft(count($dept), $col2) . // число сотрудников
  269. padLeft(getDeptSalary($dept), $col2) .
  270. padLeft(getDeptCoffeeAmount($dept), $col2) .
  271. padLeft(getDeptReportPages($dept), $col2) .
  272. padLeft(getSalaryPerPage($dept), $col1) . "\n"; //
  273. }
  274.  
  275. printDeptStats("Депт. закупок", $procurementDept);
  276. printDeptStats("Депт. продаж", $sailsDept);
  277. printDeptStats("Депт. рекламы", $adsDept);
  278. printDeptStats("Депт. логистики", $logisticDept);
  279. echo "-----------------\n";
  280.  
  281. // Среднее
  282. echo padRight("Среднее", $col1) .
  283. padLeft($averageWorkers, $col2) .
  284. padLeft($averageSalary, $col2) .
  285. padLeft($averageCoffeeAmount, $col2) .
  286. padLeft($averageReportPages, $col2) .
  287. padLeft($averageSalaryPerPage, $col1) . "\n-----------------\n";
  288.  
  289. // Всего
  290. echo padRight("Всего", $col1) .
  291. padLeft($totalWorkers, $col2) .
  292. padLeft($totalSalary, $col2) .
  293. padLeft($totalCoffeeAmount, $col2) .
  294. padLeft($totalReportPages, $col2) .
  295. padLeft($totalSalaryPerPage, $col1) . "\n-----------------\n";
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Департамент         сотр.     тугр.      кофе      стр.     тугр./стр.
----------------------------------------------------------------------
Депт. закупок          17    9612.5       350      3100            3.1
Депт. продаж           24     13550       610      3325           4.08
Депт. рекламы          36     16300       575      5450           2.99
Депт. логистики        24     11375       425      3850           2.95
-----------------
Среднее             25.25 12709.375       490   3931.25           3.28
-----------------
Всего                 101   50837.5      1960     15725          13.12
-----------------