<?php
class Employee
{
public $name; // имя-фамилия
public $rate; // часовая ставка (сколько он получает тугриков за час работы)
public $hours = array(); // массив, содержащий отработанные часы по неделям
public $overtime = 0;
public function __construct($name, $rate)
{
// задаем имя и часовую ставку
$this->name = $name;
$this->rate = $rate;
}
/** Считает общее число отработанных часов */
public function getNormalHours()
{
// Просто складываем значения часов в массиве
return array_sum($this->hours);
}
public function getOvertimeHours() {
return $this->overtime;
}
/** Считает зарплату */
public function getSalary()
{
// Получаем число отработанных часов
$hours = $this->getNormalHours() - $this->overtime;
// и умножаем на часовую ставку
$salary = $hours * $this->rate + $this->overtime * $this->rate * 2;
return $salary;
}
public function getShortName() {
return preg_replace('/(?<=(?<=(?<=[а-яё])[ ])[А-ЯЁ])[а-яё]+/ui', ".", $this->name);
}
}
function printTable ($employees) {
// Ширина колонок
$col1 = 30;
$col2 = 8;
$col3 = 10;
$col4 = 8;
$col5 = 8;
$totalSelery = 0;
$totalNormalHours = 0;
$totalOvertimeHours = 0;
// Заголовок таблицы
echo padRight("Сотрудник", $col1) .
padLeft("Часы", $col2) .
padLeft("Овертайм", $col3) .
padLeft("Ставка", $col4) .
padLeft("З/п", $col5) . "\n\n";
// Сама таблица
foreach ($employees as $employee) {
echo padRight($employee->getShortName(), $col1) .
padLeft($employee->getNormalHours(), $col2) .
padLeft($employee->getOvertimeHours(), $col3) .
padLeft($employee->rate, $col4) .
padLeft($employee->getSalary(), $col5) . "\n";
$totalSelery += $employee->getSalary();
$totalNormalHours += $employee->getNormalHours();
$totalOvertimeHours += $employee->getOvertimeHours();
}
echo padRight("Всего", $col1) .
padLeft($totalNormalHours, $col2) .
padLeft($totalOvertimeHours, $col3) .
padLeft("", $col4) .
padLeft($totalSelery, $col5);
}
function padLeft ($field, $col) {
echo str_repeat(" ", $col - mb_strlen($field)).$field;
}
function padRight ($field, $col) {
echo $field.str_repeat(" ", $col - mb_strlen($field));
}
$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);
$peter->overtime = 10;
$employees = array();
$employees[] = $ivan;
$employees[] = $peter;
printTable($employees);
- Output for git.master, git.master_jit, rfc.property-hooks
- Сотрудник Часы Овертайм Ставка З/п
Иванов И. 160 0 10 1600
Петров П. 140 10 8 1200
Всего 300 10 2800
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
61.03 ms | 402 KiB | 8 Q