fork download
  1. <?php
  2.  
  3. class Organisation //содержит методы для подсчета общих и средних параметров по конторе
  4. {
  5. protected $name;
  6. public $departments = [];
  7.  
  8. public function __construct($name)
  9. {
  10. $this->name = $name;
  11. }
  12.  
  13. public function setAnticrisysProgram3() {
  14. foreach ($this->departments as $dep) {
  15. $target = $dep->countManagers12();
  16. $target = ceil($target * 0.5);
  17. var_dump($target);
  18.  
  19. }
  20. }
  21.  
  22. protected function countTotalWorkers()
  23. {
  24. $totalWorkers = 0;
  25. foreach($this->departments as $unit) {
  26. $totalWorkers += $unit->countDepWorkers();
  27. }
  28. return $totalWorkers;
  29. }
  30.  
  31. protected function countAveregeWorkers()
  32. {
  33. return $this->countTotalWorkers() / count($this->departments);
  34. }
  35.  
  36. protected function countTotalCoffe()
  37. {
  38. $companyCoffe = 0;
  39. foreach ($this->departments as $dep) {
  40. $companyCoffe += $dep->countDepCoffe();
  41. }
  42. return $companyCoffe;
  43. }
  44.  
  45. protected function countTotalSalary()
  46. {
  47. $companySalary = 0;
  48. foreach ($this->departments as $dep) {
  49. $companySalary += $dep->countDepSalary();
  50. }
  51. return $companySalary;
  52. }
  53.  
  54. protected function countTotalDocs()
  55. {
  56. $companyDocs = 0;
  57. foreach ($this->departments as $dep) {
  58. $companyDocs += $dep->countDepDocs();
  59. }
  60. return $companyDocs;
  61. }
  62.  
  63.  
  64. protected function countAveregeCoffe()
  65. {
  66. return $this->countTotalCoffe() / count($this->departments);
  67. }
  68.  
  69. protected function countAveregeSalary()
  70. {
  71. return $this->countTotalSalary() / count($this->departments);
  72. }
  73.  
  74. protected function countAveregeDocs()
  75. {
  76. return $this->countTotalDocs() / count($this->departments);
  77. }
  78.  
  79. protected function countTotalEfficiency()
  80. {
  81. $companyEfficiency = 0;
  82. foreach ($this->departments as $dep) {
  83. $companyEfficiency += $dep->countDepEfficiency();
  84. }
  85. return $companyEfficiency;
  86. }
  87.  
  88. protected function countAveregeEfficiency()
  89. {
  90. return $this->countTotalEfficiency() / count($this->departments);
  91. }
  92.  
  93. public function makeReport()
  94. {
  95.  
  96. $col1 = 22;
  97. $col2 = 13;
  98. $col3 = 11;
  99.  
  100. function padLeft ($string, $length) {
  101. $strLength = mb_strlen($string);
  102. $num = $length - $strLength - 1;
  103. if ($num < 0) {
  104. $num = 0;
  105. }
  106. $spases = str_repeat(" ", $num);
  107. $string = "|" . $spases . $string;
  108. return $string;
  109. }
  110.  
  111. function padRight ($string, $length) {
  112. $strLength = mb_strlen($string);
  113. $num = $length - $strLength;
  114. if ($num < 0) {
  115. $num = 0;
  116. }
  117. $spases = str_repeat(" ", $num);
  118. $string .= $spases;
  119. return $string;
  120. }
  121.  
  122.  
  123. echo padRight("Департамент", $col1) .
  124. padLeft("сотрудников", $col2) .
  125. padLeft("тугрики", $col3) .
  126. padLeft("кофе", $col3) .
  127. padLeft("документы", $col2) .
  128. padLeft("тугр./документы", $col1) .
  129. "\n";
  130.  
  131. echo "--------------------------------------------------------------------------------\n";
  132.  
  133. foreach ($this->departments as $dep) {
  134. echo padRight($dep->getName(), $col1) .
  135. padLeft($dep->countDepWorkers(), $col2) .
  136. padLeft($dep->countDepSalary(), $col3) .
  137. padLeft($dep->countDepCoffe(), $col3) .
  138. padLeft($dep->countDepDocs(), $col2) .
  139. padLeft($dep->countDepEfficiency(), $col1) .
  140. "\n";
  141. }
  142.  
  143. echo "--------------------------------------------------------------------------------\n";
  144.  
  145. echo padRight("Среднее", $col1) .
  146. padLeft($this->countAveregeWorkers(), $col2) .
  147. padLeft($this->countAveregeSalary(), $col3) .
  148. padLeft($this->countAveregeCoffe(), $col3) .
  149. padLeft($this->countAveregeDocs(), $col2) .
  150. padLeft($this->countAveregeEfficiency(), $col1) .
  151. "\n" ;
  152.  
  153. echo padRight("Всего", $col1) .
  154. padLeft($this->countTotalWorkers(), $col2) .
  155. padLeft($this->countTotalSalary(), $col3) .
  156. padLeft($this->countTotalCoffe(), $col3) .
  157. padLeft($this->countTotalDocs(), $col2) .
  158. padLeft($this->countTotalEfficiency(), $col1) .
  159. "\n";
  160.  
  161.  
  162. }
  163.  
  164. }
  165.  
  166. class Department
  167. {
  168. protected $name;
  169. protected $workers = [];
  170.  
  171. public function __construct($name)
  172. {
  173. $this->name = $name;
  174. }
  175.  
  176. public function countManagers12() {
  177. $managers12 = 0;
  178. foreach ($this->workers as $name => $worker) {
  179. if (get_class($worker) == "Manager") {
  180. if (($worker->rank == 1) or ($worker->rank == 2)) {
  181. $managers12++;
  182. }
  183. }
  184.  
  185. }
  186. return $managers12;
  187. }
  188.  
  189. public function addEmployee($employee)
  190. {
  191. if (is_array($employee)) {
  192. $this->workers = array_merge($this->workers, $employee);
  193. } else {
  194. $this->workers[] = $employee;
  195. }
  196. }
  197.  
  198. public function getName()
  199. {
  200. return $this->name;
  201. }
  202.  
  203. public function countDepWorkers()
  204. {
  205. return count($this->workers);
  206. }
  207.  
  208. public function countDepCoffe()
  209. {
  210. $totalDepCoffe = 0;
  211. foreach ($this->workers as $worker) {
  212. $totalDepCoffe += $worker->getCoffeUsage();
  213. }
  214. return $totalDepCoffe;
  215. }
  216.  
  217. public function countDepSalary()
  218. {
  219. $totalDepSalary = 0;
  220. foreach ($this->workers as $worker) {
  221. $totalDepSalary += $worker->getSalary();
  222. }
  223. return $totalDepSalary;
  224. }
  225.  
  226. public function countDepDocs()
  227. {
  228. $totalDepDocs = 0;
  229. foreach ($this->workers as $worker) {
  230. $totalDepDocs += $worker->getDocs();
  231. }
  232. return $totalDepDocs;
  233. }
  234.  
  235. function countDepEfficiency()
  236. {
  237. return $this->countDepSalary() / $this->countDepDocs();
  238. }
  239.  
  240. }
  241.  
  242. class Employee
  243. {
  244. public $rank;
  245. protected $leader;
  246. protected $defaultSalary;
  247. protected $defaultCoffeUsage;
  248. protected $defaultDocs;
  249.  
  250. public function __construct($rank, $leader) {
  251. $this->rank = $rank;
  252. $this->leader = $leader;
  253. }
  254.  
  255. public function getSalary ()
  256. {
  257. if ($this->rank == 2) {
  258. $rankBonus = 1.25;
  259. } elseif ($this->rank == 3) {
  260. $rankBonus = 1.5;
  261. } else {
  262. $rankBonus = 1;
  263. }
  264.  
  265. if ($this->leader == true) {
  266. $leaderBonus = 1.5;
  267. } else {
  268. $leaderBonus = 1;
  269. }
  270. return $this->defaultSalary * $rankBonus * $leaderBonus;
  271. }
  272.  
  273. public function getCoffeUsage()
  274. {
  275. if ($this->leader) {
  276. return $this->defaultCoffeUsage * 2;
  277. } else {
  278. return $this->defaultCoffeUsage;
  279. }
  280. }
  281.  
  282. public function getDocs()
  283. {
  284. if ($this->leader) {
  285. return 0;
  286. } else {
  287. return $this->defaultDocs;
  288. }
  289. }
  290. }
  291.  
  292. class Manager extends Employee
  293. {
  294. protected $defaultSalary = 500;
  295. protected $defaultCoffeUsage = 20;
  296. protected $defaultDocs = 200;
  297. }
  298.  
  299. class MarketGuy extends Employee
  300. {
  301. protected $defaultSalary = 400;
  302. protected $defaultCoffeUsage = 15;
  303. protected $defaultDocs = 150;
  304. }
  305.  
  306. class Engineer extends Employee
  307. {
  308. protected $defaultSalary = 200;
  309. protected $defaultCoffeUsage = 5;
  310. protected $defaultDocs = 50;
  311. }
  312.  
  313. class Analyst extends Employee
  314. {
  315. protected $defaultSalary = 800;
  316. protected $defaultCoffeUsage = 50;
  317. protected $defaultDocs = 5;
  318. }
  319.  
  320.  
  321. class Factory
  322. {
  323. static function createEmployee($amount, $class, $rank, $leader) {
  324. $employees = [];
  325. for($i=1; $i<=$amount; $i++ ) {
  326. $employees[] = new $class($rank, $leader);
  327. }
  328. return $employees;
  329. }
  330.  
  331. }
  332.  
  333. //создаю организацию:
  334. $vector = new Organisation("Вектор");
  335.  
  336. //департаменты
  337. $purchase = new Department("Департамент закупок");
  338. //заполняю планктоном
  339. $purchase->addEmployee(Factory::createEmployee(9, "Manager", 1, false));
  340. $purchase->addEmployee(Factory::createEmployee(3, "Manager", 2, false));
  341. $purchase->addEmployee(Factory::createEmployee(2, "Manager", 3, false));
  342. $purchase->addEmployee(Factory::createEmployee(2, "MarketGuy", 1, false));
  343. $purchase->addEmployee(Factory::createEmployee(1, "Manager", 2, true));
  344. $vector->departments[] = $purchase; //добавляю созданный департамент в организацию
  345.  
  346. $sales = new Department("Департамент продаж");
  347. $sales->addEmployee(Factory::createEmployee(12, "Manager", 1, false));
  348. $sales->addEmployee(Factory::createEmployee(16, "MarketGuy", 1, false));
  349. $sales->addEmployee(Factory::createEmployee(3, "Analyst", 1, false));
  350. $sales->addEmployee(Factory::createEmployee(2, "Analyst", 2, false));
  351. $sales->addEmployee(Factory::createEmployee(1, "MarketGuy", 2, true));
  352. $vector->departments[] = $sales;
  353.  
  354. $advertising = new Department("Департамент рекламы");
  355. $advertising->addEmployee(Factory::createEmployee(15, "MarketGuy", 1, false));
  356. $advertising->addEmployee(Factory::createEmployee(10, "MarketGuy", 2, false));
  357. $advertising->addEmployee(Factory::createEmployee(8, "Manager", 1, false));
  358. $advertising->addEmployee(Factory::createEmployee(2, "Engineer", 1, false));
  359. $advertising->addEmployee(Factory::createEmployee(1, "MarketGuy", 3, true));
  360. $vector->departments[] = $advertising;
  361.  
  362. $logistics = new Department("Департамент логистики");
  363. $logistics->addEmployee(Factory::createEmployee(13, "Manager", 1, false));
  364. $logistics->addEmployee(Factory::createEmployee(5, "Manager", 2, false));
  365. $logistics->addEmployee(Factory::createEmployee(5, "Engineer", 1, false));
  366. $logistics->addEmployee(Factory::createEmployee(1, "Manager", 1, true));
  367. $vector->departments[] = $logistics;
  368.  
  369.  
  370. $vector->makeReport();
  371. $vector->makeReport();
  372. $vector->makeReport();
  373.  
  374. // $vector->setAnticrisysProgram3();
Runtime error #stdin #stdout #stderr 0.03s 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
stderr
PHP Fatal error:  Cannot redeclare padLeft() (previously declared in /home/FirMq9/prog.php:101) in /home/FirMq9/prog.php on line 101