Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface FactoryInterface
- {
- public static function createGeneral(string $type,int $id,string $firstName,string $lastName,float $salary,array $param):Soldier;
- public static function createPrivate(string $type,int $id,string $firstName,string $lastName,float $salary):Soldier;
- public static function createSpy(string $type,int $id,string $firstName,string $lastName,int $codeNumber):Soldier;
- public static function createEngineer(string $type,int $id,string $firstName,string $lastName,float $salary,string $repair,array $param):Soldier;
- public static function createCommando(string $type,int $id,string $firstName,string $lastName,float $salary,string $corps,array $param):Soldier;
- }
- class SoldierFactory implements FactoryInterface
- {
- public static function createGeneral(string $type, int $id, string $firstName, string $lastName, float $salary, array $param): Soldier
- {
- if (class_exists($type)) {
- return new $type($id, $firstName, $lastName, $salary, $param);
- }
- }
- public static function createPrivate(string $type, int $id, string $firstName, string $lastName, float $salary): Soldier
- {
- if (class_exists($type)) {
- return new $type($id, $firstName, $lastName, $salary);
- }
- }
- public static function createSpy(string $type, int $id, string $firstName, string $lastName, int $codeNumber): Soldier
- {
- if (class_exists($type)) {
- return new $type($id, $firstName, $lastName, $codeNumber);
- }
- }
- public static function createEngineer(string $type, int $id, string $firstName, string $lastName, float $salary,string $repair, array $param): Soldier
- {
- if (class_exists($type)) {
- return new $type($id, $firstName, $lastName, $salary,$repair, $param);
- }
- }
- public static function createCommando(string $type, int $id, string $firstName, string $lastName, float $salary,string $corps, array $param): Soldier
- {
- if (class_exists($type)){
- return new $type($id, $firstName, $lastName, $salary,$corps, $param);
- }
- }
- }
- abstract class Soldier
- {
- /**
- * @var int
- */
- private $id;
- /**
- * @var string
- */
- private $firstName;
- /**
- * @var string
- */
- private $lastName;
- /**
- * Soldier constructor.
- * @param int $id
- * @param string $firstName
- * @param string $lastName
- */
- protected function __construct(int $id, string $firstName, string $lastName)
- {
- $this->setId($id);
- $this->setFirstName($firstName);
- $this->setLastName($lastName);
- }
- /**
- * @return int
- */
- public function getId(): int
- {
- return $this->id;
- }
- /**
- * @param int $id
- */
- protected function setId(int $id): void
- {
- $this->id = $id;
- }
- /**
- * @return string
- */
- public function getFirstName(): string
- {
- return $this->firstName;
- }
- /**
- * @param string $firstName
- */
- protected function setFirstName(string $firstName): void
- {
- $this->firstName = $firstName;
- }
- /**
- * @return string
- */
- public function getLastName(): string
- {
- return $this->lastName;
- }
- /**
- * @param string $lastName
- */
- protected function setLastName(string $lastName): void
- {
- $this->lastName = $lastName;
- }
- public function __toString()
- {
- return "Name: {$this->getFirstName()} {$this->getLastName()} Id: {$this->getId()} ";
- }
- }
- class Commando extends Soldier
- {
- /**
- * @var float
- */
- private $salary;
- /**
- * @var string
- */
- private $corps;
- /**
- * @var array
- */
- private $mission;
- /**
- * Commando constructor.
- * @param int $id
- * @param string $firstName
- * @param string $lastName
- * @param float $salary
- * @param string $corps
- * @param array $mission
- */
- public function __construct(int $id, string $firstName, string $lastName,float $salary,string $corps,array $mission)
- {
- parent::__construct($id, $firstName, $lastName);
- $this->setSalary($salary);
- $this->setCorps($corps);
- $this->setMission($mission);
- }
- /**
- * @return string
- */
- public function getCorps(): string
- {
- return $this->corps;
- }
- /**
- * @param string $corps
- */
- public function setCorps(string $corps): void
- {
- $this->corps = $corps;
- }
- /**
- * @return float
- */
- public function getSalary(): float
- {
- return $this->salary;
- }
- /**
- * @param float $salary
- */
- private function setSalary(float $salary): void
- {
- $this->salary = $salary;
- }
- /**
- * @return array
- */
- public function getMission(): array
- {
- return $this->mission;
- }
- /**
- * @param array $mission
- */
- private function setMission(array $mission): void
- {
- $this->mission = $mission;
- }
- public function __toString()
- {
- $salary = number_format($this->getSalary(), 2, ".", "");
- if (count($this->getMission())>0){
- return parent::__toString() . "Salary: $salary\nCorps: {$this->getCorps()}\nMissions:\n" .implode("\n", $this->getMission())."\n" ;
- }else{
- return parent::__toString() . "Salary: $salary\nCorps: {$this->getCorps()}\nMissions:\n";
- }
- }
- }
- class Engineer extends Soldier
- {
- /**
- * @var float
- */
- private $salary;
- /**
- * @var string
- */
- private $repair;
- /**
- * @var array
- */
- private $corps;
- public function __construct(int $id, string $firstName, string $lastName, float $salary,string $repair, array $corps)
- {
- parent::__construct($id, $firstName, $lastName);
- $this->setSalary($salary);
- $this->setRepair($repair);
- $this->setCorps($corps);
- }
- /**
- * @return string
- */
- public function getRepair(): string
- {
- return $this->repair;
- }
- /**
- * @param string $repair
- */
- public function setRepair(string $repair): void
- {
- $this->repair = $repair;
- }
- /**
- * @return float
- */
- public function getSalary(): float
- {
- return $this->salary;
- }
- /**
- * @param float $salary
- */
- private function setSalary(float $salary): void
- {
- $this->salary = $salary;
- }
- /**
- * @return array
- */
- public function getCorps(): array
- {
- return $this->corps;
- }
- /**
- * @param array $corps
- */
- private function setCorps(array $corps): void
- {
- $this->corps = $corps;
- }
- public function __toString()
- {
- $salary = number_format($this->getSalary(), 2, ".", "");
- if (count($this->getCorps())>0){
- return parent::__toString() . "Salary: $salary\nCorps: {$this->getRepair()}\nRepairs:\n" . implode("\n", $this->getCorps()) . "\n";
- }else{
- return parent::__toString() . "Salary: $salary\nCorps: {$this->getRepair()}\nRepairs:\n";
- }
- }
- }
- class LeutenantGeneral extends Soldier
- {
- /**
- * @var float
- */
- private $salary;
- /**
- * @var array
- */
- private $solderId;
- /**
- * @var array
- */
- private $privateSolderId;
- public function __construct(int $id, string $firstName, string $lastName, float $salary, array $privateSolderId)
- {
- parent::__construct($id, $firstName, $lastName);
- $this->setSalary($salary);
- $this->privateSolderId=$privateSolderId;
- }
- /**
- * @return float
- */
- public function getSalary(): float
- {
- return $this->salary;
- }
- /**
- * @param float $salary
- */
- private function setSalary(float $salary): void
- {
- $this->salary = $salary;
- }
- /**
- * @return array
- */
- public function getPrivateSolderId(): array
- {
- return $this->privateSolderId;
- }
- /**
- * @param array $privateSolderId
- */
- private function setPrivateSolderId(array $privateSolderId): void
- {
- $this->privateSolderId = $privateSolderId;
- }
- public function __toString()
- {
- $salary=number_format($this->getSalary(),2,".","");
- if (count($this->getPrivateSolderId())>0){
- return parent::__toString() . "Salary: $salary\nPrivates:\n" . implode("\n", $this->getPrivateSolderId())."\n";
- }
- else{
- return parent::__toString()."Salary: $salary\nPrivates:\n";
- }
- }
- }
- class Privates extends Soldier
- {
- /**
- * @var float
- */
- private $salary;
- public function __construct(int $id, string $firstName, string $lastName, float $salary)
- {
- parent::__construct($id, $firstName, $lastName);
- $this->setSalary($salary);
- }
- /**
- * @return float
- */
- public function getSalary(): float
- {
- return $this->salary;
- }
- /**
- * @param float $salary
- */
- private function setSalary(float $salary): void
- {
- $this->salary = $salary;
- }
- public function __toString()
- {
- $salary=number_format($this->getSalary(),2,".","");
- return parent::__toString()."Salary: $salary\n";
- }
- }
- class Spy extends Soldier
- {
- /**
- * @var int
- */
- private $codeNumber;
- /**
- * Spy constructor.
- * @param int $id
- * @param string $firstName
- * @param string $lastName
- * @param int $codeNumber
- */
- public function __construct(int $id, string $firstName, string $lastName,int $codeNumber)
- {
- parent::__construct($id, $firstName, $lastName);
- $this->setCodeNumber($codeNumber);
- }
- /**
- * @return int
- */
- public function getCodeNumber(): int
- {
- return $this->codeNumber;
- }
- /**
- * @param int $codeNumber
- */
- private function setCodeNumber(int $codeNumber): void
- {
- $this->codeNumber = $codeNumber;
- }
- public function __toString()
- {
- return parent::__toString()."\nCode Number: {$this->getCodeNumber()}\n";
- }
- }
- class Start
- {
- /**
- * @var array
- */
- private $army;
- const PATTERN_INPUT = '/\s+/';
- const COMMAND_INPUT_END = 'End';
- const COMMAND_PRIVATE = 'Private';
- const COMMAND_GENERAL = 'LeutenantGeneral';
- const COMMAND_SPY = 'Spy';
- const COMMAND_ENGINEER = 'Engineer';
- const COMMAND_COMMANDO = 'Commando';
- public function run()
- {
- $this->readDate();
- }
- public function readDate()
- {
- $input = readline();
- while ($input != self::COMMAND_INPUT_END) {
- $command = preg_split(self::PATTERN_INPUT, $input, -1, PREG_SPLIT_NO_EMPTY);
- switch ($command[0]) {
- case self::COMMAND_PRIVATE:
- $this->army[$command[1]] = SoldierFactory::createPrivate($command[0]."s", intval($command[1]), $command[2], $command[3], floatval($command[4]));
- break;
- case self::COMMAND_GENERAL:
- $params = [];
- $param = $this->addSolderId($this->army, $command, $params);
- $this->army[$command[1]] = SoldierFactory::createGeneral($command[0], intval($command[1]), $command[2], $command[3], floatval($command[4]), $param);
- break;
- case self::COMMAND_SPY:
- $this->army[$command[1]] = SoldierFactory::createSpy($command[0], intval($command[1]), $command[2], $command[3], intval($command[4]));
- break;
- case self::COMMAND_ENGINEER:
- $params = [];
- $param = $this->addEngineer($command, $params);
- $this->army[$command[1]] = SoldierFactory::createEngineer($command[0], intval($command[1]), $command[2], $command[3], floatval($command[4]),$command[5], $param);
- break;
- case self::COMMAND_COMMANDO:
- $params = [];
- $param = $this->addCommando($command, $params);
- $this->army[$command[1]] = SoldierFactory::createCommando($command[0], intval($command[1]), $command[2], $command[3], floatval($command[4]),$command[5], $param);
- break;
- }
- $input = readline();
- }
- foreach ($this->army as $army) {
- echo $army;
- }
- }
- /**
- * @param array $array
- * @param array $command
- * @param array $params
- * @return array
- */
- public function addSolderId(array $array, array $command, array $params)
- {
- $solderId = array_map("intval", array_slice($command, 5));
- for ($i = 0; $i < count($solderId); $i++) {
- $currentId = intval($solderId[$i]);
- foreach ($array as $id => $value) {
- if ($currentId == $id) {
- $salary = number_format($value->getSalary(), 2, ".", "");
- $params[] = " Name: {$value->getFirstName()} {$value->getLastName()} Id: {$value->getId()} Salary: $salary";
- }
- }
- }
- return $params;
- }
- /**
- * @param array $command
- * @param array $params
- */
- public function addEngineer(array $command, array $params)
- {
- $solderId = array_slice($command, 6);
- for ($i = 0; $i < count($solderId); $i += 2) {
- $params[] = " Part Name: " . $solderId[$i] . " Hours Worked: " . $solderId[$i + 1];
- }
- return $params;
- }
- /**
- * @param array $command
- * @param array $params
- * @return array
- */
- public function addCommando(array $command, array $params)
- {
- $solderId = array_slice($command, 6);
- for ($i = 0; $i < count($solderId); $i += 2) {
- $params[] = " Code Name: " . $solderId[$i] . " State: " . $solderId[$i + 1];
- }
- return $params;
- }
- }
- $start = new Start();
- $start->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement