fork(3) download
  1. <?php
  2.  
  3.  
  4. class Employee
  5. {
  6. public $name;
  7. public $rate;
  8. public $hours = array();
  9.  
  10. public function __construct($name, $rate)
  11. {
  12. $this->name = $name;
  13. $this->rate = $rate;
  14. }
  15.  
  16. public function getTotalHoursWorked()
  17. {
  18. return array_sum($this->hours);
  19. }
  20.  
  21. //без переработок
  22. public function getNormalHours()
  23. {
  24. $normalHours = 0;
  25. foreach($this->hours as $weeks)
  26. {
  27. if($weeks <= 40) {
  28. $normalHours += $weeks;
  29. } elseif($weeks > 40) {
  30. $normalHours = $normalHours + 40;
  31. }
  32. }
  33. return $normalHours;
  34. }
  35.  
  36. //c переработками
  37. public function getOvertimeHours()
  38. {
  39. $overtimeHours = 0;
  40. foreach($this->hours as $weeks)
  41. {
  42. if($weeks > 40) {
  43. $overtimeHours = $overtimeHours + ($weeks - 40);
  44. }
  45. }
  46. return $overtimeHours;
  47. }
  48.  
  49. public function getSalary()
  50. {
  51. $hours = $this->getNormalHours();
  52. $salary = $hours * $this->rate;
  53. $overtimeHours = $this->getOvertimeHours();
  54. $salary = $salary + $overtimeHours*$this->rate*2;
  55. return $salary;
  56. }
  57.  
  58. //получаю сокращённое имя
  59. public function getShortName()
  60. {
  61. $name = $this->name;
  62. $reg = "! ([a-яёА-ЯЁ])([a-яёА-ЯЁ])+!u";
  63. $shortenedName = preg_replace($reg, " $1.", $name);
  64. return $shortenedName;
  65. }
  66.  
  67.  
  68. }
  69.  
  70.  
  71. $ivan = new Employee("Иванов Иван", 10);
  72. $ivan->hours = array(40, 40, 40, 40);
  73.  
  74. $peter = new Employee("Петров Петр", 8);
  75. $peter->hours = array(40, 10, 40, 50);
  76.  
  77. $employees = array($ivan, $peter);
  78. /*
  79. foreach ($employees as $employee) {
  80.   echo "Имя: {$employee->name}\n";
  81.   echo "Ставка, тугриков в час: {$employee->rate}\n";
  82.   echo "Отработал, часов: {$employee->getTotalHoursWorked()}\n";
  83.   echo "Заработал, тугриков: {$employee->getSalary()}\n\n";
  84. }
  85. */
  86.  
  87. //пишу функцию для вставки пробелов справа
  88. function padRight($string, $length)
  89. {
  90. $spaces = $length - mb_strlen($string);
  91. if($spaces>0)
  92. {
  93. $string=mb_str_pad($string, mb_strlen($string) + $spaces, " ", STR_PAD_RIGHT);
  94. }
  95. return $string;
  96. }
  97.  
  98. //пишу функцию для вставки пробелов слева
  99. function padLeft($string, $length)
  100. {
  101. $spaces = $length - mb_strlen($string);
  102. if($spaces>0)
  103. {
  104. $string=mb_str_pad($string, mb_strlen($string) + $spaces, " ", STR_PAD_LEFT);
  105. }
  106. return $string;
  107. }
  108.  
  109. //функция аналог str_pad, только для кириллицы
  110. function mb_str_pad($str, $pad_len, $pad_str = ' ', $dir = STR_PAD_RIGHT, $encoding = NULL)
  111. {
  112. $encoding = $encoding === NULL ? mb_internal_encoding() : $encoding;
  113. $padBefore = $dir === STR_PAD_BOTH || $dir === STR_PAD_LEFT;
  114. $padAfter = $dir === STR_PAD_BOTH || $dir === STR_PAD_RIGHT;
  115. $pad_len -= mb_strlen($str, $encoding);
  116. $targetLen = $padBefore && $padAfter ? $pad_len / 2 : $pad_len;
  117. $strToRepeatLen = mb_strlen($pad_str, $encoding);
  118. $repeatTimes = ceil($targetLen / $strToRepeatLen);
  119. $repeatedString = str_repeat($pad_str, max(0, $repeatTimes));
  120. $before = $padBefore ? mb_substr($repeatedString, 0, floor($targetLen), $encoding) : '';
  121. $after = $padAfter ? mb_substr($repeatedString, 0, ceil($targetLen), $encoding) : '';
  122. return $before . $str . $after;
  123. }
  124.  
  125. $col1 = 30;
  126. $col2 = 10;
  127. $col3 = 10;
  128. $col4 = 10;
  129.  
  130. echo padRight("Сотрудник", $col1) .
  131. padLeft("Часы", $col2) .
  132. padLeft("Овертайм", $col2) .
  133. padLeft("Ставка", $col3) .
  134. padLeft("З/п", $col4) . "\n";
  135.  
  136. $totalHours = $totalSalary = $totalOvertime = 0;
  137. foreach ($employees as $employee) {
  138. echo padRight($employee->getShortName(), $col1) .
  139. padLeft($employee->getTotalHoursWorked(), $col2) .
  140. padLeft($employee->getOvertimeHours(), $col2) .
  141. padLeft($employee->rate, $col3) .
  142. padLeft($employee->getSalary(), $col4) . "\n";
  143. $totalHours += $employee->getTotalHoursWorked();
  144. $totalSalary += $employee->getSalary();
  145. $totalOvertime += $employee->getOvertimeHours();
  146.  
  147. }
  148.  
  149. //колонка ВСЕГО
  150. echo padRight("Всего", $col1) .
  151. padLeft("$totalHours", $col2) .
  152. padLeft("$totalOvertime", $col2) .
  153. padLeft(" ", $col3) .
  154. padLeft("$totalSalary", $col4) . "\n";
  155.  
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
Сотрудник                           Часы  Овертайм    Ставка       З/п
Иванов И.                            160         0        10      1600
Петров П.                            140        10         8      1200
Всего                                300        10                2800