fork 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 countTotalCoffe()
  28. {
  29. $companyCoffe = 0;
  30. foreach ($this->departments as $dep) {
  31. $companyCoffe += $dep->countDepCoffe();
  32. }
  33. return $companyCoffe;
  34. }
  35.  
  36. public function countTotalSalary()
  37. {
  38. $companySalary = 0;
  39. foreach ($this->departments as $dep) {
  40. $companySalary += $dep->countDepSalary();
  41. }
  42. return $companySalary;
  43. }
  44.  
  45. public function countTotalDocs()
  46. {
  47. $companyDocs = 0;
  48. foreach ($this->departments as $dep) {
  49. $companyDocs += $dep->countDepDocs();
  50. }
  51. return $companyDocs;
  52. }
  53.  
  54.  
  55. public function countAveregeCoffe()
  56. {
  57. return $this->countTotalCoffe() / count($this->departments);
  58. }
  59.  
  60. public function countAveregeSalary()
  61. {
  62. return $this->countTotalSalary() / count($this->departments);
  63. }
  64.  
  65. public function countAveregeDocs()
  66. {
  67. return $this->countTotalDocs() / count($this->departments);
  68. }
  69.  
  70. public function countTotalEfficiency()
  71. {
  72. $companyEfficiency = 0;
  73. foreach ($this->departments as $dep) {
  74. $companyEfficiency += $dep->countDepEfficiency();
  75. }
  76. return $companyEfficiency;
  77. }
  78.  
  79. public function countAveregeEfficiency()
  80. {
  81. return $this->countTotalEfficiency() / count($this->departments);
  82. }
  83.  
  84. public function makeReport()
  85. {
  86.  
  87. $col1 = 22;
  88. $col2 = 13;
  89. $col3 = 11;
  90.  
  91. function padLeft ($string, $length) {
  92. $strLength = mb_strlen($string);
  93. $num = $length - $strLength - 1;
  94. if ($num < 0) {
  95. $num = 0;
  96. }
  97. $spases = str_repeat(" ", $num);
  98. $string = "|" . $spases . $string;
  99. return $string;
  100. }
  101.  
  102. function padRight ($string, $length) {
  103. $strLength = mb_strlen($string);
  104. $num = $length - $strLength;
  105. if ($num < 0) {
  106. $num = 0;
  107. }
  108. $spases = str_repeat(" ", $num);
  109. $string .= $spases;
  110. return $string;
  111. }
  112.  
  113.  
  114. echo padRight("Департамент", $col1) .
  115. padLeft("сотрудников", $col2) .
  116. padLeft("тугрики", $col3) .
  117. padLeft("кофе", $col3) .
  118. padLeft("документы", $col2) .
  119. padLeft("тугр./документы", $col1) .
  120. "\n";
  121.  
  122. echo "--------------------------------------------------------------------------------\n";
  123.  
  124. foreach ($this->departments as $dep) {
  125. echo padRight($dep->getName(), $col1) .
  126. padLeft($dep->countDepWorkers(), $col2) .
  127. padLeft($dep->countDepSalary(), $col3) .
  128. padLeft($dep->countDepCoffe(), $col3) .
  129. padLeft($dep->countDepDocs(), $col2) .
  130. padLeft($dep->countDepEfficiency(), $col1) .
  131. "\n";
  132. }
  133.  
  134. echo "--------------------------------------------------------------------------------\n";
  135.  
  136. echo padRight("Среднее", $col1) .
  137. padLeft($this->countAveregeWorkers(), $col2) .
  138. padLeft($this->countAveregeSalary(), $col3) .
  139. padLeft($this->countAveregeCoffe(), $col3) .
  140. padLeft($this->countAveregeDocs(), $col2) .
  141. padLeft($this->countAveregeEfficiency(), $col1) .
  142. "\n" ;
  143.  
  144. echo padRight("Всего", $col1) .
  145. padLeft($this->countTotalWorkers(), $col2) .
  146. padLeft($this->countTotalSalary(), $col3) .
  147. padLeft($this->countTotalCoffe(), $col3) .
  148. padLeft($this->countTotalDocs(), $col2) .
  149. padLeft($this->countTotalEfficiency(), $col1) .
  150. "\n";
  151.  
  152.  
  153. }
  154.  
  155. }
  156.  
  157. class Department
  158. {
  159. public $name;
  160. public $workers = [];
  161.  
  162. public function __construct($name)
  163. {
  164. $this->name = $name;
  165. }
  166.  
  167. public function addWorkers($amount, $class, $rank, $leader) {
  168. for($i=1; $i<=$amount; $i++ ) {
  169. $this->workers[] = new $class($rank, $leader);
  170. }
  171. }
  172.  
  173. public function getName()
  174. {
  175. return $this->name;
  176. }
  177.  
  178. public function countDepWorkers()
  179. {
  180. return count($this->workers);
  181. }
  182.  
  183. public function countDepCoffe()
  184. {
  185. $totalDepCoffe = 0;
  186. foreach ($this->workers as $worker) {
  187. $totalDepCoffe += $worker->coffeUsage;
  188. }
  189. return $totalDepCoffe;
  190. }
  191.  
  192. public function countDepSalary()
  193. {
  194. $totalDepSalary = 0;
  195. foreach ($this->workers as $worker) {
  196. $totalDepSalary += $worker->salary;
  197. }
  198. return $totalDepSalary;
  199. }
  200.  
  201. public function countDepDocs()
  202. {
  203. $totalDepDocs = 0;
  204. foreach ($this->workers as $worker) {
  205. $totalDepDocs += $worker->docs;
  206. }
  207. return $totalDepDocs;
  208. }
  209.  
  210. function countDepEfficiency()
  211. {
  212. return $this->countDepSalary() / $this->countDepDocs();
  213. }
  214.  
  215. }
  216.  
  217. class Employee
  218. {
  219. public $salary;
  220. public $coffeUsage;
  221. public $docs;
  222. public $rank;
  223. public $leader;
  224. public $defaultSalary;
  225.  
  226. public function __construct($rank, $leader) {
  227. $this->rank = $rank;
  228. $this->leader = $leader;
  229. $this->setSalary();
  230. $this->setCoffeUsage();
  231. $this->setDocs();
  232. }
  233.  
  234. public function setSalary ()
  235. {
  236. if ($this->rank == 2) {
  237. $rankBonus = 1.25;
  238. } elseif ($this->rank == 3) {
  239. $rankBonus = 1.5;
  240. } else {
  241. $rankBonus = 1;
  242. }
  243.  
  244. if ($this->leader == true) {
  245. $leaderBonus = 1.5;
  246. } else {
  247. $leaderBonus = 1;
  248. }
  249. $this->salary = $this->defaultSalary * $rankBonus * $leaderBonus;
  250. }
  251.  
  252. public function setCoffeUsage()
  253. {
  254. if ($this->leader) {
  255. $this->coffeUsage = $this->defaultCoffeUsage * 2;
  256. } else {
  257. $this->coffeUsage = $this->defaultCoffeUsage;
  258. }
  259. }
  260.  
  261. public function setDocs ()
  262. {
  263. if ($this->leader) {
  264. $this->docs = 0;
  265. } else {
  266. $this->docs = $this->defaultDocs;
  267. }
  268. }
  269. }
  270.  
  271. class Manager extends Employee
  272. {
  273. public $defaultSalary = 500;
  274. public $defaultCoffeUsage = 20;
  275. public $defaultDocs = 200;
  276. }
  277.  
  278. class MarketGuy extends Employee
  279. {
  280. public $defaultSalary = 400;
  281. public $defaultCoffeUsage = 15;
  282. public $defaultDocs = 150;
  283. }
  284.  
  285. class Engineer extends Employee
  286. {
  287. public $defaultSalary = 200;
  288. public $defaultCoffeUsage = 5;
  289. public $defaultDocs = 50;
  290. }
  291.  
  292. class Analyst extends Employee
  293. {
  294. public $defaultSalary = 800;
  295. public $defaultCoffeUsage = 50;
  296. public $defaultDocs = 5;
  297. }
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307. //создаю организацию:
  308. $vector = new Organisation("Вектор");
  309.  
  310. //департаменты
  311. $purchase = new Department("Департамент закупок");
  312.  
  313. // Попросили отдел кадров найти 3 менеджеров
  314. $ivan = new Manager(1, false);
  315. $boris = new Manager(1, false);
  316. $director = new Manager(2, true);
  317.  
  318. // А как их добавить в департамент?
  319. // интуитивно мне кажется что тут должно быть что-то вроде
  320. // $department->addEmployee($ivan);
  321. // но такого метода нет
  322. // печалька
  323.  
  324. $vector->makeReport();
  325.  
Success #stdin #stdout #stderr 0.01s 24448KB
stdin
Standard input is empty
stdout
Департамент           | сотрудников|   тугрики|      кофе|   документы|      тугр./документы
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Среднее               |            |          |          |            |                     
Всего                 |           0|         0|         0|           0|                    0
stderr
PHP Warning:  Division by zero in /home/jnFTU0/prog.php on line 25
PHP Warning:  Division by zero in /home/jnFTU0/prog.php on line 63
PHP Warning:  Division by zero in /home/jnFTU0/prog.php on line 58
PHP Warning:  Division by zero in /home/jnFTU0/prog.php on line 68
PHP Warning:  Division by zero in /home/jnFTU0/prog.php on line 82