fork(1) download
  1.  
  2. <?php
  3. class Employee{
  4. public $name;
  5. public $rate;
  6. public $hours = array();
  7. public $overtime = array();
  8. public $normaltime = array();
  9. public function __construct($name,$rate) {
  10. $this->name = $name;
  11. $this->rate = $rate;
  12. }
  13. public function getTotalHoursWorked(){
  14. return array_sum($this->hours);
  15. }
  16. public function getSalary(){
  17. $hours = $this->getTotalHoursWorked();
  18. $salary= $hours*$this->rate;
  19. return $salary;
  20. }
  21. public function getShortName(){
  22.  
  23. $i= mb_strlen($this->name);
  24. while($this->name{$i}!=" "){
  25. $i--;
  26. if ($i<0) break;
  27. }
  28. $shortname = mb_substr($this->name, 0,$i-2 );
  29. return $this->name= $shortname.".";
  30. }
  31.  
  32. public function getNormalHours() {
  33.  
  34.  
  35.  
  36. }
  37. public function getOvertimeHours(){ //подсчет часов сверх нормы
  38. foreach ($this->hours as $key => $value) {
  39.  
  40.  
  41. if ($value >40){
  42.  
  43. $this->overtime[$key]= $value -40; }
  44. else {$this->overtime[$key]= 0 ;}
  45.  
  46.  
  47. }
  48. return $this->overtime;
  49. }
  50.  
  51. }
  52.  
  53.  
  54.  
  55. function padRight($string, $length){
  56. return $string.= str_repeat(" ", $length-mb_strlen($string));
  57.  
  58. }
  59. function padLeft($string, $length){
  60. $string = str_repeat(" ", $length-mb_strlen($string)) .$string;
  61. return $string;
  62. }
  63.  
  64.  
  65.  
  66. $ivan = new Employee("Иван Иванов", 10);
  67. $ivan ->hours = array(40,40,40,40);
  68. $peter = new Employee("Петр Петров", 8);
  69. $ivan-> getShortName();
  70. $peter-> getShortName();
  71. $peter ->hours = array(40,10,40,50);
  72. $peter ->getOvertimeHours();
  73. $employees = array($ivan,$peter);
  74.  
  75.  
  76.  
  77. $col1 = 30;
  78. $col2 = 8;
  79. echo"<pre>";
  80. echo padRight("Сотрудник", $col1).
  81. padLeft("Часы", $col2).
  82. padLeft("Ставка", $col2).
  83. padLeft("З/п", $col2). " <br>";
  84.  
  85. foreach ($employees as $employee) {
  86. echo padRight($employee->name, $col1) .
  87. padLeft($employee->getTotalHoursWorked(), $col2) .
  88. padLeft($employee->rate, $col2) .
  89. padLeft($employee->getSalary(), $col2) . "<br>";
  90.  
  91.  
  92. }
  93. var_dump($ivan->getOvertimeHours()); //подсчет часов сверх нормы
  94. ?>
  95.  
  96.  
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
        <pre>Сотрудник                         Часы  Ставка     З/п <br>Иван И.                            160      10    1600<br>Петр П.                            140       8    1120<br>array(4) {
  [0]=>
  int(0)
  [1]=>
  int(0)
  [2]=>
  int(0)
  [3]=>
  int(0)
}