fork download
  1. <?php
  2.  
  3. /**
  4.  * classes.php
  5.  */
  6.  
  7. class Company {
  8. protected array $depts;
  9.  
  10. public function __construct(array $depts) {
  11. $this->depts = $depts;
  12. }
  13.  
  14. public function getDepts() {
  15. return $this->depts;
  16. }
  17.  
  18. public function getTotalStaffNumber() {
  19. $staffNumber = 0;
  20. foreach ($this->depts as $dept) {
  21. $staffNumber += $dept->getStaffNumber();
  22. }
  23. return $staffNumber;
  24. }
  25.  
  26. public function getTotalLaborCost() {
  27. $laborCost = 0;
  28. foreach ($this->depts as $dept) {
  29. $laborCost += $dept->getLaborCost();
  30. }
  31. return $laborCost;
  32. }
  33.  
  34. public function getTotalCoffeeConsumption() {
  35. $coffee = 0;
  36. foreach ($this->depts as $dept) {
  37. $coffee += $dept->getCoffeeConsumption();
  38. }
  39. return $coffee;
  40. }
  41.  
  42. public function getTotalPaperworkProduced() {
  43. $paperwork = 0;
  44. foreach ($this->depts as $dept) {
  45. $paperwork += $dept->getPaperworkProduced();
  46. }
  47. return $paperwork;
  48. }
  49.  
  50. public function getTotalCostPerPage() {
  51. $cost = 0;
  52. foreach ($this->depts as $dept) {
  53. $cost += $dept->getCostPerPage();
  54. }
  55. return $cost;
  56. }
  57.  
  58. public function getAverageStaffNumber() {
  59. return round($this->getTotalStaffNumber() / count($this->depts), 2);
  60. }
  61.  
  62. public function getAverageLaborCost() {
  63. return round($this->getTotalLaborCost() / count($this->depts), 2);
  64. }
  65.  
  66. public function getAverageCoffeeConsumption() {
  67. return round($this->getTotalCoffeeConsumption() / count($this->depts), 2);
  68. }
  69.  
  70. public function getAveragePaperworkProduced() {
  71. return round($this->getTotalPaperworkProduced() / count($this->depts), 2);
  72. }
  73.  
  74. public function getAverageCostPerPage() {
  75. return round($this->getTotalCostPerPage() / count($this->depts), 2);
  76. }
  77.  
  78. /**
  79. * should I use echo or is it better to put the entire report string in a var?
  80. */
  81. public function printReport() {
  82. $regcol = 15;
  83. $widecol = 20;
  84.  
  85. echo padString('DEPARTMENT', $widecol) . 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";
  86. echo padString('=', $widecol, 'right', '=') . padString('=', $regcol, 'right', '=') . padString('=', $regcol, 'right', '=') . padString('=', $regcol, 'right', '=') . padString('=', $regcol, 'right', '=') . padString('=', $regcol, 'right', '=') . "\n";
  87. foreach ($this->depts as $dept) {
  88. echo padString($dept->getName(), $widecol) . padString($dept->getStaffNumber(), $regcol, 'left') . padString($dept->getLaborCost(), $regcol, 'left') . padString($dept->getCoffeeConsumption(), $regcol, 'left') . padString($dept->getPaperworkProduced(), $regcol, 'left') . padString($dept->getCostPerPage(), $regcol, 'left') . "\n";
  89. }
  90. echo padString('=', $widecol, 'right', '=') . padString('=', $regcol, 'right', '=') . padString('=', $regcol, 'right', '=') . padString('=', $regcol, 'right', '=') . padString('=', $regcol, 'right', '=') . padString('=', $regcol, 'right', '=') . "\n";
  91. echo padString('TOTAL', $widecol) . padString($this->getTotalStaffNumber(), $regcol, 'left') . padString($this->getTotalLaborCost(), $regcol, 'left') . padString($this->getTotalCoffeeConsumption(), $regcol, 'left') . padString($this->getTotalPaperworkProduced(), $regcol, 'left') . padString($this->getTotalCostPerPage(), $regcol, 'left') . "\n";
  92. echo padString('AVERAGE', $widecol) . padString($this->getAverageStaffNumber(), $regcol, 'left') . padString($this->getAverageLaborCost(), $regcol, 'left') . padString($this->getAverageCoffeeConsumption(), $regcol, 'left') . padString($this->getAveragePaperworkProduced(), $regcol, 'left') . padString($this->getAverageCostPerPage(), $regcol, 'left') . "\n";
  93. }
  94. }
  95.  
  96. class Department {
  97. protected string $name;
  98. protected array $staff;
  99.  
  100. public function __construct($name) {
  101. $this->name = $name;
  102. }
  103.  
  104. public function getName() {
  105. return $this->name;
  106. }
  107.  
  108. public function addToStaff(Employee $employee) {
  109. $this->staff[] = $employee;
  110. }
  111.  
  112. public function getStaffNumber() {
  113. return count($this->staff);
  114. }
  115.  
  116. public function getLaborCost() {
  117. $laborCost = 0;
  118. foreach ($this->staff as $employee) {
  119. $laborCost += $employee->getActualPay();
  120. }
  121. return $laborCost;
  122. }
  123.  
  124. public function getCoffeeConsumption() {
  125. $coffee = 0;
  126. foreach ($this->staff as $employee) {
  127. $coffee += $employee->getActualCoffeeConsumption();
  128. }
  129. return $coffee;
  130. }
  131.  
  132. public function getPaperworkProduced() {
  133. $paperwork = 0;
  134. foreach ($this->staff as $employee) {
  135. $paperwork += $employee->getActualPaperworkProduced();
  136. }
  137. return $paperwork;
  138. }
  139.  
  140. public function getCostPerPage() {
  141. return round($this->getLaborCost() / $this->getPaperworkProduced(), 2);
  142. }
  143.  
  144. }
  145.  
  146. abstract class Employee {
  147. const MANAGER = "Manager";
  148. const MARKETER = "Marketer";
  149. const ENGINEER = "Engineer";
  150. const ANALYST = "Analyst";
  151.  
  152. protected int $grade;
  153. protected bool $chief;
  154.  
  155. public function __construct(int $grade, bool $chief = false) {
  156. $this->grade = $grade;
  157. $this->chief = $chief;
  158. }
  159.  
  160. abstract public function getBaseRate();
  161. abstract public function getBaseCoffeeConsumption();
  162. abstract public function getBasePaperworkProduced();
  163.  
  164. public function getActualPay() {
  165. $rate = $this->getBaseRate();
  166. if ($this->grade == 2) {
  167. $rate *= 1.25;
  168. } elseif ($this->grade == 3) {
  169. $rate = $rate * 1.5;
  170. }
  171.  
  172. return $this->chief ? $rate * 2 : $rate;
  173. }
  174.  
  175. public function getActualCoffeeConsumption() {
  176. return $this->chief ? $this->getBaseCoffeeConsumption() * 2 : $this->getBaseCoffeeConsumption();
  177. }
  178.  
  179. public function getActualPaperworkProduced(): int {
  180. return $this->chief ? 0 : $this->getBasePaperworkProduced();
  181. }
  182. }
  183.  
  184. class Manager extends Employee {
  185. protected $baseRate = 500;
  186. protected $baseCoffeeConsumption = 20;
  187. protected int $basePaperworkProduced = 200;
  188.  
  189. public function getBaseRate() {
  190. return $this->baseRate;
  191. }
  192.  
  193. public function getBaseCoffeeConsumption() {
  194. return $this->baseCoffeeConsumption;
  195. }
  196.  
  197. public function getBasePaperworkProduced() {
  198. return $this->basePaperworkProduced;
  199. }
  200. }
  201.  
  202. class Marketer extends Employee {
  203. protected $baseRate = 400;
  204. protected $baseCoffeeConsumption = 15;
  205. protected int $basePaperworkProduced = 150;
  206.  
  207. public function getBaseRate() {
  208. return $this->baseRate;
  209. }
  210.  
  211. public function getBaseCoffeeConsumption() {
  212. return $this->baseCoffeeConsumption;
  213. }
  214.  
  215. public function getBasePaperworkProduced() {
  216. return $this->basePaperworkProduced;
  217. }
  218. }
  219.  
  220. class Engineer extends Employee {
  221. protected $baseRate = 200;
  222. protected $baseCoffeeConsumption = 5;
  223. protected int $basePaperworkProduced = 50;
  224.  
  225. public function getBaseRate() {
  226. return $this->baseRate;
  227. }
  228.  
  229. public function getBaseCoffeeConsumption() {
  230. return $this->baseCoffeeConsumption;
  231. }
  232.  
  233. public function getBasePaperworkProduced() {
  234. return $this->basePaperworkProduced;
  235. }
  236. }
  237.  
  238. class Analyst extends Employee {
  239. protected $baseRate = 800;
  240. protected $baseCoffeeConsumption = 50;
  241. protected int $basePaperworkProduced = 5;
  242.  
  243. public function getBaseRate() {
  244. return $this->baseRate;
  245. }
  246.  
  247. public function getBaseCoffeeConsumption() {
  248. return $this->baseCoffeeConsumption;
  249. }
  250.  
  251. public function getBasePaperworkProduced() {
  252. return $this->basePaperworkProduced;
  253. }
  254. }
  255.  
  256. /**
  257.  * input.php
  258.  */
  259.  
  260. $input = [
  261. 'Purchasing' => [
  262. [9, Employee::MANAGER, 1],
  263. [3, Employee::MANAGER, 2],
  264. [2, Employee::MANAGER, 3],
  265. [2, Employee::MARKETER, 1],
  266. [1, Employee::MANAGER, 2, true]
  267. ],
  268.  
  269. 'Sales' => [
  270. [12, Employee::MANAGER, 1],
  271. [6, Employee::MARKETER, 1],
  272. [3, Employee::ANALYST, 1],
  273. [2, Employee::ANALYST, 2],
  274. [1, Employee::MARKETER, 2, true]
  275. ],
  276.  
  277.  
  278. 'Advertising' => [
  279. [15, Employee::MARKETER, 1],
  280. [10, Employee::MARKETER, 2],
  281. [8, Employee::MANAGER, 1],
  282. [2, Employee::ENGINEER, 1],
  283. [1, Employee::MARKETER, 3, true]
  284. ],
  285.  
  286.  
  287. 'Logistics' => [
  288. [13, Employee::MANAGER, 1],
  289. [5, Employee::MANAGER, 2],
  290. [5, Employee::ENGINEER, 1],
  291. [1, Employee::MANAGER, 1, true]
  292. ]
  293.  
  294. ];
  295.  
  296. /**
  297.  * padstring.php
  298.  */
  299.  
  300. function padString($string, $length, $side = "right", $pad = " ") {
  301. if (strlen($string) == $length) {
  302. return $string;
  303. } else {
  304. $charsNeeded = $length - strlen($string); // 5
  305. $padding = str_repeat($pad, $charsNeeded);
  306. ($side == "right") ? ($string = $string . $padding) : ($string = $padding . $string);
  307. return $string;
  308. }
  309. }
  310.  
  311. /**
  312.  * main.php
  313.  */
  314.  
  315. function makeDepts(array $input): array {
  316. $depts = [];
  317. foreach ($input as $dept => $staff) {
  318. $currentDept = new Department($dept);
  319. foreach ($staff as $employeeGroup) {
  320. $quantity = $employeeGroup[0];
  321. $type = $employeeGroup[1];
  322. $grade = $employeeGroup[2];
  323. $chief = isset($employeeGroup[3]) ? true : false;
  324. for ($c = 0; $c < $quantity; $c++) {
  325. $employeeObject = new $type($grade, $chief);
  326. $currentDept->addToStaff($employeeObject);
  327. }
  328. }
  329. $depts[] = $currentDept;
  330. }
  331. return $depts;
  332. }
  333.  
  334. $depts = makeDepts($input);
  335. $company = new Company($depts);
  336.  
  337. $company->printReport();
Runtime error #stdin #stdout #stderr 0.02s 24416KB
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/FWbVhX/prog.php on line 8