fork(2) 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 getWorkersAndDestroy($count_in_percent,$proffesion)
  165. {
  166. foreach ($this->workers as $worker) {
  167. if ($worker->getProffesion() == $proffesion) {
  168. $count_workers ++;
  169. }
  170. }
  171. $sum_to_destroy = 0;
  172. $sum_to_destroy = ceil($count_workers*$count_in_percent-$count_workers);
  173. $rank = 1;
  174. for ($i = 0; $i < $sum_to_destroy; $rank++ ) {
  175. foreach ($this->workers as $number => $worker) {
  176. if ($worker->getProffesion() == $proffesion &&
  177. $worker->getRank() == $rank &&
  178. $i < $sum_to_destroy &&
  179. $worker->getIsHead() == false ) {
  180. unset($this->workers[$number]);
  181. $i++;
  182. }
  183. }
  184. }
  185. }
  186. }
  187.  
  188.  
  189. abstract class Worker
  190. {
  191. private $rank;
  192. private $proffesion;
  193. private $is_Head;
  194. private $salary;
  195. private $consumed_coffee;
  196. private $made_pages;
  197.  
  198. public function __construct($rank, $proffesion, $is_Head, $salary, $consumed_coffe, $made_pages)
  199. {
  200. $this->setRank($rank);
  201. $this->setProffesion($proffesion);
  202. $this->setIsHead($is_Head);
  203. $this->setSalary($salary);
  204. $this->setConsumedCoffee($consumed_coffe);
  205. $this->setMadePages($made_pages);
  206. }
  207.  
  208. public function setRank($rank)
  209. {
  210. $this->rank = $rank;
  211. }
  212.  
  213. public function setProffesion($proffesion)
  214. {
  215. $this->proffesion = $proffesion;
  216. }
  217.  
  218. public function setIsHead($is_Head)
  219. {
  220. $this->is_Head = $is_Head;
  221. }
  222.  
  223. public function setSalary($salary)
  224. {
  225. $this->salary = $salary;
  226. }
  227.  
  228. public function setConsumedCoffee($consumed_coffee)
  229. {
  230. $this->consumed_coffee = $consumed_coffee;
  231. }
  232.  
  233. public function setMadePages($made_pages)
  234. {
  235. $this->made_pages = $made_pages;
  236. }
  237.  
  238. public function getSalary($worker)
  239. {
  240. $rank = $this->rank;
  241. if ($rank == 1) {
  242. $coef_salary = 1;
  243. }elseif ($rank == 2) {
  244. $coef_salary = 1.25;
  245. }elseif ($rank == 3) {
  246. $coef_salary = 1.5;
  247. }
  248. if ($this->is_Head == true) {
  249. $coef_head = 1.5;
  250. }else{
  251. $coef_head = 1;
  252. }
  253. return $this->salary*$coef_salary*$coef_head;
  254. }
  255.  
  256. public function getCoffee($worker)
  257. {
  258. if ($this->is_Head == true) {
  259. $coef_head = 2;
  260. }else{
  261. $coef_head = 1;
  262. }
  263. return $this->consumed_coffee*$coef_head;
  264. }
  265.  
  266. public function getPages($worker)
  267. {
  268. if ($this->is_Head == true) {
  269. $coef_head = 0;
  270. }else{
  271. $coef_head = 1;
  272. }
  273. return $this->made_pages*$coef_head;
  274. }
  275.  
  276. public function getProffesion()
  277. {
  278. return $this->proffesion;
  279. }
  280.  
  281. public function getRank()
  282. {
  283. return $this->rank;
  284. }
  285.  
  286. public function getIsHead()
  287. {
  288. return $this->is_Head;
  289. }
  290. }
  291.  
  292. class Manager extends Worker
  293. {
  294. public function __construct($rank, $is_Head)
  295. {
  296. parent::__construct($rank, 'Manager', $is_Head, 500, 20, 200);
  297. }
  298. }
  299.  
  300. class Marketer extends Worker
  301. {
  302. public function __construct($rank, $is_Head)
  303. {
  304. parent::__construct($rank, 'Marketer', $is_Head, 400, 15, 150);
  305. }
  306. }
  307. class Engineer extends Worker
  308. {
  309. public function __construct($rank, $is_Head)
  310. {
  311. parent::__construct($rank, 'Engineer', $is_Head, 200, 5, 50);
  312. }
  313. }
  314. class Analyst extends Worker
  315. {
  316. public function __construct($rank, $is_Head)
  317. {
  318. parent::__construct($rank, 'Analyst', $is_Head, 800, 50, 5);
  319. }
  320. }
  321.  
  322. class Crisis
  323. {
  324. //Cократить в каждом департаменте 40% (округляя в большую сторону) инженеров,
  325. //преимущественно самого низкого ранга. Если инженер является боссом, вместо него
  326. //надо уволить другого инженера, не босса.
  327. public function destroyWorkers($count_in_percent, $proffesion, $company)
  328. {
  329. // foreach ($company->getDepartaments() as $departament) {
  330. // $count_workers = $departament->getWorkersAndDestroy($count_in_percent, $proffesion);
  331. // }
  332. }
  333. }
  334. function createWorkers($count, $rank, $proffesion, $is_Head)
  335. {
  336. $workers_array = array();
  337. for ($i = 0; $i < $count; $i++) {
  338. $worker = new $proffesion($rank, $is_Head);
  339. $workers_array[] = $worker;
  340. }
  341. return $workers_array;
  342. }
  343. $company = new Company;
  344.  
  345. $departament_purchase = new Departament("Purchase");
  346. $departament_purchase->addWorkers(createWorkers(9, 1, 'Manager', false));
  347. $departament_purchase->addWorkers(createWorkers(3, 2, 'Manager', false));
  348. $departament_purchase->addWorkers(createWorkers(2, 3, 'Manager', false));
  349. $departament_purchase->addWorkers(createWorkers(2, 1, 'Marketer', false));
  350. $departament_purchase->addWorker(new Manager(2, true));
  351. $company->addDepartament($departament_purchase);
  352.  
  353. $departament_sales = new Departament("Sales");
  354. $departament_sales->addWorkers(createWorkers(12, 1, 'Manager', false));
  355. $departament_sales->addWorkers(createWorkers(6, 1, 'Marketer', false));
  356. $departament_sales->addWorkers(createWorkers(3, 1, 'Analyst', false));
  357. $departament_sales->addWorkers(createWorkers(2, 2, 'Analyst', false));
  358. $departament_sales->addWorker(new Marketer (2, true));
  359. $company->addDepartament($departament_sales);
  360.  
  361. $departament_advertising = new Departament("advertising");
  362. $departament_advertising->addWorkers(createWorkers(15, 1, 'Marketer', false));
  363. $departament_advertising->addWorkers(createWorkers(10, 2, 'Marketer', false));
  364. $departament_advertising->addWorkers(createWorkers(8, 1, 'Manager', false));
  365. $departament_advertising->addWorkers(createWorkers(2, 1, 'Engineer', false));
  366. $departament_advertising->addWorker(new Marketer (3, true));
  367. $company->addDepartament($departament_advertising);
  368.  
  369. $departament_logistics = new Departament("Logistics");
  370. $departament_logistics->addWorkers(createWorkers(13, 1, 'Manager', false));
  371. $departament_logistics->addWorkers(createWorkers(5, 2, 'Manager', false));
  372. $departament_logistics->addWorkers(createWorkers(5, 1, 'Engineer', false));
  373. $departament_logistics->addWorker(new Manager(1, true));
  374. $company->addDepartament($departament_logistics);
  375.  
  376. function padRight($string, $length)
  377. {
  378. $long_string = strlen ($string);
  379. $x = $length-$long_string;
  380. $space = str_repeat(" ",$x);
  381. echo $string.$space;
  382. }
  383.  
  384. function padLeft($string, $length)
  385. {
  386. $long_string = strlen ($string);
  387. $x = $length-$long_string;
  388. $space = str_repeat(" ",$x);
  389. echo $space.$string;
  390. }
  391.  
  392. function printer($company)
  393. {
  394. $col1 = 12;
  395. $col2 = 12;
  396. $col3 = 16;
  397.  
  398. // Заголовок таблицы
  399. echo padRight ("Departaments",$col1).
  400. padLeft("Workers", $col2) .
  401. padLeft("Salary", $col2) .
  402. padLeft("Coffee", $col2) .
  403. padLeft("Pages", $col2) .
  404. padLeft("Salary/Pages", $col3) ."\n";
  405.  
  406. echo "----------------------------------------------------------------------\n";
  407. $departaments = $company->getDepartaments();
  408. foreach ($departaments as $departament) {
  409. echo padRight($departament->getDepartamentName(), $col2);
  410. echo padLeft($departament->getSumWorkers(), $col2);
  411. echo padLeft($departament->getSumSalary(), $col2);
  412. echo padLeft($departament->getSumCoffee(), $col2);
  413. echo padLeft($departament->getSumPages(), $col2);
  414. echo padLeft($departament->getSalaryForPage(), $col3)."\n";
  415. }
  416.  
  417. echo "----------------------------------------------------------------------\n";
  418. echo padRight('Average', $col2);
  419. echo padLeft($company->getAverageWorkers(), $col2);
  420. echo padLeft($company->getAverageSalary(), $col2);
  421. echo padLeft($company->getAverageCoffee(), $col2);
  422. echo padLeft($company->getAveragePages(), $col2);
  423. echo padLeft($company->getAverageSalaryForPage(), $col3)."\n";
  424. echo "----------------------------------------------------------------------\n";
  425. echo padRight('VSEGO', $col2);
  426. echo padLeft($company->getCountWorkers(), $col2);
  427. echo padLeft($company->getCountSalary(), $col2);
  428. echo padLeft($company->getCountCoffee(), $col2);
  429. echo padLeft($company->getCountPages(), $col2);
  430. echo padLeft($company->getCountSalaryForPage(), $col3)."\n\n\n";
  431. }
  432.  
  433.  
  434. printer($company);
  435. //destroyWorkers(1.4, Engineer, $company);
  436. //printer($company);
  437.  
  438.  
  439.  
  440.  
Success #stdin #stdout 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