Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | None | 0 0
  1. <?php
  2.  
  3. class Student
  4. {
  5.     public $comments = [];
  6.     public $dates = [];
  7.     public $name;
  8.  
  9.     /**
  10.      * @return array
  11.      */
  12.     public function getComments(): array
  13.     {
  14.         return $this->comments;
  15.     }
  16.  
  17.     /**
  18.      * @param array $comments
  19.      */
  20.     public function setComments($comments): void
  21.     {
  22.         array_push($this->comments, $comments);
  23.     }
  24.  
  25.     /**
  26.      * @return array
  27.      */
  28.     public function getDates(): array
  29.     {
  30.         return $this->dates;
  31.     }
  32.  
  33.     /**
  34.      * @param array $dates
  35.      */
  36.     public function setDates($dates): void
  37.     {
  38.         array_push($this->dates, $dates);
  39.     }
  40.  
  41.     /**
  42.      * @return mixed
  43.      */
  44.     public function getName()
  45.     {
  46.         return $this->name;
  47.     }
  48.  
  49.     /**
  50.      * @param mixed $name
  51.      */
  52.     public function setName($name): void
  53.     {
  54.         $this->name = $name;
  55.     }
  56. }
  57.  
  58. /**
  59.  * @param $data
  60.  * @param $user
  61.  * @return bool
  62.  */
  63. function userExist($data, $user)
  64. {
  65.     foreach ($data as $val) {
  66.         if ($val->getName() == $user) {
  67.             return true;
  68.         }
  69.     }
  70.  
  71.     return false;
  72. }
  73.  
  74. $students = [];
  75. while (($input = readline()) != "end of dates") {
  76.     $tempStudent = new Student();
  77.     $tokens = explode(" ", $input);
  78.     $name = $tokens[0];
  79.     $tempStudent->setName($name);
  80.     if (count($tokens) > 1) {
  81.         $tokens = explode(",", $tokens[1]);
  82.         for ($i = 0; $i < count($tokens); $i++) {
  83.             $tempStudent->dates[] = $tokens[$i];
  84.         }
  85.     }
  86.     if (!userExist($students, $name)) {
  87.         array_push($students, $tempStudent);
  88.     } else {
  89.         if ($tempStudent->dates != null) {
  90.             foreach ($students as $student) {
  91.                 if ($name == $student->name) {
  92.                     for ($i = 0; $i < count($tokens); $i++) {
  93.                         $student->dates[] = $tokens[$i];
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.     }
  99. }
  100. while (($input = readline()) != "end of comments") {
  101.     $tokens = explode("-", $input);
  102.     $name = $tokens[0];
  103.     if(count($tokens) > 1) {
  104.         $comment = $tokens[1];
  105.     } else {
  106.         $comment = "";
  107.     }
  108.     foreach ($students as $student) {
  109.         if ($name == $student->name) {
  110.             $student->comments[] = $comment;
  111.         }
  112.     }
  113. }
  114. uasort(
  115. /**
  116.  * @param $a
  117.  * @param $b
  118.  * @return int
  119.  */
  120.     $students,
  121.     function ($a, $b) {
  122.         return $a->name <=> $b->name;
  123.     }
  124. );
  125. foreach ($students as $output) {
  126.     echo $output->name.PHP_EOL;
  127.     echo "Comments:".PHP_EOL;
  128.     if (count($output->comments) > 0) {
  129.         foreach ($output->comments as $comment) {
  130.             echo "- $comment".PHP_EOL;
  131.         }
  132.     }
  133.     echo "Dates attended:".PHP_EOL;
  134.     if ($output->dates != null) {
  135.         asort($output->dates);
  136.         foreach ($output->dates as $date) {
  137.             echo "-- $date".PHP_EOL;
  138.         }
  139.     }
  140. }
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement