Advertisement
Guest User

Untitled

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