3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); abstract class AbstractWorker{ public $rank; public $isBoss; public $coffee; public $salary; public $pages; public function __construct( $rank, $isBoss) { $this->rank = $rank; $this->isBoss = $isBoss; } public function getSalary(){ $salary = $this->salary; if ($this->rank == 2){ $salary = $this->salary * 1.25; } elseif ($this->rank == 3){ $salary = $this->salary * 1.5; } return $salary; } public function getCoffee(){ $coffee = $this->coffee; if ($this->isBoss == 1){ $coffee = $this->coffee * 2; } return $coffee; } public function getPages(){ $pages = $this->pages; if ($this->isBoss == 1){ $pages = $this->pages * 0; } return $pages; } } class Manager extends AbstractWorker{ public $coffee = 20; public $salary = 500; public $pages = 200; } class Marketer extends AbstractWorker{ public $coffee = 15; public $salary = 400; public $pages = 150; } class Engineer extends AbstractWorker{ public $coffee = 5; public $salary = 200; public $pages = 50; } class Analyst extends AbstractWorker{ public $coffee = 50; public $salary = 800; public $pages = 5; } class Department{ public $name; public $workers = array(); public function __construct($name) { $this->name = $name; } public function addWorker(AbstractWorker $worker){ $this->workers[] = $worker; } public function getNumberWorkers(){ return count($this->workers); } public function getDepartmentSalary(){ $totalSalary = 0; foreach ($this->workers as $worker) { $totalSalary += $worker->getSalary(); } return $totalSalary; } public function getDepartmentCoffee(){ $totalCoffee = 0; foreach ($this->workers as $worker) { $totalCoffee += $worker->getCoffee(); } return $totalCoffee; } public function getDepartmentPages(){ $totalPages = 0; foreach ($this->workers as $worker) { $totalPages += $worker->getPages(); } return $totalPages; } // public function getWorkers(){ // return $this->workers; // } public function getDepartmentName(){ return $this->name; } public function addWorkersToDepartment(string $profession, int $count, int $rang, int $isBoss){ if ($profession == "Manager"){ for ($i = 0; $i < $count; $i++){ //$this->addWorker(Manager $worker($rang, $isBoss)); $this->addWorker(new Manager($rang, $isBoss)); } } elseif ($profession == "Marketer"){ for ($i = 0; $i < $count; $i++){ $this->addWorker(new Marketer($rang, $isBoss)); } } elseif ($profession == "Engineer"){ for ($i = 0; $i < $count; $i++){ $this->addWorker(new Engineer ($rang, $isBoss)); } } elseif ($profession == "Analyst"){ for ($i = 0; $i < $count; $i++){ $this->addWorker(new Engineer ($rang, $isBoss)); } } } } class Company{ public $departments = array(); public function addDepartment(Department $department){ $this->departments[] = $department; } public function getDepartments(){ return $this->departments; } public function getDepartmentCount(){ return count($this->departments); } } class Tabel{ private function padLeft($value, $columnLength){ echo $value; echo str_repeat(" ", $columnLength - mb_strlen($value)); } private function calculatiotOfOutput(array $informarion){ $col1 = 15; $col2 = 10; $col3 = 10; $col4 = 8; $col5 = 8; $col6 = 15; echo $this->padLeft($informarion["name"], $col1) . $this->padLeft($informarion["count"], $col2) . $this->padLeft($informarion["salary"], $col3) . $this->padLeft($informarion["coffee"], $col4) . $this->padLeft($informarion["pages"], $col5) . $this->padLeft($informarion["salaryDividePages"], $col6) . "\n"; } public function printTabel(Company $company){ $columnName = array("name" => "Департамент", "count" => "сотр.", "salary" => "тугр.", "coffee" => "кофе", "pages" => "стр.", "salaryDividePages" => "тугр./стр."); $this->calculatiotOfOutput($columnName); $information = array("name" => "", "count" => 0, "salary" => 0, "coffee" => 0, "pages" => 0, "salaryDividePages" => 0); foreach ($company->getDepartments() as $department) { $information["name"] = $department->getDepartmentName(); $information["count"] = $department->getNumberWorkers(); $information["salary"] = $department->getDepartmentSalary(); $information["coffee"] = $department->getDepartmentCoffee(); $information["pages"] = $department->getDepartmentPages(); $information["salaryDividePages"] = round($department->getDepartmentSalary() / 2, 1); $this->calculatiotOfOutput($information); } echo "\n"; $totalInformation = array("name" => "Всего", "count" => 0, "salary" => 0, "coffee" => 0, "pages" => 0, "salaryDividePages" => 0); $totalInformation["name"] = "Всего"; foreach ($company->getDepartments() as $department) { $totalInformation["count"] += $department->getNumberWorkers(); $totalInformation["salary"] += $department->getDepartmentSalary(); $totalInformation["coffee"] += $department->getDepartmentCoffee(); $totalInformation["pages"] += $department->getDepartmentPages(); } $totalInformarion["salaryDividePages"] = round($totalInformation["salary"] / $totalInformation["pages"], 1); $this->calculatiotOfOutput($totalInformation); $averageInformation = array("name" => "Среднее", "count" => $totalInformation["count"] / $company->getDepartmentCount(), "salary" => $totalInformation["salary"] / $company->getDepartmentCount(), "coffee" => $totalInformation["coffee"] / $company->getDepartmentCount(), "pages" => $totalInformation["pages"] / $company->getDepartmentCount(), "salaryDividePages" => $totalInformation["salaryDividePages"] / $company->getDepartmentCount()); $this->calculatiotOfOutput($averageInformation); } } $vektor = new Company; $zakupki = new Department("Запуки"); $zakupki->addWorkersToDepartment("Manager", 5, 2, 0); $vektor->addDepartment($zakupki); $tabel = new Tabel; $tabel->printTabel($vektor);
Output for 7.0.0 - 7.0.25, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Департамент сотр. тугр. кофе стр. тугр./стр. Запуки 5 3125 100 1000 1562.5 Всего 5 3125 100 1000 0 Среднее 5 3125 100 1000 0
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Департамент сотр. тугр. кофе стр. тугр./стр. Запуки 5 3125 100 1000 1562.5 Всего 5 3125 100 1000 0 Среднее 5 3125 100 1000 0

preferences:
202.18 ms | 402 KiB | 224 Q