fork(1) download
  1. <?php
  2.  
  3.  
  4. class Department
  5. {
  6. public $name;
  7. public $staff = array();
  8. public $chief;
  9.  
  10. public function __construct($name)
  11. {
  12. $this->name = $name;
  13. }
  14.  
  15. public function setChief($chief)
  16. {
  17. $this->chief = $chief;
  18. }
  19.  
  20. public function addEmployee(array $employees)
  21. {
  22. foreach ($employees as $workers) {
  23. $this->staff[] = $workers;
  24. }
  25. }
  26.  
  27. public function getTotalSalary()
  28. {
  29. $totalSalary = 0;
  30. foreach ($this->staff as $type) {
  31. $totalSalary += $type->getSalary(false);
  32. }
  33.  
  34. $totalSalary += $this->chief->getSalary(true);
  35.  
  36. return $totalSalary;
  37. }
  38.  
  39. public function getTotalCoffee()
  40. {
  41. $totalCoffee = 0;
  42. foreach ($this->staff as $type) {
  43. $totalCoffee += $type->coffee;
  44. }
  45.  
  46. $totalCoffee += $this->chief->getChiefCoffee();
  47.  
  48. return $totalCoffee;
  49. }
  50.  
  51. public function getTotalOutput()
  52. {
  53. $totalOutput = 0;
  54. foreach ($this->staff as $type) {
  55. $totalOutput += $type->output;
  56. }
  57. return $totalOutput;
  58. }
  59. }
  60.  
  61. abstract class Employee
  62. {
  63. const RANK_1 = 1;
  64. const RANK_2 = 2;
  65. const RANK_3 = 3;
  66.  
  67. const MANAGER = "me";
  68. const MARKETER = "ma";
  69. const ENGINEER = "en";
  70. const ANALYST = "an";
  71.  
  72. public $baseSalary, $coffee, $output, $rank;
  73.  
  74. public function __construct($rank)
  75. {
  76. $this->rank = $rank;
  77. }
  78.  
  79. public function getSalary($isChief)
  80. {
  81. $salaryMult = array(
  82. self::RANK_1 => 1,
  83. self::RANK_2 => 1.25,
  84. self::RANK_3 => 1.5,
  85. );
  86.  
  87. if ($isChief == false) {
  88. return $this->baseSalary * $salaryMult[$this->rank];
  89. } else {
  90. return $this->baseSalary * $salaryMult[$this->rank] * 1.5;
  91. }
  92. }
  93.  
  94. public function getChiefCoffee()
  95. {
  96. return $this->coffee*2;
  97. }
  98. }
  99.  
  100. class Manager extends Employee
  101. {
  102. public $baseSalary = 500;
  103. public $coffee = 20;
  104. public $output = 200;
  105. }
  106.  
  107. class Marketer extends Employee
  108. {
  109. public $baseSalary = 400;
  110. public $coffee = 15;
  111. public $output = 150;
  112. }
  113.  
  114. class Engineer extends Employee
  115. {
  116. public $baseSalary = 400;
  117. public $coffee = 5;
  118. public $output = 50;
  119. }
  120.  
  121. class Analyst extends Employee
  122. {
  123. public $salary = 400;
  124. public $coffee = 50;
  125. public $output = 5;
  126. }
  127.  
  128. class EmployeeFactory
  129. {
  130. public static function chooseEmployee($name, $rank)
  131. {
  132. if ($name == "me") {
  133. return new Manager($rank);
  134. } elseif ($name == "ma") {
  135. return new Marketer($rank);
  136. } elseif ($name == "en") {
  137. return new Engineer($rank);
  138. } elseif ($name == "an") {
  139. return new Analyst($rank);
  140. }
  141. }
  142.  
  143. public static function createEmployees($employees)
  144. {
  145. $new = array();
  146. foreach ($employees as $workers) {
  147.  
  148. preg_match_all('/^(\d*)(\w{2})([1-3])$/i', $workers, $res, PREG_SET_ORDER);
  149. $number = $res[0][1];
  150. $name = $res[0][2];
  151. $rank = $res[0][3];
  152.  
  153. if (!$number) {
  154. $number = 1;
  155. }
  156.  
  157. for ($i = 0; $i < $number; $i++) {
  158.  
  159. $new[] = self::chooseEmployee($name, $rank);
  160. }
  161. }
  162. return $new;
  163. }
  164.  
  165. public static function createChief($employee)
  166. {
  167. preg_match_all('/^(\w{2})([1-3])$/i', $employee, $res, PREG_SET_ORDER);
  168. $name = $res[0][1];
  169. $rank = $res[0][2];
  170.  
  171. $chief = self::chooseEmployee($name, $rank);
  172. $chief->output = 0;
  173.  
  174. return $chief;
  175. }
  176. }
  177.  
  178. $purchase = new Department("Закупки");
  179. $purchase->addEmployee(EmployeeFactory::createEmployees(array("9me1", "3me2", "2me3", "2ma1")));
  180. $purchase->setChief(EmployeeFactory::createChief("me2"));
  181.  
  182. $sales = new Department("Продажи");
  183. $sales->addEmployee(EmployeeFactory::createEmployees(array("12me1", "6ma1", "3an1", "2an2")));
  184. $sales->setChief(EmployeeFactory::createChief("ma2"));
  185.  
  186. $ads = new Department("Реклама");
  187. $ads->addEmployee(EmployeeFactory::createEmployees(array("15ma1", "10ma2", "8me1", "2en1")));
  188. $ads->setChief(EmployeeFactory::createChief("ma3"));
  189.  
  190. $log = new Department("Логистика");
  191. $log->addEmployee(EmployeeFactory::createEmployees(array("13me1", "5me2", "5en1")));
  192. $log->setChief(EmployeeFactory::createChief("me1"));
  193.  
  194. $departments = array($purchase, $sales, $ads, $log);
  195.  
  196. // ВЫВОД
  197. function padLeft($string, $length)
  198. {
  199. $countString = mb_strlen($string);
  200. if ($countString < $length) {
  201. $pad = $length - $countString;
  202. $string = str_repeat(" ", $pad).$string;
  203. }
  204. return $string;
  205. }
  206.  
  207. function padRight($string, $length)
  208. {
  209. $countString = mb_strlen($string);
  210. if ($countString < $length) {
  211. $pad = $length - $countString;
  212. $string .= str_repeat(" ", $pad);
  213. }
  214. return $string;
  215. }
  216.  
  217. // Ширина колонок
  218. $col1 = 15;
  219. $col2 = 12;
  220. $col3 = 12;
  221. $col4 = 12;
  222. $col5 = 12;
  223. $col6 = 20;
  224.  
  225. // Заголовок таблицы
  226. echo padRight("Департамент", $col1) .
  227. padLeft("сотр.", $col2) .
  228. padLeft("тугр.", $col3) .
  229. padLeft("кофе", $col4) .
  230. padLeft("стр.", $col5) .
  231. padLeft("тугр./стр.", $col6) ."\n";
  232.  
  233. // Граница заголовков
  234. echo str_repeat("-", 70)."\n";
  235.  
  236. $sumCount = 0;
  237. $sumSalary = 0;
  238. $sumCoffee = 0;
  239. $sumOutput = 0;
  240. $sumExpence = 0;
  241.  
  242. foreach ($departments as $department) {
  243.  
  244. echo padRight($department->name, $col1) .
  245. padLeft(count($department->staff) + 1, $col2) .
  246. padLeft($department->getTotalSalary(), $col3) .
  247. padLeft($department->getTotalCoffee(), $col4) .
  248. padLeft($department->getTotalOutput(), $col5) .
  249. padLeft(round($department->getTotalSalary()/$department->getTotalOutput(), 2), $col6) ."\n";
  250. $sumCount += count($department->staff) + 1;
  251. $sumSalary += $department->getTotalSalary();
  252. $sumCoffee += $department->getTotalCoffee();
  253. $sumOutput += $department->getTotalOutput();
  254. $sumExpence += round($department->getTotalSalary()/$department->getTotalOutput(), 2);
  255. }
  256.  
  257. $n = count($departments);
  258.  
  259. echo padRight("Среднее", $col1) .
  260. padLeft($sumCount / $n, $col2) .
  261. padLeft($sumSalary / $n, $col3) .
  262. padLeft($sumCoffee / $n, $col4) .
  263. padLeft($sumOutput / $n, $col5) .
  264. padLeft($sumExpence / $n, $col6) ."\n";
  265.  
  266. echo padRight("Всего", $col1) .
  267. padLeft($sumCount, $col2) .
  268. padLeft($sumSalary, $col3) .
  269. padLeft($sumCoffee, $col4) .
  270. padLeft($sumOutput, $col5) .
  271. padLeft($sumExpence, $col6) ."\n";
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Департамент           сотр.       тугр.        кофе        стр.          тугр./стр.
----------------------------------------------------------------------
Закупки                  17      9612.5         350        3100                 3.1
Продажи                  24        9150         610        3325                2.75
Реклама                  36       16700         575        5450                3.06
Логистика                24       12375         425        3850                3.21
Среднее               25.25   11959.375         490     3931.25                3.03
Всего                   101     47837.5        1960       15725               12.12