fork download
  1. <?php
  2. header('Content-type: text/plain');
  3.  
  4. abstract class Employee
  5. {
  6. private $rank;
  7. private $boss;
  8. protected $data;
  9. public const TYPE_EMPLOYEES = ['manager' => '0',
  10. 'marketer' => '1',
  11. 'engineer' => '2',
  12. 'analytic' => '3'];
  13.  
  14. function __construct(int $rank, int $boss) {
  15. $this->rank = $rank;
  16. $this->boss = $boss;
  17. }
  18.  
  19. public function getMoney() {
  20. $money = $this->data['money'];
  21. if ($this->rank == 2 ) {
  22. $money *= 1.25;
  23. } elseif ($this->rank == 3) {
  24. $money *= 1.5;
  25. }
  26. if ($this->boss) {
  27. $money *= 1.5;
  28. }
  29. return $money;
  30. }
  31.  
  32. public function getCoffee() {
  33. $coffee = $this->data['coffee'];
  34. if ($this->boss) {
  35. $coffee *= 2;
  36. }
  37. return $coffee;
  38. }
  39.  
  40. public function getPages() {
  41. $pages = $this->data['pages'];
  42. if ($this->boss) {
  43. $pages = 0;
  44. }
  45. return $pages;
  46. }
  47. }
  48.  
  49. class Manager extends Employee
  50. {
  51. protected $data = ['money' => '500',
  52. 'coffee' => '20',
  53. 'pages' => '200'];
  54. }
  55.  
  56. class Marketer extends Employee
  57. {
  58. protected $data = ['money' => '400',
  59. 'coffee' => '15',
  60. 'pages' => '150'];
  61. }
  62.  
  63. class Engineer extends Employee
  64. {
  65. protected $data = ['money' => '200',
  66. 'coffee' => '5',
  67. 'pages' => '50'];
  68. }
  69.  
  70. class Analytic extends Employee
  71. {
  72. protected $data = ['money' => '800',
  73. 'coffee' => '50',
  74. 'pages' => '5'];
  75. }
  76.  
  77. class Department
  78. {
  79. private $name;
  80. private $employees = [];
  81.  
  82. function __construct(string $department) {
  83. $this->name = $department;
  84. }
  85. /* Добавляем работников в департамент */
  86. public function createEmployee(int $prof, int $rank, int $boss, int $count) {
  87. for ($i = 0; $i < $count; $i++) {
  88. switch ($prof) {
  89. case Employee::TYPE_EMPLOYEES['manager']:
  90. $this->employees[] = new Manager($rank, $boss);
  91. break;
  92. case Employee::TYPE_EMPLOYEES['marketer']:
  93. $this->employees[] = new Marketer($rank, $boss);
  94. break;
  95. case Employee::TYPE_EMPLOYEES['engineer']:
  96. $this->employees[] = new Engineer($rank, $boss);
  97. break;
  98. case Employee::TYPE_EMPLOYEES['analytic']:
  99. $this->employees[] = new Analytic($rank, $boss);
  100. break;
  101. }
  102.  
  103. }
  104. }
  105.  
  106. /* Создаём массив со статистикой департамента */
  107. public function getDepStat() {
  108. $depData = ['name' => $this->name,
  109. 'count' => count($this->employees),
  110. 'money' => 0,
  111. 'coffee' => 0,
  112. 'pages' => 0,
  113. 'average' => 0];
  114.  
  115. foreach ($this->employees as $item) {
  116. $depData['money'] += $item->getMoney();
  117. $depData['coffee'] += $item->getCoffee();
  118. $depData['pages'] += $item->getPages();
  119. }
  120. $depData['average'] = round($depData['money'] / $depData['pages'], 1);
  121.  
  122. return $depData;
  123. }
  124. }
  125.  
  126. class Company
  127. {
  128. private $name;
  129. private $departments;
  130.  
  131. function __construct( string $name) {
  132. $this->name = $name;
  133. }
  134.  
  135. public function addDepartment($department) {
  136. $this->departments[] = $department;
  137. }
  138.  
  139. /* Получаем статистику департаментов и заносим в массив */
  140. public function getCompanyStat() {
  141. foreach ($this->departments as $item) {
  142. $companyStat[] = $item->getDepStat();
  143. }
  144. return $companyStat;
  145. }
  146.  
  147. /* Полная статистика компании */
  148. public function totalCompanyStat() {
  149. $statistic = $this->getCompanyStat();
  150. $totalStat = ['name' => 'Всего',
  151. 'count' => 0,
  152. 'money' => 0,
  153. 'coffee' => 0,
  154. 'pages' => 0];
  155.  
  156. foreach ($statistic as $item) {
  157. $totalStat['count'] += $item['count'];
  158. $totalStat['money'] += $item['money'];
  159. $totalStat['coffee'] += $item['coffee'];
  160. $totalStat['pages'] += $item['pages'];
  161. }
  162. return $totalStat;
  163. }
  164. /* Средние значения полной статистики */
  165. public function averageCompanyStat() {
  166. $depCount = count($this->departments);
  167. $statistic = $this->totalCompanyStat();
  168.  
  169. $averageStat = ['name' => 'Среднее',
  170. 'money' => 0,
  171. 'coffee' => 0,
  172. 'pages' => 0,
  173. 'average' => 0];
  174.  
  175. $averageStat['count'] = round($statistic['count'] / $depCount, 1);
  176. $averageStat['money'] = round($statistic['money'] / $depCount, 1);
  177. $averageStat['coffee'] = round($statistic['coffee'] / $depCount, 1);
  178. $averageStat['pages'] = round($statistic['pages'] / $depCount, 1);
  179. $averageStat['average'] = round($statistic['money'] / $statistic['pages'], 1);
  180. return $averageStat;
  181. }
  182.  
  183. }
  184.  
  185. /* Создаём компанию и добавляем в неё департаменты */
  186. $company = new Company("Вектор");
  187.  
  188. $dep = new Department("Закупок");
  189. $dep1 = new Department("Продаж");
  190. $dep2 = new Department("Рекламы");
  191. $dep3 = new Department("Логистики");
  192.  
  193. $company->addDepartment($dep);
  194. $company->addDepartment($dep1);
  195. $company->addDepartment($dep2);
  196. $company->addDepartment($dep3);
  197.  
  198. $dep->createEmployee(Employee::TYPE_EMPLOYEES['manager'], "1", "0", "9");
  199. $dep->createEmployee(Employee::TYPE_EMPLOYEES['manager'], "2", "0", "3");
  200. $dep->createEmployee(Employee::TYPE_EMPLOYEES['manager'], "3", "0", "2");
  201. $dep->createEmployee(Employee::TYPE_EMPLOYEES['marketer'], "1", "0", "2");
  202. $dep->createEmployee(Employee::TYPE_EMPLOYEES['manager'], "2", "1", "1");
  203.  
  204. $dep1->createEmployee(Employee::TYPE_EMPLOYEES['manager'], "1", "0", "12");
  205. $dep1->createEmployee(Employee::TYPE_EMPLOYEES['marketer'], "1", "0", "6");
  206. $dep1->createEmployee(Employee::TYPE_EMPLOYEES['analytic'], "1", "0", "3");
  207. $dep1->createEmployee(Employee::TYPE_EMPLOYEES['analytic'], "2", "0", "2");
  208. $dep1->createEmployee(Employee::TYPE_EMPLOYEES['marketer'], "2", "1", "1");
  209.  
  210. $dep2->createEmployee(Employee::TYPE_EMPLOYEES['marketer'], "1", "0", "15");
  211. $dep2->createEmployee(Employee::TYPE_EMPLOYEES['marketer'], "2", "0", "10");
  212. $dep2->createEmployee(Employee::TYPE_EMPLOYEES['manager'], "1", "0", "8");
  213. $dep2->createEmployee(Employee::TYPE_EMPLOYEES['engineer'], "1", "0", "2");
  214. $dep2->createEmployee(Employee::TYPE_EMPLOYEES['marketer'], "3", "1", "1");
  215.  
  216. $dep3->createEmployee(Employee::TYPE_EMPLOYEES['manager'], "1", "0", "13");
  217. $dep3->createEmployee(Employee::TYPE_EMPLOYEES['manager'], "2", "0", "5");
  218. $dep3->createEmployee(Employee::TYPE_EMPLOYEES['engineer'], "1", "0", "5");
  219. $dep3->createEmployee(Employee::TYPE_EMPLOYEES['manager'], "1", "1", "1");
  220.  
  221.  
  222. $companyStat = $company->getCompanyStat();
  223. $totalStat = $company->totalCompanyStat();
  224. $averageStat = $company->averageCompanyStat();
  225.  
  226. /* Вывод информации */
  227. $col1 = 15;
  228. $col2 = 10;
  229.  
  230. /* Заголовок */
  231. echo padLeft("Департамент", $col1) .
  232. padLeft("сотр.", $col2) .
  233. padLeft("тугр.", $col2) .
  234. padLeft("кофе", $col2) .
  235. padLeft("стр.", $col2) .
  236. padLeft("тугр./стр.", $col2) . "\n\n";
  237.  
  238. /* Перебор департаментов */
  239. foreach ($companyStat as $item) {
  240. echo padLeft($item['name'],$col1) .
  241. padLeft($item['count'], $col2) .
  242. padLeft($item['money'], $col2) .
  243. padLeft($item['coffee'], $col2) .
  244. padLeft($item['pages'], $col2) .
  245. padLeft($item['average'], $col2) . "\n";
  246. }
  247. /* Всего */
  248. echo padLeft($totalStat['name'],$col1) .
  249. padLeft($totalStat['count'], $col2) .
  250. padLeft($totalStat['money'], $col2) .
  251. padLeft($totalStat['coffee'], $col2) .
  252. padLeft($totalStat['pages'], $col2) . "\n";
  253.  
  254. /* Среднее */
  255. echo padLeft($averageStat['name'],$col1) .
  256. padLeft($averageStat['count'], $col2) .
  257. padLeft($averageStat['money'], $col2) .
  258. padLeft($averageStat['coffee'], $col2) .
  259. padLeft($averageStat['pages'], $col2) .
  260. padLeft($averageStat['average'], $col2);
  261.  
  262. function padLeft($value, $col){
  263. $count = iconv_strlen($value); // mb_strlen не работает
  264. if ($col > $count) {
  265. $value = $value . str_repeat(" ", $col - $count);
  266. }
  267. return $value;
  268. }
  269.  
Success #stdin #stdout 0.01s 23644KB
stdin
Standard input is empty
stdout
Департамент    сотр.     тугр.     кофе      стр.      тугр./стр.

Закупок        17        9612.5    350       3100      3.1       
Продаж         24        13550     610       3325      4.1       
Рекламы        36        16300     575       5450      3         
Логистики      24        11375     425       3850      3         
Всего          101       50837.5   1960      15725     
Среднее        25.3      12709.4   490       3931.3    3.2