Advertisement
RAYCHEV

Untitled

Mar 11th, 2017
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. <?php
  2.  
  3. class Person
  4. {
  5.     private $name;
  6.     private $age;
  7.  
  8.     public function __construct(string $name, int $age) {
  9.         $this->name = $name;
  10.         $this->age = $age;
  11.     }
  12.  
  13.     public function __toString()
  14.     {
  15.         return $this->name ." - ". $this->age;
  16.     }
  17.  
  18.     public function getName(): string
  19.     {
  20.         return $this->name;
  21.     }
  22.  
  23.     public function getAge(): int
  24.     {
  25.         return $this->age;
  26.     }
  27. }
  28.  
  29. $numberOfInputs = trim(fgets(STDIN));
  30.  
  31.  
  32. $persons = [];
  33. while ($numberOfInputs > 0){
  34.     $input = trim(fgets(STDIN));
  35.     $input = explode(' ', $input);
  36.  
  37.     $name = $input[0];
  38.     $age = intval($input[1]);
  39.     $check = checkAge($age);
  40.  
  41.  
  42.     if ($check) {
  43.  
  44.         $persons [] = new Person($name, $age);
  45.     }
  46.  
  47.     $numberOfInputs--;
  48. }
  49.  
  50. function checkAge ($age){
  51.     return $age > 30;
  52. }
  53.  
  54. usort($persons, function ($a, $b)
  55. {
  56.     return strcmp($a->getName(), $b->getName());
  57. });
  58.  
  59. foreach ($persons as $person)
  60. {
  61.     echo $person. PHP_EOL;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement