Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface CitizenInterface
- {
- public function setId(): void;
- }
- class Citizen implements CitizenInterface
- {
- private $name;
- private $age;
- private $id;
- public function __construct(string $name, int $age, string $id)
- {
- $this->name = $name;
- $this->age = $age;
- $this->id = $id;
- }
- public function getName(): string
- {
- return $this->name;
- }
- public function setName(string $name): void
- {
- $this->name = $name;
- }
- public function getAge(): int
- {
- return $this->age;
- }
- public function setAge(int $age): void
- {
- $this->age = $age;
- }
- public function getId(): string
- {
- return $this->id;
- }
- public function setId(): void
- {
- $this->setId();
- }
- }
- class Robot implements CitizenInterface
- {
- private $model;
- private $id;
- public function __construct(string $model, string $id)
- {
- $this->model = $model;
- $this->id = $id;
- }
- public function getModel(): string
- {
- return $this->model;
- }
- public function getId(): string
- {
- return $this->id;
- }
- public function setId(): void
- {
- $this->setId();
- }
- }
- class Main
- {
- private $citizens;
- public function run()
- {
- $this->readData();
- }
- const PATTERN = '/\s+/';
- public function readData()
- {
- $line = readline();
- while ($line != "End") {
- $input = preg_split(self::PATTERN, $line, -1);
- if (count($input) == 3) {
- $name = $input[0];
- $age = $input[1];
- $id = $input[2];
- $this->citizens[] = new Citizen($name, $age, $id);
- } elseif (count($input) == 2) {
- $model = $input[0];
- $id = $input[1];
- $this->citizens[] = new Robot($model, $id);
- }
- $line = readline();
- }
- $line = readline();
- $str = "";
- foreach ($this->citizens as $citizen) {
- if ($this->endsWith($citizen->getId(), $line)) {
- $str .= $citizen->getId() . "\n";
- }
- }
- echo trim($str, "\n");
- // print_r($this->citizen);
- }
- public function endsWith($haystack, $needle)
- {
- $length = strlen($needle);
- if ($length == 0) {
- return true;
- }
- return (substr($haystack, -$length) === $needle);
- }
- }
- $main = new Main();
- $main->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement