Advertisement
Guest User

Untitled

a guest
Jun 21st, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.28 KB | None | 0 0
  1. <?php
  2. interface FactoryInterface
  3. {
  4. public static function createGeneral(string $type,int $id,string $firstName,string $lastName,float $salary,array $param):Soldier;
  5. public static function createPrivate(string $type,int $id,string $firstName,string $lastName,float $salary):Soldier;
  6. public static function createSpy(string $type,int $id,string $firstName,string $lastName,int $codeNumber):Soldier;
  7. public static function createEngineer(string $type,int $id,string $firstName,string $lastName,float $salary,string $repair,array $param):Soldier;
  8. public static function createCommando(string $type,int $id,string $firstName,string $lastName,float $salary,string $corps,array $param):Soldier;
  9. }
  10. class SoldierFactory implements FactoryInterface
  11. {
  12.  
  13. public static function createGeneral(string $type, int $id, string $firstName, string $lastName, float $salary, array $param): Soldier
  14. {
  15. if (class_exists($type)) {
  16. return new $type($id, $firstName, $lastName, $salary, $param);
  17. }
  18. }
  19.  
  20. public static function createPrivate(string $type, int $id, string $firstName, string $lastName, float $salary): Soldier
  21. {
  22. if (class_exists($type)) {
  23. return new $type($id, $firstName, $lastName, $salary);
  24. }
  25. }
  26.  
  27. public static function createSpy(string $type, int $id, string $firstName, string $lastName, int $codeNumber): Soldier
  28. {
  29. if (class_exists($type)) {
  30. return new $type($id, $firstName, $lastName, $codeNumber);
  31. }
  32. }
  33.  
  34. public static function createEngineer(string $type, int $id, string $firstName, string $lastName, float $salary,string $repair, array $param): Soldier
  35. {
  36. if (class_exists($type)) {
  37. return new $type($id, $firstName, $lastName, $salary,$repair, $param);
  38. }
  39. }
  40.  
  41. public static function createCommando(string $type, int $id, string $firstName, string $lastName, float $salary,string $corps, array $param): Soldier
  42. {
  43. if (class_exists($type)){
  44. return new $type($id, $firstName, $lastName, $salary,$corps, $param);
  45. }
  46. }
  47. }
  48. abstract class Soldier
  49. {
  50. /**
  51. * @var int
  52. */
  53. private $id;
  54. /**
  55. * @var string
  56. */
  57. private $firstName;
  58. /**
  59. * @var string
  60. */
  61. private $lastName;
  62.  
  63. /**
  64. * Soldier constructor.
  65. * @param int $id
  66. * @param string $firstName
  67. * @param string $lastName
  68. */
  69. protected function __construct(int $id, string $firstName, string $lastName)
  70. {
  71. $this->setId($id);
  72. $this->setFirstName($firstName);
  73. $this->setLastName($lastName);
  74. }
  75.  
  76. /**
  77. * @return int
  78. */
  79. public function getId(): int
  80. {
  81. return $this->id;
  82. }
  83.  
  84. /**
  85. * @param int $id
  86. */
  87. protected function setId(int $id): void
  88. {
  89. $this->id = $id;
  90. }
  91.  
  92. /**
  93. * @return string
  94. */
  95. public function getFirstName(): string
  96. {
  97. return $this->firstName;
  98. }
  99.  
  100. /**
  101. * @param string $firstName
  102. */
  103. protected function setFirstName(string $firstName): void
  104. {
  105. $this->firstName = $firstName;
  106. }
  107.  
  108. /**
  109. * @return string
  110. */
  111. public function getLastName(): string
  112. {
  113. return $this->lastName;
  114. }
  115.  
  116. /**
  117. * @param string $lastName
  118. */
  119. protected function setLastName(string $lastName): void
  120. {
  121. $this->lastName = $lastName;
  122. }
  123.  
  124. public function __toString()
  125. {
  126. return "Name: {$this->getFirstName()} {$this->getLastName()} Id: {$this->getId()} ";
  127. }
  128. }
  129. class Commando extends Soldier
  130. {
  131. /**
  132. * @var float
  133. */
  134. private $salary;
  135. /**
  136. * @var string
  137. */
  138. private $corps;
  139. /**
  140. * @var array
  141. */
  142. private $mission;
  143.  
  144. /**
  145. * Commando constructor.
  146. * @param int $id
  147. * @param string $firstName
  148. * @param string $lastName
  149. * @param float $salary
  150. * @param string $corps
  151. * @param array $mission
  152. */
  153. public function __construct(int $id, string $firstName, string $lastName,float $salary,string $corps,array $mission)
  154. {
  155. parent::__construct($id, $firstName, $lastName);
  156. $this->setSalary($salary);
  157. $this->setCorps($corps);
  158. $this->setMission($mission);
  159. }
  160.  
  161. /**
  162. * @return string
  163. */
  164. public function getCorps(): string
  165. {
  166. return $this->corps;
  167. }
  168.  
  169. /**
  170. * @param string $corps
  171. */
  172. public function setCorps(string $corps): void
  173. {
  174. $this->corps = $corps;
  175. }
  176.  
  177. /**
  178. * @return float
  179. */
  180. public function getSalary(): float
  181. {
  182. return $this->salary;
  183. }
  184.  
  185. /**
  186. * @param float $salary
  187. */
  188. private function setSalary(float $salary): void
  189. {
  190. $this->salary = $salary;
  191. }
  192.  
  193. /**
  194. * @return array
  195. */
  196. public function getMission(): array
  197. {
  198. return $this->mission;
  199. }
  200.  
  201. /**
  202. * @param array $mission
  203. */
  204. private function setMission(array $mission): void
  205. {
  206. $this->mission = $mission;
  207. }
  208. public function __toString()
  209. {
  210. $salary = number_format($this->getSalary(), 2, ".", "");
  211. if (count($this->getMission())>0){
  212. return parent::__toString() . "Salary: $salary\nCorps: {$this->getCorps()}\nMissions:\n" .implode("\n", $this->getMission())."\n" ;
  213. }else{
  214. return parent::__toString() . "Salary: $salary\nCorps: {$this->getCorps()}\nMissions:\n";
  215.  
  216. }
  217.  
  218. }
  219. }
  220. class Engineer extends Soldier
  221. {
  222. /**
  223. * @var float
  224. */
  225. private $salary;
  226. /**
  227. * @var string
  228. */
  229. private $repair;
  230. /**
  231. * @var array
  232. */
  233. private $corps;
  234.  
  235. public function __construct(int $id, string $firstName, string $lastName, float $salary,string $repair, array $corps)
  236. {
  237. parent::__construct($id, $firstName, $lastName);
  238. $this->setSalary($salary);
  239. $this->setRepair($repair);
  240. $this->setCorps($corps);
  241. }
  242.  
  243. /**
  244. * @return string
  245. */
  246. public function getRepair(): string
  247. {
  248. return $this->repair;
  249. }
  250.  
  251. /**
  252. * @param string $repair
  253. */
  254. public function setRepair(string $repair): void
  255. {
  256. $this->repair = $repair;
  257. }
  258.  
  259. /**
  260. * @return float
  261. */
  262. public function getSalary(): float
  263. {
  264. return $this->salary;
  265. }
  266.  
  267. /**
  268. * @param float $salary
  269. */
  270. private function setSalary(float $salary): void
  271. {
  272. $this->salary = $salary;
  273. }
  274.  
  275. /**
  276. * @return array
  277. */
  278. public function getCorps(): array
  279. {
  280. return $this->corps;
  281. }
  282.  
  283. /**
  284. * @param array $corps
  285. */
  286. private function setCorps(array $corps): void
  287. {
  288. $this->corps = $corps;
  289. }
  290.  
  291. public function __toString()
  292. {
  293. $salary = number_format($this->getSalary(), 2, ".", "");
  294. if (count($this->getCorps())>0){
  295. return parent::__toString() . "Salary: $salary\nCorps: {$this->getRepair()}\nRepairs:\n" . implode("\n", $this->getCorps()) . "\n";
  296. }else{
  297. return parent::__toString() . "Salary: $salary\nCorps: {$this->getRepair()}\nRepairs:\n";
  298.  
  299. }
  300.  
  301. }
  302. }
  303. class LeutenantGeneral extends Soldier
  304. {
  305. /**
  306. * @var float
  307. */
  308. private $salary;
  309. /**
  310. * @var array
  311. */
  312. private $solderId;
  313. /**
  314. * @var array
  315. */
  316. private $privateSolderId;
  317.  
  318. public function __construct(int $id, string $firstName, string $lastName, float $salary, array $privateSolderId)
  319. {
  320. parent::__construct($id, $firstName, $lastName);
  321. $this->setSalary($salary);
  322. $this->privateSolderId=$privateSolderId;
  323. }
  324.  
  325. /**
  326. * @return float
  327. */
  328. public function getSalary(): float
  329. {
  330. return $this->salary;
  331. }
  332.  
  333. /**
  334. * @param float $salary
  335. */
  336. private function setSalary(float $salary): void
  337. {
  338. $this->salary = $salary;
  339. }
  340.  
  341. /**
  342. * @return array
  343. */
  344. public function getPrivateSolderId(): array
  345. {
  346. return $this->privateSolderId;
  347. }
  348.  
  349. /**
  350. * @param array $privateSolderId
  351. */
  352. private function setPrivateSolderId(array $privateSolderId): void
  353. {
  354. $this->privateSolderId = $privateSolderId;
  355. }
  356. public function __toString()
  357. {
  358. $salary=number_format($this->getSalary(),2,".","");
  359. if (count($this->getPrivateSolderId())>0){
  360. return parent::__toString() . "Salary: $salary\nPrivates:\n" . implode("\n", $this->getPrivateSolderId())."\n";
  361. }
  362. else{
  363. return parent::__toString()."Salary: $salary\nPrivates:\n";
  364. }
  365. }
  366.  
  367. }
  368. class Privates extends Soldier
  369. {
  370. /**
  371. * @var float
  372. */
  373. private $salary;
  374.  
  375. public function __construct(int $id, string $firstName, string $lastName, float $salary)
  376. {
  377. parent::__construct($id, $firstName, $lastName);
  378. $this->setSalary($salary);
  379. }
  380.  
  381. /**
  382. * @return float
  383. */
  384. public function getSalary(): float
  385. {
  386. return $this->salary;
  387. }
  388.  
  389. /**
  390. * @param float $salary
  391. */
  392. private function setSalary(float $salary): void
  393. {
  394. $this->salary = $salary;
  395. }
  396.  
  397. public function __toString()
  398. {
  399. $salary=number_format($this->getSalary(),2,".","");
  400. return parent::__toString()."Salary: $salary\n";
  401. }
  402. }
  403. class Spy extends Soldier
  404. {
  405. /**
  406. * @var int
  407. */
  408. private $codeNumber;
  409.  
  410. /**
  411. * Spy constructor.
  412. * @param int $id
  413. * @param string $firstName
  414. * @param string $lastName
  415. * @param int $codeNumber
  416. */
  417. public function __construct(int $id, string $firstName, string $lastName,int $codeNumber)
  418. {
  419. parent::__construct($id, $firstName, $lastName);
  420. $this->setCodeNumber($codeNumber);
  421. }
  422.  
  423. /**
  424. * @return int
  425. */
  426. public function getCodeNumber(): int
  427. {
  428. return $this->codeNumber;
  429. }
  430.  
  431. /**
  432. * @param int $codeNumber
  433. */
  434. private function setCodeNumber(int $codeNumber): void
  435. {
  436. $this->codeNumber = $codeNumber;
  437. }
  438. public function __toString()
  439. {
  440. return parent::__toString()."\nCode Number: {$this->getCodeNumber()}\n";
  441. }
  442. }
  443. class Start
  444. {
  445. /**
  446. * @var array
  447. */
  448. private $army;
  449. const PATTERN_INPUT = '/\s+/';
  450. const COMMAND_INPUT_END = 'End';
  451. const COMMAND_PRIVATE = 'Private';
  452. const COMMAND_GENERAL = 'LeutenantGeneral';
  453. const COMMAND_SPY = 'Spy';
  454. const COMMAND_ENGINEER = 'Engineer';
  455. const COMMAND_COMMANDO = 'Commando';
  456.  
  457. public function run()
  458. {
  459. $this->readDate();
  460. }
  461.  
  462. public function readDate()
  463. {
  464. $input = readline();
  465. while ($input != self::COMMAND_INPUT_END) {
  466. $command = preg_split(self::PATTERN_INPUT, $input, -1, PREG_SPLIT_NO_EMPTY);
  467. switch ($command[0]) {
  468. case self::COMMAND_PRIVATE:
  469. $this->army[$command[1]] = SoldierFactory::createPrivate($command[0]."s", intval($command[1]), $command[2], $command[3], floatval($command[4]));
  470. break;
  471. case self::COMMAND_GENERAL:
  472. $params = [];
  473. $param = $this->addSolderId($this->army, $command, $params);
  474. $this->army[$command[1]] = SoldierFactory::createGeneral($command[0], intval($command[1]), $command[2], $command[3], floatval($command[4]), $param);
  475. break;
  476. case self::COMMAND_SPY:
  477. $this->army[$command[1]] = SoldierFactory::createSpy($command[0], intval($command[1]), $command[2], $command[3], intval($command[4]));
  478. break;
  479. case self::COMMAND_ENGINEER:
  480. $params = [];
  481. $param = $this->addEngineer($command, $params);
  482. $this->army[$command[1]] = SoldierFactory::createEngineer($command[0], intval($command[1]), $command[2], $command[3], floatval($command[4]),$command[5], $param);
  483. break;
  484. case self::COMMAND_COMMANDO:
  485. $params = [];
  486. $param = $this->addCommando($command, $params);
  487. $this->army[$command[1]] = SoldierFactory::createCommando($command[0], intval($command[1]), $command[2], $command[3], floatval($command[4]),$command[5], $param);
  488. break;
  489. }
  490. $input = readline();
  491. }
  492. foreach ($this->army as $army) {
  493. echo $army;
  494. }
  495. }
  496.  
  497. /**
  498. * @param array $array
  499. * @param array $command
  500. * @param array $params
  501. * @return array
  502. */
  503. public function addSolderId(array $array, array $command, array $params)
  504. {
  505. $solderId = array_map("intval", array_slice($command, 5));
  506. for ($i = 0; $i < count($solderId); $i++) {
  507. $currentId = intval($solderId[$i]);
  508. foreach ($array as $id => $value) {
  509. if ($currentId == $id) {
  510. $salary = number_format($value->getSalary(), 2, ".", "");
  511. $params[] = " Name: {$value->getFirstName()} {$value->getLastName()} Id: {$value->getId()} Salary: $salary";
  512. }
  513. }
  514. }
  515. return $params;
  516. }
  517.  
  518. /**
  519. * @param array $command
  520. * @param array $params
  521. */
  522. public function addEngineer(array $command, array $params)
  523. {
  524. $solderId = array_slice($command, 6);
  525. for ($i = 0; $i < count($solderId); $i += 2) {
  526. $params[] = " Part Name: " . $solderId[$i] . " Hours Worked: " . $solderId[$i + 1];
  527. }
  528. return $params;
  529. }
  530.  
  531. /**
  532. * @param array $command
  533. * @param array $params
  534. * @return array
  535. */
  536. public function addCommando(array $command, array $params)
  537. {
  538. $solderId = array_slice($command, 6);
  539. for ($i = 0; $i < count($solderId); $i += 2) {
  540. $params[] = " Code Name: " . $solderId[$i] . " State: " . $solderId[$i + 1];
  541. }
  542. return $params;
  543. }
  544. }
  545.  
  546. $start = new Start();
  547. $start->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement