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