<?php
class Employee
{
public $name; // имя-фамилия
public $rate; // часовая ставка (сколько он получает тугриков за час работы)
public function __construct($name, $rate)
{
// задаем имя и часовую ставку
$this->name = $name;
$this->rate = $rate;
}
public function getFullName()
{
return $this->name . " " . $this->getShortName();
}
public function getTotalHoursWorked()
{
// Просто складываем значения часов в массиве
}
public function getNormalHoursWorked()
{
return $this->getTotalHoursWorked() - $this->getOverHoursWorked();
}
public function getOverHoursWorked()
{
$overtime = 0;
foreach ($this->hours as $week) {
if ($week > 40) {
$overtime += $week - 40;
}
}
return $overtime;
}
/** Считает зарплату */
public function getSalary()
{
// Получаем число отработанных часов
$hoursNormal = $this->getNormalHoursWorked();
// и умножаем на часовую ставку
$salaryNormal = $hoursNormal * $this->rate;
$hoursOver = $this->getOverHoursWorked();
// и умножаем на часовую ставку
$salaryOver = $hoursOver * $this->rate * 2;
$salaryTotal = $salaryNormal + $salaryOver;
$salaryFull = $salaryTotal . " (". $salaryNormal . "+" . $salaryOver . ")";
return $salaryFull;
}
public function getShortName()
{
$names = explode(" ", $this->name); $initials = [];
foreach ($names as $name) {
}
$shortName = implode(".", $initials); return "(" . $shortName. ".)";
}
}
$ivan = new Employee("Иванов Иван", 10);
// name и rate заданы через конструктор, потому осталось лишь написать часы
$ivan->hours = array(40, 40, 40, 40); // Иван работает по 40 часов в неделю
$peter = new Employee("Петров Петр", 8);
$peter->hours = array(40, 10, 40, 50); // Петр взял отгул и потому отработал меньше часов,
$vlad = new Employee("Владимир Владимирович", 90);
$vlad->hours = [100,100,100,100];
$maria = new Employee("Мария Ивановна Петрова", 6);
$maria->hours = [60,10,20,30];
$employees = array($ivan, $peter, $vlad, $maria);
function padLeft ($string, $length) {
$spases = str_repeat(" ", ($length - $strLength - 1)); $string = "|" . $spases . $string;
return $string;
}
function padRight ($string, $length) {
$spases = str_repeat(" ", ($length - $strLength)); $string .= $spases;
return $string;
}
// Ширина колонок
$col1 = 35;
$col2 = 8;
$col3 = 10;
$col4 = 25;
// Заголовок таблицы
echo padRight("Сотрудник", $col1) .
padLeft("Часы", $col2) .
padLeft("Ставка", $col3) .
padLeft("Овертайм", $col3) .
padLeft("З/п", $col4) . "\n\n";
// Сама таблица
foreach ($employees as $employee) {
echo padRight($employee->getFullName(), $col1) .
padLeft($employee->getTotalHoursWorked(), $col2) .
padLeft($employee->rate, $col3) .
padLeft($employee->getOverHoursWorked(), $col3) .
padLeft($employee->getSalary(), $col4) . "\n";
}
PD9waHAKCmVycm9yX3JlcG9ydGluZygtMSk7CgpjbGFzcyBFbXBsb3llZSAgICAgICAgICAgICAKewogICAgcHVibGljICRuYW1lOyAgICAgICAgICAgICAgIC8vINC40LzRjy3RhNCw0LzQuNC70LjRjwogICAgcHVibGljICRyYXRlOyAgICAgICAgICAgICAgIC8vINGH0LDRgdC+0LLQsNGPINGB0YLQsNCy0LrQsCAo0YHQutC+0LvRjNC60L4g0L7QvSDQv9C+0LvRg9GH0LDQtdGCINGC0YPQs9GA0LjQutC+0LIg0LfQsCDRh9Cw0YEg0YDQsNCx0L7RgtGLKQogICAgcHVibGljICRob3VycyA9IGFycmF5KCk7CgogICAgCiAgICBwdWJsaWMgZnVuY3Rpb24gX19jb25zdHJ1Y3QoJG5hbWUsICRyYXRlKQogICAgewogICAgICAgIC8vINC30LDQtNCw0LXQvCDQuNC80Y8g0Lgg0YfQsNGB0L7QstGD0Y4g0YHRgtCw0LLQutGDCiAgICAgICAgJHRoaXMtPm5hbWUgPSAkbmFtZTsKICAgICAgICAkdGhpcy0+cmF0ZSA9ICRyYXRlOwogICAgfQoKCXB1YmxpYyBmdW5jdGlvbiBnZXRGdWxsTmFtZSgpCgl7CgkJcmV0dXJuICR0aGlzLT5uYW1lIC4gIiAiIC4gJHRoaXMtPmdldFNob3J0TmFtZSgpOwoJfQoKCXB1YmxpYyBmdW5jdGlvbiBnZXRUb3RhbEhvdXJzV29ya2VkKCkKICAgIHsKICAgICAgICAvLyDQn9GA0L7RgdGC0L4g0YHQutC70LDQtNGL0LLQsNC10L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