Guest User

Teamwork Projects

a guest
Apr 21st, 2017
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 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.  
  8. class TeamworkProjects
  9. {
  10.     static void Main()
  11.     {
  12.         var list = new List<Team>();
  13.  
  14.         int n = int.Parse(Console.ReadLine());
  15.  
  16.         RegisteringTeams(list, n);
  17.         AddingTeamMembers(list);
  18.         PrintingResults(list);
  19.     }
  20.  
  21.     private static void RegisteringTeams(List<Team> list, int n)
  22.     {
  23.         for (int i = 0; i < n; i++)
  24.         {
  25.             Team newTeam = new Team();
  26.  
  27.             var str = Console.ReadLine().Split('-');
  28.             var creatorName = str[0];
  29.             var team = str[1];
  30.  
  31.             newTeam.TeamName = team;
  32.             newTeam.CreatorName = creatorName;
  33.             newTeam.Members = new List<string>();
  34.  
  35.             //When you move this check before the other, ano more test passes 62
  36.             if (list.Any(t => t.TeamName == team))
  37.             {
  38.                 Console.WriteLine($"Team {team} was already created!");
  39.                 continue;
  40.             }
  41.  
  42.             if (list.Any(c => c.CreatorName == creatorName))
  43.             {
  44.                 Console.WriteLine($"{creatorName} cannot create another team!");
  45.                 continue;
  46.             }
  47.  
  48.  
  49.  
  50.             Console.WriteLine($"Team {team} has been created by {creatorName}!");
  51.             list.Add(newTeam);
  52.         }
  53.     }
  54.  
  55.     private static void AddingTeamMembers(List<Team> list)
  56.     {
  57.         while (true)
  58.         {
  59.             var input = Console.ReadLine();
  60.             if (input == "end of assignment") break;
  61.             var split = input.Split(new char[] { '-', '>' });
  62.             var user = split[0];
  63.             var teamJoin = split[2];
  64.  
  65.             if (!list.Any(t => t.TeamName == teamJoin))
  66.             {
  67.                 Console.WriteLine($"Team {teamJoin} does not exist!");
  68.                 continue;
  69.             }
  70.  
  71.             if (list.Any(nz => nz.CreatorName == user) || list.Any(x => x.Members.Contains(user)))
  72.             {
  73.                 Console.WriteLine($"Member {user} cannot join team {teamJoin}!");
  74.                 continue;
  75.             }
  76.  
  77.             if (list.Any(t => t.TeamName == teamJoin))
  78.             {
  79.                 var existingTeam = list.First(t => t.TeamName == teamJoin);
  80.  
  81.                 existingTeam.Members.Add(user);
  82.                 continue;
  83.             }
  84.  
  85.         }
  86.     }
  87.  
  88.     private static void PrintingResults(List<Team> list)
  89.     {
  90.         var teamsDisband = list.Where(t => t.Members.Count == 0).Select(x => x.TeamName).ToList();
  91.  
  92.         foreach (var team in list.OrderByDescending(m => m.Members.Count).ThenBy(z => z.TeamName))
  93.         {
  94.             if (team.Members.Count == 0) continue;
  95.  
  96.             Console.WriteLine(team.TeamName);
  97.             Console.WriteLine($"- {team.CreatorName}");
  98.  
  99.             foreach (var pl in team.Members.OrderBy(x => x))
  100.             {
  101.                 Console.WriteLine($"-- {pl}");
  102.             }
  103.         }
  104.         Console.WriteLine("Teams to disband:");
  105.         foreach (var item in teamsDisband.OrderBy(x => x))
  106.         {
  107.  
  108.             Console.WriteLine(item);
  109.         }
  110.     }
  111. }
  112.  
  113. class Team
  114. {
  115.     public string TeamName { get; set; }
  116.     public List<string> Members { get; set; }
  117.     public string CreatorName { get; set; }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment