fork(2) download
  1. <?php
  2.  
  3. abstract class Employee
  4. {
  5.  
  6. public $salary;
  7. public $pages;
  8. public $cofee;
  9. public $name;
  10. public $isHead = false;
  11.  
  12.  
  13. function __construct($rank)
  14. {
  15.  
  16.  
  17. $this->rank = $rank;
  18. }
  19.  
  20. function getSalary()
  21. {
  22. $extra = 1;
  23. if ($this->isHead == true) {
  24. $extra = 1.50;
  25. }
  26. $multiply = 1;
  27. switch ($this->rank) {
  28. case 2:
  29. $multiply = 1.25;
  30. break;
  31. case 3:
  32. $multiply = 1.50;
  33. break;
  34. }
  35. return $this->salary * $multiply * $extra;
  36.  
  37. }
  38.  
  39. function setHead()
  40. {
  41. $this->isHead = true;
  42.  
  43. }
  44.  
  45. }
  46.  
  47. class Manager extends Employee
  48. {
  49. public $name = "Менеджер";
  50. public $salary = 500;
  51. public $cofee = 20;
  52. public $pages = 200;
  53. }
  54.  
  55. class Market extends Employee
  56. {
  57. public $name = "Маркетолог";
  58. public $salary = 400;
  59. public $cofee = 15;
  60. public $pages = 150;
  61. }
  62.  
  63. class Engineer extends Employee
  64. {
  65. public $name = "Инженер";
  66. public $salary = 200;
  67. public $cofee = 5;
  68. public $pages = 50;
  69. }
  70.  
  71. class Analyst extends Employee
  72. {
  73. public $name = "Аналитик";
  74. public $salary = 800;
  75. public $cofee = 50;
  76. public $pages = 5;
  77. }
  78.  
  79.  
  80. class Department
  81. {
  82. public $data;
  83. public $name;
  84. function workers()
  85. {
  86. return count($this->data);
  87. }
  88. function cofee()
  89. {
  90. $cofee = 0;
  91. foreach ($this->data as $key => $worker) {
  92. if ($worker->isHead == true) {
  93. $cofee += $worker->cofee * 2;
  94. } else {
  95. $cofee += $worker->cofee;
  96. }
  97. }
  98. return $cofee;
  99. }
  100. function costs()
  101. {
  102. $costs = 0;
  103. foreach ($this->data as $key => $worker) {
  104.  
  105. $costs += $worker->getSalary();
  106. }
  107. return $costs;
  108. }
  109. function pages()
  110. {
  111. $pages = 0;
  112. foreach ($this->data as $key => $worker) {
  113. if ($worker->isHead == true) {
  114. continue;
  115. }
  116. $pages += $worker->pages;
  117. }
  118. return $pages;
  119. }
  120. function costPerPage()
  121. {
  122. return round(($this->costs() / $this->pages()), 1);
  123. }
  124.  
  125. function __construct($name)
  126. {
  127.  
  128. $this->name = $name;
  129.  
  130. }
  131.  
  132. function addEmployee($data)
  133. {
  134. if (is_array($data)) {
  135. foreach ($data as $employee) {
  136. $this->addEmployee($employee);
  137. }
  138. } else {
  139. return $this->data[] = $data;
  140. }
  141. }
  142. function addBoss($boss)
  143. {
  144. $boss->setHead();
  145. return $this->data[] = $boss;
  146. }
  147. }
  148.  
  149.  
  150. function createEmployee($type, $rank, $quantity)
  151. {
  152. $array = array();
  153. for ($i = 0; $i < $quantity; $i++) {
  154. $array[] = new $type($rank);
  155. }
  156. return $array;
  157.  
  158. }
  159.  
  160. function padRight($name, $col)
  161. {
  162. while (mb_strlen($name) < $col) {
  163. $name .= " ";
  164. }
  165. return $name;
  166. }
  167.  
  168. function padLeft($name, $col)
  169. {
  170. while (mb_strlen($name) < $col) {
  171. $name = " " . $name;
  172. }
  173. return $name;
  174. }
  175.  
  176.  
  177. function showDepartment($department)
  178. {
  179.  
  180. $col1 = 20;
  181. $col2 = 6;
  182. $col3 = 10;
  183.  
  184.  
  185. echo padRight($department->name, $col1);
  186. echo padLeft($department->workers(), $col2);
  187. echo padLeft($department->costs(), $col3);
  188. echo padLeft($department->cofee(), $col2);
  189. echo padLeft($department->pages(), $col3);
  190. echo padLeft($department->costPerPage(), $col3);
  191. echo "\n";
  192. }
  193.  
  194. $department1 = new Department('Закупок');
  195. $department1->addEmployee(createEmployee('Manager', 1, 9));
  196. $department1->addEmployee(createEmployee('Manager', 2, 3));
  197. $department1->addEmployee(createEmployee('Manager', 3, 2));
  198. $department1->addEmployee(createEmployee('Market', 1, 2));
  199. $department1->addBoss(new Manager(2));
  200.  
  201. $department2 = new Department("Продаж");
  202. $department2->addEmployee(createEmployee('Manager', 1, 12));
  203. $department2->addEmployee(createEmployee('Market', 1, 6));
  204. $department2->addEmployee(createEmployee('Analyst', 1, 3));
  205. $department2->addEmployee(createEmployee('Analyst', 2, 2));
  206. $department2->addBoss(new Market(2));
  207.  
  208.  
  209. $department3 = new Department("Рекламы");
  210. $department3->addEmployee(createEmployee('Market', 1, 15));
  211. $department3->addEmployee(createEmployee('Market', 2, 10));
  212. $department3->addEmployee(createEmployee('Manager', 1, 8));
  213. $department3->addEmployee(createEmployee('Engineer', 1, 2));
  214. $department3->addBoss(new Market(3));
  215.  
  216.  
  217.  
  218. $col1 = 20;
  219. $col2 = 12;
  220.  
  221. echo padRight("Департамент", $col1) . padLeft("Сотр.", $col2) . padLeft("тугр.", $col2) . padLeft("кофе", $col2) . padLeft("стр.", $col2) . padLeft("тугр./стр.", 20) . "\n";
  222.  
  223. showDepartment($department1);
  224. showDepartment($department2);
  225. showDepartment($department3);
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Департамент   Сотр.   тугр.    кофе     стр.   тугр./стр.
Закупок          17    9612.5   350      3100       3.1
Продаж            24     13550   610      3325       4.1
Рекламы          36     16300   575      5450         3