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

Department of Procurement                  16           8675            310           3100            2.8
Department of Sales                        23          12800            520           2725            4.7
Department of Advertising                  35          15400            545           5450            2.8
Department of Logistics                    23          10625            385           3850            2.8