3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php error_reporting(-1); abstract class AbstractWorker{ private $rank; private $isBoss; private $coffee; private $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);
based on ZYG6d