<?php
class Employee // employee значит «сотрудник»
{
public $name; // имя-фамилия
public $rate; // часовая ставка (сколько он получает тугриков за час работы)
public $hours = array(); // массив, содержащий отработанные часы по неделям public $overtimeHours = 0; // переменная, которая будет содержать данные о переработанных часах
/* Считает общее число отработанных часов */
public function getTotalHoursWorked()
{
// Просто складываем значения часов в массиве
}
/* "Волшебный" метод */
public function __construct($name, $rate)
{
// задаем имя и часовую ставку
$this->name = $name;
$this->rate = $rate;
}
public function getNormalHours() {
$normalHours = 0;
foreach ($this->hours as $hoursAWeek) {
if ($hoursAWeek <= 40) {
$normalHours += $hoursAWeek;
} if ($hoursAWeek > 40) {
$normalHours += $hoursAWeek - ($hoursAWeek - 40);
}
}
return $normalHours;
}
public function getOvertimeHours() {
$overtime = 0;
foreach ($this->hours as $hoursAWeek) {
if ($hoursAWeek > 40) {
$overtime += ($hoursAWeek - 40);
}
}
return $overtime;
}
/* Считает зарплату, включая и нормальные, и переработанные часы */
public function getSalary()
{
// Получаем число отработанных часов
$allHours = $this->getTotalHoursWorked();
// Получаем число нормальных часов
$normalHours = $this->getNormalHours();
// и умножаем на часовую ставку
$normalHoursSalary = $normalHours * $this->rate;
// Получаем число переработанных часов
$overtimeHours = $this->getOvertimeHours();
// и умножаем на удвоенную часовую ставку
$overtimeHoursSalary = $overtimeHours * ($this->rate * 2);
// Получаем общую зарплату сотрудника
$salary = $normalHoursSalary + $overtimeHoursSalary;
'allHours' => $allHours,
'normalHours' => $normalHours,
'normalHoursSalary' => $normalHoursSalary,
'overtimeHours' => $overtimeHours,
'overtimeHoursSalary' => $overtimeHoursSalary,
'salary' => $salary
);
return $allIndex;
}
}
function padRight($x, $y){
return $tableCell;
}
function padLeft($x, $y){
return $tableCell;
}
// Ширина колонок
$col1 = 15;
$col2 = $col4 = $col5 = 8;
$col3 = 10;
$ivan = new Employee("Иванов Иван", 10);
$ivan->hours = array(40, 40, 40, 40);
$peter = new Employee("Петров Петр", 8);
$peter->hours = array(40, 10, 40, 50); // но в последнюю неделю решил поработать побольше
$sidor = new Employee("Сидоров Сидор", 10);
$sidor->hours = array(40, 60, 40, 40);
$employees = array($ivan, $peter, $sidor);
// Заголовок таблицы
echo padRight("Сотрудник", $col1) .
padLeft("Часы", $col2) .
padLeft("Овертайм", $col3) .
padLeft("Ставка", $col4) .
padLeft("З/п", $col5) . "\n";
// Сама таблица
$hours = 0;
$overtime = 0;
$allSalary = 0;
foreach ($employees as $employee) {
$shortName = preg_replace('/( )([А-ЯЁ])([а-яё]+)/u', ' $2.', $employee->name); $allIndex = $employee->getSalary();
echo padRight($shortName, $col1) .
padLeft($allIndex['allHours'], $col2) .
padLeft($allIndex['overtimeHours'], $col3) .
padLeft($employee->rate, $col4) .
padLeft($allIndex['salary'], $col5) . "\n";
$hours += $allIndex['allHours'];
$overtime += $allIndex['overtimeHours'];
$allSalary += $allIndex['salary'];
}
echo padRight("Всего:", $col1) .
padLeft($hours, $col2) .
padLeft($overtime, $col3) .
padLeft(null, $col4) .
padLeft($allSalary, $col5) . "\n";
?>
PD9waHAKCmVycm9yX3JlcG9ydGluZygtMSk7Cm1iX2ludGVybmFsX2VuY29kaW5nKCJVVEYtOCIpOwoKY2xhc3MgRW1wbG95ZWUgICAgICAgICAgICAgIC8vIGVtcGxveWVlINC30L3QsNGH0LjRgiDCq9GB0L7RgtGA0YPQtNC90LjQusK7CnsKICAgIHB1YmxpYyAkbmFtZTsgICAgICAgICAgICAgICAvLyDQuNC80Y8t0YTQsNC80LjQu9C40Y8KICAgIHB1YmxpYyAkcmF0ZTsgICAgICAgICAgICAgICAvLyDRh9Cw0YHQvtCy0LDRjyDRgdGC0LDQstC60LAgKNGB0LrQvtC70YzQutC+INC+0L0g0L/QvtC70YPRh9Cw0LXRgiDRgtGD0LPRgNC40LrQvtCyINC30LAg0YfQsNGBINGA0LDQsdC+0YLRiykKICAgIHB1YmxpYyAkaG91cnMgPSBhcnJheSgpOyAgICAvLyDQvNCw0YHRgdC40LIsINGB0L7QtNC10YDQttCw0YnQu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