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