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. public function nameTrimmer(){
  50. $firstLetter = substr($this->name, 0, 1);
  51. $regexp = '/([a-zA-Z])* /';
  52. $replacement = $firstLetter;
  53. $trimmedName = preg_replace($regexp, $replacement . ". ", $this->name);
  54. return $trimmedName;
  55.  
  56. }
  57. }
  58.  
  59. function padRight($string, $length)
  60. {
  61.  
  62. $string = str_pad ( $string, $length, " " );
  63. return $string;
  64.  
  65. }
  66.  
  67. function padLeft($string, $length)
  68. {
  69.  
  70. $string = str_pad ( $string, $length, " ", STR_PAD_LEFT );
  71. return $string;
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79. $petya = new Employee("Peter Sunberg", 10);
  80. $petya->hours = array(40, 50, 10, 40);
  81.  
  82. $tom = new Employee("Tomas Holloway", 8);
  83. $tom->hours = array(40, 40, 40, 40);
  84.  
  85. $saymon = new Employee("Saymon Smit", 15);
  86. $saymon->hours = array(50, 60, 50, 50);
  87.  
  88. $employees = array($petya, $tom, $saymon);
  89.  
  90.  
  91.  
  92. $col1 = 30;
  93. $col2 = 8;
  94. $col3 = 10;
  95. $col4 = 8;
  96. $col5 = 8;
  97.  
  98. echo padRight("Name", $col1) .
  99. padLeft("Hours", $col2) .
  100. padLeft("Overdraft", $col3) .
  101. padLeft("Rate", $col4) .
  102. padLeft("Salary", $col5) . "\n";
  103. echo str_repeat("_", 64) . "\n";
  104.  
  105. foreach ($employees as $employee) {
  106. echo padRight($employee->nameTrimmer(), $col1) .
  107. padLeft($employee->getTotalHoursWorked(), $col2) .
  108. padLeft($employee->isOverdrafted(), $col3) .
  109. padLeft($employee->rate, $col4) .
  110. padLeft($employee->getTotalPayment(), $col5) . "\n";
  111. }
  112.  
  113. echo str_repeat("_", 64);
  114.  
  115.  
Success #stdin #stdout 0.02s 24084KB
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
________________________________________________________________