fork download
  1. <?php
  2.  
  3. class Department {
  4. public $employees = array();
  5.  
  6. public function __construct($boss, $name,
  7. $managers1, $managers2, $managers3,
  8. $marketers1, $marketers2, $marketers3,
  9. $engineers1,
  10. $analysts1, $analysts2) {
  11. $this->name = $name;
  12. for ($i = 0; $i < $managers1; $i++){
  13. $this->employees[] = new Manager(1, false);
  14. }
  15. for ($i = 0; $i < $managers2; $i++){
  16. $this->employees[] = new Manager(2, false);
  17. }
  18. for ($i = 0; $i < $managers3; $i++){
  19. $this->employees[] = new Manager(3, false);
  20. }
  21. for ($i = 0; $i < $marketers1; $i++){
  22. $this->employees[] = new Marketer(1, false);
  23. }
  24. for ($i = 0; $i < $marketers2; $i++){
  25. $this->employees[] = new Marketer(2, false);
  26. }
  27. for ($i = 0; $i < $marketers3; $i++){
  28. $this->employees[] = new Marketer(3, false);
  29. }
  30. for ($i = 0; $i < $engineers1; $i++){
  31. $this->employees[] = new Engineer(1, false);
  32. }
  33. for ($i = 0; $i < $analysts1; $i++){
  34. $this->employees[] = new Analyst(1, false);
  35. }
  36. for ($i = 0; $i < $analysts2; $i++){
  37. $this->employees[] = new Analyst(2, false);
  38. }
  39. $this->employees[] = $boss;
  40. }
  41.  
  42. public function calcCoffeeConsumption() {
  43. $consumption = 0;
  44. foreach ($this->employees as $employee) {
  45. $consumption = $consumption + $employee->coffee;
  46. }
  47. return $consumption;
  48. }
  49. public function calcMoneyConsumption() {
  50. $consumption = 0;
  51. foreach ($this->employees as $employee) {
  52. $consumption = $consumption + $employee->salary;
  53. }
  54. return $consumption;
  55. }
  56. public function calcReportsGeneration() {
  57. $generation = 0;
  58. foreach ($this->employees as $employee) {
  59. $generation = $generation + $employee->reports;
  60. }
  61. return $generation;
  62. }
  63. public function calcToogreeksPerPage() {
  64. $toogreeks = $this->calcMoneyConsumption() / $this->calcReportsGeneration();
  65. return round($toogreeks, 2);
  66. }
  67. public function getNumberOfEmployees() {
  68. return count($this->employees);
  69. }
  70. }
  71.  
  72. abstract class Employee {
  73. public function countSalary($salary) {
  74. if ($this->rank == 2) {
  75. $salary = $salary * 1.25;
  76. }elseif ($this->rank == 3) {
  77. $salary = $salary * 1.5;
  78. }
  79. if ($this->isBoss == true) {
  80. $salary = $salary * 1.5;
  81. }
  82. return $salary;
  83. }
  84. public function countCoffee($coffee) {
  85. if ($this->isBoss == true) {
  86. $coffee = $coffee * 2;
  87. }
  88. return $coffee;
  89. }
  90. public function countReports($reports) {
  91. if ($this->isBoss == true) {
  92. $reports = 0;
  93. }
  94. return $reports;
  95. }
  96. }
  97.  
  98. class Manager extends Employee {
  99. public function __construct($rank, $isBoss) {
  100. $salary = 500;
  101. $coffee = 20;
  102. $reports = 200;
  103. $this->rank = $rank;
  104. $this->isBoss = $isBoss;
  105. $this->salary = $this->countSalary($salary);
  106. $this->coffee = $this->countCoffee($coffee);
  107. $this->reports = $this->countReports($reports);
  108. }
  109. }
  110. class Marketer extends Employee {
  111. public function __construct($rank, $isBoss) {
  112. $salary = 400;
  113. $coffee = 15;
  114. $reports = 150;
  115. $this->rank = $rank;
  116. $this->isBoss = $isBoss;
  117. $this->salary = $this->countSalary($salary);
  118. $this->coffee = $this->countCoffee($coffee);
  119. $this->reports = $this->countReports($reports);
  120. }
  121. }
  122. class Engineer extends Employee {
  123. public function __construct($rank, $isBoss) {
  124. $salary = 200;
  125. $coffee = 5;
  126. $reports = 50;
  127. $this->rank = $rank;
  128. $this->isBoss = $isBoss;
  129. $this->salary = $this->countSalary($salary);
  130. $this->coffee = $this->countCoffee($coffee);
  131. $this->reports = $this->countReports($reports);
  132. }
  133. }
  134. class Analyst extends Employee {
  135. public function __construct($rank, $isBoss) {
  136. $salary = 800;
  137. $coffee = 50;
  138. $reports = 5;
  139. $this->rank = $rank;
  140. $this->isBoss = $isBoss;
  141. $this->salary = $this->countSalary($salary);
  142. $this->coffee = $this->countCoffee($coffee);
  143. $this->reports = $this->countReports($reports);
  144. }
  145. }
  146.  
  147. function padLeft($string, $length) {
  148. $stlen = mb_strlen($string);
  149. if ($stlen < $length) {
  150. for ($i = 0; $i < $length - $stlen; $i++) {
  151. $string = " " . $string;
  152. }
  153. }
  154. return $string;
  155. }
  156. function padRight($string, $length) {
  157. $stlen = mb_strlen($string);
  158. if ($stlen < $length) {
  159. for ($i = 0; $i < $length - $stlen; $i++) {
  160. $string = $string . " ";
  161. }
  162. }
  163. return $string;
  164. }
  165. function calcAverage($company, $method) {
  166. $summary = 0;
  167. foreach ($company as $department) {
  168. $summary = $summary + $department->$method();
  169. }
  170. return round($summary / count($company), 2);
  171. }
  172. function calcTotal($company, $method) {
  173. $summary = 0;
  174. foreach ($company as $department) {
  175. $summary = $summary + $department->$method();
  176. }
  177. return $summary;
  178. }
  179.  
  180. $purchasing = new Department(new Manager(2, true), "Закупок",
  181. 9, 3, 2,
  182. 2, 0, 0,
  183. 0,
  184. 0, 0);
  185.  
  186. $sales = new Department(new Marketer(2, true), "Продаж",
  187. 12, 0, 0,
  188. 6, 0, 0,
  189. 0,
  190. 3, 2);
  191.  
  192. $marketing = new Department(new Marketer(3, true), "Рекламы",
  193. 8, 0, 0,
  194. 15, 10, 0,
  195. 2,
  196. 0, 0);
  197.  
  198. $logistics = new Department(new Manager(1, true), "Логистики",
  199. 13, 5, 0,
  200. 0, 0, 0,
  201. 5,
  202. 0, 0);
  203.  
  204. $company = array($purchasing, $sales, $marketing, $logistics);
  205.  
  206. $col1 = 20;
  207. $col2 = 8;
  208. $col3 = 10;
  209. $col4 = 10;
  210. $col5 = 10;
  211. $col6 = 12;
  212. echo padRight("Департамент", $col1) .
  213. padLeft("сотр.", $col2) .
  214. padLeft("тугр.", $col3) .
  215. padLeft("кофе", $col4) .
  216. padLeft("стр.", $col5) .
  217. padLeft("тугр./стр.", $col6) . "\n----------------------------------------------------------------------\n-\n";
  218. foreach ($company as $department) {
  219. echo padRight($department->name, $col1) .
  220. padLeft(count($department->employees), $col2) .
  221. padLeft($department->calcMoneyConsumption(), $col3) .
  222. padLeft($department->calcCoffeeConsumption(), $col4) .
  223. padLeft($department->calcReportsGeneration(), $col5) .
  224. padLeft($department->calcToogreeksPerPage(), $col6) . "\n";
  225. }
  226. echo "-\n" . padRight("Среднее", $col1) .
  227. padLeft(calcAverage($company, 'getNumberOfEmployees'), $col2) .
  228. padLeft(calcAverage($company, 'calcMoneyConsumption'), $col3) .
  229. padLeft(calcAverage($company, 'calcCoffeeConsumption'), $col4) .
  230. padLeft(calcAverage($company, 'calcReportsGeneration'), $col5) .
  231. padLeft(calcAverage($company, 'calcToogreeksPerPage'), $col6) . "\n";
  232. echo padRight("Всего", $col1) .
  233. padLeft(calcTotal($company, 'getNumberOfEmployees'), $col2) .
  234. padLeft(calcTotal($company, 'calcMoneyConsumption'), $col3) .
  235. padLeft(calcTotal($company, 'calcCoffeeConsumption'), $col4) .
  236. padLeft(calcTotal($company, 'calcReportsGeneration'), $col5) .
  237. padLeft(calcTotal($company, 'calcToogreeksPerPage'), $col6) . "\n";
  238. ?>
  239.  
Success #stdin #stdout 0.02s 24448KB
stdin
Standard input is empty
stdout
Департамент            сотр.     тугр.      кофе      стр.  тугр./стр.
----------------------------------------------------------------------
-
Закупок                   17    9612.5       350      3100         3.1
Продаж                    24     13550       610      3325        4.08
Рекламы                   36     16300       575      5450        2.99
Логистики                 24     11375       425      3850        2.95
-
Среднее                25.25  12709.38       490   3931.25        3.28
Всего                    101   50837.5      1960     15725       13.12