fork download
  1. <?php
  2.  
  3. /**
  4.  * input.php
  5.  */
  6.  
  7. $input = [
  8. // count, profession, grade, [chief]
  9.  
  10. 'Purchasing' => [
  11. [9, Employee::MANAGER, 1],
  12. [3, Employee::MANAGER, 2],
  13. [2, Employee::MANAGER, 3],
  14. [2, Employee::MARKETER, 1],
  15. [1, Employee::MANAGER, 2, true]
  16. ],
  17.  
  18. 'Sales' => [
  19. [12, Employee::MANAGER, 1],
  20. [6, Employee::MARKETER, 1],
  21. [3, Employee::ANALYST, 1],
  22. [2, Employee::ANALYST, 2],
  23. [1, Employee::MARKETER, 2, true]
  24. ],
  25.  
  26.  
  27. 'Advertising' => [
  28. [15, Employee::MARKETER, 1],
  29. [10, Employee::MARKETER, 2],
  30. [8, Employee::MANAGER, 1],
  31. [2, Employee::ENGINEER, 1],
  32. [1, Employee::MARKETER, 3, true]
  33. ],
  34.  
  35.  
  36. 'Logistics' => [
  37. [13, Employee::MANAGER, 1],
  38. [5, Employee::MANAGER, 2],
  39. [5, Employee::ENGINEER, 1],
  40. [1, Employee::MANAGER, 1, true]
  41. ]
  42.  
  43. ];
  44.  
  45. /**
  46.  * classes.php
  47.  */
  48.  
  49. class Company {
  50. protected array $departments;
  51. protected int $numOfDepts;
  52.  
  53. public function __construct(array $input) {
  54. $departments = [];
  55. foreach ($input as $department => $employees) {
  56. $departments[] = new Department($department, $employees);
  57. }
  58.  
  59. $this->departments = $departments;
  60. $this->numOfDepts = count($departments);
  61. }
  62.  
  63. public function getDepartments(): array {
  64. return $this->departments;
  65. }
  66.  
  67. public function getTotalStaff(): int {
  68. $staff = 0;
  69. foreach ($this->departments as $dept) {
  70. $staff += $dept->getStaffNumber();
  71. }
  72. return $staff;
  73. }
  74.  
  75. public function getTotalLaborCost() {
  76. $cost = 0;
  77. foreach ($this->departments as $dept) {
  78. $cost += $dept->getLaborCost();
  79. }
  80. return $cost;
  81. }
  82.  
  83. public function getTotalCoffeeDrunk() {
  84. $amount = 0;
  85. foreach ($this->departments as $dept) {
  86. $amount += $dept->getCoffeeDrunk();
  87. }
  88. return $amount;
  89. }
  90.  
  91. public function getTotalPagesProduced(): int {
  92. $pages = 0;
  93. foreach ($this->departments as $dept) {
  94. $pages += $dept->getPagesProduced();
  95. }
  96. return $pages;
  97. }
  98.  
  99. public function getTotalCostPerPage() {
  100. $cost = 0;
  101. foreach ($this->departments as $dept) {
  102. $cost += $dept->getCostPerPage();
  103. }
  104. return $cost;
  105. }
  106.  
  107. public function getAverageStaff() {
  108. $staff = 0;
  109. foreach ($this->departments as $dept) {
  110. $staff += $dept->getStaffNumber();
  111. }
  112. return $staff / $this->numOfDepts;
  113. }
  114.  
  115. public function getAverageLaborCost() {
  116. $cost = 0;
  117. foreach ($this->departments as $dept) {
  118. $cost += $dept->getLaborCost();
  119. }
  120. return $cost / $this->numOfDepts;
  121. }
  122.  
  123. public function getAverageCoffeeDrunk() {
  124. $amount = 0;
  125. foreach ($this->departments as $dept) {
  126. $amount += $dept->getCoffeeDrunk();
  127. }
  128. return $amount / $this->numOfDepts;
  129. }
  130.  
  131. public function getAveragePagesProduced() {
  132. $pages = 0;
  133. foreach ($this->departments as $dept) {
  134. $pages += $dept->getPagesProduced();
  135. }
  136. return $pages / $this->numOfDepts;
  137. }
  138.  
  139. public function getAverageCostPerPage() {
  140. $cost = 0;
  141. foreach ($this->departments as $dept) {
  142. $cost += $dept->getCostPerPage();
  143. }
  144. return $cost / $this->numOfDepts;
  145. }
  146.  
  147. public function printReport() {
  148. $firstCol = 20;
  149. $regCol = 15;
  150.  
  151. echo padString('DEPARTMENT', $firstCol) . padString('STAFF', $regCol, 'left') . padString('LABOR COST', $regCol, 'left') . padString('COFFEE DRUNK', $regCol, 'left') . padString('PAGES', $regCol, 'left') . padString('COST PER PAGE', $regCol, 'left') . "\n";
  152. echo padString('=', $firstCol, 'right', '=') . padString('=', $regCol, 'left', '=') . padString('=', $regCol, 'left', '=') . padString('=', $regCol, 'left', '=') . padString('=', $regCol, 'left', '=') . padString('=', $regCol, 'left', '=') . "\n";
  153.  
  154. foreach ($this->getDepartments() as $department) {
  155. echo padString($department->getDeptName(), $firstCol) . padString($department->getStaffNumber(), $regCol, 'left') . padString($department->getLaborCost(), $regCol, 'left') . padString($department->getCoffeeDrunk(), $regCol, 'left') . padString($department->getPagesProduced(), $regCol, 'left') . padString($department->getCostPerPage(), $regCol, 'left') . "\n";
  156. }
  157.  
  158. echo padString('=', $firstCol, 'right', '=') . padString('=', $regCol, 'left', '=') . padString('=', $regCol, 'left', '=') . padString('=', $regCol, 'left', '=') . padString('=', $regCol, 'left', '=') . padString('=', $regCol, 'left', '=') . "\n";
  159.  
  160. echo padString('TOTAL', $firstCol) . padString($this->getTotalStaff(), $regCol, 'left') . padString($this->getTotalLaborCost(), $regCol, 'left') . padString($this->getTotalCoffeeDrunk(), $regCol, 'left') . padString($this->getTotalPagesProduced(), $regCol, 'left') . padString($this->getTotalCostPerPage(), $regCol, 'left') . "\n";
  161.  
  162. echo padString('AVERAGE', $firstCol) . padString($this->getAverageStaff(), $regCol, 'left') . padString($this->getAverageLaborCost(), $regCol, 'left') . padString($this->getAverageCoffeeDrunk(), $regCol, 'left') . padString($this->getAveragePagesProduced(), $regCol, 'left') . padString($this->getAverageCostPerPage(), $regCol, 'left') . "\n";
  163. }
  164. }
  165.  
  166. class Department {
  167. protected string $name;
  168. protected array $staff;
  169.  
  170. public function __construct(string $name, array $employees) {
  171. $this->name = $name;
  172. $this->staff = [];
  173. foreach ($employees as $empGroup) {
  174. $empQuantity = $empGroup[0];
  175. $empType = $empGroup[1];
  176. $empGrade = $empGroup[2]; // 1
  177. $empChief = isset($empGroup[3]) ? true : false;
  178. for ($c = 0; $c < $empQuantity; $c++) {
  179. $this->staff[] = new $empType($empGrade, $empChief);
  180. }
  181. }
  182. }
  183.  
  184. public function getDeptName(): string {
  185. return $this->name;
  186. }
  187.  
  188. public function getStaffNumber(): int {
  189. $staffNumber = count($this->staff);
  190. return $staffNumber;
  191. }
  192.  
  193. public function getLaborCost(): int {
  194. $aggregateCost = 0;
  195. foreach ($this->staff as $employee) {
  196. $aggregateCost += $employee->getActualPay();
  197. }
  198. return $aggregateCost;
  199. }
  200.  
  201. public function getCoffeeDrunk(): int {
  202. $totalCoffeeDrunk = 0;
  203. foreach ($this->staff as $employee) {
  204. $totalCoffeeDrunk += $employee->getActualCoffeeConsumption();
  205. }
  206. return $totalCoffeeDrunk;
  207. }
  208.  
  209. public function getPagesProduced() {
  210. $totalPagesProduced = 0;
  211. foreach ($this->staff as $employee) {
  212. $totalPagesProduced += $employee->getActualPaperworkProduced();
  213. }
  214. return $totalPagesProduced;
  215. }
  216.  
  217. public function getCostPerPage() {
  218. $pages = $this->getPagesProduced();
  219. $laborCost = $this->getLaborCost();
  220. return round($laborCost / $pages, 2);
  221. }
  222. }
  223.  
  224. abstract class Employee {
  225. const MANAGER = "Manager";
  226. const MARKETER = "Marketer";
  227. const ENGINEER = "Engineer";
  228. const ANALYST = "Analyst";
  229.  
  230. protected int $grade;
  231. protected $baseRate;
  232. protected bool $chief;
  233. protected $baseCoffeeConsumption;
  234. protected int $basePaperworkProduced;
  235.  
  236. public function __construct(int $grade, bool $chief = false) {
  237. $this->grade = $grade;
  238. $this->chief = $chief;
  239. }
  240.  
  241. public function getActualPay() {
  242. $rate = $this->baseRate;
  243. if ($this->grade == 2) {
  244. $rate *= 1.25;
  245. } elseif ($this->grade == 3) {
  246. $rate = $rate * 1.5;
  247. }
  248.  
  249. return $this->chief ? $rate * 2 : $rate;
  250. }
  251.  
  252. public function getActualCoffeeConsumption() {
  253. return $this->chief ? $this->baseCoffeeConsumption * 2 : $this->baseCoffeeConsumption;
  254. }
  255.  
  256. public function getActualPaperworkProduced(): int {
  257. return $this->chief ? 0 : $this->basePaperworkProduced;
  258. }
  259. }
  260.  
  261. class Manager extends Employee {
  262. protected $baseRate = 500;
  263. protected $baseCoffeeConsumption = 20;
  264. protected int $basePaperworkProduced = 200;
  265. }
  266.  
  267. class Marketer extends Employee {
  268. protected $baseRate = 400;
  269. protected $baseCoffeeConsumption = 15;
  270. protected int $basePaperworkProduced = 150;
  271. }
  272.  
  273. class Engineer extends Employee {
  274. protected $baseRate = 200;
  275. protected $baseCoffeeConsumption = 5;
  276. protected int $basePaperworkProduced = 50;
  277. }
  278.  
  279. class Analyst extends Employee {
  280. protected $baseRate = 800;
  281. protected $baseCoffeeConsumption = 50;
  282. protected int $basePaperworkProduced = 5;
  283. }
  284.  
  285. /**
  286.  * padstring.php
  287.  */
  288.  
  289. function padString($string, $length, $side = "right", $pad = " ") {
  290. if (strlen($string) == $length) {
  291. return $string;
  292. } else {
  293. $charsNeeded = $length - strlen($string);
  294. $padding = str_repeat($pad, $charsNeeded);
  295. $side == "right" ? ($string = $string . $padding) : ($string = $padding . $string);
  296. return $string;
  297. }
  298. }
  299.  
  300. /**
  301.  * main.php
  302.  */
  303.  
  304. include('classes.php');
  305. include('input.php');
  306. include('padstring.php');
  307.  
  308. $company = new Company($input);
  309.  
  310. $company->printReport();
Runtime error #stdin #stdout #stderr 0.02s 24308KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Parse error:  syntax error, unexpected 'array' (T_ARRAY), expecting function (T_FUNCTION) or const (T_CONST) in /home/KWp8nS/prog.php on line 50