Advertisement
kolioi

Untitled

Jun 22nd, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TeamworkProjects
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             List<Team> teams = new List<Team>(n);
  13.  
  14.             #region Register teams
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 string[] tokens = Console.ReadLine().Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
  18.                 string userName = tokens[0],
  19.                        teamName = tokens[1];
  20.  
  21.                 if (GetTeamIndex(teams, teamName) != -1)
  22.                     Console.WriteLine($"Team {teamName} was already created!");
  23.                 else if (GetUserIndex(teams, userName) != -1)
  24.                     Console.WriteLine($"{userName} cannot create another team!");
  25.                 else
  26.                 {
  27.                     teams.Add(new Team(teamName, userName));
  28.                     Console.WriteLine($"Team {teamName} has been created by {userName}!");
  29.                 }
  30.             }
  31.             #endregion
  32.  
  33.             #region Populate teams
  34.             string input = Console.ReadLine();
  35.             while (!input.Equals("end of assignment"))
  36.             {
  37.                 string[] tokens = input.Split(new string[] { "->" }, StringSplitOptions.RemoveEmptyEntries);
  38.                 string userName = tokens[0],
  39.                        teamName = tokens[1];
  40.  
  41.                 int teamIndex;
  42.                 if ((teamIndex = GetTeamIndex(teams, teamName)) == -1)
  43.                     Console.WriteLine($"Team {teamName} does not exist!");
  44.                 else
  45.                 {
  46.                     int userIndex = GetUserIndex(teams, userName);
  47.                     if (userIndex != -1)
  48.                         Console.WriteLine($"Member {userName} cannot join team {teamName}!");
  49.                     else
  50.                         teams[teamIndex].teamMembers.Add(userName);
  51.                 }
  52.  
  53.                 input = Console.ReadLine();
  54.             }
  55.             #endregion
  56.  
  57.             #region Print teams
  58.             List<int> teamsToDisband = new List<int>();
  59.  
  60.             teams = teams.OrderByDescending(x => x.teamMembers.Count).ThenBy(x => x.teamName).ToList();
  61.  
  62.             for (int i = 0; i < teams.Count; i++)
  63.             {
  64.                 if (teams[i].teamMembers.Count > 0)
  65.                     Console.WriteLine($"{teams[i].teamName}\n- {teams[i].teamCreator}\n-- {string.Join("\n-- ", teams[i].teamMembers)}");
  66.                 else
  67.                     teamsToDisband.Add(i);
  68.             }
  69.  
  70.             Console.WriteLine($"Teams to disband:");
  71.             for (int i = 0; i < teamsToDisband.Count; i++)
  72.                 Console.WriteLine($"{teams[teamsToDisband[i]].teamName}");
  73.             #endregion
  74.         }
  75.  
  76.         private static int GetTeamIndex(List<Team> teams, string teamName)
  77.         {
  78.             for (int i = 0; i < teams.Count; i++)
  79.                 if (teams[i].teamName.Equals(teamName))
  80.                     return i;
  81.  
  82.             return -1;
  83.         }
  84.  
  85.         private static int GetUserIndex(List<Team> teams, string userName)
  86.         {
  87.             for (int i = 0; i < teams.Count; i++)
  88.                 if (teams[i].teamMembers.Contains(userName) || teams[i].teamCreator.Equals(userName))
  89.                     return i;
  90.  
  91.             return -1;
  92.         }
  93.  
  94.         private class Team
  95.         {
  96.             public string teamName { get; set; }
  97.             public string teamCreator { get; set; }
  98.             public SortedSet<string> teamMembers;
  99.  
  100.             public Team(string team, string user)
  101.             {
  102.                 this.teamName = team;
  103.                 this.teamCreator = user;
  104.                 teamMembers = new SortedSet<string>();
  105.             }
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement