fork(3) download
  1. <?php
  2.  
  3. class Organisation //содержит методы для подсчета общих и средних параметров по конторе
  4. {
  5. public $name;
  6. public $departments = [];
  7.  
  8. public function __construct($name)
  9. {
  10. $this->name = $name;
  11. }
  12.  
  13. public function countTotalWorkers()
  14. {
  15. $totalWorkers = 0;
  16. foreach($this->departments as $unit) {
  17. $totalWorkers += $unit->countDepWorkers();
  18. }
  19. return $totalWorkers;
  20. }
  21.  
  22. public function countAveregeWorkers()
  23. {
  24. return $this->countTotalWorkers() / count($this->departments);
  25. }
  26.  
  27. public function countTotalStuff($stuff)
  28. {
  29. $companyStuff = 0;
  30. foreach ($this->departments as $dep) {
  31. $companyStuff += $dep->countDepStuff($stuff);
  32. }
  33. return $companyStuff;
  34. }
  35.  
  36. public function countAveregeStuff($stuff)
  37. {
  38. return $this->countTotalStuff($stuff) / count($this->departments);
  39. }
  40.  
  41. public function countTotalEfficiency()
  42. {
  43. $companyEfficiency = 0;
  44. foreach ($this->departments as $dep) {
  45. $companyEfficiency += $dep->countDepEfficiency();
  46. }
  47. return $companyEfficiency;
  48. }
  49.  
  50. public function countAveregeEfficiency()
  51. {
  52. return $this->countTotalEfficiency() / count($this->departments);
  53. }
  54.  
  55. }
  56.  
  57. class Department
  58. {
  59. public $name;
  60. public $workers = [];
  61.  
  62. public function __construct($name)
  63. {
  64. $this->name = $name;
  65. }
  66.  
  67. public function addWorkers($amount, $class, $rank, $leader) {
  68. for($i=1; $i<=$amount; $i++ ) {
  69. $this->workers[] = new $class($rank, $leader);
  70. }
  71. }
  72.  
  73. public function getName()
  74. {
  75. return $this->name;
  76. }
  77.  
  78. public function countDepWorkers()
  79. {
  80. return count($this->workers);
  81. }
  82.  
  83. public function countDepStuff($stuff)
  84. {
  85. $totalDepStuff = 0;
  86. foreach ($this->workers as $worker) {
  87. $totalDepStuff += $worker->$stuff;
  88. }
  89. return $totalDepStuff;
  90. }
  91.  
  92. function countDepEfficiency()
  93. {
  94. return $this->countDepStuff("salary") / $this->countDepStuff("docs");
  95. }
  96.  
  97. }
  98.  
  99. class Employee
  100. {
  101. public $salary;
  102. public $coffeUsage;
  103. public $docs;
  104. public $rank;
  105. public $leader;
  106. public $defaultSalary;
  107.  
  108. public function __construct($rank, $leader) {
  109. $this->rank = $rank;
  110. $this->leader = $leader;
  111. $this->setSalary();
  112. $this->setCoffeUsage();
  113. $this->setDocs();
  114. }
  115.  
  116. public function setSalary ()
  117. {
  118. if ($this->rank == 2) {
  119. $rankBonus = 1.25;
  120. } elseif ($this->rank == 3) {
  121. $rankBonus = 1.5;
  122. } else {
  123. $rankBonus = 1;
  124. }
  125.  
  126. if ($this->leader == true) {
  127. $leaderBonus = 1.5;
  128. } else {
  129. $leaderBonus = 1;
  130. }
  131. $this->salary = $this->defaultSalary * $rankBonus * $leaderBonus;
  132. }
  133.  
  134. public function setCoffeUsage()
  135. {
  136. if ($this->leader) {
  137. $this->coffeUsage = $this->defaultCoffeUsage * 2;
  138. } else {
  139. $this->coffeUsage = $this->defaultCoffeUsage;
  140. }
  141. }
  142.  
  143. public function setDocs ()
  144. {
  145. if ($this->leader) {
  146. $this->docs = 0;
  147. } else {
  148. $this->docs = $this->defaultDocs;
  149. }
  150. }
  151. }
  152.  
  153. class Manager extends Employee
  154. {
  155. public $defaultSalary = 500;
  156. public $defaultCoffeUsage = 20;
  157. public $defaultDocs = 200;
  158. }
  159.  
  160. class MarketGuy extends Employee
  161. {
  162. public $defaultSalary = 400;
  163. public $defaultCoffeUsage = 15;
  164. public $defaultDocs = 150;
  165. }
  166.  
  167. class Engineer extends Employee
  168. {
  169. public $defaultSalary = 200;
  170. public $defaultCoffeUsage = 5;
  171. public $defaultDocs = 50;
  172. }
  173.  
  174. class Analyst extends Employee
  175. {
  176. public $defaultSalary = 800;
  177. public $defaultCoffeUsage = 50;
  178. public $defaultDocs = 5;
  179. }
  180.  
  181.  
  182.  
  183. function padLeft ($string, $length) {
  184. $strLength = mb_strlen($string);
  185. $spases = str_repeat(" ", ($length - $strLength - 1));
  186. $string = "|" . $spases . $string;
  187. return $string;
  188. }
  189.  
  190. function padRight ($string, $length) {
  191. $strLength = mb_strlen($string);
  192. $spases = str_repeat(" ", ($length - $strLength));
  193. $string .= $spases;
  194. return $string;
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. //создаю организацию:
  203. $vector = new Organisation("Вектор");
  204.  
  205. //департаменты
  206. $purchase = new Department("Департамент закупок");
  207. //заполняю планктоном
  208. $purchase->addWorkers(9, "Manager", 1, false); //9 менеджеров 1 ранга, не лидеры
  209. $purchase->addWorkers(3, "Manager", 2, false);
  210. $purchase->addWorkers(2, "Manager", 3, false);
  211. $purchase->addWorkers(2, "MarketGuy", 1, false);
  212. $purchase->addWorkers(1, "Manager", 2, true); //1 менеджер 2 ранга, начальник
  213. $vector->departments[] = $purchase;
  214.  
  215. $sales = new Department("Департамент продаж");
  216. $sales->addWorkers(12, "Manager", 1, false);
  217. $sales->addWorkers(16, "MarketGuy", 1, false);
  218. $sales->addWorkers(3, "Analyst", 1, false);
  219. $sales->addWorkers(2, "Analyst", 2, false);
  220. $sales->addWorkers(1, "MarketGuy", 2, true);
  221. $vector->departments[] = $sales;
  222.  
  223. $advertising = new Department("Департамент рекламы");
  224. $advertising->addWorkers(15, "MarketGuy", 1, false);
  225. $advertising->addWorkers(10, "MarketGuy", 2, false);
  226. $advertising->addWorkers(8, "Manager", 1, false);
  227. $advertising->addWorkers(2, "Engineer", 1, false);
  228. $advertising->addWorkers(1, "MarketGuy", 3, true);
  229. $vector->departments[] = $advertising;
  230.  
  231. $logistics = new Department("Департамент логистики");
  232. $logistics->addWorkers(13, "Manager", 1, false);
  233. $logistics->addWorkers(5, "Manager", 2, false);
  234. $logistics->addWorkers(5, "Engineer", 1, false);
  235. $logistics->addWorkers(1, "Manager", 1, true);
  236. $vector->departments[] = $logistics;
  237.  
  238. //var_dump($departments);
  239.  
  240. $col1 = 22;
  241. $col2 = 13;
  242. $col3 = 11;
  243.  
  244.  
  245. echo padRight("Департамент", $col1) .
  246. padLeft("сотрудников", $col2) .
  247. padLeft("тугрики", $col3) .
  248. padLeft("кофе", $col3) .
  249. padLeft("документы", $col2) .
  250. padLeft("тугр./документы", $col1) .
  251. "\n";
  252.  
  253. echo "--------------------------------------------------------------------------------\n";
  254.  
  255. foreach ($vector->departments as $dep) {
  256. echo padRight($dep->getName(), $col1) .
  257. padLeft($dep->countDepWorkers(), $col2) .
  258. padLeft($dep->countDepStuff("salary"), $col3) .
  259. padLeft($dep->countDepStuff("coffeUsage"), $col3) .
  260. padLeft($dep->countDepStuff("docs"), $col2) .
  261. padLeft($dep->countDepEfficiency(), $col1) .
  262. "\n";
  263. }
  264.  
  265. echo "--------------------------------------------------------------------------------\n";
  266.  
  267. echo padRight("Среднее", $col1) .
  268. padLeft($vector->countAveregeWorkers(), $col2) .
  269. padLeft($vector->countAveregeStuff("salary"), $col3) .
  270. padLeft($vector->countAveregeStuff("coffeUsage"), $col3) .
  271. padLeft($vector->countAveregeStuff("docs"), $col2) .
  272. padLeft($vector->countAveregeEfficiency(), $col1) .
  273. "\n" ;
  274.  
  275. echo padRight("Всего", $col1) .
  276. padLeft($vector->countTotalWorkers(), $col2) .
  277. padLeft($vector->countTotalStuff("salary"), $col3) .
  278. padLeft($vector->countTotalStuff("coffeUsage"), $col3) .
  279. padLeft($vector->countTotalStuff("docs"), $col2) .
  280. padLeft($vector->countTotalEfficiency(), $col1) .
  281. "\n";
  282.  
  283.  
  284.  
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
Департамент           | сотрудников|   тугрики|      кофе|   документы|      тугр./документы
--------------------------------------------------------------------------------
Департамент закупок   |          17|    9612.5|       350|        3100|      3.1008064516129
Департамент продаж    |          34|     17550|       760|        4825|      3.6373056994819
Департамент рекламы   |          36|     16300|       575|        5450|      2.9908256880734
Департамент логистики |          24|     11375|       425|        3850|      2.9545454545455
--------------------------------------------------------------------------------
Среднее               |       27.75| 13709.375|     527.5|     4306.25|      3.1708708234284
Всего                 |         111|   54837.5|      2110|       17225|      12.683483293714