fork(1) download
  1. <?php
  2.  
  3.  
  4. class Employee
  5. {
  6. public $name;
  7. public $rate;
  8. public $hours = array(); //hours worked in a week
  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. public function getNormalHours()
  22. {
  23. $normalHours = 0;
  24.  
  25. foreach ($this->hours as $weekHours) {
  26. if ($weekHours <= 40) {
  27. $normalHours += $weekHours;
  28. } else {
  29. $normalHours += 40;
  30. }
  31. }
  32. return $normalHours;
  33. }
  34.  
  35. public function getOvertimeHours()
  36. {
  37. $overtimeHours = $this->getTotalHoursWorked() - $this->getNormalHours();
  38. return $overtimeHours;
  39. }
  40.  
  41. public function getSalary()
  42. {
  43. $normalHours = $this->getNormalHours();
  44. $overtimeHours = $this->getOvertimeHours();
  45. $salary = $normalHours * $this->rate;
  46. $salary += $overtimeHours * $this->rate*2;
  47. return $salary;
  48. }
  49.  
  50. public function getShortName()
  51. {
  52. $shortName = preg_replace("/([а-яёА-ЯЁ]+\s[а-яёА-ЯЁ])[а-яёА-ЯЁ]+/ui", '$1.', $this->name);
  53. return $shortName;
  54. }
  55. }
  56.  
  57. $ivan = new Employee("Иванов Иван", 10);
  58. $ivan->hours = array(40, 40, 40, 40);
  59.  
  60. $petr = new Employee("Петров Петр", 8);
  61. $petr->hours = array(40, 10, 40, 50);
  62.  
  63. $employees = array($ivan, $petr);
  64.  
  65. $col1 = 20;
  66. $col2 = 8;
  67. $col3 = 8;
  68. $col4 = 8;
  69. $col5 = 8;
  70.  
  71. function padRight($string, $length)
  72. {
  73. $padding = $length - mb_strlen($string);
  74. $string .= str_repeat(" ", $padding);
  75. return $string;
  76. }
  77.  
  78. function padLeft($string, $length)
  79. {
  80. $padding = $length - mb_strlen($string)+2;
  81. $string = str_repeat(" ", $padding) . $string;
  82. return $string;
  83. }
  84.  
  85. $totalHours = 0;
  86. $totalOvertimeHours = 0;
  87. $totalSalary = 0;
  88.  
  89. //header
  90. echo padRight("Сотрудник", $col1) .
  91. padLeft("Часы", $col2) .
  92. padLeft("Овертайм", $col3) .
  93. padLeft("Ставка", $col4) .
  94. padLeft("З/п", $col5) . "\n\n";
  95.  
  96. //table
  97. foreach ($employees as $employee) {
  98. echo padRight($employee->getShortName(), $col1) .
  99. padLeft($employee->getTotalHoursWorked(), $col2) .
  100. padLeft($employee->getOvertimeHours(), $col3) .
  101. padLeft($employee->rate, $col4) .
  102. padLeft($employee->getSalary(), $col5) . "\n";
  103. $totalHours += $employee->getTotalHoursWorked();
  104. $totalOvertimeHours += $employee->getOvertimeHours();
  105. $totalSalary += $employee->getSalary();
  106. }
  107.  
  108. //footer
  109. echo padRight("Всего", $col1) .
  110. padLeft($totalHours, $col2) .
  111. padLeft($totalOvertimeHours, $col3) .
  112. padLeft(" ", $col4) .
  113. padLeft($totalSalary, $col5) . "\n";
  114.  
  115.  
Success #stdin #stdout 0s 52488KB
stdin
Standard input is empty
stdout
Сотрудник                 Часы  Овертайм    Ставка       З/п

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