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

Иванов И.                          160       0      10    1600
Петров П.                          140      10       8    1200
Михайлов М.                        180      20      12    2400
Всего :                            480      30      30    5200