Advertisement
Guest User

Military Elite

a guest
Jun 21st, 2020
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.33 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. class Engineer extends Soldier
  219. {
  220.     /**
  221.      * @var float
  222.      */
  223.     private $salary;
  224.     /**
  225.      * @var string
  226.      */
  227.     private $repair;
  228.     /**
  229.      * @var array
  230.      */
  231.     private $corps;
  232.  
  233.     public function __construct(int $id, string $firstName, string $lastName, float $salary, string $repair, array $corps)
  234.     {
  235.         parent::__construct($id, $firstName, $lastName);
  236.         $this->setSalary($salary);
  237.         $this->setRepair($repair);
  238.         $this->setCorps($corps);
  239.     }
  240.  
  241.     /**
  242.      * @return string
  243.      */
  244.     public function getRepair(): string
  245.     {
  246.         return $this->repair;
  247.     }
  248.  
  249.     /**
  250.      * @param string $repair
  251.      */
  252.     public function setRepair(string $repair): void
  253.     {
  254.         $this->repair = $repair;
  255.     }
  256.  
  257.     /**
  258.      * @return float
  259.      */
  260.     public function getSalary(): float
  261.     {
  262.         return $this->salary;
  263.     }
  264.  
  265.     /**
  266.      * @param float $salary
  267.      */
  268.     private function setSalary(float $salary): void
  269.     {
  270.         $this->salary = $salary;
  271.     }
  272.  
  273.     /**
  274.      * @return array
  275.      */
  276.     public function getCorps(): array
  277.     {
  278.         return $this->corps;
  279.     }
  280.  
  281.     /**
  282.      * @param array $corps
  283.      */
  284.     private function setCorps(array $corps): void
  285.     {
  286.         $this->corps = $corps;
  287.     }
  288.  
  289.     public function __toString()
  290.     {
  291.         $salary = number_format($this->getSalary(), 2, ".", "");
  292.         if (count($this->getCorps()) > 0) {
  293.             return parent::__toString() . "Salary: $salary\nCorps: {$this->getRepair()}\nRepairs:\n" . implode("\n", $this->getCorps()) . "\n";
  294.         } else {
  295.             return parent::__toString() . "Salary: $salary\nCorps: {$this->getRepair()}\nRepairs:\n";
  296.         }
  297.     }
  298. }
  299. class LeutenantGeneral extends Soldier
  300. {
  301.     /**
  302.      * @var float
  303.      */
  304.     private $salary;
  305.     /**
  306.      * @var array
  307.      */
  308.     private $solderId;
  309.     /**
  310.      * @var array
  311.      */
  312.     private $privateSolderId;
  313.  
  314.     public function __construct(int $id, string $firstName, string $lastName, float $salary, array $privateSolderId)
  315.     {
  316.         parent::__construct($id, $firstName, $lastName);
  317.         $this->setSalary($salary);
  318.         $this->privateSolderId = $privateSolderId;
  319.     }
  320.  
  321.     /**
  322.      * @return float
  323.      */
  324.     public function getSalary(): float
  325.     {
  326.         return $this->salary;
  327.     }
  328.  
  329.     /**
  330.      * @param float $salary
  331.      */
  332.     private function setSalary(float $salary): void
  333.     {
  334.         $this->salary = $salary;
  335.     }
  336.  
  337.     /**
  338.      * @return array
  339.      */
  340.     public function getPrivateSolderId(): array
  341.     {
  342.         return $this->privateSolderId;
  343.     }
  344.  
  345.     /**
  346.      * @param array $privateSolderId
  347.      */
  348.     private function setPrivateSolderId(array $privateSolderId): void
  349.     {
  350.         $this->privateSolderId = $privateSolderId;
  351.     }
  352.     public function __toString()
  353.     {
  354.         $salary = number_format($this->getSalary(), 2, ".", "");
  355.         if (count($this->getPrivateSolderId()) > 0) {
  356.             return parent::__toString() . "Salary: $salary\nPrivates:\n" . implode("\n", $this->getPrivateSolderId()) . "\n";
  357.         } else {
  358.             return parent::__toString() . "Salary: $salary\nPrivates:\n";
  359.         }
  360.     }
  361. }
  362. class Privates extends Soldier
  363. {
  364.     /**
  365.      * @var float
  366.      */
  367.     private $salary;
  368.  
  369.     public function __construct(int $id, string $firstName, string $lastName, float $salary)
  370.     {
  371.         parent::__construct($id, $firstName, $lastName);
  372.         $this->setSalary($salary);
  373.     }
  374.  
  375.     /**
  376.      * @return float
  377.      */
  378.     public function getSalary(): float
  379.     {
  380.         return $this->salary;
  381.     }
  382.  
  383.     /**
  384.      * @param float $salary
  385.      */
  386.     private function setSalary(float $salary): void
  387.     {
  388.         $this->salary = $salary;
  389.     }
  390.  
  391.     public function __toString()
  392.     {
  393.         $salary = number_format($this->getSalary(), 2, ".", "");
  394.         return parent::__toString() . "Salary: $salary\n";
  395.     }
  396. }
  397. class Spy extends Soldier
  398. {
  399.     /**
  400.      * @var int
  401.      */
  402.     private $codeNumber;
  403.  
  404.     /**
  405.      * Spy constructor.
  406.      * @param int $id
  407.      * @param string $firstName
  408.      * @param string $lastName
  409.      * @param string $codeNumber
  410.      */
  411.     public function __construct(int $id, string $firstName, string $lastName, string $codeNumber)
  412.     {
  413.         parent::__construct($id, $firstName, $lastName);
  414.         $this->setCodeNumber($codeNumber);
  415.     }
  416.  
  417.     /**
  418.      * @return int
  419.      */
  420.     public function getCodeNumber(): string
  421.     {
  422.         return $this->codeNumber;
  423.     }
  424.  
  425.     /**
  426.      * @param int $codeNumber
  427.      */
  428.     private function setCodeNumber(string $codeNumber): void
  429.     {
  430.         $this->codeNumber = $codeNumber;
  431.     }
  432.     public function __toString()
  433.     {
  434.         return parent::__toString() . "\nCode Number: {$this->getCodeNumber()}\n";
  435.     }
  436. }
  437. class Start
  438. {
  439.     /**
  440.      * @var array
  441.      */
  442.     private $army;
  443.     const PATTERN_INPUT = '/\s+/';
  444.     const COMMAND_INPUT_END = 'End';
  445.     const COMMAND_PRIVATE = 'Private';
  446.     const COMMAND_GENERAL = 'LeutenantGeneral';
  447.     const COMMAND_SPY = 'Spy';
  448.     const COMMAND_ENGINEER = 'Engineer';
  449.     const COMMAND_COMMANDO = 'Commando';
  450.  
  451.     public function run()
  452.     {
  453.         $this->readDate();
  454.     }
  455.  
  456.     public function readDate()
  457.     {
  458.         $input = readline();
  459.         while ($input != self::COMMAND_INPUT_END) {
  460.             $command = preg_split(self::PATTERN_INPUT, $input, -1, PREG_SPLIT_NO_EMPTY);
  461.             switch ($command[0]) {
  462.                 case self::COMMAND_PRIVATE:
  463.                     $this->army[$command[1]] = SoldierFactory::createPrivate($command[0] . "s", intval($command[1]), $command[2], $command[3], floatval($command[4]));
  464.                     break;
  465.                 case self::COMMAND_GENERAL:
  466.                     $params = [];
  467.                     $param = $this->addSolderId($this->army, $command, $params);
  468.                     $this->army[] = SoldierFactory::createGeneral($command[0], intval($command[1]), $command[2], $command[3], floatval($command[4]), $param);
  469.                     break;
  470.                 case self::COMMAND_SPY:
  471.                     $this->army[] = SoldierFactory::createSpy($command[0], intval($command[1]), $command[2], $command[3], $command[4]);
  472.                     break;
  473.                 case self::COMMAND_ENGINEER:
  474.                     $params = [];
  475.                     $param = $this->addEngineer($command, $params);
  476.                     $this->army[] = SoldierFactory::createEngineer($command[0], intval($command[1]), $command[2], $command[3], floatval($command[4]), $command[5], $param);
  477.                     break;
  478.                 case self::COMMAND_COMMANDO:
  479.                     $params = [];
  480.                     $param = $this->addCommando($command, $params);
  481.                     $this->army[] = SoldierFactory::createCommando($command[0], intval($command[1]), $command[2], $command[3], floatval($command[4]), $command[5], $param);
  482.                     break;
  483.             }
  484.             $input = readline();
  485.         }
  486.         foreach ($this->army as $soldier) {
  487.             echo $soldier;
  488.         }
  489.     }
  490.  
  491.     /**
  492.      * @param array $array
  493.      * @param array $command
  494.      * @param array $params
  495.      * @return array
  496.      */
  497.     public function addSolderId(array $array, array $command, array $params)
  498.     {
  499.         $solderId = array_map("intval", array_slice($command, 5));
  500.         for ($i = 0; $i < count($solderId); $i++) {
  501.             $currentId = intval($solderId[$i]);
  502.             foreach ($array as $id => $value) {
  503.                 if ($currentId == $id) {
  504.                     $salary = number_format($value->getSalary(), 2, ".", "");
  505.                     $params[] = "  Name: {$value->getFirstName()} {$value->getLastName()} Id: {$value->getId()} Salary: $salary";
  506.                 }
  507.             }
  508.         }
  509.         return $params;
  510.     }
  511.  
  512.     /**
  513.      * @param array $command
  514.      * @param array $params
  515.      */
  516.     public function addEngineer(array $command, array $params)
  517.     {
  518.         $solderId = array_slice($command, 6);
  519.         for ($i = 0; $i < count($solderId); $i += 2) {
  520.             $params[] = "  Part Name: " . $solderId[$i] . " Hours Worked: " . $solderId[$i + 1];
  521.         }
  522.         return $params;
  523.     }
  524.  
  525.     /**
  526.      * @param array $command
  527.      * @param array $params
  528.      * @return array
  529.      */
  530.     public function addCommando(array $command, array $params)
  531.     {
  532.         $solderId = array_slice($command, 6);
  533.         for ($i = 0; $i < count($solderId); $i += 2) {
  534.             $params[] = "  Code Name: " . $solderId[$i] . " State: " . $solderId[$i + 1];
  535.         }
  536.         return $params;
  537.     }
  538. }
  539.  
  540. $start = new Start();
  541. $start->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement