fork(1) download
  1. <?php
  2.  
  3.  
  4. class Report
  5. {
  6. private $col1 = 25;
  7. private $col2 = 15;
  8.  
  9. private function padLeft($string, $length)
  10. {
  11. $countSpace = $length - strlen($string); //количество добавляемых в ячейку пробелов
  12. return str_repeat(' ',$countSpace) . $string;
  13. }
  14.  
  15. private function padRight($string, $length)
  16. {
  17. $countSpace = $length - strlen($string); //количество добавляемых в ячейку пробелов
  18. return $string . str_repeat(' ',$countSpace);
  19. }
  20.  
  21. public function writeTable(Company $company)
  22. {// Заголовок таблицы
  23. echo $this->padRight("Departmen", $this->col1) .
  24. $this->padLeft("employe", $this->col2) .
  25. $this->padLeft("money", $this->col2) .
  26. $this->padLeft("coffee", $this->col2) .
  27. $this->padLeft("lists", $this->col2) .
  28. $this->padLeft("money/lists", $this->col2) . "\n\n";
  29.  
  30. $departmentsCompany = $company->getDepartments();
  31.  
  32. // Сама таблица
  33. foreach ($departmentsCompany as $departmentCompany) {
  34. echo $this->padRight($departmentCompany->getName(), $this->col1) .
  35. $this->padLeft($departmentCompany->getEmployeCount(), $this->col2) .
  36. $this->padLeft($departmentCompany->getSalary(), $this->col2) .
  37. $this->padLeft($departmentCompany->getCoffeeCount(), $this->col2) .
  38. $this->padLeft($departmentCompany->getDocumentsCount(), $this->col2) .
  39. $this->padLeft($departmentCompany->getMoneyPerList(), $this->col2) . "\n";
  40. }
  41. }
  42.  
  43. }
  44.  
  45. class Company
  46. {
  47. protected $departments = array();//массив депортаментов
  48. protected $professions = array();//массив профессий
  49.  
  50. public function __construct()
  51. {
  52. //создаются список профессий востребованных в этой компании b
  53. $this->professions[] = new Manager(); // 'MN'
  54. $this->professions[] = new Marketer(); // 'MR'
  55. $this->professions[] = new Engineer(); // 'EN'
  56. $this->professions[] = new Analyst(); // 'AN';
  57. }
  58.  
  59. public function createDepartment(string $name, array $employeesData)
  60. {
  61. $employees = array();
  62.  
  63. foreach ($employeesData as $employeeStack)
  64. {
  65. $employeCount = $employeeStack[0];
  66. $employeProfession = $employeeStack[1];
  67. $employeRang = $employeeStack[2];
  68.  
  69. if ($employeCount == "BOSS") { //Босс у меня это такое количество. Ага, да.
  70.  
  71. $employees[] = $this->createEmploye($employeProfession, $employeRang, "BOSS");
  72. } else {
  73. for ($i=0; $i < $employeCount; $i++) {
  74. $employees[] = $this->createEmploye($employeProfession, $employeRang);
  75. }
  76. }
  77.  
  78. }
  79.  
  80. $department = new Department($name, $employees);
  81.  
  82. $this->departments[] = $department;
  83.  
  84. }
  85.  
  86. public function getDepartments()
  87. {
  88. return $this->departments;
  89. }
  90.  
  91. //приходит заявка на создаваемую профессию. Строчка и цифра например - ('en', 2), то функция делает engineera ранга 2
  92. public function createEmploye(string $professionName, int $rang, string $isBoss = "NOTBOSS")
  93. {
  94.  
  95. foreach ($this->professions as $profession) {
  96.  
  97. //создает работника требуемой профессии.
  98. if ($profession->getName() == $professionName) {
  99. $employe = new Employe($rang, $profession);
  100. if ($isBoss == "BOSS") {
  101. $employe->appointBoss();
  102. }
  103. }
  104. }
  105. return $employe;
  106. }
  107. }
  108.  
  109. class Department
  110. {
  111. private $name;
  112. private $employees = array();//массив из работников этого департамента
  113.  
  114. //согласно массиву-шаблону создаём работников департаменты и про
  115. public function __construct(string $name, array $employees)
  116. {
  117. $this->name = $name;
  118. $this->employees = $employees;
  119. }
  120.  
  121. public function getName()
  122. {
  123. return $this->name;
  124. }
  125.  
  126. public function getEmployees()
  127. {
  128. return $this->employees;
  129. }
  130.  
  131. public function getEmployeCount()
  132. {
  133. return count($this->employees);
  134. }
  135.  
  136. public function getSalary()
  137. {
  138. $salary = 0;
  139. foreach ($this->employees as $employe) {
  140. $salary = $salary + $employe->getSalary();
  141. }
  142. return $salary;
  143. }
  144.  
  145. public function getCoffeeCount()
  146. {
  147. $coffee = 0;
  148. foreach ($this->employees as $employe) {
  149. $coffee = $coffee + $employe->getCoffee();
  150. }
  151. return $coffee;
  152. }
  153.  
  154. public function getDocumentsCount()
  155. {
  156. $listCount = 0;
  157. foreach ($this->employees as $employe) {
  158.  
  159.  
  160. $listCount = $listCount + $employe->getDocumentsCount();
  161. }
  162. return $listCount;
  163. }
  164.  
  165. public function getMoneyPerList()
  166. {
  167. if ($this->getDocumentsCount() == 0) {
  168. }
  169. return round(($this->getSalary() / $this->getDocumentsCount()), 1);
  170. }
  171.  
  172. }
  173.  
  174. class Employe
  175. {
  176. private $profession;
  177. private $employeLevel;
  178. private $isBoss;
  179.  
  180. public function __construct(int $employeLevel, Profession $profession, bool $isBoss = false)
  181. {
  182. $this->employeLevel = $employeLevel;
  183. $this->profession = $profession;
  184. $this->isBoss = $isBoss;
  185. }
  186.  
  187. public function appointBoss()
  188. {
  189. $this->isBoss = true;
  190. }
  191.  
  192. //определяет является ли сотрудник боссом
  193. public function isBoss()
  194. {
  195. return $this->isBoss;
  196. }
  197.  
  198. //расчёт зарплаты сотрудника
  199. public function getSalary()
  200. {
  201. $rateMoneyProfession = $this->profession->getMoneyRate(); //узнаём коэфициент зарплаты для его професси
  202. $rateLevel = $this->employeLevel * 0.25 + 0.75; //узнаём коэфициент зарплаты для его ранга
  203. if ($this->isBoss())
  204. { //является ли сотрудник боссом департамента
  205. $bossRate = 1.5; //и устанавливаем соответствующую ставку
  206. } else {
  207. $bossRate = 1;
  208. }
  209. return $rateMoneyProfession * $rateLevel * $bossRate;
  210. }
  211.  
  212. //расчёт потребления кофе сотрудником
  213. public function getCoffee()
  214. {
  215. $cofeeRate = $this->profession->getCoffeeRate(); //узнаём коэфициент потребления кофе для его професси
  216.  
  217. if ($this->isBoss()) { //является ли сотрудник боссом департамента
  218. $bossRate = 2; //и устанавливаем соответствующее поглощение кофе для босса
  219. } else {
  220. $bossRate = 1;
  221. }
  222. return $cofeeRate * $bossRate;
  223. }
  224.  
  225. public function getDocumentsCount()
  226. {
  227. $documentsCount = $this->profession->getDocumentsCount();
  228. if ($this->isBoss()) { //является ли сотрудник боссом департамента
  229. $bossRate = 0; //босс не занимается бумагами
  230. } else {
  231. $bossRate = 1; //если простой работник то придётся заниматся чертежами отчётами и т.п.
  232. }
  233. return $bossRate * $documentsCount;
  234. }
  235.  
  236. }
  237.  
  238. abstract class Profession
  239. {
  240. private $name;
  241. private $moneyRate;
  242. private $coffeeRate;
  243. private $documentsCount;
  244.  
  245. abstract function getDocumentsCount();
  246.  
  247. abstract function getName();
  248.  
  249. abstract function getCoffeeRate();
  250.  
  251. abstract function getMoneyRate();
  252. }
  253.  
  254. Class Manager Extends Profession
  255. {
  256. private $name = 'MN';
  257. private $documentsCount = 200; //Манагер делает 200 листов отчёта
  258. private $moneyRate = 500;
  259. private $coffeeRate = 20;
  260.  
  261. public function getName()
  262. {
  263. return $this->name;
  264. }
  265.  
  266. public function getCoffeeRate()
  267. {
  268. return $this->coffeeRate;
  269. }
  270.  
  271. public function getMoneyRate()
  272. {
  273. return $this->moneyRate;
  274. }
  275.  
  276. public function getDocumentsCount()
  277. {
  278. return $this->documentsCount;
  279. }
  280.  
  281. }
  282.  
  283. Class Marketer Extends Profession
  284. {
  285. private $name = 'MR';
  286. private $documentsCount = 150; //Маркетёр делает 150 листов отчёта
  287. private $moneyRate = 400;
  288. private $coffeeRate = 15;
  289.  
  290. public function getName()
  291. {
  292. return $this->name;
  293. }
  294. public function getCoffeeRate()
  295. {
  296. return $this->coffeeRate;
  297. }
  298. public function getMoneyRate()
  299. {
  300. return $this->moneyRate;
  301. }
  302.  
  303. public function getDocumentsCount()
  304. {
  305. return $this->documentsCount;
  306. }
  307. }
  308.  
  309. Class Engineer Extends Profession
  310. {
  311. private $name = 'EN';
  312. private $documentsCount = 50; //Инжинер делает 50 листов чертежей и проектов
  313. private $moneyRate = 200;
  314. private $coffeeRate = 5;
  315.  
  316. public function getName()
  317. {
  318. return $this->name;
  319. }
  320. public function getCoffeeRate()
  321. {
  322. return $this->coffeeRate;
  323. }
  324. public function getMoneyRate()
  325. {
  326. return $this->moneyRate;
  327. }
  328.  
  329. public function getDocumentsCount()
  330. {
  331. return $this->documentsCount;
  332. }
  333. }
  334.  
  335. Class Analyst Extends Profession
  336. {
  337. private $name = 'AN';
  338. private $documentsCount = 5; //Аналист делает 5 листов исследований
  339. private $moneyRate = 800;
  340. private $coffeeRate = 50;
  341.  
  342. public function getName()
  343. {
  344. return $this->name;
  345. }
  346. public function getCoffeeRate()
  347. {
  348. return $this->coffeeRate;
  349. }
  350. public function getMoneyRate()
  351. {
  352. return $this->moneyRate;
  353. }
  354.  
  355. public function getDocumentsCount()
  356. {
  357. return $this->documentsCount;
  358. }
  359. }
  360.  
  361. // создаётсяя компания
  362. $company = new Company();
  363.  
  364. /*массивы для формирования сотрудников отдело и т.п.
  365.   первая цифра в ячейке массива - колличество сотрудников. буква b означает босса
  366.   (mn-Manager mr-Marketer en-Engineer an-Analyst) - ранг сотрудника
  367.   цифра в третьей ячеке - ранг сортудников
  368. */
  369. $name='Department of Procurement';
  370. $employeesData=[[ 9, 'MN', 1], [3, 'MN', 2], [2, 'MN', 3], [2, 'MR', 1], ["BOSS", 'MN', 2]];
  371. $company->createDepartment($name, $employeesData);
  372.  
  373. $name='Department of Sales';
  374. $employeesData=[[12, 'MR', 1], [6, 'MR', 2], [3, 'AN', 2], [2, 'AN', 2], ["BOSS", 'MR', 2]];
  375. $company->createDepartment($name, $employeesData);
  376.  
  377. $name='Department of Advertising';
  378. $employeesData=[[15, 'MR', 1], [10, 'MR', 2], [8, 'MN', 1], [2, 'EN', 1], ["BOSS", 'MR', 3]];
  379. $company->createDepartment($name, $employeesData);
  380.  
  381.  
  382. $name='Department of Logistics';
  383. $employeesData=[[13, 'MN', 1], [5, 'MN', 2], [5, 'EN', 1], ["BOSS", 'MN', 1]];
  384. $company->createDepartment($name, $employeesData);
  385.  
  386.  
  387. $report= new Report();
  388. $report->writeTable($company);
  389.  
  390.  
  391.  
Success #stdin #stdout 0.02s 23896KB
stdin
Standard input is empty
stdout
Departmen                        employe          money         coffee          lists    money/lists

Department of Procurement             17         9612.5            350           3100            3.1
Department of Sales                   24          13550            550           2725              5
Department of Advertising             36          16300            575           5450              3
Department of Logistics               24          11375            425           3850              3