fork download
  1. <?php
  2.  
  3.  
  4. class Employee{
  5. public $department;
  6. public $profession;
  7. public $coffee;
  8. public $boss;
  9. public $rank;
  10. public $pages;
  11. public $basicSalary;
  12.  
  13. public function __construct($department, $profession, $boss, $rank, $basicSalary)
  14. {
  15. $this->department = $department;
  16. $this->profession = $profession;
  17. $this->rank = $rank;
  18. $this->boss = $boss;
  19. $this->basicSalary = $basicSalary;
  20. }
  21.  
  22. public function getDepartment() {
  23. $department = $this->department;
  24. return $department;
  25. }
  26.  
  27. public function setRate()
  28. {
  29. $rate = 0;
  30. if ($this->boss == true) {
  31. $bossRank = 1.5;
  32. } else {
  33. $bossRank = 1;
  34. }
  35. if ($this->rank == 1) {
  36. $rate = 1;
  37. } elseif ($this->rank == 2) {
  38. $rate = 1.25;
  39. } elseif ($this->rank == 3) {
  40. $rate = 1.5;
  41. }
  42. $rate *= $bossRank;
  43. return $rate;
  44. }
  45.  
  46. public function getSalary()
  47. {
  48. $salary = $this->basicSalary * $this->setRate();
  49. return $salary;
  50. }
  51.  
  52. public function getCoffeeByProfession($coffeeByProfession){
  53. foreach ($coffeeByProfession as $professionName => $coffeeNumber) {
  54. if ($this->profession == $professionName) {
  55. if ($this->boss == true) {
  56. $this->coffee = $coffeeNumber * 2;
  57. break;
  58. } else {
  59. $this->coffee = $coffeeNumber;
  60. break;
  61. }
  62. }
  63. }
  64. return $this->coffee;
  65. }
  66.  
  67. public function getPagesByProfession($pagesByProfession){
  68. foreach ($pagesByProfession as $professionName => $pagesNumber) {
  69. if ($this->profession == $professionName) {
  70. if ($this->boss == true) {
  71. $this->pages = 0;
  72. break;
  73. } else {
  74. $this->pages = $pagesNumber;
  75. break;
  76. }
  77. }
  78. }
  79. return $this->pages;
  80. }
  81. }
  82. class Department{
  83. public $departmentName;
  84. public $departmentCoffee;
  85. public $departmentPages;
  86. public $departmentSalary;
  87.  
  88. public function getDepartmentName()
  89. {
  90. $department = $this->getDepartment();
  91. return $department;
  92. }
  93. public function getDepartmentCoffee($employees, $coffeeByProfession)
  94. {
  95. $departmentCoffee = 0;
  96. for ($i = 0; $i < count($employees); $i++) {
  97. $departmentCoffee += $employees[$i]->getCoffeeByProfession($coffeeByProfession);
  98. }
  99. return $departmentCoffee;
  100. }
  101. public function getDepartmentPages($employees, $pagesByProfession)
  102. {
  103. $departmentPages = 0;
  104. for ($i = 0; $i < count($employees); $i++) {
  105. $departmentPages += $employees[$i]->getPagesByProfession($pagesByProfession);
  106. }
  107. return $departmentPages;
  108. }
  109. public function getDepartmentSalary($employees)
  110. {
  111. $departmentSalary = 0;
  112. for ($i = 0; $i < count($employees); $i++) {
  113. $departmentSalary += $employees[$i]->getSalary;
  114. }
  115. return $departmentSalary;
  116. }
  117. }
  118. class Company{
  119. public $departments;
  120. public $employeesNumber;
  121. public $companiesCoffee;
  122. public $companiesPages;
  123. public $companiesSalary;
  124.  
  125. public $averageEmployeesNumber;
  126. public $averageCompaniesCoffee;
  127. public $averageCompaniesPages;
  128. public $averageCompaniesSalaryPerPages;
  129. public $averageCompaniesSalary;
  130.  
  131. public function getDepartments()
  132. {
  133. }
  134. public function getEmployeesNumber()
  135. {
  136. }
  137. public function getCompaniesCoffee()
  138. {
  139. }
  140. public function getCompaniesPages()
  141. {
  142. }
  143. public function getCompaniesSalary()
  144. {
  145. }
  146. public function getAverageEmployeesNumber()
  147. {
  148. }
  149. public function getAverageCompaniesCoffee()
  150. {
  151. }
  152. public function getAverageCompaniesPages()
  153. {
  154. }
  155. public function getAaverageCompaniesSalaryPerPages()
  156. {
  157. }
  158. public function getAverageCompaniesSalary()
  159. {
  160. }
  161.  
  162. }
  163. function padRight($string, $widthOfTableCell){
  164. $tableCell = $string . str_repeat(" ", $widthOfTableCell - mb_strlen($string));
  165. return $tableCell;
  166. }
  167.  
  168. function padLeft($string, $widthOfTableCell){
  169. $tableCell = str_repeat(" ", $widthOfTableCell - mb_strlen($string)) . $string;
  170. return $tableCell;
  171. }
  172.  
  173. $col1 = 15;
  174. $col2 = $col3 = $col4 = $col5 = $col6 = 12;
  175.  
  176. $coffeeByProfession = array(
  177. 'Me' => 20,
  178. 'Ma' => 15,
  179. 'En' => 5,
  180. 'An' => 50
  181. );
  182.  
  183. $pagesByProfession = array(
  184. 'Me' => 200,
  185. 'Ma' => 150,
  186. 'En' => 50,
  187. 'An' => 5
  188. );
  189.  
  190. function getEmployeesOfDepartment($employeesOfDepartment, $matches, $i, $x) {
  191. $bossStatus = false;
  192. $arrayOfResults = [];
  193. $numberOfEmployees = (int)$matches[$i][2] + $x;
  194. if (isset($matches[$i][5])) {
  195. $bossStatus = true;
  196. }
  197.  
  198. for ($x; $x < $numberOfEmployees; $x++) {
  199. $employeesOfDepartment[] = new Employee($matches[$i][1], $matches[$i][3], $bossStatus, $matches[$i][4], 400);
  200. }
  201.  
  202. $arrayOfResults = [
  203. 'x' => $x,
  204. 'employeesOfDepartment' => $employeesOfDepartment
  205. ];
  206. return $arrayOfResults;
  207. }
  208.  
  209. $employees = ['pu9me1', 'pu3me2', 'pu2me3', 'pu2ma1', 'pu1me2boss', 're12me1', 're6ma1', 're3an1', 're2an2', 're1ma2boss', 'ad15ma1', 'ad10ma2', 'ad8me1', 'ad2en1', 'ad1ma3boss', 'lo13me1', 'lo5me2', 'lo5en1', 'lo1me1boss'];
  210. $regexp = '/([a-z]{2})([0-9]*)([a-z]{2})([1-3])(boss)?/';
  211. $i = 0;
  212. $x = 0;
  213. $employeesOfPurchaseDepartment = [];
  214. $employeesOfRealizationDepartment = [];
  215. $employeesOfAdvertisingDepartment = [];
  216. $employeesOfLogisticDepartment = [];
  217. $matches = [];
  218. $numberOfEmployees = 0;
  219. foreach ($employees as $key => $employee) {
  220. preg_match($regexp, $employee, $matches[$i]);
  221. if ($matches[$i][1] == 'pu') {
  222. $employeesOfPurchaseDepartment = getEmployeesOfDepartment($employeesOfPurchaseDepartment, $matches, $i, $x);
  223. $x = $employeesOfPurchaseDepartment['x'];
  224. $employeesOfPurchaseDepartment = $employeesOfPurchaseDepartment['employeesOfDepartment'];
  225. } elseif ($matches[$i][1] == 're') {
  226. $employeesOfRealizationDepartment = getEmployeesOfDepartment($employeesOfRealizationDepartment, $matches, $i, $x);
  227. $x = $employeesOfRealizationDepartment['x'];
  228. $employeesOfRealizationDepartment = $employeesOfRealizationDepartment['employeesOfDepartment'];
  229. } elseif ($matches[$i][1] == 'ad') {
  230. $employeesOfAdvertisingDepartment = getEmployeesOfDepartment($employeesOfAdvertisingDepartment, $matches, $i, $x);
  231. $x = $employeesOfAdvertisingDepartment['x'];
  232. $employeesOfAdvertisingDepartment = $employeesOfAdvertisingDepartment['employeesOfDepartment'];
  233. } elseif ($matches[$i][1] == 'lo') {
  234. $employeesOfLogisticDepartment = getEmployeesOfDepartment($employeesOfLogisticDepartment, $matches, $i, $x);
  235. $x = $employeesOfLogisticDepartment['x'];
  236. $employeesOfLogisticDepartment = $employeesOfLogisticDepartment['employeesOfDepartment'];
  237. }
  238. $i++;
  239. }
  240.  
  241. function getIndex($employees)
  242. {
  243. $salary = 0;
  244. $pages = 0;
  245. $coffee = 0;
  246. $i = 0;
  247. $department = $employees[$i]->department;
  248. for (; $i < count($employees); $i++) {
  249. $salary += $employees[$i]->getSalary();
  250. $pages += $employees[$i]->pages;
  251. $coffee += $employees[$i]->coffee;
  252. }
  253. $employeesNumber = count($employees);
  254. $index = array(
  255. 'salary' => $salary,
  256. 'pages' => $pages,
  257. 'coffee' => $coffee,
  258. 'department' => $department,
  259. 'employeesNumber' => $employeesNumber
  260. );
  261. return $index;
  262. }
  263. $departmentsIndex = [];
  264. $purchaseDepartment = getIndex($employeesOfPurchaseDepartment);
  265. $realizationDepartment = getIndex($employeesOfRealizationDepartment);
  266. $advertisingDepartment = getIndex($employeesOfAdvertisingDepartment);
  267. $logisticDepartment = getIndex($employeesOfLogisticDepartment);
  268.  
  269. $departmentsIndex[] = $purchaseDepartment;
  270. $departmentsIndex[] = $realizationDepartment;
  271. $departmentsIndex[] = $advertisingDepartment;
  272. $departmentsIndex[] = $logisticDepartment;
  273.  
  274. // Заголовок таблицы
  275. echo padRight("Департамент", $col1) .
  276. padLeft("сотр.", $col2) .
  277. padLeft("тугр.", $col3) .
  278. padLeft("кофе", $col4) .
  279. padLeft("стр.", $col5) .
  280. padLeft("тугр./стр.", $col6) ."\n";
  281.  
  282. // Сама таблица
  283. foreach ($departmentsIndex as $department) {
  284. echo padRight($department['department'], $col1) .
  285. padLeft($department['employeesNumber'], $col2) .
  286. padLeft($department['salary'], $col3) .
  287. padLeft($department['coffee'], $col4) .
  288. padLeft($department['pages'], $col5) .
  289. padLeft(0, $col6) . "\n";
  290. }
  291.  
  292. ?>
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Департамент           сотр.       тугр.        кофе        стр.  тугр./стр.
pu                       17        7850           0           0           0
re                       24       10150           0           0           0
ad                       36       15900           0           0           0
lo                       24       10300           0           0           0