fork download
  1. <?php
  2.  
  3. //Debug class
  4. class Dbg {
  5.  
  6. //cool and readable dump function
  7. public static function cd($var) {
  8. echo "<pre>";
  9. echo "\n";
  10. var_dump($var);
  11. }
  12. }
  13.  
  14. class Reporter {
  15. //ну а почему бы и нет, когда можно сделать красиво)
  16. static public function pageHeader() {
  17. echo '<!DOCTYPE html><html lang="en"><head><title>Bootstrap Example</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://m...content-available-to-author-only...n.com/bootstrap/3.3.7/css/bootstrap.min.css"><script src="https://a...content-available-to-author-only...s.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script><script src="https://m...content-available-to-author-only...n.com/bootstrap/3.3.7/js/bootstrap.min.js"></script></head><body>';
  18. }
  19. static public function pageFooter() {
  20. echo '</body></html>';
  21. }
  22.  
  23. static public function browserReport(Organisation $org) {
  24. echo '<div class="container">';
  25. echo '<table class="table"><thead>';
  26. echo "<tr>";
  27. echo "<th>Департамент</th> <th>сотр.</th> <th>тугр.</th> <th>кофе</th> <th>стр.</th> <th>тугр/стр</th>";
  28. echo "</tr></thead><tbody>";
  29. foreach ($org->getDepartments() as $dep) {
  30. echo "<tr>";
  31. echo "<td>". $dep->getName() . "</td>";
  32. echo "<td>". $dep->countDepEmployees() . "</td>";
  33. echo "<td>". $dep->countDepSalary() . "</td>";
  34. echo "<td>". $dep->countDepCoffe() . "</td>";
  35. echo "<td>". $dep->countDepPapers() . "</td>";
  36. echo "<td>". $dep->countDepPageCost() . "</td>";
  37. echo "</tr>";
  38. }
  39.  
  40. $totals = $org->getOrgTotals();
  41.  
  42. echo "<tr><td>Средне</td>";
  43. echo "<td>" . $totals->avgPeople . "</td>";
  44. echo "<td>" . $totals->avgSalary . "</td>";
  45. echo "<td>" . $totals->avgCoffe . "</td>";
  46. echo "<td>" . $totals->avgPapers . "</td>";
  47. echo "<td>" . $totals->avgCost . "</td>";
  48. echo "</tr>";
  49.  
  50. echo "<tr><td>Всего</td>";
  51. echo "<td>" . $totals->totalPeople . "</td>";
  52. echo "<td>" . $totals->totalSalary . "</td>";
  53. echo "<td>" . $totals->totalCoffe . "</td>";
  54. echo "<td>" . $totals->totalPapers . "</td>";
  55. echo "<td>" . $totals->totalCost . "</td>";
  56. echo "</tr>";
  57.  
  58.  
  59.  
  60. echo "</tbody></table></div>";
  61.  
  62. }
  63.  
  64. static public function ideoneReport(Organisation $org) {
  65. //echo "<pre>";
  66. echo "Департамент сотр. тугр. кофе стр. тугр/стр.\n";
  67. foreach ($org->getDepartments() as $dep) {
  68. echo $dep->getName(). " " .
  69. $dep->countDepEmployees(). " " .
  70. $dep->countDepSalary(). " " .
  71. $dep->countDepCoffe(). " " .
  72. $dep->countDepPapers(). " " .
  73. $dep->countDepPageCost() . "\n";
  74. }
  75. $totals = $org->getOrgTotals();
  76. echo "\nСредне" . " " .
  77. $totals->avgPeople . " " .
  78. $totals->avgSalary . " " .
  79. $totals->avgCoffe . " " .
  80. $totals->avgPapers . " " .
  81. $totals->avgCost . "\n";
  82. echo "Всего" . " " .
  83. $totals->totalPeople . " " .
  84. $totals->totalSalary . " " .
  85. $totals->totalCoffe . " " .
  86. $totals->totalPapers . " " .
  87. $totals->totalCost . "\n";
  88. }
  89.  
  90. }
  91.  
  92. class Organisation {
  93.  
  94. private $name;
  95. private $departments = [];
  96.  
  97. public function __construct($name) {
  98. $this->name = $name;
  99. }
  100.  
  101. public function addDepartment(Department $dep) {
  102.  
  103. if (!in_array($dep, $this->departments, true)) {
  104. $this->departments[] = $dep;
  105. }
  106.  
  107. }
  108.  
  109. public function getDepartments() {
  110. return $this->departments;
  111. }
  112.  
  113. public function countDepartments() {
  114. return count($this->departments);
  115. }
  116.  
  117. public function getOrgTotals() {
  118. $info = new stdClass();
  119. $info->totalPeople = 0;
  120. $info->avgPeople = 0;
  121.  
  122. $info->totalSalary = 0;
  123. $info->avgSalary = 0;
  124.  
  125. $info->totalCoffe = 0;
  126. $info->avgCoffe = 0;
  127.  
  128. $info->totalPapers = 0;
  129. $info->avgPapers = 0;
  130.  
  131. $info->totalCost = 0;
  132. $info->avgCost = 0;
  133.  
  134. foreach($this->getDepartments() as $dep) {
  135. $info->totalPeople += $dep->countDepEmployees();
  136. $info->totalSalary += $dep->countDepSalary();
  137. $info->totalCoffe += $dep->countDepCoffe();
  138. $info->totalPapers += $dep->countDepPapers();
  139. $info->totalCost += $dep->countDepPageCost();
  140. }
  141.  
  142. $depsCount = $this->countDepartments();
  143.  
  144. if ($depsCount > 0) {
  145. $info->avgPeople = ($info->totalPeople / $depsCount);
  146. $info->avgSalary = ($info->totalSalary / $depsCount);
  147. $info->avgCoffe = ($info->totalCoffe / $depsCount);
  148. $info->avgPapers = ($info->totalPapers / $depsCount);
  149. $info->avgCost = ($info->totalCost / $depsCount);
  150.  
  151. }
  152.  
  153.  
  154. return $info;
  155. }
  156.  
  157. }
  158.  
  159.  
  160. class Department {
  161.  
  162. private $name;
  163. private $employees = array();
  164.  
  165. public function __construct($name) {
  166. $this->name = $name;
  167. }
  168.  
  169. public function getName() {
  170. return $this->name;
  171. }
  172.  
  173. public function addEmployee(Employee $employee) {
  174. if (!in_array($employee, $this->employees, true)) {
  175. $this->employees[] = $employee;
  176. }
  177. }
  178.  
  179. public function addEmployees(array $employees) {
  180. foreach ($employees as $employee) {
  181. if (is_object($employee) and get_parent_class($employee) == 'Employee') {
  182. $this->addEmployee($employee);
  183. }
  184. }
  185. }
  186.  
  187. public function getEmployees() {
  188. return $this->employees;
  189. }
  190.  
  191. public function countDepEmployees() {
  192. return count($this->getEmployees());
  193. }
  194.  
  195. public function countDepCoffe() {
  196. $depCoffe = 0;
  197. foreach ($this->getEmployees() as $employee) {
  198. $depCoffe += $employee->getCoffe();
  199. }
  200. return $depCoffe;
  201. }
  202.  
  203. public function countDepPapers() {
  204. $depPapers = 0;
  205. foreach ($this->getEmployees() as $employee) {
  206. $depPapers += $employee->getPapers();
  207. }
  208. return $depPapers;
  209. }
  210.  
  211. public function countDepSalary() {
  212. $depSalary = 0;
  213. foreach ($this->getEmployees() as $employee) {
  214. $depSalary += $employee->getSalary();
  215. }
  216. return $depSalary;
  217. }
  218.  
  219. public function countDepPageCost() {
  220. if ($this->countDepPapers() == 0) {
  221. return 0;
  222. } else {
  223. return ($this->countDepSalary() / $this->countDepPapers());
  224. }
  225. }
  226.  
  227. }
  228.  
  229. class Names {
  230. public static function generateFullName() {
  231. $fLength = mt_rand(2, 6);
  232. $firstName = self::generateName($fLength);
  233.  
  234. $sLength = mt_rand(2, 6);
  235. $secondName = self::generateName($sLength);
  236.  
  237. return $firstName . " " . $secondName;
  238. }
  239.  
  240. private static function generateName($length) {
  241. $syllables = [
  242. 'а', 'и', 'у', 'о', 'е',
  243. 'на', 'ни','ну', 'но', 'не',
  244. 'ка', 'ки', 'ку', 'ко','ке',
  245. 'та', 'ти', 'ту', 'то', 'те',
  246. 'са', 'си', 'су', 'со', 'се',
  247. ];
  248. $name = '';
  249.  
  250. for ($i = 1; $i <= $length; $i++) {
  251. $name .= $syllables[mt_rand(0, count($syllables)-1)];
  252. }
  253. $first = mb_strtoupper(mb_substr($name, 0, 1));
  254. $caseTitleName = $first . mb_substr($name, 1);
  255.  
  256. return $caseTitleName;
  257. }
  258. }
  259.  
  260. abstract class Employee {
  261.  
  262. private $name;
  263. private $rang;
  264. private $leader;
  265. private $baseSalary;
  266. private $baseCoffe;
  267. Private $basePapers;
  268.  
  269. public function __construct($rang, $leader, $name) {
  270. $this->name = $name;
  271. $this->rang = $rang;
  272. $this->leader = $leader;
  273. }
  274.  
  275. public function setBaseSalary($salary) {
  276. $this->baseSalary = $salary;
  277. }
  278.  
  279. public function setBaseCoffe($coffe) {
  280. $this->baseCoffe = $coffe;
  281. }
  282.  
  283. public function setBasePapers($papers) {
  284. $this->basePapers = $papers;
  285. }
  286.  
  287. public function getSalary() {
  288. if ($this->rang == 1) {
  289. $rangCoeff = 1;
  290. } elseif ($this->rang == 2) {
  291. $rangCoeff = 1.25;
  292. } elseif ($this->rang == 3) {
  293. $rangCoeff = 1.5;
  294. }
  295.  
  296. if ($this->leader == true) {
  297. $leaderCoeff = 1.5;
  298. } else {
  299. $leaderCoeff = 1;
  300. }
  301.  
  302. $salary = $this->baseSalary * $rangCoeff * $leaderCoeff;
  303.  
  304. return $salary;
  305. }
  306.  
  307. public function getCoffe() {
  308. if ($this->leader == true) {
  309. $leaderCoeff = 2;
  310. } else {
  311. $leaderCoeff = 1;
  312. }
  313.  
  314. $coffe = $this->baseCoffe * $leaderCoeff;
  315. return $coffe;
  316. }
  317.  
  318. public function getPapers() {
  319. if ($this->leader == true) {
  320. $leaderCoeff = 0;
  321. } else {
  322. $leaderCoeff = 1;
  323. }
  324.  
  325. $papers = $this->basePapers * $leaderCoeff;
  326. return $papers;
  327. }
  328.  
  329. }
  330.  
  331.  
  332. class Manager extends Employee {
  333. public function __construct($rang, $leader, $name) {
  334. parent::__construct($rang, $leader, $name);
  335. $this->setBaseSalary(500);
  336. $this->setBaseCoffe(20);
  337. $this->setBasePapers(200);
  338. }
  339. }
  340.  
  341. class Marketer extends Employee {
  342. public function __construct($rang, $leader, $name) {
  343. parent::__construct($rang, $leader, $name);
  344. $this->setBaseSalary(400);
  345. $this->setBaseCoffe(15);
  346. $this->setBasePapers(150);
  347. }
  348. }
  349. class Engineer extends Employee {
  350. public function __construct($rang, $leader, $name) {
  351. parent::__construct($rang, $leader, $name);
  352. $this->setBaseSalary(200);
  353. $this->setBaseCoffe(5);
  354. $this->setBasePapers(50);
  355. }
  356. }
  357. class Analyst extends Employee {
  358. public function __construct($rang, $leader, $name) {
  359. parent::__construct($rang, $leader, $name);
  360. $this->setBaseSalary(800);
  361. $this->setBaseCoffe(50);
  362. $this->setBasePapers(5);
  363. }
  364. }
  365.  
  366. class PeopleFactory {
  367. public static function create($class, $rang, $leader, $amount) {
  368.  
  369. if (get_parent_class($class) == 'Employee') {
  370. $people = [];
  371. for ($i = 0; $i < $amount; $i++) {
  372. $people[] = new $class($rang, $leader, Names::generateFullName());
  373. }
  374. return $people;
  375. }
  376. }
  377. }
  378.  
  379.  
  380. //как более элегантно забить работягами я просто не знаю:
  381. $org = new Organisation('Вектор');
  382.  
  383. // Департамент закупок: 9×ме1, 3×ме2, 2×ме3, 2×ма1 + руководитель департамента ме2
  384. $dep1 = new Department('Закупок');
  385. $org->addDepartment($dep1);
  386. $dep1->addEmployees(PeopleFactory::create('Manager', 1, false, 9));
  387. $dep1->addEmployees(PeopleFactory::create('Manager', 2, false, 3));
  388. $dep1->addEmployees(PeopleFactory::create('Manager', 3, false, 2));
  389. $dep1->addEmployees(PeopleFactory::create('Marketer', 1, false, 2));
  390. $dep1->addEmployees(PeopleFactory::create('Manager', 2, true, 1));
  391.  
  392. // Департамент продаж: 12×ме1, 6×ма1, 3×ан1, 2×ан2 + руководитель ма2
  393. $dep2 = new Department('Продаж');
  394. $org->addDepartment($dep2);
  395. $dep2->addEmployees(PeopleFactory::create('Manager', 1, false, 12));
  396. $dep2->addEmployees(PeopleFactory::create('Marketer', 1, false, 6));
  397. $dep2->addEmployees(PeopleFactory::create('Analyst', 1, false, 3));
  398. $dep2->addEmployees(PeopleFactory::create('Analyst', 2, false, 2));
  399. $dep2->addEmployees(PeopleFactory::create('Marketer', 2, true, 1));
  400.  
  401. // Департамент рекламы: 15×ма1, 10×ма2, 8×ме1, 2×ин1 + руководитель ма3
  402. $dep3 = new Department('Рекламы');
  403. $org->addDepartment($dep3);
  404. $dep3->addEmployees(PeopleFactory::create('Marketer', 1, false, 15));
  405. $dep3->addEmployees(PeopleFactory::create('Marketer', 2, false, 10));
  406. $dep3->addEmployees(PeopleFactory::create('Manager', 1, false, 8));
  407. $dep3->addEmployees(PeopleFactory::create('Engineer', 1, false, 2));
  408. $dep3->addEmployees(PeopleFactory::create('Marketer', 3, true, 1));
  409.  
  410. // Департамент логистики: 13×ме1, 5×ме2, 5×ин1 + руководитель ме1
  411. $dep4 = new Department('Логистики');
  412. $org->addDepartment($dep4);
  413. $dep4->addEmployees(PeopleFactory::create('Manager', 1, false, 13));
  414. $dep4->addEmployees(PeopleFactory::create('Manager', 2, false, 5));
  415. $dep4->addEmployees(PeopleFactory::create('Engineer', 1, false, 5));
  416. $dep4->addEmployees(PeopleFactory::create('Manager', 1, true, 1));
  417.  
  418.  
  419. //Dbg::cd($org);
  420.  
  421. // $testDep = new Department('Тестовый');
  422. // $testDep->addEmployees(PeopleFactory::create('Manager', 1, false, 2));
  423. // $salary = $testDep->countDepSalary();
  424.  
  425. // echo $salary;
  426. // //Dbg::cd($testDep);
  427. // foreach ($testDep->getEmployees() as $emp) {
  428. // //echo $emp->getSalary();
  429. // Dbg::cd($emp);
  430. // }
  431.  
  432. //разкоментить это если запускать на локалке
  433. // Reporter::pageHeader();
  434. // Reporter::browserReport($org);
  435. // Reporter::pageFooter();
  436.  
  437.  
  438. Reporter::ideoneReport($org);
  439.  
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       3.2803413910391
Всего             101         50837.5        1960        15725       13.121365564157