fork download
  1. <?php
  2.  
  3. class Employee
  4. {
  5.  
  6. public $name;
  7. public $rate;
  8. public $hours = array();
  9.  
  10. public function __construct($name, $rate)
  11. {
  12.  
  13. $this->name = $name;
  14. $this->rate = $rate;
  15.  
  16. }
  17.  
  18. public function getTotalHoursWorked()
  19. {
  20.  
  21. return array_sum($this->hours);
  22.  
  23. }
  24.  
  25. public function isOverdrafted()
  26. {
  27. $totalOverdraft = 0;
  28. $overdraft = 0;
  29. foreach($this->hours as $hour) {
  30. if($hour > 40) {
  31. $overdraft = $hour - 40;
  32. } else {
  33. $overdraft = 0;
  34. }
  35. $totalOverdraft += $overdraft;
  36. }
  37. return $totalOverdraft;
  38. }
  39.  
  40. public function getTotalPayment()
  41. {
  42.  
  43. $hours = $this->getTotalHoursWorked();
  44. $salary = ($hours - $this->isOverdrafted()) * $this->rate + ($this->isOverdrafted() * ($this->rate *= 2));
  45. return $salary;
  46.  
  47. }
  48.  
  49.  
  50. public function nameTrimmer(){
  51. $firstLetter = substr($this->name, 0, 1);
  52. $regexp = '/([a-zA-Z])* /';
  53. $replacement = $firstLetter;
  54. $trimmedName = preg_replace($regexp, $replacement . ". ", $this->name);
  55. return $trimmedName;
  56.  
  57. }
  58. }
  59.  
  60. function padRight($string, $length)
  61. {
  62.  
  63. $string = str_pad ( $string, $length, " " );
  64. return $string;
  65.  
  66. }
  67.  
  68. function padLeft($string, $length)
  69. {
  70.  
  71. $string = str_pad ( $string, $length, " ", STR_PAD_LEFT );
  72. return $string;
  73.  
  74. }
  75.  
  76. $petya = new Employee("Peter Sunberg", 10);
  77. $petya->hours = array(40, 50, 10, 40);
  78.  
  79. $tom = new Employee("Tomas Holloway", 8);
  80. $tom->hours = array(40, 40, 40, 40);
  81.  
  82. $saymon = new Employee("Saymon Smit", 15);
  83. $saymon->hours = array(50, 60, 50, 50);
  84.  
  85. $employees = array($petya, $tom, $saymon);
  86.  
  87. foreach ($employees as $employee) {
  88. $allRates += $employee->rate;
  89. $totalHours += $employee->getTotalHoursWorked();
  90. $totalOverdraft += $employee->isOverdrafted();
  91. $totalSalary += $employee->getTotalPayment();
  92. }
  93.  
  94. $avarageRate = $allRates / count($employees);
  95. $avarageHours = $totalHours / count($employees);
  96. $avarageOverdraft = $totalOverdraft / count($employees);
  97. $avarageSalary = round($totalSalary / count($employees));
  98.  
  99. $col1 = 30;
  100. $col2 = 8;
  101. $col3 = 10;
  102. $col4 = 8;
  103. $col5 = 8;
  104.  
  105.  
  106. echo padRight("Name", $col1) .
  107. padLeft("Hours", $col2) .
  108. padLeft("Overdraft", $col3) .
  109. padLeft("Rate", $col4) .
  110. padLeft("Salary", $col5) . "\n";
  111. echo str_repeat("_", 64) . "\n";
  112.  
  113. foreach ($employees as $employee) {
  114.  
  115. echo padRight($employee->nameTrimmer(), $col1) .
  116. padLeft($employee->getTotalHoursWorked(), $col2) .
  117. padLeft($employee->isOverdrafted(), $col3) .
  118. padLeft($employee->rate / 2, $col4) .
  119. padLeft($employee->getTotalPayment() / 2, $col5) . "\n";
  120.  
  121. }
  122.  
  123. echo str_repeat("_", 64) . "\n";
  124.  
  125. echo padRight("Total:", $col1) .
  126. padLeft($totalHours, $col2) .
  127. padLeft($totalOverdraft, $col3) .
  128. padLeft(" ", $col4) .
  129. padLeft($totalSalary, $col5) . "\n";
  130.  
  131. echo str_repeat("_", 64) . "\n";
  132.  
  133. echo padRight("Avarage:", $col1) .
  134. padLeft($avarageHours, $col2) .
  135. padLeft($avarageOverdraft, $col3) .
  136. padLeft($avarageRate, $col4) .
  137. padLeft($avarageSalary, $col5) . "\n";
  138.  
  139.  
  140.  
  141.  
Success #stdin #stdout #stderr 0.01s 23688KB
stdin
Standard input is empty
stdout
Name                             Hours Overdraft    Rate  Salary
________________________________________________________________
P. Sunberg                         140        10      10    1500
T. Holloway                        160         0       8    1280
S. Smit                            210        50      15    3900
________________________________________________________________
Total:                             510        60            6680
________________________________________________________________
Avarage:                           170        20      11    2227
stderr
PHP Notice:  Undefined variable: allRates in /home/FAy6n4/prog.php on line 88
PHP Notice:  Undefined variable: totalHours in /home/FAy6n4/prog.php on line 89
PHP Notice:  Undefined variable: totalOverdraft in /home/FAy6n4/prog.php on line 90
PHP Notice:  Undefined variable: totalSalary in /home/FAy6n4/prog.php on line 91