fork(1) download
  1. <?php
  2. class Company
  3. {
  4. private $departaments = array();
  5.  
  6. public function addDepartament($departament)
  7. {
  8. $this->departaments [] = $departament;
  9. }
  10.  
  11. public function getDepartaments()
  12. {
  13. return $this->departaments;
  14. }
  15.  
  16. public function getCountWorkers()
  17. {
  18. $count_workers = 0;
  19. foreach ($this->departaments as $departament) {
  20. $count_workers += $departament->getSumWorkers();
  21. }
  22. return $count_workers;
  23. }
  24.  
  25. public function getAverageWorkers()
  26. {
  27. return $this->getCountWorkers()/count($this->departaments);
  28. }
  29.  
  30. public function getCountSalary()
  31. {
  32. $count_salary = 0;
  33. foreach ($this->departaments as $departament) {
  34. $count_salary += $departament->getSumSalary();
  35. }
  36. return $count_salary;
  37. }
  38.  
  39. public function getAverageSalary()
  40. {
  41. return $this->getCountSalary()/count($this->departaments);
  42. }
  43.  
  44. public function getCountCoffee()
  45. {
  46. $count_coffee = 0;
  47. foreach ($this->departaments as $departament) {
  48. $count_coffee += $departament->getSumCoffee();
  49. }
  50. return $count_coffee;
  51. }
  52.  
  53. public function getAverageCoffee()
  54. {
  55. return $this->getCountCoffee()/count($this->departaments);
  56. }
  57.  
  58. public function getCountPages()
  59. {
  60. $count_pages = 0;
  61. foreach ($this->departaments as $departament) {
  62. $count_pages += $departament->getSumPages();
  63. }
  64. return $count_pages;
  65. }
  66.  
  67. public function getAveragePages()
  68. {
  69. return $this->getCountPages()/count($this->departaments);
  70. }
  71.  
  72. public function getCountSalaryForPage()
  73. {
  74. $count_salary_for_page = 0;
  75. foreach ($this->departaments as $departament) {
  76. $count_salary_for_page += $departament->getSalaryForPage();
  77. }
  78. return $count_salary_for_page;
  79. }
  80.  
  81. public function getAverageSalaryForPage()
  82. {
  83. return $this->getCountSalaryForPage()/count($this->departaments);
  84. }
  85.  
  86. }
  87.  
  88.  
  89. class Departament
  90. {
  91. private $departament_name;
  92. private $workers = array();
  93.  
  94. public function __construct($name)
  95. {
  96. $this->setDepartamentName($name);
  97. }
  98.  
  99. public function getDepartamentName()
  100. {
  101. return $this->departament_name;
  102. }
  103.  
  104. public function getWorkers()
  105. {
  106. return $this->workers;
  107. }
  108. public function setDepartamentName($name)
  109. {
  110. $this->departament_name = $name;
  111. }
  112.  
  113. public function addWorker($worker)
  114. {
  115. $this->workers[] = $worker;
  116. }
  117.  
  118. public function addWorkers($workers)
  119. {
  120. foreach ($workers as $worker) {
  121. $this->workers[] = $worker;
  122. }
  123. }
  124. public function getSumWorkers()
  125. {
  126. return count($this->workers);
  127. echo count($this->workers->salary);
  128. }
  129.  
  130. public function getSumSalary()
  131. {
  132. $sum_salary = 0;
  133. foreach ($this->workers as $worker) {
  134. $sum_salary = $sum_salary+$worker->getSalary($worker);
  135. }
  136. return $sum_salary;
  137. }
  138.  
  139. public function getSumCoffee()
  140. {
  141. $sum_coffee = 0;
  142. foreach ($this->workers as $worker) {
  143. $sum_coffee += $worker->getCoffee($worker);
  144. }
  145. return $sum_coffee;
  146. }
  147.  
  148. public function getSumPages()
  149. {
  150. $sum_pages = 0;
  151. foreach ($this->workers as $worker) {
  152. $sum_pages += $worker->getPages($worker);
  153. }
  154. return $sum_pages;
  155. }
  156.  
  157. public function getSalaryForPage()
  158. {
  159. $salary = $this->getSumSalary();
  160. $pages = $this->getSumPages();
  161. return round($salary/$pages, 3);
  162. }
  163.  
  164. public function deleteWorker($number)
  165. {
  166. unset($this->workers[$number]);
  167. }
  168.  
  169. public function getHeadWorker()
  170. {
  171. foreach ($this->workers as $worker) {
  172. if ($worker->getIsHead() == true) {
  173. return $worker;
  174. }
  175. }
  176. }
  177. }
  178.  
  179.  
  180. abstract class Worker
  181. {
  182. private $rank;
  183. private $proffesion;
  184. private $is_Head;
  185. private $salary;
  186. private $consumed_coffee;
  187. private $made_pages;
  188.  
  189. public function __construct($rank, $proffesion, $is_Head, $salary, $consumed_coffe, $made_pages)
  190. {
  191. $this->setRank($rank);
  192. $this->setProffesion($proffesion);
  193. $this->setIsHead($is_Head);
  194. $this->setSalary($salary);
  195. $this->setConsumedCoffee($consumed_coffe);
  196. $this->setMadePages($made_pages);
  197. }
  198.  
  199. public function setRank($rank)
  200. {
  201. $this->rank = $rank;
  202. }
  203.  
  204. public function setProffesion($proffesion)
  205. {
  206. $this->proffesion = $proffesion;
  207. }
  208.  
  209. public function setIsHead($is_Head)
  210. {
  211. $this->is_Head = $is_Head;
  212. }
  213.  
  214. public function setSalary($salary)
  215. {
  216. $this->salary = $salary;
  217. }
  218.  
  219. public function setConsumedCoffee($consumed_coffee)
  220. {
  221. $this->consumed_coffee = $consumed_coffee;
  222. }
  223.  
  224. public function setMadePages($made_pages)
  225. {
  226. $this->made_pages = $made_pages;
  227. }
  228.  
  229. public function getSalary($worker)
  230. {
  231. $rank = $this->rank;
  232. if ($rank == 1) {
  233. $coef_salary = 1;
  234. }elseif ($rank == 2) {
  235. $coef_salary = 1.25;
  236. }elseif ($rank == 3) {
  237. $coef_salary = 1.5;
  238. }
  239. if ($this->is_Head == true) {
  240. $coef_head = 1.5;
  241. }else{
  242. $coef_head = 1;
  243. }
  244. return $this->salary*$coef_salary*$coef_head;
  245. }
  246.  
  247. public function getCoffee($worker)
  248. {
  249. if ($this->is_Head == true) {
  250. $coef_head = 2;
  251. }else{
  252. $coef_head = 1;
  253. }
  254. return $this->consumed_coffee*$coef_head;
  255. }
  256.  
  257. public function getPages($worker)
  258. {
  259. if ($this->is_Head == true) {
  260. $coef_head = 0;
  261. }else{
  262. $coef_head = 1;
  263. }
  264. return $this->made_pages*$coef_head;
  265. }
  266.  
  267. public function getProffesion()
  268. {
  269. return $this->proffesion;
  270. }
  271.  
  272. public function getRank()
  273. {
  274. return $this->rank;
  275. }
  276.  
  277. public function getIsHead()
  278. {
  279. return $this->is_Head;
  280. }
  281. }
  282.  
  283. class Manager extends Worker
  284. {
  285. public function __construct($rank, $is_Head)
  286. {
  287. parent::__construct($rank, 'Manager', $is_Head, 500, 20, 200);
  288. }
  289. }
  290.  
  291. class Marketer extends Worker
  292. {
  293. public function __construct($rank, $is_Head)
  294. {
  295. parent::__construct($rank, 'Marketer', $is_Head, 400, 15, 150);
  296. }
  297. }
  298. class Engineer extends Worker
  299. {
  300. public function __construct($rank, $is_Head)
  301. {
  302. parent::__construct($rank, 'Engineer', $is_Head, 200, 5, 50);
  303. }
  304.  
  305. }
  306. class Analyst extends Worker
  307. {
  308. static private $salary = 800;
  309. static private $coffee = 50;
  310.  
  311. public function __construct($rank, $is_Head)
  312. {
  313.  
  314. parent::__construct($rank, 'Analyst', $is_Head, Analyst::getSalaryForAnalyst(), Analyst::getCoffeeForAnalyst(), 5);
  315. }
  316.  
  317. static public function getSalaryForAnalyst()
  318. {
  319. return Analyst::$salary;
  320. }
  321.  
  322. static public function setSalaryForAnalyst($new_salary)
  323. {
  324. Analyst::$salary = $new_salary;
  325. }
  326.  
  327. static public function getCoffeeForAnalyst()
  328. {
  329. return Analyst::$coffee;
  330. }
  331.  
  332. static public function setCoffeeForAnalyst($new_coffee)
  333. {
  334. Analyst::$coffee = $new_coffee;
  335. }
  336. }
  337.  
  338. class Crisis
  339. {
  340. //Cократить в каждом департаменте 40% (округляя в большую сторону) инженеров,
  341. //преимущественно самого низкого ранга. Если инженер является боссом, вместо него
  342. //надо уволить другого инженера, не босса.
  343. public static function destroyWorkers($count_in_percent, $proffesion, $company)
  344. {
  345. foreach ($company->getDepartaments() as $departament) {
  346. $count_workers = 0;// Количество работников нужной профессии
  347. foreach ($departament->getWorkers() as $worker) {
  348. if ($worker->getProffesion() == $proffesion) {
  349. $count_workers ++;
  350. }
  351. }
  352. $sum_to_destroy = 0; // Количество работников которых нужно уволить исходя от заданых процентов
  353. $sum_to_destroy = ceil($count_workers*$count_in_percent-$count_workers);
  354. $rank = 1;
  355. for ($i = 0; $i < $sum_to_destroy; $rank++ ) {
  356. foreach ($departament->getWorkers() as $number => $worker) {
  357. if ($worker->getProffesion() == $proffesion &&
  358. $worker->getRank() == $rank &&
  359. $i < $sum_to_destroy &&
  360. $worker->getIsHead() == false ) {
  361. $departament->deleteWorker($number);
  362. $i++;
  363. }
  364. }
  365. }
  366. }
  367. }
  368.  
  369. /** Увеличить базовую ставку аналитика с 800 до 1100 тугриков, а количество выпиваемого им кофе с 50 до 75 литров.
  370. * тех департаментах, где руководитель не является аналитиком, заменить его на аналитика самого высшего ранга из этого департамента
  371. * (а бывшего руководителя вернуть к обычной работе)
  372. *
  373. * Так же изменена зарпалата и кофе для аналитики при создание, то есть теперь если создать аналитика,
  374. * то его запрплата будет такая же как и у измененых "Analyst::setSalaryForAnalyst($salary);".
  375. **/
  376. public static function increaseSalaryAndCoffee($salary, $coffee, $proffesion, $company)
  377. {
  378. Analyst::setSalaryForAnalyst($salary);
  379. Analyst::setCoffeeForAnalyst($coffee);
  380. foreach ($company->getDepartaments() as $departament) {
  381. foreach ($departament->getWorkers() as $worker) {
  382. if ($worker->getProffesion() == $proffesion) {
  383. $worker->setSalary($salary);
  384. }
  385.  
  386. if ($worker->getProffesion() == $proffesion && $departament->getHeadWorker()->getProffesion() != $proffesion) {
  387.  
  388. for ($rank = 3; $departament->getHeadWorker()->getProffesion() != $proffesion; $rank--) {
  389. foreach ($departament->getWorkers() as $worker) {
  390. if ($worker->getProffesion() == $proffesion && $worker->getRank() == $rank) {
  391. $worker->setIsHead(true);
  392. }
  393. }
  394. }
  395. }
  396. }
  397. }
  398. }
  399.  
  400. /** каждом департаменте повысить 50% (округляя в большую сторону)
  401. * менеджеров 1-го и 2-го ранга на один ранг с целью расширить их полномочия.
  402. **/
  403. public static function increaseRank($count_in_percent, $proffesion, $company)
  404. {
  405. foreach ($company->getDepartaments() as $departament) {
  406. $count_workers = 0;// Количество работников нужной профессии
  407. foreach ($departament->getWorkers() as $worker) {
  408. if ($worker->getProffesion() == $proffesion && $worker->getRank() < 3) {
  409. $count_workers ++;
  410. }
  411. }
  412. $sum_to_destroy = 0;
  413. // Количество работников которых нужно уволить исходя от заданых процентов
  414. $sum_to_destroy = ceil($count_workers*$count_in_percent-$count_workers);
  415. }
  416. $i = 0;
  417. foreach ($company->getDepartaments() as $departament) {
  418. foreach ($departament->getWorkers() as $worker) {
  419. if ($worker->getProffesion() == $proffesion && $worker->getRank() < 3 && $i < $sum_to_destroy) {
  420. $worker->setRank($worker->getRank()+1);
  421. $i++;
  422. }
  423.  
  424. }
  425. }
  426. }
  427. }
  428. function createWorkers($count, $rank, $proffesion, $is_Head)
  429. {
  430. $workers_array = array();
  431. for ($i = 0; $i < $count; $i++) {
  432. $worker = new $proffesion($rank, $is_Head);
  433. $workers_array[] = $worker;
  434. }
  435. return $workers_array;
  436. }
  437. function createCompany()
  438. {
  439. $company = new Company;
  440.  
  441. $departament_purchase = new Departament("Purchase");
  442. $departament_purchase->addWorkers(createWorkers(9, 1, 'Manager', false));
  443. $departament_purchase->addWorkers(createWorkers(3, 2, 'Manager', false));
  444. $departament_purchase->addWorkers(createWorkers(2, 3, 'Manager', false));
  445. $departament_purchase->addWorkers(createWorkers(2, 1, 'Marketer', false));
  446. $departament_purchase->addWorker(new Manager(2, true));
  447. $company->addDepartament($departament_purchase);
  448.  
  449. $departament_sales = new Departament("Sales");
  450. $departament_sales->addWorkers(createWorkers(12, 1, 'Manager', false));
  451. $departament_sales->addWorkers(createWorkers(6, 1, 'Marketer', false));
  452. $departament_sales->addWorkers(createWorkers(3, 1, 'Analyst', false));
  453. $departament_sales->addWorkers(createWorkers(2, 2, 'Analyst', false));
  454. $departament_sales->addWorker(new Marketer (2, true));
  455. $company->addDepartament($departament_sales);
  456.  
  457. $departament_advertising = new Departament("advertising");
  458. $departament_advertising->addWorkers(createWorkers(15, 1, 'Marketer', false));
  459. $departament_advertising->addWorkers(createWorkers(10, 2, 'Marketer', false));
  460. $departament_advertising->addWorkers(createWorkers(8, 1, 'Manager', false));
  461. $departament_advertising->addWorkers(createWorkers(2, 1, 'Engineer', false));
  462. $departament_advertising->addWorker(new Marketer (3, true));
  463. $company->addDepartament($departament_advertising);
  464.  
  465. $departament_logistics = new Departament("Logistics");
  466. $departament_logistics->addWorkers(createWorkers(13, 1, 'Manager', false));
  467. $departament_logistics->addWorkers(createWorkers(5, 2, 'Manager', false));
  468. $departament_logistics->addWorkers(createWorkers(5, 1, 'Engineer', false));
  469. $departament_logistics->addWorker(new Manager(1, true));
  470. $company->addDepartament($departament_logistics);
  471.  
  472. return $company;
  473.  
  474. }
  475. function padRight($string, $length)
  476. {
  477. $long_string = strlen ($string);
  478. $x = $length-$long_string;
  479. $space = str_repeat(" ",$x);
  480. echo $string.$space;
  481. }
  482.  
  483. function padLeft($string, $length)
  484. {
  485. $long_string = strlen ($string);
  486. $x = $length-$long_string;
  487. $space = str_repeat(" ",$x);
  488. echo $space.$string;
  489. }
  490.  
  491. function printer($company)
  492. {
  493. $col1 = 12;
  494. $col2 = 12;
  495. $col3 = 16;
  496.  
  497. // Заголовок таблицы
  498. echo padRight ("Departaments",$col1).
  499. padLeft("Workers", $col2) .
  500. padLeft("Salary", $col2) .
  501. padLeft("Coffee", $col2) .
  502. padLeft("Pages", $col2) .
  503. padLeft("Salary/Pages", $col3) ."\n";
  504.  
  505. echo "----------------------------------------------------------------------\n";
  506. $departaments = $company->getDepartaments();
  507. foreach ($departaments as $departament) {
  508. echo padRight($departament->getDepartamentName(), $col2);
  509. echo padLeft($departament->getSumWorkers(), $col2);
  510. echo padLeft($departament->getSumSalary(), $col2);
  511. echo padLeft($departament->getSumCoffee(), $col2);
  512. echo padLeft($departament->getSumPages(), $col2);
  513. echo padLeft($departament->getSalaryForPage(), $col3)."\n";
  514. }
  515.  
  516. echo "----------------------------------------------------------------------\n";
  517. echo padRight('Average', $col2);
  518. echo padLeft($company->getAverageWorkers(), $col2);
  519. echo padLeft($company->getAverageSalary(), $col2);
  520. echo padLeft($company->getAverageCoffee(), $col2);
  521. echo padLeft($company->getAveragePages(), $col2);
  522. echo padLeft($company->getAverageSalaryForPage(), $col3)."\n";
  523. echo "----------------------------------------------------------------------\n";
  524. echo padRight('VSEGO', $col2);
  525. echo padLeft($company->getCountWorkers(), $col2);
  526. echo padLeft($company->getCountSalary(), $col2);
  527. echo padLeft($company->getCountCoffee(), $col2);
  528. echo padLeft($company->getCountPages(), $col2);
  529. echo padLeft($company->getCountSalaryForPage(), $col3)."\n\n";
  530. }
  531.  
  532. $company = createCompany();
  533.  
  534.  
  535. printer($company);
  536.  
  537. /**
  538. echo "1 АНТИ КРИЗИС \n----------------------------------------------------\n";
  539. Crisis::destroyWorkers(1.4, 'Engineer', $company);
  540. printer($company);
  541. **/
  542.  
  543.  
  544. /**
  545. echo "2 АНТИ КРИЗИС \n----------------------------------------------------\n";
  546. Crisis::increaseSalaryAndCoffee(1100, 75, 'Analyst', $company);
  547. printer($company);
  548. **/
  549.  
  550. /**
  551.  * echo "3 АНТИ КРИЗИС \n----------------------------------------------------\n";
  552. Crisis::increaseRank(1.5, 'Manager', $company);
  553. printer($company);
  554.  
  555.  
  556.  
  557.  
Success #stdin #stdout #stderr 0s 82880KB
stdin
Standard input is empty
stdout
Departaments     Workers      Salary      Coffee       Pages    Salary/Pages
----------------------------------------------------------------------
Purchase              17      9612.5         350        3100           3.101
Sales                 24       13550         610        3325           4.075
advertising           36       16300         575        5450           2.991
Logistics             24       11375         425        3850           2.955
----------------------------------------------------------------------
Average            25.25   12709.375         490     3931.25          3.2805
----------------------------------------------------------------------
VSEGO                101     50837.5        1960       15725          13.122

stderr
PHP Warning:  Unterminated comment starting line 550 in /home/1GYGB4/prog.php on line 550