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