fork download
  1. <?php
  2. //Убрал в функция createEmployee, padLeft, padRight - тайп-хинт
  3. //Для int и string, т.к. ideone считает их за объекты
  4.  
  5.  
  6. define("SMALL_COL", 7);
  7. define("MEDIUM_COL", 12);
  8. define("LARGE_COL", 20);
  9.  
  10. class Company
  11. {
  12. private $name;
  13. private $listOfDepatments;
  14.  
  15. public function __construct($name)
  16. {
  17. $this->name = $name;
  18. $this->listOfDepatments = array();
  19. }
  20.  
  21. /**
  22.   * Метод возвращающий имя компании
  23.   *
  24.   * @return string Имя компании
  25.   */
  26. public function getNameCompany()
  27. {
  28. return $this->name;
  29. }
  30.  
  31. /**
  32.   * Метод возвращающий количество департаментов
  33.   *
  34.   * @return integer Имя компании
  35.   */
  36. public function getCountDeparments()
  37. {
  38. return count($this->listOfDepatments);
  39. }
  40.  
  41. /**
  42.   * Метод добавляющий новый департамент в компанию
  43.   *
  44.   * @param Department $newDepartment Добавляемый департамент
  45.   * @return void
  46.   */
  47. public function addDepartmentToCompany(Department $newDepartment)
  48. {
  49. $this->listOfDepatments[] = $newDepartment;
  50. }
  51.  
  52. /**
  53.   * Метод возвращающий список департаментов
  54.   *
  55.   * @return array Массив департаментов
  56.   */
  57. public function getListOfDepartments()
  58. {
  59. return $this->listOfDepatments;
  60. }
  61. }
  62.  
  63. class Department
  64. {
  65. private $name;
  66. private $listOfEmployee;
  67.  
  68. public function __construct($name)
  69. {
  70. $this->name = $name;
  71. $this->listOfEmployee = array();
  72. }
  73.  
  74. /**
  75.   * Метод возвращающий имя департамента
  76.   *
  77.   * @return string Имя департамент
  78.   */
  79. public function getDepartmentName()
  80. {
  81. return $this->name;
  82. }
  83.  
  84. /**
  85.   * Метод добавляющий нового сотрудника
  86.   *
  87.   * $param Employee $newEmployee Новый сотрудник
  88.   * @return void
  89.   */
  90. public function addEmployeeToDepartment(Employee $newEmployee)
  91. {
  92. $this->listOfEmployee[] = $newEmployee;
  93. }
  94.  
  95. /**
  96.   * Метод возвращающий количество сотрудников
  97.   *
  98.   * @return integer Количество сотрудников
  99.   */
  100. public function getCountEmployee()
  101. {
  102. return count($this->listOfEmployee);
  103. }
  104.  
  105. /**
  106.   * Метод возвращающий зарплату всего департамента
  107.   *
  108.   * @return integer Зарплата департамента
  109.   */
  110. public function getTotalSalary()
  111. {
  112. $totalSalary = 0;
  113. foreach ($this->listOfEmployee as $employee) {
  114. $totalSalary += $employee->getSalary();
  115. }
  116. return $totalSalary;
  117. }
  118.  
  119. /**
  120.   * Метод возвращающий общее число наработанных страниц
  121.   *
  122.   * @return integer Количество страниц
  123.   */
  124. public function getTotalPages()
  125. {
  126. $totalPages = 0;
  127. foreach ($this->listOfEmployee as $employee) {
  128. $totalPages += $employee->getPages();
  129. }
  130. return $totalPages;
  131. }
  132.  
  133. /**
  134.   * Метод возвращающий общее число выпитых кружен
  135.   *
  136.   * @return integer Число выпитых кружек
  137.   */
  138. public function getTotalCups()
  139. {
  140. $totalCups = 0;
  141. foreach ($this->listOfEmployee as $employee) {
  142. $totalCups += $employee->getCupOfCoffee();
  143. }
  144. return $totalCups;
  145. }
  146. }
  147.  
  148. abstract class Employee
  149. {
  150. protected $isDirector;
  151. protected $rank;
  152. protected $proffession;
  153. protected $salary;
  154. protected $pages;
  155. protected $cupOfCoffee;
  156.  
  157. public function __construct($rank, $isDirector = false)
  158. {
  159. $this->rank = $rank;
  160. $this->isDirector = $isDirector;
  161. $this->setSalary();
  162. $this->setPages();
  163. $this->setCupOfCoffee();
  164. $this->setProffession();
  165. }
  166.  
  167. abstract protected function setSalary();
  168. abstract protected function setPages();
  169. abstract protected function setCupOfCoffee();
  170. abstract protected function setProffession();
  171.  
  172. /**
  173.   * Метод возвращающий профессию сотрудника
  174.   *
  175.   * @return string Профессия сотрудника
  176.   */
  177. public function getEmployeeProffession()
  178. {
  179. return $this->proffession;
  180. }
  181.  
  182. /**
  183.   * Метод возвращающий ранг сотрудника
  184.   *
  185.   * @return integer Ранг сотрудника
  186.   */
  187. public function getEmployeeRank()
  188. {
  189. return $this->rank;
  190. }
  191.  
  192. /**
  193.   * Метод возвращающий зарплату сотрудника
  194.   *
  195.   * @return integer Зарплата сотрудника
  196.   */
  197. public function getSalary()
  198. {
  199. $totalSalary = $this->salary;
  200. if ($this->rank == 2) {
  201. $totalSalary *= 1.25;
  202. } elseif ($this->rank == 3) {
  203. $totalSalary *= 1.5;
  204. }
  205.  
  206. if ($this->isDirector) {
  207. $totalSalary *= 1.5;
  208. }
  209. return $totalSalary;
  210. }
  211.  
  212. /**
  213.   * Метод возвращающий количество наработанных страниц сотрудником
  214.   *
  215.   * @return integer Общее число страниц сотрудника
  216.   */
  217. public function getPages()
  218. {
  219. return ($this->isDirector ? 0 : $this->pages);
  220. }
  221.  
  222. /**
  223.   * Метод возвращающий количество выпитых кружек сотрудником
  224.   *
  225.   * @return integer Количество выпитых кружек
  226.   */
  227. public function getCupOfCoffee()
  228. {
  229. return ($this->isDirector ? ($this->cupOfCoffee * 2) : $this->cupOfCoffee);
  230. }
  231.  
  232. }
  233.  
  234. class Manager extends Employee
  235. {
  236. protected function setSalary()
  237. {
  238. $this->salary = 500;
  239. }
  240.  
  241. protected function setPages()
  242. {
  243. $this->pages = 200;
  244. }
  245.  
  246. protected function setCupOfCoffee()
  247. {
  248. $this->cupOfCoffee = 20;
  249. }
  250.  
  251. protected function setProffession()
  252. {
  253. $this->proffession = 'Менеджер';
  254. }
  255. }
  256.  
  257. class Marketer extends Employee
  258. {
  259. protected function setSalary()
  260. {
  261. $this->salary = 400;
  262. }
  263.  
  264. protected function setPages()
  265. {
  266. $this->pages = 150;
  267. }
  268.  
  269. protected function setCupOfCoffee()
  270. {
  271. $this->cupOfCoffee = 15;
  272. }
  273.  
  274. protected function setProffession()
  275. {
  276. $this->proffession = 'Маркетолог';
  277. }
  278. }
  279.  
  280. class Engineer extends Employee
  281. {
  282. protected function setSalary()
  283. {
  284. $this->salary = 200;
  285. }
  286.  
  287. protected function setPages()
  288. {
  289. $this->pages = 50;
  290. }
  291.  
  292. protected function setCupOfCoffee()
  293. {
  294. $this->cupOfCoffee = 5;
  295. }
  296.  
  297. protected function setProffession()
  298. {
  299. $this->proffession = 'Инженер';
  300. }
  301. }
  302.  
  303. class Analyst extends Employee
  304. {
  305. protected function setSalary()
  306. {
  307. $this->salary = 800;
  308. }
  309.  
  310. protected function setPages()
  311. {
  312. $this->pages = 5;
  313. }
  314.  
  315. protected function setCupOfCoffee()
  316. {
  317. $this->cupOfCoffee = 50;
  318. }
  319.  
  320. protected function setProffession()
  321. {
  322. $this->proffession = 'Аналитик';
  323. }
  324. }
  325.  
  326. /**
  327.   * Класс для создания отчета по компании
  328.   */
  329. class CompanyReport
  330. {
  331. public function printInfoAboutCompany(Company $company)
  332. {
  333. $countDepartment = $company->getCountDeparments();
  334. $total = array('сотр' => 0, 'тугр' => 0, 'кофе' => 0, 'стр' => 0);
  335.  
  336. echo "\"{$company->getNameCompany()}\"\n";
  337. echo padRigth("Деп. ", LARGE_COL) . padLeft("сотр.", SMALL_COL) . padLeft("тугр.", MEDIUM_COL) .
  338. padLeft("кофе", SMALL_COL) . padLeft("стр.", SMALL_COL) . padLeft("тугр./стр.", LARGE_COL) . "\n";
  339. $listOfDepatments = $company->getListOfDepartments();
  340. foreach ($listOfDepatments as $departament) {
  341. echo padRigth($departament->getDepartmentName(), LARGE_COL) . padLeft($departament->getCountEmployee(), SMALL_COL) .
  342. padLeft($departament->getTotalSalary(), MEDIUM_COL) . padLeft($departament->getTotalCups(), SMALL_COL) .
  343. padLeft($departament->getTotalPages(), MEDIUM_COL) . padLeft($departament->getTotalSalary() / $departament->getTotalPages(), LARGE_COL) . "\n";
  344. $total['сотр'] += $departament->getCountEmployee();
  345. $total['тугр'] += $departament->getTotalSalary();
  346. $total['кофе'] += $departament->getTotalCups();
  347. $total['стр'] += $departament->getTotalPages();
  348. }
  349.  
  350. echo padRigth('Среднее', LARGE_COL) . padLeft($total['сотр'] / $countDepartment, SMALL_COL) .
  351. padLeft($total['тугр'] / $countDepartment, MEDIUM_COL) . padLeft($total['кофе'] / $countDepartment, SMALL_COL) .
  352. padLeft($total['стр'] / $countDepartment, MEDIUM_COL) . padLeft(($total['тугр'] / $total['стр']) / $countDepartment, LARGE_COL) . "\n";
  353. echo padRigth('Всего', LARGE_COL) . padLeft($total['сотр'], SMALL_COL) .
  354. padLeft($total['тугр'], MEDIUM_COL) . padLeft($total['кофе'], SMALL_COL) .
  355. padLeft($total['стр'], MEDIUM_COL) . padLeft($total['тугр'] / $total['стр'], LARGE_COL) . "\n";
  356. }
  357. }
  358.  
  359. function padLeft($text, $length)
  360. {
  361. if (mb_strlen($text) < $length) {
  362. $text = str_repeat(" ", $length - mb_strlen($text)) . $text;
  363. }
  364.  
  365. return $text;
  366. }
  367.  
  368. function padRigth($text, $length) {
  369. if (mb_strlen($text) < $length) {
  370. $text .= str_repeat(" ", $length - mb_strlen($text));
  371. }
  372.  
  373. return $text;
  374. }
  375.  
  376. /**
  377.   * Функция создания массива сотрудников
  378.   *
  379.   * @param string $message Строка вида:9xме1;3xмe2;2xмe3;2xмa1;1xмe2 (последний идет руководитель)
  380.   * @return array Список сотрудников
  381.   */
  382. function createEmployee($message)
  383. {
  384. $employee = array();
  385.  
  386. $people = explode(';', $message);
  387. $totalPeople = count($people);
  388.  
  389.  
  390. for ($i = 0; $i < $totalPeople; $i++) {
  391.  
  392. $matches = array();
  393. preg_match('/([0-9]+)x([а-яё]+)([0-9])/iu', $people[$i], $matches);
  394.  
  395. $countEmployee = (int) $matches[1];
  396. $proffession = $matches[2];
  397. $rank = (int) $matches[3];
  398.  
  399. $isDirector = ($i == ($totalPeople - 1)) ? true : false;
  400.  
  401. for ($j = 0; $j < $countEmployee; $j++ ) {
  402. switch ($proffession) {
  403. case "ме": $employee[] = new Manager($rank, $isDirector);
  404. break;
  405. case "ма": $employee[] = new Marketer($rank, $isDirector);
  406. break;
  407. case "ан": $employee[] = new Analyst($rank, $isDirector);
  408. break;
  409. case "ин": $employee[] = new Engineer($rank, $isDirector);
  410. break;
  411. default: echo "Проверьте входные данные! {$proffession}";
  412. exit();
  413. }
  414. }
  415. }
  416.  
  417. return $employee;
  418. }
  419.  
  420. /**
  421.   * Функция добавляет созданный список сотрудников в департамент
  422.   *
  423.   * @return void
  424.   */
  425. function addAllEmployeeToDepartment(array $employee, Department $departament)
  426. {
  427. $totalEmployee = count($employee);
  428.  
  429. for ($i = 0; $i < $totalEmployee; $i++) {
  430. $departament->addEmployeeToDepartment($employee[$i]);
  431. }
  432. }
  433.  
  434. $vector = new Company('Вектор');
  435.  
  436.  
  437. $procurementDepartment = new Department('закупок');
  438. $vector->addDepartmentToCompany($procurementDepartment);
  439.  
  440. $salesDepartament = new Department('продаж');
  441. $vector->addDepartmentToCompany($salesDepartament);
  442.  
  443. $advertisingDepartment = new Department('рекламы');
  444. $vector->addDepartmentToCompany($advertisingDepartment);
  445.  
  446. $logisticsDepartament = new Department('логистики');
  447. $vector->addDepartmentToCompany($logisticsDepartament);
  448.  
  449.  
  450. $procurementEmployee = array();
  451. $procurementEmployee = createEmployee('9xме1;3xме2;2xме3;2xма1;1xме2');
  452. addAllEmployeeToDepartment($procurementEmployee, $procurementDepartment);
  453.  
  454. $salesEmployee = array();
  455. $salesEmployee = createEmployee('12xме1;6xма1;3xан1;2xан2;1xма2');
  456. addAllEmployeeToDepartment($salesEmployee, $salesDepartament);
  457.  
  458. $advertisingEmployee = array();
  459. $advertisingEmployee = createEmployee('15xма1;10xма2;8xме1;2xин1;1xма3');
  460. addAllEmployeeToDepartment($advertisingEmployee, $advertisingDepartment);
  461.  
  462. $logisticsEmployee = array();
  463. $logisticsEmployee = createEmployee('13xме1;5xме2;5xин1;1xме1');
  464. addAllEmployeeToDepartment($logisticsEmployee, $logisticsDepartament);
  465.  
  466.  
  467. $newReport = new CompanyReport();
  468. $newReport->printInfoAboutCompany($vector);
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
"Вектор"
Деп.                  сотр.       тугр.   кофе   стр.          тугр./стр.
закупок                  17      9612.5    350        3100     3.1008064516129
продаж                   24       13550    610        3325     4.0751879699248
рекламы                  36       16300    575        5450     2.9908256880734
логистики                24       11375    425        3850     2.9545454545455
Среднее               25.25   12709.375    490     3931.25    0.80822734499205
Всего                   101     50837.5   1960       15725     3.2329093799682