fork download
  1. <?php
  2. abstract class Employee {
  3. protected $salary;
  4. protected $rank;
  5. protected $coffeeConsumption;
  6. protected $isBoss;
  7. protected $production;
  8. public function setRank($rank){
  9. switch ($rank){
  10. case 1:
  11. $this->rank = 1;
  12. break;
  13. case 2:
  14. $this->rank = 1.25;
  15. break;
  16. case 3:
  17. $this->rank = 1.75;
  18. break;
  19. default:
  20. //die("Employee rank must be between 1 and 3");
  21. $this->rank = 1;
  22. }
  23. }
  24. public function setSalary($baseSalary){
  25. $this->salary = $baseSalary;
  26. }
  27. public function setCoffeeConsumption($coffee){
  28. $this->coffeeConsumption = $coffee;
  29. }
  30. public function setProduction($prod){
  31. $this->production = $prod;
  32. }
  33. public function getSalary(){
  34. return ($this->isBoss)?$this->salary*$this->rank*1.5:$this->salary*$this->rank;
  35. }
  36. public function getCoffeeConsumption(){
  37. return ($this->isBoss)?$this->coffeeConsumption*2:$this->coffeeConsumption;
  38. }
  39. public function getProduction(){
  40. return $this->production;
  41. }
  42. public function getRank(){
  43. return $this->rank;
  44. }
  45. public function __construct($rank,$isBoss)
  46. {
  47. //$this->salary = $baseSalary;
  48. //$this->coffeeConsumption = $coffee;
  49. $this->setRank($rank);
  50. $this->isBoss = $isBoss;
  51. //$this->production = $production;
  52. }
  53. }
  54.  
  55. class Manager extends Employee{
  56. protected $coffeeConsumption = 20;
  57. protected $salary = 500;
  58. protected $production = 200;
  59. public function __construct($rank, $isBoss)
  60. {
  61. parent::__construct($rank, $isBoss);
  62. }
  63. }
  64. class Marketer extends Employee{
  65. protected $coffeeConsumption = 15;
  66. protected $salary = 400;
  67. protected $production = 150;
  68. public function __construct($rank, $isBoss)
  69. {
  70.  
  71. parent::__construct($rank, $isBoss);
  72. }
  73. }
  74. class Engineer extends Employee{
  75. protected $coffeeConsumption = 5;
  76. protected $salary = 200;
  77. protected $production = 50;
  78. public function __construct($rank, $isBoss)
  79. {
  80.  
  81. parent::__construct($rank, $isBoss);
  82. }
  83. }
  84. class Analyst extends Employee{
  85. protected $coffeeConsumption = 50;
  86. protected $salary = 800;
  87. protected $production = 5;
  88. public function __construct($rank, $isBoss)
  89. {
  90.  
  91. parent::__construct($rank, $isBoss);
  92. }
  93. }
  94.  
  95. class Department{
  96. private $employees = [];
  97. private $name;
  98. public function setName($name){
  99. $this->name = $name;
  100. }
  101. public function getName(){
  102. return $this->name;
  103. }
  104. public function addEmployee(Employee $e){
  105. array_push($this->employees,$e);
  106. }
  107. public function getEmployeesCount(){
  108. return sizeof($this->employees);
  109. }
  110. public function getEmployeesSalary(){
  111. $total = 0;
  112. if ($this->employees)
  113. foreach ($this->employees as $employee){
  114. $total+=$employee->getSalary();
  115. }
  116. return $total;
  117. }
  118. public function getEmployeesCoffeeConsumption(){
  119. $total = 0;
  120. if ($this->employees)
  121. foreach ($this->employees as $employee){
  122. $total+=$employee->getCoffeeConsumption();
  123. }
  124. return $total;
  125. }
  126. public function getEmployeesProduction(){
  127. $total = 0;
  128. if ($this->employees)
  129. foreach ($this->employees as $employee){
  130. $total+=$employee->getProduction();
  131. }
  132. return $total;
  133. }
  134. public function __construct($name)
  135. {
  136. $this->setName($name);
  137. }
  138. }
  139. class Writer {
  140. private function padLeft($title,$length){
  141. $strlen = strlen($title);
  142. if ($strlen<$length){
  143. $result = $title.str_repeat(' ',$length-$strlen);
  144. } else {
  145. $result = $title;
  146. }
  147. return $result;
  148. }
  149. private function padRight($title,$length){
  150. $strlen = strlen($title);
  151. if ($strlen<$length){
  152. $result = str_repeat(' ',$length-$strlen).$title;
  153. } else {
  154. $result = $title;
  155. }
  156. return $result;
  157. }
  158. public function writeDepartmentsInfo($depts){
  159. $totalSal = 0;
  160. $totalCount = 0;
  161. $totalCoffee = 0;
  162. $totalProd = 0;
  163. $totalPageCost = 0;
  164. $deptsCount = sizeof($depts);
  165. echo $this->padLeft("Department",30).
  166. $this->padRight("Employees",12).
  167. $this->padRight("Salary",12).
  168. $this->padRight("Coffee",12).
  169. $this->padRight("Pages",12).
  170. $this->padRight("Sal\\pgs",12)."\n";
  171. foreach ($depts as $d){
  172. $salary =$d->getEmployeesSalary() ;
  173. $prod = $d->getEmployeesProduction();
  174.  
  175. $totalCoffee+=$d->getEmployeesCoffeeConsumption();
  176. $totalCount+=$d->getEmployeesCount();
  177. $totalProd+=$prod;
  178. $totalSal+=$salary;
  179. $totalPageCost+= $salary/$prod;
  180. echo $this->padLeft($d->getName(),30).
  181. $this->padRight($d->getEmployeesCount(),12).
  182. $this->padRight($salary,12).
  183. $this->padRight($d->getEmployeesCoffeeConsumption(),12).
  184. $this->padRight($prod,12).
  185. $this->padRight(round($salary/$prod,2),12)."\n";
  186.  
  187. }
  188. echo $this->padLeft("Average",30).
  189. $this->padRight(round($totalCount/$deptsCount,2),12).
  190. $this->padRight(round($totalSal/$deptsCount,2),12).
  191. $this->padRight(round($totalCoffee/$deptsCount,2),12).
  192. $this->padRight(round($totalProd/$deptsCount,2),12).
  193. $this->padRight(round($totalPageCost/$deptsCount,2),12)."\n";
  194. echo $this->padLeft("Total",30).
  195. $this->padRight(round($totalCount,2),12).
  196. $this->padRight(round($totalSal,2),12).
  197. $this->padRight(round($totalCoffee,2),12).
  198. $this->padRight(round($totalProd,2),12).
  199. $this->padRight(round($totalPageCost,2),12)."\n";
  200. }
  201. }
  202.  
  203. $dpts = [];
  204. $deptZ = new Department('Buy');
  205. for ($i = 0;$i<9;$i++){
  206. $deptZ->addEmployee(new Manager(1,0));
  207. }
  208. for ($i = 0;$i<3;$i++){
  209. $deptZ->addEmployee(new Manager(2,0));
  210. }
  211. for ($i = 0;$i<2;$i++){
  212. $deptZ->addEmployee(new Manager(3,0));
  213. }
  214. for ($i = 0;$i<2;$i++){
  215. $deptZ->addEmployee(new Marketer(1,0));
  216. }
  217. $deptZ->addEmployee(new Manager(2,1));
  218. $dpts[]=$deptZ;
  219.  
  220. $deptZ = new Department('Sales');
  221. for ($i = 0;$i<12;$i++){
  222. $deptZ->addEmployee(new Manager(1,0));
  223. }
  224. for ($i = 0;$i<6;$i++){
  225. $deptZ->addEmployee(new Marketer(1,0));
  226. }
  227. for ($i = 0;$i<3;$i++){
  228. $deptZ->addEmployee(new Analyst(1,0));
  229. }
  230. for ($i = 0;$i<2;$i++){
  231. $deptZ->addEmployee(new Analyst(2,0));
  232. }
  233. $deptZ->addEmployee(new Manager(2,1));
  234. $dpts[]=$deptZ;
  235.  
  236. $deptZ = new Department('Marketing');
  237. for ($i = 0;$i<15;$i++){
  238. $deptZ->addEmployee(new Marketer(1,0));
  239. }
  240. for ($i = 0;$i<10;$i++){
  241. $deptZ->addEmployee(new Marketer(2,0));
  242. }
  243. for ($i = 0;$i<8;$i++){
  244. $deptZ->addEmployee(new Manager(1,0));
  245. }
  246. for ($i = 0;$i<2;$i++){
  247. $deptZ->addEmployee(new Engineer(1,0));
  248. }
  249. $deptZ->addEmployee(new Manager(3,1));
  250. $dpts[]=$deptZ;
  251.  
  252. $deptZ = new Department('Logistyc');
  253. for ($i = 0;$i<13;$i++){
  254. $deptZ->addEmployee(new Manager(1,0));
  255. }
  256. for ($i = 0;$i<5;$i++){
  257. $deptZ->addEmployee(new Manager(2,0));
  258. }
  259. for ($i = 0;$i<5;$i++){
  260. $deptZ->addEmployee(new Engineer(1,0));
  261. }
  262. $deptZ->addEmployee(new Manager(1,1));
  263. $dpts[]=$deptZ;
  264.  
  265. $writer = new Writer();
  266. //echo phpinfo();
  267.  
  268. $writer->writeDepartmentsInfo($dpts);
Success #stdin #stdout 0.02s 23588KB
stdin
Standard input is empty
stdout
Department                       Employees      Salary      Coffee       Pages     Sal\pgs
Buy                                     17      9862.5         350        3300        2.99
Sales                                   24     13737.5         620        3525         3.9
Marketing                               36     16712.5         585        5650        2.96
Logistyc                                24       11375         425        4050        2.81
Average                              25.25    12921.88         495     4131.25        3.16
Total                                  101     51687.5        1980       16525       12.65