fork(4) download
  1. <?php
  2. class Employee
  3. {
  4. public $name;
  5. public $rate;
  6. public $hours = array();
  7. public function __construct($name, $rate)
  8. {
  9. $this->name = $name;
  10. $this->rate = $rate;
  11. }
  12. public function getTotalHoursWorked()
  13. {
  14. return array_sum($this->hours);
  15. }
  16. public function getNormalHours()
  17. {
  18. $normal = 0;
  19. foreach ($this->hours as $week) {
  20. if ($week <= 40) {
  21. $normal = $week + $normal;
  22. } else {
  23. $normal = 40 + $normal;
  24. }
  25. }
  26. return $normal;
  27. }
  28. public function getOvertimeHours()
  29. {
  30. $overtime = 0;
  31. foreach ($this->hours as $week) {
  32. if ($week > 40) {
  33. $overtime = ($week - 40) + $overtime;
  34. } else {
  35. $overtime = $overtime + 0;
  36. }
  37. }
  38. return $overtime;
  39. }
  40. public function getSalary()
  41. {
  42. $hours = $this->getNormalHours();
  43. $overtime = $this->getOvertimeHours();
  44. $salary = $hours * $this->rate + 2 * $this->rate * $overtime;
  45. return $salary;
  46. }
  47. public function getShortName()
  48. {
  49. $firstletter = mb_substr($this->name, 0, 1) . ".";
  50. $reg = '/[а-яА-Яё]+\s/u';
  51. $shortname = preg_replace($reg, "", $this->name) . " " . $firstletter;
  52. return $shortname;
  53. }
  54. }
  55.  
  56. $ivan = new Employee('Иван Иванов', 10);
  57.  
  58.  
  59. $ivan->hours = array(
  60. 40,
  61. 40,
  62. 40,
  63. 40
  64. );
  65.  
  66. $peter = new Employee('Пётр Петров', 8); //с другими именами всё работает
  67.  
  68.  
  69. $peter->hours = array(
  70. 40,
  71. 10,
  72. 40,
  73. 50
  74. );
  75.  
  76. $employees = array(
  77. $ivan,
  78. $peter
  79. );
  80.  
  81. $col1 = 30;
  82. $col2 = 8;
  83. $col3 = 8;
  84. $col4 = 8;
  85.  
  86.  
  87. function padRight($string, $length)
  88. {
  89. while (mb_strlen($string) < $length) {
  90. $string = $string . " ";
  91.  
  92. }
  93. return $string;
  94. }
  95. function padLeft($string, $length)
  96. {
  97. while (mb_strlen($string) < $length) {
  98. $string = " " . $string;
  99. }
  100. return $string;
  101. }
  102.  
  103. //заголовок таблицы
  104.  
  105. echo padRight("Сотрудник", $col1) . padLeft("Ставка", $col2) . padLeft("Часы", $col3) . padLeft("Овертайм", 10) . padLeft("З/П", $col4) . "\n\n";
  106.  
  107. //сама таблица
  108. $hours = 0;
  109. $salary = 0;
  110. $overtime = 0;
  111.  
  112. foreach ($employees as $employee) {
  113. echo padRight($employee->getShortName(), $col1) . padLeft($employee->rate, $col2) . padLeft($employee->getTotalHoursWorked(), $col3) . padLeft($employee->getOverTimeHours(), 10) . padLeft($employee->getSalary(), $col4) . "\n";
  114. $hours = $hours + $employee->getTotalHoursWorked();
  115. $salary = $salary + $employee->getSalary();
  116. $overtime = $overtime + $employee->getOverTimeHours();
  117.  
  118. }
  119. echo padRight("Всего", 30) . padLeft("", $col2) . padLeft($hours, $col2) . padLeft($overtime, 10) . padLeft($salary, $col2);
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Сотрудник                       Ставка    Часы  Овертайм     З/П

Иванов И.                           10     160         0    1600
Петров П.                            8     140        10    1200
Всего                                      300        10    2800