emodev

Untitled

Apr 1st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: emotu
  5.  * Date: 001 01.4.2019 г.
  6.  * Time: 22:16
  7.  */
  8.  
  9. class Student
  10. {
  11.     private $firstName;
  12.     private $lastName;
  13.     private $age;
  14.     private $city;
  15.  
  16.     public function __construct($firstN, $lastN, $age, $city)
  17.     {
  18.         $this->firstName = $firstN;
  19.         $this->lastName = $lastN;
  20.         $this->age = $age;
  21.         $this->city = $city;
  22.     }
  23.  
  24.     public function getFirstName()
  25.     {
  26.         return $this->firstName;
  27.     }
  28.  
  29.     public function getLastName()
  30.     {
  31.         return $this->lastName;
  32.     }
  33.  
  34.     public function getAge()
  35.     {
  36.         return $this->age;
  37.     }
  38.  
  39.     public function getCity()
  40.     {
  41.         return $this->city;
  42.     }
  43. }
  44.  
  45. $persons = [];
  46. while (true) {
  47.     $line = readline();
  48.     if ($line == "end") {
  49.         break;
  50.     }
  51.     $current = explode(" ", $line);//towa e masiv
  52.     $firstName = $current[0];
  53.     $lastName = $current[1];
  54.     $age = $current[2];
  55.     $city = $current[3];
  56.  
  57.     if (isExisting($persons, $firstName, $lastName)) {
  58.         $person = getStudent($persons, $firstName, $lastName);
  59.         $person = new Student($firstName, $lastName, $age, $city);
  60.     } else {
  61.         $person = new Student($firstName, $lastName, $age, $city);
  62.     }
  63.     array_push($persons, $person);
  64. }
  65. $cityName = readline();
  66. foreach ($persons as $key) {
  67.     if ($key->getCity() == $cityName) {
  68.         echo $key->getFirstName(), " ", $key->getLastName(), " is ", $key->getAge(), " years old." . PHP_EOL;
  69.     }
  70. }
  71.  
  72. function isExisting($list, $firstName, $lastName)
  73. {
  74.     foreach ($list as $student) {
  75.         if ($student->getFirstName() == $firstName && $student->getLastName() == $lastName) {
  76.             return true;
  77.         }
  78.         return false;
  79.     }
  80.  
  81. }
  82.  
  83.  
  84. function getStudent($list, $firstName, $lastName)
  85. {
  86.     $existingStudent = null;
  87.  
  88.     foreach ($list as $student) {
  89.         if ($student->getFirstName() == $firstName && $student->getLastName() == $lastName) {
  90.             $existingStudent = $student;
  91.         }
  92.         return $existingStudent;
  93.     }
  94.  
  95. }
Add Comment
Please, Sign In to add comment