Advertisement
Guest User

Задача в "Объекты в PHP, часть 2"

a guest
Mar 19th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.17 KB | None | 0 0
  1. <html>
  2.     <head>
  3.     <meta charset="utf-8"/>
  4.         <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  5.     </head>
  6.     <body>
  7.  
  8.         <?php
  9.  
  10.         class Table {
  11.  
  12.            public $columns=0;
  13.            public $table = array();
  14.            public $tableBody;
  15.            public $id=0;
  16.            public $columnNames = array();
  17.  
  18.  
  19.             public function __construct($columns, $names)
  20.  
  21.             {
  22.                 $this->columns = $columns;
  23.  
  24.                 if(isset($names)) {
  25.                     $this->columnNames = $names;
  26.                 }
  27.  
  28.                 else {
  29.                     for($i=1;$i<=$this->columns;$i++) {
  30.                         $this->columnNames['column' . $i] = 'column' . $i;
  31.  
  32.                     }
  33.                 }
  34.  
  35.  
  36.             }
  37.  
  38.             public function addCell($item) {
  39.  
  40.                 $columnsTotal = $this->columns;
  41.                 $id = count($this->table['items'])+1;
  42.                 $this->table['items'][] = $item;
  43.  
  44.             }
  45.  
  46.             public function renderTable() {
  47.  
  48.                 echo "<table class='table'>";
  49.                 echo "<thead>";
  50.                 echo "<tr>";
  51.  
  52.  
  53.  
  54.                 foreach($this->columnNames as $key=>$value) {
  55.  
  56.                     echo "<th>{$value}</th>";
  57.  
  58.                 }
  59.  
  60.                 echo "</tr>";
  61.                 echo "</thead>";
  62.  
  63.                 echo "<tbody>";
  64.  
  65.  
  66.  
  67.                 foreach($this->table['items'] as $key => $value) {
  68.                     echo "<tr>";
  69.                     foreach($value as $cell) {
  70.                         echo "<td> {$cell} </td>";
  71.                     }
  72.                     echo "</tr>";
  73.                 }
  74.  
  75.                 echo "</tbody>";
  76.  
  77.  
  78.                 echo "</table>";
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.         }
  86.  
  87. }
  88.  
  89.         class Worker {
  90.  
  91.             public $firstName;
  92.             public $lastName;
  93.             public $middleName;
  94.             public $hourRate;
  95.             public $hours = array();
  96.  
  97.  
  98.             public function __construct($firstName,
  99.                                         $lastName,
  100.                                         $middleName,
  101.                                         $hourRate)
  102.  
  103.             {
  104.  
  105.              $this->firstName = $firstName;
  106.              $this->lastName = $lastName;
  107.              $this->middleName = $middleName;
  108.              $this->hourRate = $hourRate;
  109.  
  110.             }
  111.  
  112.             public function getTotalHours() {
  113.  
  114.                 return array_sum($this->hours);
  115.  
  116.             }
  117.  
  118.             public function getOverTimePay() {
  119.                 $needOverPay = 0;
  120.                 foreach($this->hours as $hoursPerWeek) {
  121.                     if($hoursPerWeek>40) {
  122.                         $payedTime = $hoursPerWeek - 40;
  123.                         $overTimePay = $payedTime * $hourRate;
  124.                         $needOverPay = $needOverPay + $overTimePay;
  125.                         return $needOverPay;
  126.  
  127.                     }
  128.                 }
  129.             }
  130.  
  131.             public function getOverTimeHours() {
  132.                 foreach($this->hours as $hoursPerWeek) {
  133.                     if($hoursPerWeek>40) {
  134.                         $overTime = $hoursPerWeek - 40;
  135.                     }
  136.                     else {
  137.                         $overTime = 0;
  138.                     }
  139.                     return $overTime;
  140.                     var_dump($overTime);
  141.                 }
  142.             }
  143.  
  144.             public function getFullName() {
  145.                 $fullName = $this->firstName . " " . $this->lastName . " ". $this->middleName;
  146.                 return $fullName;
  147.             }
  148.  
  149.             public function getShortName() {
  150.                 $shortName = $this->lastName . ' ' . iconv_substr($this->firstName, 0, 2) . '.';
  151.                 return $shortName;
  152.             }
  153.  
  154.             public function getSalary() {
  155.  
  156.                 $totalHours = $this->getTotalHours();
  157.                 $salary = $totalHours * $this->hourRate + $this->getOverTimePay();
  158.                 return $salary;
  159.  
  160.             }
  161.  
  162.             public function getInfo() {
  163.                 return array($this->getShortName(),
  164.                              $this->hourRate . ' руб в час',
  165.                              $this->getSalary() . ' руб',
  166.                              $this->getOverTimeHours() . ' ч',
  167.                              $this->getTotalHours() . ' ч');
  168.             }
  169.  
  170.       }
  171.  
  172. class Control {
  173.     public $totalSalary = 0;
  174.     public $totalOverTime = 0;
  175.     public $totalHours = 0;
  176.  
  177.     public function workersTotalData($workers) {
  178.         foreach($workers as $worker) {
  179.             $this->totalSalary = $this->totalSalary + $worker->getSalary();
  180.             $this->totalOverTime = $this->totalOverTime + $worker->getOverTimeHours();
  181.             $this->totalHours = $this->totalHours +  $worker->getTotalHours();
  182.         }
  183.         return array('totalSalary'=>$this->totalSalary,
  184.                      'totalOverTime'=>$this->totalOverTime,
  185.                      'totalHours'=>$this->totalHours);
  186.  
  187.     }
  188.  
  189. }
  190.  
  191. $ivan = new Worker("Иван", "Иванов", "Иванович", 12);
  192. $ivan->hours = array(100, 40, 40, 40);
  193.  
  194. $tagil = new Worker("Тагил", "Алексеев", "Рафисович", 15);
  195. $tagil->hours = array(100, 40, 40, 40);
  196.  
  197. $gamaz = new Worker("Гамаз", "Тугрев", "Тугрикович", 25);
  198. $gamaz->hours = array(120, 40, 40, 40);
  199.  
  200.  
  201. $workers = array($ivan, $tagil, $gamaz);
  202.  
  203. $columnNames = array('column1' => 'Сотрудник',
  204.                        'сolumn2'=>'Ставка',
  205.                        'column3'=>'ЗП',
  206.                        'column4' => 'Овертайм',
  207.                        'column5' =>'Всего часов');
  208.  
  209. $result = new Table($columns=5, $columnNames);
  210. foreach($workers as $worker) {
  211.     $result->AddCell($worker->getInfo());
  212.  
  213. }
  214. $report = new Control();
  215.  
  216. $report->workersTotalData($workers);
  217.  
  218. $result->AddCell(array('Всего', '-',
  219.                  $report->totalSalary . ' руб',
  220.                  $report->totalOverTime . ' ч',
  221.                  $report->totalHours . ' ч'));
  222.  
  223. $result->renderTable();
  224.  
  225.  
  226.  
  227.         ?>
  228.  
  229.     </body>
  230. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement