Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: emotu
- * Date: 001 01.4.2019 г.
- * Time: 22:16
- */
- class Student
- {
- private $firstName;
- private $lastName;
- private $age;
- private $city;
- public function __construct($firstN, $lastN, $age, $city)
- {
- $this->firstName = $firstN;
- $this->lastName = $lastN;
- $this->age = $age;
- $this->city = $city;
- }
- public function getFirstName()
- {
- return $this->firstName;
- }
- public function getLastName()
- {
- return $this->lastName;
- }
- public function getAge()
- {
- return $this->age;
- }
- public function getCity()
- {
- return $this->city;
- }
- }
- $persons = [];
- while (true) {
- $line = readline();
- if ($line == "end") {
- break;
- }
- $current = explode(" ", $line);//towa e masiv
- $firstName = $current[0];
- $lastName = $current[1];
- $age = $current[2];
- $city = $current[3];
- if (isExisting($persons, $firstName, $lastName)) {
- $person = getStudent($persons, $firstName, $lastName);
- $person = new Student($firstName, $lastName, $age, $city);
- } else {
- $person = new Student($firstName, $lastName, $age, $city);
- }
- array_push($persons, $person);
- }
- $cityName = readline();
- foreach ($persons as $key) {
- if ($key->getCity() == $cityName) {
- echo $key->getFirstName(), " ", $key->getLastName(), " is ", $key->getAge(), " years old." . PHP_EOL;
- }
- }
- function isExisting($list, $firstName, $lastName)
- {
- foreach ($list as $student) {
- if ($student->getFirstName() == $firstName && $student->getLastName() == $lastName) {
- return true;
- }
- return false;
- }
- }
- function getStudent($list, $firstName, $lastName)
- {
- $existingStudent = null;
- foreach ($list as $student) {
- if ($student->getFirstName() == $firstName && $student->getLastName() == $lastName) {
- $existingStudent = $student;
- }
- return $existingStudent;
- }
- }
Add Comment
Please, Sign In to add comment