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. public $rate;
  13. public $salary;
  14.  
  15. public function __construct($department, $profession, $boss, $rank)
  16. {
  17. $this->department = $department;
  18. $this->profession = $profession;
  19. $this->rank = $rank;
  20. $this->boss = $boss;
  21. }
  22.  
  23.  
  24. public function getRate()
  25. {
  26. if ($this->boss == true) {
  27. $bossRank = 1.5;
  28. } else {
  29. $bossRank = 1;
  30. }
  31. if ($this->rank == 1) {
  32. $this->rate = 1;
  33. } elseif ($this->rank == 2) {
  34. $this->rate = 1.25;
  35. } elseif ($this->rank == 3) {
  36. $this->rate = 1.5;
  37. }
  38. $this->rate *= $bossRank;
  39. return $this->rate;
  40. }
  41.  
  42. public function getSalary()
  43. {
  44. $this->salary = $this->basicSalary * $this->getRate();
  45. return $this->salary;
  46. }
  47.  
  48. public function getPages()
  49. {
  50. $pages = $this->pages;
  51. return $pages;
  52. }
  53.  
  54. public function getCoffee()
  55. {
  56. $coffee = $this->coffee;
  57. return $coffee;
  58. }
  59.  
  60. public function getCoffeeByProfession($coffeeByProfession){
  61. foreach ($coffeeByProfession as $professionName => $coffeeNumber) {
  62. if ($this->profession == $professionName) {
  63. if ($this->boss == true) {
  64. $this->coffee = $coffeeNumber * 2;
  65. break;
  66. } else {
  67. $this->coffee = $coffeeNumber;
  68. break;
  69. }
  70. }
  71. }
  72. return $this->coffee;
  73. }
  74.  
  75. public function getBasicSalaryByProfession($basicSalaryByProfession){
  76. foreach ($basicSalaryByProfession as $professionName => $salaryAmount) {
  77. if ($this->profession == $professionName) {
  78. $this->basicSalary = $salaryAmount;
  79. break;
  80. }
  81. }
  82. return $this->basicSalary;
  83. }
  84.  
  85. public function getPagesByProfession($pagesByProfession){
  86. foreach ($pagesByProfession as $professionName => $pagesNumber) {
  87. if ($this->profession == $professionName) {
  88. if ($this->boss == true) {
  89. $this->pages = 0;
  90. break;
  91. } else {
  92. $this->pages = $pagesNumber;
  93. break;
  94. }
  95. }
  96. }
  97. return $this->pages;
  98. }
  99. }
  100.  
  101. function padRight($string, $widthOfTableCell){
  102. $lengthOfString = mb_strlen($string);
  103. $missingSpaces = $widthOfTableCell - $lengthOfString;
  104. $tableCell = $string . str_repeat(" ", $missingSpaces);
  105. return $tableCell;
  106. }
  107.  
  108. function padLeft($string, $widthOfTableCell){
  109. $lengthOfString = mb_strlen($string);
  110. $missingSpaces = $widthOfTableCell - $lengthOfString;
  111. $tableCell = str_repeat(" ", $missingSpaces) . $string;
  112. return $tableCell;
  113. }
  114.  
  115. $col1 = 15;
  116. $col2 = $col3 = $col4 = $col5 = $col6 = 12;
  117.  
  118. $basicSalaryByProfession = array(
  119. 'Me' => 500,
  120. 'Ma' => 400,
  121. 'En' => 200,
  122. 'An' => 800
  123. );
  124.  
  125. $coffeeByProfession = array(
  126. 'Me' => 20,
  127. 'Ma' => 15,
  128. 'En' => 5,
  129. 'An' => 50
  130. );
  131.  
  132. $pagesByProfession = array(
  133. 'Me' => 200,
  134. 'Ma' => 150,
  135. 'En' => 50,
  136. 'An' => 5
  137. );
  138.  
  139. $employeesPurchaseDepartment = [];
  140. $number = 9;
  141. for ($i = 0; $i < $number; $i++) {
  142. $employeesPurchaseDepartment[$i] = new Employee('Закупки', 'Me', false, 1);
  143. $employeesPurchaseDepartment[$i]->coffee = $employeesPurchaseDepartment[$i]->getCoffeeByProfession($coffeeByProfession);
  144. $employeesPurchaseDepartment[$i]->basicSalary = $employeesPurchaseDepartment[$i]->getBasicSalaryByProfession($basicSalaryByProfession);
  145. $employeesPurchaseDepartment[$i]->pages = $employeesPurchaseDepartment[$i]->getPagesByProfession($pagesByProfession);
  146. }
  147. $number = $i + 3;
  148. for ($i; $i < $number; $i++) {
  149. $employeesPurchaseDepartment[$i] = new Employee('Закупки', 'Me', false, 2);
  150. $employeesPurchaseDepartment[$i]->coffee = $employeesPurchaseDepartment[$i]->getCoffeeByProfession($coffeeByProfession);
  151. $employeesPurchaseDepartment[$i]->basicSalary = $employeesPurchaseDepartment[$i]->getBasicSalaryByProfession($basicSalaryByProfession);
  152. $employeesPurchaseDepartment[$i]->pages = $employeesPurchaseDepartment[$i]->getPagesByProfession($pagesByProfession);
  153. }
  154. $number = $i + 2;
  155. for ($i; $i < $number; $i++) {
  156. $employeesPurchaseDepartment[$i] = new Employee('Закупки', 'Me', false, 3);
  157. $employeesPurchaseDepartment[$i]->coffee = $employeesPurchaseDepartment[$i]->getCoffeeByProfession($coffeeByProfession);
  158. $employeesPurchaseDepartment[$i]->basicSalary = $employeesPurchaseDepartment[$i]->getBasicSalaryByProfession($basicSalaryByProfession);
  159. $employeesPurchaseDepartment[$i]->pages = $employeesPurchaseDepartment[$i]->getPagesByProfession($pagesByProfession);
  160. }
  161. $number = $i + 2;
  162. for ($i; $i < $number; $i++) {
  163. $employeesPurchaseDepartment[$i] = new Employee('Закупки', 'Ma', false, 1);
  164. $employeesPurchaseDepartment[$i]->coffee = $employeesPurchaseDepartment[$i]->getCoffeeByProfession($coffeeByProfession);
  165. $employeesPurchaseDepartment[$i]->basicSalary = $employeesPurchaseDepartment[$i]->getBasicSalaryByProfession($basicSalaryByProfession);
  166. $employeesPurchaseDepartment[$i]->pages = $employeesPurchaseDepartment[$i]->getPagesByProfession($pagesByProfession);
  167. }
  168. $number = $i + 1;
  169. for ($i; $i < $number; $i++) {
  170. $employeesPurchaseDepartment[$i] = new Employee('Закупки', 'Me', true, 2);
  171. $employeesPurchaseDepartment[$i]->coffee = $employeesPurchaseDepartment[$i]->getCoffeeByProfession($coffeeByProfession);
  172. $employeesPurchaseDepartment[$i]->basicSalary = $employeesPurchaseDepartment[$i]->getBasicSalaryByProfession($basicSalaryByProfession);
  173. $employeesPurchaseDepartment[$i]->pages = $employeesPurchaseDepartment[$i]->getPagesByProfession($pagesByProfession);
  174. }
  175.  
  176. function getIndex($employees)
  177. {
  178. $salary = 0;
  179. $pages = 0;
  180. $coffee = 0;
  181. $department = 0;
  182. $employeesNumber = 0;
  183. $index = [];
  184. $i = 0;
  185. $department = $employees[$i]->department;
  186. for ($i; $i < count($employees); $i++) {
  187. $salary += $employees[$i]->getSalary();
  188. $pages += $employees[$i]->getPages();
  189. $coffee += $employees[$i]->getCoffee();
  190. }
  191. $employeesNumber = count($employees);
  192. $index = array(
  193. 'salary' => $salary,
  194. 'pages' => $pages,
  195. 'coffee' => $coffee,
  196. 'department' => $department,
  197. 'employeesNumber' => $employeesNumber
  198. );
  199. return $index;
  200. }
  201.  
  202. $purchaseDepartment = getIndex($employeesPurchaseDepartment);
  203.  
  204. // Заголовок таблицы
  205. echo padRight("Департамент", $col1) .
  206. padLeft("сотр.", $col2) .
  207. padLeft("тугр.", $col3) .
  208. padLeft("кофе", $col4) .
  209. padLeft("стр.", $col5) .
  210. padLeft("тугр./стр.", $col6) ."\n";
  211.  
  212. // Сама таблица
  213. echo padRight($purchaseDepartment['department'], $col1) .
  214. padLeft($purchaseDepartment['employeesNumber'], $col2) .
  215. padLeft($purchaseDepartment['salary'], $col3) .
  216. padLeft($purchaseDepartment['coffee'], $col4) .
  217. padLeft($purchaseDepartment['pages'], $col5) .
  218. padLeft(0, $col6) . "\n";
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
Департамент           сотр.       тугр.        кофе        стр.  тугр./стр.
Закупки                  17      9612.5         350        3100           0