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

Иванов И.                                  160              10            1600
Петров П.                                  140               8            1120
Всего :                                    300              18            2720