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(Employee $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. public function getStaffCount()
  61. {
  62. return count($this->staff) + count($this->chief);
  63. }
  64. }
  65.  
  66. abstract class Employee
  67. {
  68. const RANK_1 = 1;
  69. const RANK_2 = 2;
  70. const RANK_3 = 3;
  71.  
  72. const MANAGER = "me";
  73. const MARKETER = "ma";
  74. const ENGINEER = "en";
  75. const ANALYST = "an";
  76.  
  77. public $baseSalary, $coffee, $output, $rank;
  78.  
  79. public function __construct($rank)
  80. {
  81. $this->rank = $rank;
  82. }
  83.  
  84. public function getSalary($isChief)
  85. {
  86. $salaryMult = array(
  87. self::RANK_1 => 1,
  88. self::RANK_2 => 1.25,
  89. self::RANK_3 => 1.5,
  90. );
  91.  
  92. if ($isChief == false) {
  93. return $this->baseSalary * $salaryMult[$this->rank];
  94. } else {
  95. return $this->baseSalary * $salaryMult[$this->rank] * 1.5;
  96. }
  97. }
  98.  
  99. public function getChiefCoffee()
  100. {
  101. return $this->coffee*2;
  102. }
  103. }
  104.  
  105. class Manager extends Employee
  106. {
  107. public $baseSalary = 500;
  108. public $coffee = 20;
  109. public $output = 200;
  110. }
  111.  
  112. class Marketer extends Employee
  113. {
  114. public $baseSalary = 400;
  115. public $coffee = 15;
  116. public $output = 150;
  117. }
  118.  
  119. class Engineer extends Employee
  120. {
  121. public $baseSalary = 400;
  122. public $coffee = 5;
  123. public $output = 50;
  124. }
  125.  
  126. class Analyst extends Employee
  127. {
  128. public $salary = 400;
  129. public $coffee = 50;
  130. public $output = 5;
  131. }
  132.  
  133. class EmployeeFactory
  134. {
  135. public static function chooseEmployee($name, $rank)
  136. {
  137. if ($name == "me") {
  138. return new Manager($rank);
  139. } elseif ($name == "ma") {
  140. return new Marketer($rank);
  141. } elseif ($name == "en") {
  142. return new Engineer($rank);
  143. } elseif ($name == "an") {
  144. return new Analyst($rank);
  145. }
  146.  
  147. throw new Exception("Invalid type: $name");
  148. }
  149.  
  150. public static function createEmployees($employees)
  151. {
  152. $new = array();
  153. foreach ($employees as $workers) {
  154.  
  155. preg_match('/^(\d*)(\w{2})([1-3])$/i', $workers, $res);
  156. $number = $res[1];
  157. $name = $res[2];
  158. $rank = $res[3];
  159.  
  160. if (!$number) {
  161. $number = 1;
  162. }
  163.  
  164. for ($i = 0; $i < $number; $i++) {
  165.  
  166. $new[] = self::chooseEmployee($name, $rank);
  167. }
  168. }
  169. return $new;
  170. }
  171.  
  172. public static function createChief($employee)
  173. {
  174. preg_match('/^(\w{2})([1-3])$/i', $employee, $res);
  175. $name = $res[1];
  176. $rank = $res[2];
  177.  
  178. $chief = self::chooseEmployee($name, $rank);
  179. $chief->output = 0;
  180.  
  181. return $chief;
  182. }
  183. }
  184.  
  185. $purchase = new Department("Закупки");
  186. $purchase->addEmployee(EmployeeFactory::createEmployees(array("9me1", "3me2", "2me3", "2ma1")));
  187. $purchase->setChief(EmployeeFactory::createChief("me2"));
  188.  
  189. $sales = new Department("Продажи");
  190. $sales->addEmployee(EmployeeFactory::createEmployees(array("12me1", "6ma1", "3an1", "2an2")));
  191. $sales->setChief(EmployeeFactory::createChief("ma2"));
  192.  
  193. $ads = new Department("Реклама");
  194. $ads->addEmployee(EmployeeFactory::createEmployees(array("15ma1", "10ma2", "8me1", "2en1")));
  195. $ads->setChief(EmployeeFactory::createChief("ma3"));
  196.  
  197. $log = new Department("Логистика");
  198. $log->addEmployee(EmployeeFactory::createEmployees(array("13me1", "5me2", "5en1")));
  199. $log->setChief(EmployeeFactory::createChief("me1"));
  200.  
  201. $departments = array($purchase, $sales, $ads, $log);
  202.  
  203. // ВЫВОД
  204. function padLeft($string, $length)
  205. {
  206. $countString = mb_strlen($string);
  207. if ($countString < $length) {
  208. $pad = $length - $countString;
  209. $string = str_repeat(" ", $pad).$string;
  210. }
  211. return $string;
  212. }
  213.  
  214. function padRight($string, $length)
  215. {
  216. $countString = mb_strlen($string);
  217. if ($countString < $length) {
  218. $pad = $length - $countString;
  219. $string .= str_repeat(" ", $pad);
  220. }
  221. return $string;
  222. }
  223.  
  224. // Ширина колонок
  225. $col1 = 15;
  226. $col2 = 12;
  227. $col3 = 12;
  228. $col4 = 12;
  229. $col5 = 12;
  230. $col6 = 20;
  231.  
  232. // Заголовок таблицы
  233. echo padRight("Департамент", $col1) .
  234. padLeft("сотр.", $col2) .
  235. padLeft("тугр.", $col3) .
  236. padLeft("кофе", $col4) .
  237. padLeft("стр.", $col5) .
  238. padLeft("тугр./стр.", $col6) ."\n";
  239.  
  240. // Граница заголовков
  241. echo str_repeat("-", 70)."\n";
  242.  
  243. $sumCount = 0;
  244. $sumSalary = 0;
  245. $sumCoffee = 0;
  246. $sumOutput = 0;
  247. $sumExpence = 0;
  248.  
  249. foreach ($departments as $department) {
  250.  
  251. echo padRight($department->name, $col1) .
  252. padLeft($department->getStaffCount(), $col2) .
  253. padLeft($department->getTotalSalary(), $col3) .
  254. padLeft($department->getTotalCoffee(), $col4) .
  255. padLeft($department->getTotalOutput(), $col5) .
  256. padLeft(round($department->getTotalSalary()/$department->getTotalOutput(), 2), $col6) ."\n";
  257. $sumCount += $department->getStaffCount();
  258. $sumSalary += $department->getTotalSalary();
  259. $sumCoffee += $department->getTotalCoffee();
  260. $sumOutput += $department->getTotalOutput();
  261. $sumExpence += round($department->getTotalSalary()/$department->getTotalOutput(), 2);
  262. }
  263.  
  264. $n = count($departments);
  265.  
  266. echo padRight("Среднее", $col1) .
  267. padLeft($sumCount / $n, $col2) .
  268. padLeft($sumSalary / $n, $col3) .
  269. padLeft($sumCoffee / $n, $col4) .
  270. padLeft($sumOutput / $n, $col5) .
  271. padLeft($sumExpence / $n, $col6) ."\n";
  272.  
  273. echo padRight("Всего", $col1) .
  274. padLeft($sumCount, $col2) .
  275. padLeft($sumSalary, $col3) .
  276. padLeft($sumCoffee, $col4) .
  277. padLeft($sumOutput, $col5) .
  278. 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