fork download
  1. <?php
  2.  
  3. //Вот что надо сделать:
  4. //
  5. //- заменить один класс на несколько, чтобы каждый соответствовал одной сущности
  6. //- убрать из классов все что к ним не относится. Например разбор строки с должностью и уровнем надо убрать. Также, посмотреть и избавиться от лишних массивов.
  7. //- проставить правильно private/public
  8. //- сделать еще и вторую часть задания
  9. class Department {
  10. private function chooseProfession($post) {
  11. switch($post) {
  12. case 'ме':
  13. return new Manager;
  14. case 'ма':
  15. return new Marketer;
  16. case 'ин':
  17. return new Ingeneer;
  18. case 'ан':
  19. return new Analyst;
  20. }
  21. }
  22. public function countTotalSalary($qty, $rank, $post, $boss) {
  23. $employee = $this->chooseProfession($post);
  24. $employee->rank = $rank;
  25. $employee->post = $post;
  26. $employee->boss = $boss;
  27. $totalSalary = $qty * $employee->countSalary();
  28. return $totalSalary;
  29. }
  30. public function countTotalCoffee($qty, $post, $boss) {
  31. $employee = $this->chooseProfession($post);
  32. $employee->boss = $boss;
  33. $coffee = $qty * $employee->countCoffee();
  34. return $coffee;
  35. }
  36. public function countTotalPages($qty, $post, $boss) {
  37. $employee = $this->chooseProfession($post);
  38. $employee->boss = $boss;
  39. $pages = $qty * $employee->countPages();
  40. return $pages;
  41. }
  42. }
  43. abstract class Employee extends Department {
  44. protected $rate;
  45. protected $rank;
  46. protected $boss;
  47. protected $post;
  48. //На основе ранга определяет ставку
  49. protected function rateScaleBasedOnRank($rate) {
  50. switch($this->rank) {
  51. case 2:
  52. $rate += $rate * 0.25;
  53. break;
  54. case 3:
  55. $rate += $rate * 0.5;
  56. }
  57. return $rate;
  58. }
  59. abstract protected function countSalary();
  60. abstract protected function countCoffee();
  61. abstract protected function countPages();
  62. }
  63. class Manager extends Employee {
  64. protected function countSalary() {
  65. $this->rate = 500;
  66. $salary = $this->rateScaleBasedOnRank($this->rate);
  67. if($this->boss) { $salary += $salary*0.5; }
  68. return $salary;
  69. }
  70. protected function countCoffee() {
  71. $coffee = 20;
  72. if($this->boss) { $coffee *= 2; }
  73. return $coffee;
  74. }
  75. protected function countPages() {
  76. if($this->boss) { return 0; }
  77. return $pages = 200;
  78. }
  79. }
  80. class Marketer extends Employee {
  81. protected function countSalary() {
  82. $this->rate = 400;
  83. $salary = $this->rateScaleBasedOnRank($this->rate);
  84. if($this->boss) { $salary += $salary*0.5; }
  85. return $salary;
  86. }
  87. protected function countCoffee() {
  88. $coffee = 15;
  89. if($this->boss) { $coffee *= 2; }
  90. return $coffee;
  91. }
  92. protected function countPages() {
  93. if($this->boss) { return 0; }
  94. return $pages = 150;
  95. }
  96. }
  97. class Ingeneer extends Employee {
  98. protected function countSalary() {
  99. $this->rate = 200;
  100. $salary = $this->rateScaleBasedOnRank($this->rate);
  101. if($this->boss) { $salary += $salary*0.5; }
  102. return $salary;
  103. }
  104. protected function countCoffee() {
  105. $coffee = 5;
  106. if($this->boss) { $coffee *= 2; }
  107. return $coffee;
  108. }
  109. protected function countPages() {
  110. if($this->boss) { return 0; }
  111. return $pages = 50;
  112. }
  113. }
  114. class Analyst extends Employee {
  115. protected function countSalary() {
  116. $this->rate = 800;
  117. $salary = $this->rateScaleBasedOnRank($this->rate);
  118. if($this->boss) { $salary += $salary*0.5; }
  119. return $salary;
  120. }
  121. protected function countCoffee() {
  122. $coffee = 50;
  123. if($this->boss) { $coffee *= 2; }
  124. return $coffee;
  125. }
  126. protected function countPages() {
  127. if($this->boss) { return 0; }
  128. return $pages = 5;
  129. }
  130. }
  131. function splitQtyPostRank($qtyPostRank) {
  132. $qty = preg_split('/х?ме|ма|ин|ан/u', $qtyPostRank);
  133. if($qty[0] == "") {
  134. $qty[0] = 1;
  135. }
  136. $rank = $qty[1]; //Ранг
  137. $qty = $qty[0]; //Кол-во
  138. $pattern = ["/[0-9]/u", "/х/u"];
  139. $post = preg_filter($pattern, "",$qtyPostRank); //Должность
  140. $qtyPostRank = ["qty" => $qty, "post" => $post, "rank" => $rank];
  141. return $qtyPostRank;
  142. }
  143. $purchaseDepartment = ['9хме1', '3хме2', '2хме3', 'ме2'];
  144. $sellingDepartment = ['12хме1','6хма1','3хан1','2хан2','ма2'];
  145. $advertisementDepartment = ['15хма1', '10хма2', '8хме1', '2хин1', 'ма3'];
  146. $logisticsDepartment = ['13хме1','5хме2','5хин1','ме1'];
  147. $departments = [
  148. 'Закупок' => $purchaseDepartment,
  149. 'Продаж' => $sellingDepartment,
  150. 'Рекламы' => $advertisementDepartment,
  151. 'Логистики' => $logisticsDepartment
  152. ];
  153. $total = [
  154. 'employees' => 0,
  155. 'salary' => 0,
  156. 'coffee' => 0,
  157. 'pages' => 0,
  158. 'tugrPerPage' => 0
  159. ];
  160. $avrg = [
  161. 'employees' => 0,
  162. 'salary' => 0,
  163. 'coffee' => 0,
  164. 'pages' => 0,
  165. 'tugrPerPage' => 0
  166. ];
  167. // Ширина колонок
  168. $col1 = 30;
  169. $col2 = 6;
  170. $col3 = 10;
  171. $col4 = 10;
  172. $col5 = 9;
  173. $col6 = 15;
  174. function padRight($string, $length){
  175. if(mb_strlen($string) < $length) {
  176. $string = $string.str_repeat(" ", $length-mb_strlen($string));
  177. return $string;
  178. }else{
  179. die('Ширина столбца меньше длины строки');
  180. }
  181. }
  182. function padLeft($string, $length) {
  183. if(mb_strlen($string) < $length) {
  184. $string = str_repeat(" ",$length-mb_strlen($string)).$string;
  185. return $string;
  186. }else{
  187. return $string;
  188. }
  189. }
  190. // Шапка таблицы
  191. echo padRight('Департамент', $col1);
  192. echo padLeft('сотр.', $col2);
  193. echo padLeft('тугр.', $col3);
  194. echo padLeft('кофе.', $col4);
  195. echo padLeft('стр.', $col5);
  196. echo padLeft('тугр./стр..', $col6)."\n";
  197. foreach($departments as $name => $department) {
  198. $departmentTotals = [ // Результаты для опеределенного департамента
  199. 'employees' => 0,
  200. 'salary' => 0,
  201. 'coffee' => 0,
  202. 'pages' => 0,
  203. 'tugrPerPage' => 0
  204. ];
  205. echo padRight($name, $col1);
  206. foreach($department as $number => $consist) {
  207. $qtyPostRank = splitQtyPostRank($consist);
  208. if($number == count($purchaseDepartment)-1) {
  209. $boss = true;
  210. }else{
  211. $boss = false;
  212. }
  213. $qty = $qtyPostRank['qty'];
  214. $post = $qtyPostRank['post'];
  215. $rank = $qtyPostRank['rank'];
  216. $partOfDepartment = new Department();
  217. if($partOfDepartment->countTotalPages($qty, $post, $boss) != 0) {
  218. $total['tugrPerPage'] += round(
  219. $partOfDepartment->countTotalSalary($qty, $rank, $post, $boss) /
  220. $partOfDepartment->countTotalPages($qty, $post, $boss)
  221. , 2);
  222. $departmentTotals['tugrPerPage'] += round(
  223. $partOfDepartment->countTotalSalary($qty, $rank, $post, $boss) /
  224. $partOfDepartment->countTotalPages($qty, $post, $boss)
  225. , 2);
  226. }else{
  227. $total['tugrPerPage'] += 0;
  228. $departmentTotals['tugrPerPage'] += 0;
  229. }
  230. $total['employees'] += $qty;
  231. $total['salary'] += $partOfDepartment->countTotalSalary($qty, $rank, $post, $boss);
  232. $total['coffee'] += $partOfDepartment->countTotalCoffee($qty, $post, $boss);
  233. $total['pages'] += $partOfDepartment->countTotalPages($qty, $post, $boss);
  234. $departmentTotals['employees'] += $qty;
  235. $departmentTotals['salary'] += $partOfDepartment->countTotalSalary($qty, $rank, $post, $boss);
  236. $departmentTotals['coffee'] += $partOfDepartment->countTotalCoffee($qty, $post, $boss);
  237. $departmentTotals['pages'] += $partOfDepartment->countTotalPages($qty, $post, $boss);
  238. }
  239. echo padLeft($departmentTotals['employees'], $col2);
  240. echo padLeft($departmentTotals['salary'], $col3);
  241. echo padLeft($departmentTotals['coffee'], $col4);
  242. echo padLeft($departmentTotals['pages'], $col5);
  243. echo padLeft($departmentTotals['tugrPerPage'], $col6)."\n";
  244. }
  245. $avrg['employees'] = round($total['employees']/count($departments), 2);
  246. $avrg['salary'] = round($total['salary']/count($departments),2);
  247. $avrg['coffee'] = round($total['coffee']/count($departments),2);
  248. $avrg['pages'] = round($total['pages']/count($departments),2);
  249. $avrg['tugrPerPage'] = round($total['tugrPerPage']/count($departments),2);
  250. var_dump($total);
  251. var_dump($avrg);
  252. /* TEST
  253. $post = 'ме';
  254. $rank = 2;
  255. $boss = false;
  256. $test = new Department();
  257. $test->countTotalSalary(3, $rank, $post, $boss);
  258. $test->countTotalCoffee(3, $post, $boss);
  259. $test->countTotalPages(14, $post, $boss);
  260.  */
Success #stdin #stdout 0.02s 52480KB
stdin
Standard input is empty
stdout
Департамент                    сотр.     тугр.     кофе.     стр.    тугр./стр..
Закупок                           15    8812.5       320     2800           9.38
Продаж                            24     14300       695     3465          168.5
Рекламы                           36     16200       570     5500           12.5
Логистики                         24     11375       425     3850           9.63
array(5) {
  ["employees"]=>
  int(99)
  ["salary"]=>
  float(50687.5)
  ["coffee"]=>
  int(2010)
  ["pages"]=>
  int(15615)
  ["tugrPerPage"]=>
  float(200.01)
}
array(5) {
  ["employees"]=>
  float(24.75)
  ["salary"]=>
  float(12671.88)
  ["coffee"]=>
  float(502.5)
  ["pages"]=>
  float(3903.75)
  ["tugrPerPage"]=>
  float(50)
}