Advertisement
Guest User

Teamwork projects

a guest
Nov 17th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. <?php
  2. class Team
  3. {
  4. private $teamName;
  5. private $creator;
  6. private $names = [];
  7.  
  8. public function __construct($teamName, $creator)
  9. {
  10. $this->teamName = $teamName;
  11. $this->creator = $creator;
  12. }
  13.  
  14. public function getNames()
  15. {
  16. return $this->names;
  17. }
  18.  
  19. public function getTeamName()
  20. {
  21. return $this->teamName;
  22. }
  23.  
  24. public function getCreator() {
  25. return $this->creator;
  26. }
  27.  
  28. public function addName($name)
  29. {
  30. $this->names[] = $name;
  31. }
  32.  
  33. public function sortNames()
  34. {
  35. sort($this->names);
  36. }
  37. }
  38.  
  39. $teamCount = intval(readline());
  40. $allTeams = [];
  41. $creatorExists = false;
  42.  
  43. for ($i = 0; $i < $teamCount;$i++){
  44. list($creator, $teamName) = explode("-", readline());
  45. $creatorExists = false;
  46. if (!array_key_exists($teamName, $allTeams)) {
  47. foreach ($allTeams as $team) {
  48. if ($team->getCreator() === $creator) {
  49. $creatorExists = true;
  50. break;
  51. }
  52. }
  53.  
  54. if ($creatorExists) {
  55. echo "$creator cannot create another team!" . PHP_EOL;
  56. continue;
  57. }
  58.  
  59. echo "Team $teamName has been created by $creator!" . PHP_EOL;
  60. $currTeam = new Team($teamName, $creator);
  61. $allTeams[$teamName] = $currTeam;
  62. } else {
  63. echo "Team $teamName was already created!" . PHP_EOL;
  64. }
  65. }
  66.  
  67. $command = readline();
  68. $nameExists = false;
  69. $repeater = false;
  70. $teamExists = 0;
  71. $teamsToDisband = [];
  72.  
  73.  
  74. while ($command !== "end of assignment") {
  75. list($name, $addTeamName) = explode("->", $command);
  76. $nameExists = false;
  77. $repeater = false;
  78. $teamExists = 0;
  79.  
  80. //check if team exists
  81. foreach ($allTeams as $team) {
  82. if ($addTeamName !== $team->getTeamName()) {
  83. $teamExists++;
  84. }
  85. }
  86.  
  87. if ((count($allTeams) - $teamExists) === 0) {
  88. echo "Team $addTeamName does not exist!" . PHP_EOL;
  89. $command = readline();
  90. continue;
  91. }
  92. //
  93.  
  94. //check if creator exists
  95. foreach ($allTeams as $team) {
  96. if ($name === $team->getCreator()) {
  97. $repeater = true;
  98. break;
  99. }
  100. }
  101.  
  102. if ($repeater) {
  103. echo "Member $name cannot join team $addTeamName!" . PHP_EOL;
  104. $command = readline();
  105. continue;
  106. }
  107. //
  108.  
  109. //check if name is already on a team
  110. foreach ($allTeams as $team) {
  111. if (in_array($name, $team->getNames())) {
  112. $nameExists = true;
  113. break;
  114. }
  115. }
  116.  
  117. if ($nameExists) {
  118. echo "Member $name cannot join team $addTeamName!" . PHP_EOL;
  119. $command = readline();
  120. continue;
  121. } else {
  122. $allTeams[$addTeamName]->addName($name);
  123. }
  124. //
  125.  
  126. $command = readline();
  127. }
  128.  
  129. usort($allTeams, function(Team $team1, Team $team2) use ($allTeams) {
  130. $countNames1 = count($team1->getNames());
  131. $countNames2 = count($team2->getNames());
  132.  
  133. $name1 = $team1->getTeamName();
  134. $name2 = $team2->getTeamName();
  135.  
  136. if ($countNames1===$countNames2) {
  137. return $name1 <=> $name2;
  138. }
  139.  
  140. return $countNames2 <=> $countNames1;
  141. });
  142.  
  143. foreach ($allTeams as $team) {
  144. $currTeam = $team->getNames();
  145. if (count($team->getNames()) === 0) {
  146. $teamsToDisband[] = $team->getTeamName();
  147. continue;
  148. }
  149. echo $team->getTeamName().PHP_EOL;
  150. echo "- {$team->getCreator()}" . PHP_EOL;
  151. $team->sortNames();
  152. foreach ($team->getNames() as $teamMembers) {
  153. echo "-- $teamMembers" . PHP_EOL;
  154. }
  155. }
  156. echo "Teams to disband:" . PHP_EOL;
  157. foreach ($teamsToDisband as $disbandedTeam) {
  158. echo $disbandedTeam . PHP_EOL;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement