fork download
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Саша Поляны
  5.  * Date: 07.04.2016
  6.  * Time: 16:07
  7.  */
  8.  
  9.  
  10. class Employee{
  11. public $department;
  12. public $profession;
  13. public $coffee;
  14. public $rank;
  15. public $boss;
  16. public $pages;
  17. public $salary;
  18. public $rate;
  19.  
  20. public function __construct($department, $profession, $coffee, $boss, $rank, $pages, $basicSalary)
  21. {
  22. $this->department = $department;
  23. $this->profession = $profession;
  24. $this->coffee = $coffee;
  25. $this->rank = $rank;
  26. $this->boss = $boss;
  27. $this->pages = $pages;
  28. $this->basicSalary = $basicSalary;
  29. }
  30.  
  31. public function getRate()
  32. {
  33. if ($this->boss == true) {
  34. $bossRank = 1.5;
  35. } else {
  36. $bossRank = 1;
  37. }
  38. if ($this->rank == 1) {
  39. $this->rate = 1;
  40. } elseif ($this->rank == 2) {
  41. $this->rate = 1.25;
  42. } elseif ($this->rank == 3) {
  43. $this->rate = 1.5;
  44. }
  45. $this->rate *= $bossRank;
  46. return $this->rate;
  47. }
  48. public function getSalary()
  49. {
  50. $this->salary = $this->basicSalary * $this->getRate();
  51. return $this->salary;
  52. }
  53. public function getPages()
  54. {
  55. $pages = $this->pages;
  56. return $pages;
  57. }
  58. public function getCoffee()
  59. {
  60. $coffee = $this->coffee;
  61. return $coffee;
  62. }
  63. }
  64. function padLeft($string, $widthOfTableCell){
  65. $lengthOfString = mb_strlen($string);
  66. $missingSpaces = $widthOfTableCell - $lengthOfString;
  67. $tableCell = $string . str_repeat(" ", $missingSpaces);
  68. return $tableCell;
  69. }
  70. function padRight($string, $widthOfTableCell){
  71. $lengthOfString = mb_strlen($string);
  72. $missingSpaces = $widthOfTableCell - $lengthOfString;
  73. $tableCell = str_repeat(" ", $missingSpaces) . $string;
  74. return $tableCell;
  75. }
  76. $col1 = 15;
  77. $col2 = $col3 = $col4 = $col5 = $col6 = 12;
  78.  
  79. $employeesPurchaseDepartment = [];
  80. for ($i = 1; $i <= 9; $i++) {
  81. $employeesPurchaseDepartment[] = new Employee('Закупки', 'Me', 20, false, 1, 200, 500);
  82. }
  83. for ($i = 1; $i <= 3; $i++) {
  84. $employeesPurchaseDepartment[] = new Employee('Закупки', 'Me', 20, false, 2, 200, 500);
  85. }
  86. for ($i = 1; $i <= 2; $i++) {
  87. $employeesPurchaseDepartment[] = new Employee('Закупки', 'Me', 20, false, 3, 200, 500);
  88. }
  89. for ($i = 1; $i <= 2; $i++) {
  90. $employeesPurchaseDepartment[] = new Employee('Закупки', 'Ma', 15, false, 2, 150, 400);
  91. }
  92. for ($i = 1; $i <= 1; $i++) {
  93. $employeesPurchaseDepartment[] = new Employee('Закупки', 'Me', 20, true, 2, 200, 500);
  94. }
  95.  
  96. function getIndex($employees)
  97. {
  98. $salary = 0;
  99. $pages = 0;
  100. $coffee = 0;
  101. $department = 0;
  102. $employeesNumber = 0;
  103. $index = [];
  104. for ($i = 0; $i < count($employees); $i++) {
  105. $salary += $employees[$i]->getSalary();
  106. $pages += $employees[$i]->getPages();
  107. $coffee += $employees[$i]->getCoffee();
  108. }
  109. $department = $employees[$i - 1]->department;
  110. $employeesNumber = count($employees);
  111. $index = array(
  112. 'salary' => $salary,
  113. 'pages' => $pages,
  114. 'coffee' => $coffee,
  115. 'department' => $department,
  116. 'employeesNumber' => $employeesNumber
  117. );
  118. return $index;
  119. }
  120. $purchaseDepartment = getIndex($employeesPurchaseDepartment);
  121.  
  122. // Заголовок таблицы
  123. echo padLeft("Департамент", $col1) .
  124. padRight("сотр.", $col2) .
  125. padRight("тугр.", $col3) .
  126. padRight("кофе", $col4) .
  127. padRight("стр.", $col5) .
  128. padRight("тугр./стр.", $col6) ."\n";
  129.  
  130. // Сама таблица
  131. echo padLeft($purchaseDepartment['department'], $col1) .
  132. padRight($purchaseDepartment['employeesNumber'], $col2) .
  133. padRight($purchaseDepartment['salary'], $col3) .
  134. padRight($purchaseDepartment['coffee'], $col4) .
  135. padRight($purchaseDepartment['pages'], $col5) .
  136. padRight(0, $col6) . "\n";
Success #stdin #stdout 0.03s 52472KB
stdin
Standard input is empty
stdout
Департамент           сотр.       тугр.        кофе        стр.  тугр./стр.
Закупки                  17      9812.5         330        3300           0