Advertisement
viraco4a

Untitled

Jan 9th, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 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 task_24
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14. List<Team> AllTeams = new List<Team>();
  15. TwoListsOfTeams TwoLists = new TwoListsOfTeams();
  16. // Adding new teams with name, creator name and 1 member (the creator):
  17. for (int i = 0; i < n; i++)
  18. {
  19. Input inputLine = ReadInputLine();
  20. Team NewTeam = GetNewTeam(inputLine.input);
  21. bool newUniqueTeam = true;
  22. bool FirstTeamForCreator = true;
  23. foreach (Team item in AllTeams)
  24. {
  25. if (item.Name == NewTeam.Name)
  26. {
  27. newUniqueTeam = false;
  28. Console.WriteLine($"Team {NewTeam.Name} was already created!");
  29. break;
  30. }
  31. }
  32.  
  33. foreach (Team item in AllTeams)
  34. {
  35. if (item.CreatorName == NewTeam.CreatorName)
  36. {
  37. FirstTeamForCreator = false;
  38. Console.WriteLine($"{NewTeam.CreatorName} cannot create another team!");
  39. break;
  40. }
  41. }
  42.  
  43. if (newUniqueTeam && FirstTeamForCreator)
  44. {
  45. AllTeams.Add(NewTeam);
  46. Console.WriteLine($"Team {NewTeam.Name} has been created by {NewTeam.CreatorName}!");
  47. }
  48. }
  49.  
  50. // Adding members to teams:
  51. for (int i = 0; ; i++)
  52. {
  53. Input inputLine = ReadInputLine();
  54. if (inputLine.CheckKeyWord)
  55. {
  56. TwoLists = SepareteTeams(AllTeams);
  57. List<Team> ValidList = new List<Team>();
  58. List<Team> ZeroList = new List<Team>();
  59. ValidList = TwoLists.ValidList;
  60. ZeroList = TwoLists.ZeroList;
  61. ValidList = OrderListOfTeams(ValidList);
  62. ZeroList = OrderListOfTeams(ZeroList);
  63. PrintValidTeams(ValidList);
  64. PrintZeroTeams(ZeroList);
  65. break;
  66. }
  67. AllTeams = JoinUserToTeam(AllTeams, inputLine.input);
  68. }
  69. }
  70.  
  71. static void PrintZeroTeams(List<Team> ZeroListOfTeams)
  72. {
  73. Console.WriteLine("Teams to disband:");
  74. foreach (Team team in ZeroListOfTeams)
  75. {
  76. Console.WriteLine(team.Name);
  77. }
  78. }
  79.  
  80. static void PrintValidTeams(List<Team> ValidListOfTeams)
  81. {
  82. foreach (Team team in ValidListOfTeams)
  83. {
  84. Console.WriteLine(team.Name);
  85. Console.WriteLine($"- {team.CreatorName}");
  86. List<string> orderedList = new List<string>();
  87. orderedList = OrderListOfMembers(team.Members);
  88. foreach (string item in orderedList)
  89. {
  90. Console.WriteLine($"-- {item}");
  91. }
  92. }
  93. }
  94.  
  95. static List<string> OrderListOfMembers(List<string> ListOfMembers)
  96. {
  97. List<string> OrderedListOfMembers = new List<string>();
  98. OrderedListOfMembers = ListOfMembers.OrderBy(o => o).ToList();
  99. return OrderedListOfMembers;
  100. }
  101.  
  102. static List<Team> OrderListOfTeams(List<Team> ListToOrder)
  103. {
  104. List<Team> OrderedListOfTeams = new List<Team>();
  105. OrderedListOfTeams = ListToOrder.OrderByDescending(o => o.Members.Count).ThenBy(o => o.Name).ToList();
  106. return OrderedListOfTeams;
  107. }
  108.  
  109. static List<Team> OrderZeroList(List<Team> ZeroListToOrder)
  110. {
  111. List<Team> OrderedZeroList = new List<Team>();
  112. OrderedZeroList = ZeroListToOrder.OrderBy(o => o.Name).ToList();
  113. return OrderedZeroList;
  114. }
  115.  
  116. static TwoListsOfTeams SepareteTeams(List<Team> ListToCheck)
  117. {
  118. List<Team> ValidList = new List<Team>();
  119. List<Team> ZeroList = new List<Team>();
  120. TwoListsOfTeams TwoLists = new TwoListsOfTeams();
  121. for (int i = 0; i < ListToCheck.Count; i++)
  122. {
  123. if (ListToCheck.ElementAt(i).Members.Count == 0)
  124. {
  125. ZeroList.Add(ListToCheck.ElementAt(i));
  126. }
  127. else
  128. {
  129. ValidList.Add(ListToCheck.ElementAt(i));
  130. }
  131. }
  132. TwoLists.ValidList = ValidList;
  133. TwoLists.ZeroList = ZeroList;
  134. return TwoLists;
  135. }
  136.  
  137. static List<Team> JoinUserToTeam(List<Team> ListOfInterest, string inputLine)
  138. {
  139. string[] input = inputLine.Split(new string[] { "->" }, StringSplitOptions.None).ToArray();
  140. if (CheckIfTeamExists(ListOfInterest, input[1]))
  141. {
  142. List<string> ListOfMembers = new List<string>();
  143. if (CheckIfMemberExistsInAnotherTeam(ListOfInterest, input[0]))
  144. {
  145. Console.WriteLine($"Member {input[0]} cannot join team {input[1]}!");
  146. return ListOfInterest;
  147. }
  148. for (int i = 0; i < ListOfInterest.Count; i++)
  149. {
  150. if (ListOfInterest.ElementAt(i).Name == input[1])
  151. {
  152. ListOfMembers = ListOfInterest.ElementAt(i).Members;
  153. ListOfMembers.Add(input[0]);
  154. ListOfInterest.ElementAt(i).Members = ListOfMembers;
  155. return ListOfInterest;
  156. }
  157. }
  158. }
  159. return ListOfInterest;
  160. }
  161.  
  162. static bool CheckIfTeamExists(List<Team> ListOfTeams, string team)
  163. {
  164. bool CheckTeam = false;
  165. foreach (Team item in ListOfTeams)
  166. {
  167. if (item.Name == team)
  168. {
  169. CheckTeam = true;
  170. return CheckTeam;
  171. }
  172. }
  173. Console.WriteLine($"Team {team} does not exist!");
  174. return CheckTeam;
  175. }
  176.  
  177. static bool CheckIfMemberExistsInAnotherTeam(List<Team> ListOfTeams, string Member)
  178. {
  179. bool Check = false;
  180. foreach (Team team in ListOfTeams)
  181. {
  182. if (team.Members.Contains(Member) || team.CreatorName == Member)
  183. {
  184. Check = true;
  185. return Check;
  186. }
  187. }
  188. return Check;
  189. }
  190.  
  191. static Team GetNewTeam(string inputLine)
  192. {
  193. string[] input = inputLine.Split('-').ToArray();
  194. Team NewTeam = new Team();
  195. NewTeam.Name = input[1];
  196. NewTeam.CreatorName = input[0];
  197. List<string> Members = new List<string>();
  198. NewTeam.Members = Members;
  199. return NewTeam;
  200. }
  201.  
  202. static Input ReadInputLine()
  203. {
  204. Input inputLogic = new Input();
  205. string input = Console.ReadLine();
  206. bool WordForBreak = false;
  207. if (input == "end of assignment")
  208. {
  209. WordForBreak = true;
  210. }
  211. inputLogic.input = input;
  212. inputLogic.CheckKeyWord = WordForBreak;
  213. return inputLogic;
  214. }
  215.  
  216. }
  217.  
  218. public class Team
  219. {
  220. public string Name { get; set; }
  221. public List<string> Members { get; set; }
  222. public string CreatorName { get; set; }
  223. }
  224.  
  225. public class Input
  226. {
  227. public bool CheckKeyWord { get; set; }
  228. public string input { get; set; }
  229. }
  230.  
  231. public class TwoListsOfTeams
  232. {
  233. public List<Team> ValidList { get; set; }
  234. public List<Team> ZeroList { get; set; }
  235. }
  236.  
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement