fork download
  1. <?php
  2. header("Content-Type: text/plain; charset=utf-8");
  3.  
  4. class Organisation {
  5. private $name;
  6. private $departments = array();
  7.  
  8. public function __construct($name) {
  9. $this->name = $name;
  10. }
  11.  
  12. public function getOrganisationName() {
  13. return $this->name;
  14. }
  15.  
  16. public function getDepartments() {
  17. return $this->departments;
  18. }
  19.  
  20. public function addDepartment(Department $department) {
  21. $this->departments[] = $department;
  22. }
  23.  
  24. public function makeReport() { //хз куда если не сюда сложить подобный метод
  25. foreach ($this->departments as $dep) {
  26. echo $dep->getDepName() . " " . $dep->getTotalDepWorkers() . " " . $dep->getTotalDepStuff('Salary') . " ";
  27. echo $dep->getTotalDepStuff('Coffe') . " " . $dep->getTotalDepStuff('Papers') . " " . $dep->getTotalDepEffectiveness();
  28. echo "\n";
  29. }
  30. }
  31. }
  32.  
  33. class Department {
  34. private $name;
  35. private $workers = array();
  36.  
  37. public function __construct($name) {
  38. $this->name = $name;
  39. }
  40.  
  41. public function getDepName() {
  42. return $this->name;
  43. }
  44.  
  45. public function addWorker(Worker $worker) {
  46. $this->workers[] = $worker;
  47. }
  48.  
  49. public function addWorkers(array $workers) {
  50. foreach ($workers as $worker) {
  51. if (is_subclass_of($worker, 'Worker')) {
  52. $this->addWorker($worker);
  53. }
  54. }
  55. }
  56.  
  57. public function getWorkers() {
  58. return $this->workers;
  59. }
  60.  
  61. public function getTotalDepWorkers() {
  62. return count($this->workers);
  63. }
  64.  
  65. public function getTotalDepStuff ($something) { //Salary, Coffe, Papers В процессе написания оказались 3 одинаковых функции, объединил их
  66. $result = 0;
  67. $something = 'getWorker' . mb_convert_case($something, MB_CASE_TITLE);
  68. foreach ($this->workers as $worker) {
  69. $result += $worker->$something();
  70. }
  71. return $result;
  72. }
  73.  
  74. public function getTotalDepEffectiveness() {
  75. if ($this->getTotalDepStuff('Papers') == 0) { //если есть пустой департамент, то ловим деление на 0, как почитаю про исключения, то заменю
  76. return "N/A";
  77. }
  78. return round($this->getTotalDepStuff('Salary') / $this->getTotalDepStuff('Papers'), 2);
  79. }
  80.  
  81. }
  82.  
  83. abstract class Worker {
  84. //private $salary; //и без них всё работает, но если их разкоментить, то будут в наследниках мусорные пустые поля от родителя
  85. //private $coffe;
  86. //private $papers;
  87. private $rang;
  88. private $leader;
  89.  
  90. public function __construct($rang = 1, $leader = false) {
  91. $this->rang = $rang;
  92. $this->leader = $leader;
  93. }
  94.  
  95. public function getWorkerSalary() {
  96. if ($this->rang == 3) {
  97. $result = $this->salary * 1.5;
  98. } elseif ($this->rang == 2) {
  99. $result = $this->salary * 1.25;
  100. } else {
  101. $result = $this->salary;
  102. }
  103. if ($this->leader == true) {
  104. $result *= 1.5;
  105. }
  106. return $result;
  107. }
  108.  
  109. public function getWorkerCoffe() {
  110. if ($this->leader == true) {
  111. return $this->coffe * 2;
  112. } else {
  113. return $this->coffe;
  114. }
  115. }
  116.  
  117. public function getWorkerPapers() {
  118. if ($this->leader == true) {
  119. return 0;
  120. } else {
  121. return $this->papers;
  122. }
  123. }
  124. }
  125. class Manager extends Worker{
  126. protected $salary = 500; //private нельзя, тогда у родителя нет доступа к свойствам.
  127. protected $coffe = 20;
  128. protected $papers = 200;
  129. }
  130.  
  131. class Marketer extends Worker {
  132. protected $salary = 400;
  133. protected $coffe = 15;
  134. protected $papers = 150;
  135. }
  136.  
  137. class Engineer extends Worker {
  138. protected $salary = 200;
  139. protected $coffe = 5;
  140. protected $papers = 50;
  141. }
  142.  
  143. class Analyst extends Worker {
  144. protected $salary = 800;
  145. protected $coffe = 50;
  146. protected $papers = 5;
  147. }
  148.  
  149. class WorkersFactory {
  150. static public function createWorkers($class, $amount, $rang, $leader){
  151. $workers = [];
  152. for ($i = 0; $i < $amount; $i++) {
  153. $workers[] = new $class($rang, $leader);
  154. }
  155. return $workers;
  156. }
  157.  
  158. }
  159.  
  160. $vector = new Organisation('Вектор');
  161.  
  162. $buyDep = new Department('Закупок');
  163. $vector->addDepartment($buyDep);
  164. $buyDep->addWorkers(WorkersFactory::createWorkers('Manager', 9, 1, false));
  165. $buyDep->addWorkers(WorkersFactory::createWorkers('Manager', 3, 2, false));
  166. $buyDep->addWorkers(WorkersFactory::createWorkers('Manager', 2, 3, false));
  167. $buyDep->addWorkers(WorkersFactory::createWorkers('Marketer', 2, 1, false));
  168. $buyDep->addWorkers(WorkersFactory::createWorkers('Manager', 1, 2, true));
  169.  
  170. $saleDep = new Department('Продаж');
  171. $vector->addDepartment($saleDep);
  172. $saleDep->addWorkers(WorkersFactory::createWorkers('Manager', 12, 1, false));
  173. $saleDep->addWorkers(WorkersFactory::createWorkers('Marketer', 6, 1, false));
  174. $saleDep->addWorkers(WorkersFactory::createWorkers('Analyst', 3, 1, false));
  175. $saleDep->addWorkers(WorkersFactory::createWorkers('Analyst', 2, 2, false));
  176. $saleDep->addWorkers(WorkersFactory::createWorkers('Marketer', 1, 2, true));
  177.  
  178. $advDep = new Department('Рекламы');
  179. $vector->addDepartment($advDep);
  180. $advDep->addWorkers(WorkersFactory::createWorkers('Marketer', 15, 1, false));
  181. $advDep->addWorkers(WorkersFactory::createWorkers('Marketer', 10, 2, false));
  182. $advDep->addWorkers(WorkersFactory::createWorkers('Manager', 8, 1, false));
  183. $advDep->addWorkers(WorkersFactory::createWorkers('Engineer', 2, 1, false));
  184. $advDep->addWorkers(WorkersFactory::createWorkers('Marketer', 1, 3, true));
  185.  
  186. $logDep = new Department('Логистики');
  187. $vector->addDepartment($logDep);
  188. $logDep->addWorkers(WorkersFactory::createWorkers('Manager', 13, 1, false));
  189. $logDep->addWorkers(WorkersFactory::createWorkers('Manager', 5, 2, false));
  190. $logDep->addWorkers(WorkersFactory::createWorkers('Engineer', 5, 1, false));
  191. $logDep->addWorkers(WorkersFactory::createWorkers('Manager', 1, 1, true));
  192.  
  193. $vector->makeReport();
  194.  
  195. //var_dump($vector);
  196.  
  197. //var_dump($dep1);
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
Закупок   17   9612.5   350   3100   3.1
Продаж   24   13550   610   3325   4.08
Рекламы   36   16300   575   5450   2.99
Логистики   24   11375   425   3850   2.95