Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TeamworkProjects
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int creationCount = int.Parse(Console.ReadLine());
  14.  
  15. List<Team> teams = new List<Team>();
  16.  
  17. for (int i = 0; i < creationCount; i++)
  18. {
  19. string[] teamArgs = Console.ReadLine().Split('-');
  20.  
  21. string creator = teamArgs[0];
  22.  
  23. string teamName = teamArgs[1];
  24.  
  25. CreateTeam(creator, teamName, teams);
  26. }
  27.  
  28. string input = Console.ReadLine();
  29.  
  30. while (input != "end of assignment")
  31. {
  32. string[] teamJoinArgs = input.Split(new string[] { "->" }, StringSplitOptions.None);
  33.  
  34. string joinerName = teamJoinArgs[0];
  35.  
  36. string teamToJoin = teamJoinArgs[1];
  37.  
  38. JoinTeam(joinerName, teamToJoin, teams);
  39.  
  40. input = Console.ReadLine();
  41. }
  42.  
  43. List<Team> populatedTeams = teams.Where(x => x.Members.Count > 1)
  44. .OrderByDescending(x => x.Members.Count)
  45. .ThenBy(x => x.TeamName).ToList();
  46.  
  47. List<Team> emptyTeams = teams.Where(x => x.Members.Count <= 1).OrderBy(x => x.TeamName).ToList();
  48.  
  49. foreach (Team team in populatedTeams)
  50. {
  51. Console.WriteLine(team.TeamName);
  52.  
  53. string creator = team.Members.First();
  54.  
  55. team.Members.Remove(creator);
  56.  
  57. Console.WriteLine($"- {creator}");
  58.  
  59. foreach (string member in team.Members.OrderBy(x => x))
  60. {
  61. Console.WriteLine($"-- {member}");
  62. }
  63. }
  64.  
  65. Console.WriteLine("Teams to disband:");
  66.  
  67. foreach (Team emptyTeam in emptyTeams)
  68. {
  69. Console.WriteLine(emptyTeam.TeamName);
  70. }
  71. }
  72.  
  73. private static void JoinTeam(string joinerName, string teamToJoin, List<Team> teams)
  74. {
  75. if (DoesTeamExist(joinerName, teamToJoin, teams))
  76. {
  77. if (IsPlayerFree(joinerName, teamToJoin, teams))
  78. {
  79. foreach (Team team in teams)
  80. {
  81. if (team.TeamName == teamToJoin)
  82. {
  83. team.Members.Add(joinerName);
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. }
  90.  
  91. private static bool DoesTeamExist(string joinerName, string teamToJoin, List<Team> teams)
  92. {
  93. bool teamExistFlag = true;
  94.  
  95. foreach (Team team in teams)
  96. {
  97. if (team.TeamName != teamToJoin)
  98. {
  99. teamExistFlag = false;
  100. }
  101. else
  102. {
  103. teamExistFlag = true;
  104. break;
  105. }
  106. }
  107.  
  108. if (!teamExistFlag)
  109. {
  110. Console.WriteLine($"Team {teamToJoin} does not exist!");
  111. return false;
  112. }
  113.  
  114. return true;
  115. }
  116.  
  117. private static bool IsPlayerFree(string joinerName, string teamToJoin, List<Team> teams)
  118. {
  119.  
  120. foreach (Team team in teams)
  121. {
  122. foreach (string teamMember in team.Members)
  123. {
  124. if (teamMember == joinerName)
  125. {
  126. Console.WriteLine($"Member {joinerName} cannot join team {teamToJoin}!");
  127. return false;
  128. }
  129. }
  130. }
  131.  
  132. return true;
  133. }
  134.  
  135. private static void CreateTeam(string creator, string teamName, List<Team> teams)
  136. {
  137. if (ValidateTeamArguments(creator, teamName, teams))
  138. {
  139. Console.WriteLine($"Team {teamName} has been created by {creator}!");
  140.  
  141. Team team = new Team();
  142.  
  143. team.Members = new List<string>();
  144.  
  145. team.CreatorName = creator;
  146.  
  147. team.TeamName = teamName;
  148.  
  149. team.Members.Add(creator);
  150.  
  151. teams.Add(team);
  152. }
  153. }
  154.  
  155. private static bool ValidateTeamArguments(string creator, string teamName, List<Team> teams)
  156. {
  157. foreach (Team team in teams)
  158. {
  159. if (team.TeamName == teamName)
  160. {
  161. Console.WriteLine($"Team {teamName} was already created!");
  162.  
  163. return false;
  164. }
  165.  
  166. if (team.CreatorName == creator)
  167. {
  168. Console.WriteLine($"{creator} cannot create another team!");
  169.  
  170. return false;
  171. }
  172. }
  173.  
  174. return true;
  175. }
  176. }
  177.  
  178. class Team
  179. {
  180. public string TeamName { get; set; }
  181. public List<string> Members { get; set; }
  182. public string CreatorName { get; set; }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement