Advertisement
Guest User

BorderControl

a guest
Jun 14th, 2020
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1. <?php
  2. interface CitizenInterface
  3. {
  4.  
  5.     public function setId(): void;
  6. }
  7.  
  8. class Citizen implements CitizenInterface
  9. {
  10.     private $name;
  11.     private $age;
  12.     private $id;
  13.  
  14.     public function __construct(string $name, int $age, string $id)
  15.     {
  16.         $this->name = $name;
  17.         $this->age = $age;
  18.         $this->id = $id;
  19.     }
  20.  
  21.     public function getName(): string
  22.     {
  23.         return $this->name;
  24.     }
  25.  
  26.     public function setName(string $name): void
  27.     {
  28.         $this->name = $name;
  29.     }
  30.  
  31.     public function getAge(): int
  32.     {
  33.         return $this->age;
  34.     }
  35.  
  36.     public function setAge(int $age): void
  37.     {
  38.         $this->age = $age;
  39.     }
  40.  
  41.     public function getId(): string
  42.     {
  43.         return $this->id;
  44.     }
  45.  
  46.     public function setId(): void
  47.     {
  48.         $this->setId();
  49.     }
  50. }
  51.  
  52. class Robot implements CitizenInterface
  53. {
  54.     private $model;
  55.     private $id;
  56.  
  57.     public function __construct(string $model, string $id)
  58.     {
  59.         $this->model = $model;
  60.         $this->id = $id;
  61.     }
  62.  
  63.     public function getModel(): string
  64.     {
  65.         return $this->model;
  66.     }
  67.  
  68.     public function getId(): string
  69.     {
  70.         return $this->id;
  71.     }
  72.     public function setId(): void
  73.     {
  74.         $this->setId();
  75.     }
  76. }
  77.  
  78. class Main
  79. {
  80.     private $citizens;
  81.  
  82.     public function run()
  83.     {
  84.         $this->readData();
  85.     }
  86.  
  87.     const PATTERN = '/\s+/';
  88.  
  89.     public function readData()
  90.     {
  91.         $line = readline();
  92.         while ($line != "End") {
  93.             $input = preg_split(self::PATTERN, $line, -1);
  94.             if (count($input) == 3) {
  95.                 $name = $input[0];
  96.                 $age = $input[1];
  97.                 $id = $input[2];
  98.                 $this->citizens[] = new Citizen($name, $age, $id);
  99.             } elseif (count($input) == 2) {
  100.                 $model = $input[0];
  101.                 $id = $input[1];
  102.                 $this->citizens[] = new Robot($model, $id);
  103.             }
  104.             $line = readline();
  105.         }
  106.         $line = readline();
  107.         $str = "";
  108.         foreach ($this->citizens as $citizen) {
  109.             if ($this->endsWith($citizen->getId(), $line)) {
  110.                 $str .= $citizen->getId() . "\n";
  111.             }
  112.         }
  113.         echo trim($str, "\n");
  114.         // print_r($this->citizen);
  115.     }
  116.  
  117.     public function endsWith($haystack, $needle)
  118.     {
  119.         $length = strlen($needle);
  120.         if ($length == 0) {
  121.             return true;
  122.         }
  123.         return (substr($haystack, -$length) === $needle);
  124.     }
  125. }
  126.  
  127. $main = new Main();
  128. $main->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement