<?php
class Employee
{
public $name; // имя-фамилия
public $rate; // часовая ставка (сколько он получает тугриков за час работы)
public $fullName;
public function __construct($name, $rate)
{
// задаем имя и часовую ставку
$this->name = $name;
$this->rate = $rate;
$this->fullName = $this->name . " " . $this->getShortName();
}
public function getTotalHoursWorked()
{
// Просто складываем значения часов в массиве
}
public function getNormalHoursWorked()
{
$normal = 0;
foreach ($this->hours as $week) {
if ($week <= 40) {
$normal += $week;
}
}
return $normal;
}
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;
$salary = $salaryNormal + $salaryOver;
return $salary;
}
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("Владимир Владимирович", 9000);
$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 = array_fill(0, ($length - $strLength - 1), " "); $string = "|" . $spases . $string;
return $string;
}
function padRight ($string, $length) {
$spases = array_fill(0, ($length - $strLength), " "); $string .= $spases;
return $string;
}
// Ширина колонок
$col1 = 35;
$col2 = 12;
$col3 = 12;
$col4 = 12;
// Заголовок таблицы
echo padRight("Сотрудник", $col1) .
padLeft("Часы", $col2) .
padLeft("Ставка", $col3) .
padLeft("Овертайм", $col3) .
padLeft("З/п", $col4) . "\n\n";
// Сама таблица
foreach ($employees as $employee) {
echo padRight($employee->fullName, $col1) .
padLeft($employee->getTotalHoursWorked(), $col2) .
padLeft($employee->rate, $col3) .
padLeft($employee->getOverHoursWorked(), $col3) .
padLeft($employee->getSalary(), $col4) . "\n";
}
PD9waHAKCmVycm9yX3JlcG9ydGluZygtMSk7CgpjbGFzcyBFbXBsb3llZSAgICAgICAgICAgICAKewogICAgcHVibGljICRuYW1lOyAgICAgICAgICAgICAgIC8vINC40LzRjy3RhNCw0LzQuNC70LjRjwogICAgcHVibGljICRyYXRlOyAgICAgICAgICAgICAgIC8vINGH0LDRgdC+0LLQsNGPINGB0YLQsNCy0LrQsCAo0YHQutC+0LvRjNC60L4g0L7QvSDQv9C+0LvRg9GH0LDQtdGCINGC0YPQs9GA0LjQutC+0LIg0LfQsCDRh9Cw0YEg0YDQsNCx0L7RgtGLKQogICAgcHVibGljICRob3VycyA9IGFycmF5KCk7CiAgICBwdWJsaWMgJGZ1bGxOYW1lOyAKICAgIAogICAgcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KCRuYW1lLCAkcmF0ZSkKICAgIHsKICAgICAgICAvLyDQt9Cw0LTQsNC10Lwg0LjQvNGPINC4INGH0LDRgdC+0LLRg9GOINGB0YLQsNCy0LrRgwogICAgICAgICR0aGlzLT5uYW1lID0gJG5hbWU7CiAgICAgICAgJHRoaXMtPnJhdGUgPSAkcmF0ZTsKICAgICAgICAkdGhpcy0+ZnVsbE5hbWUgPSAkdGhpcy0+bmFtZSAuICIgIiAuICR0aGlzLT5nZXRTaG9ydE5hbWUoKTsKICAgIH0KCglwdWJsaWMgZnVuY3Rpb24gZ2V0VG90YWxIb3Vyc1dvcmtlZCgpCiAgICB7CiAgICAgICAgLy8g0J/RgNC+0YHRgtC+INGB0LrQu9Cw0LTRi9Cy0LDQtdC8INC30L3QsNGH0LXQvdC40Y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