tanya_zheleva

9

Feb 12th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5.  
  6. namespace ExamPreparation
  7. {
  8.     public sealed class Team
  9.     {
  10.         public Team(string creator, string teamName)
  11.         {
  12.             this.Creator = creator;
  13.             this.TeamName = teamName;
  14.             this.Members = new SortedSet<string>();
  15.         }
  16.  
  17.         public string Creator { get; private set; }
  18.  
  19.         public string TeamName { get; private set; }
  20.  
  21.         public SortedSet<string> Members { get; set; }
  22.  
  23.         public override string ToString()
  24.         {
  25.             StringBuilder output = new StringBuilder();
  26.  
  27.             if (this.Members.Any())
  28.             {
  29.                 output.AppendLine(this.TeamName);
  30.                 output.AppendLine($"- {this.Creator}");
  31.  
  32.                 foreach (var member in this.Members)
  33.                 {
  34.                     output.AppendLine($"-- {member}");
  35.                 }
  36.             }
  37.             else
  38.             {
  39.                 output.Append(this.TeamName);
  40.             }
  41.  
  42.             return output.ToString().Trim();
  43.         }
  44.     }
  45.  
  46.     public sealed class Preparation
  47.     {
  48.         public static void Main()
  49.         {
  50.             int teamsCount = int.Parse(Console.ReadLine());
  51.             Dictionary<string, Team> teams = new Dictionary<string, Team>();
  52.  
  53.             for (int i = 0; i < teamsCount; i++)
  54.             {
  55.                 string[] tokens = Console.ReadLine().Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
  56.                 string creator = tokens[0];
  57.                 string teamName = tokens[1];
  58.  
  59.                 if (teams.ContainsKey(teamName))
  60.                 {
  61.                     Console.WriteLine($"Team {teamName} was already created!");
  62.                 }
  63.                 else if (teams.Any(x => x.Value.Creator == creator))
  64.                 {
  65.                     Console.WriteLine($"{creator} cannot create another team!");
  66.                 }
  67.                 else
  68.                 {
  69.                     teams.Add(teamName, new Team(creator, teamName));
  70.                     Console.WriteLine($"Team {teamName} has been created by {creator}!");
  71.                 }
  72.             }
  73.  
  74.             string input = Console.ReadLine();
  75.  
  76.             while (input != "end of assignment")
  77.             {
  78.                 string[] tokens = input.Split(new[] { '-', '>' }, StringSplitOptions.RemoveEmptyEntries);
  79.                 string user = tokens[0];
  80.                 string teamName = tokens[1];
  81.  
  82.                 if (teams.ContainsKey(teamName))
  83.                 {
  84.                     if (teams.Values.Any(x => x.Members.Contains(user)) || teams.Any(x => x.Value.Creator == user))
  85.                     {
  86.                         Console.WriteLine($"Member {user} cannot join team {teamName}!");
  87.                     }
  88.                     else
  89.                     {
  90.                         teams[teamName].Members.Add(user);
  91.                     }
  92.                 }
  93.                 else
  94.                 {
  95.                     Console.WriteLine($"Team {teamName} does not exist!");
  96.                 }
  97.  
  98.                 input = Console.ReadLine();
  99.             }
  100.  
  101.             foreach (var team in teams.Where(x => x.Value.Members.Any()).OrderByDescending(x => x.Value.Members.Count).ThenBy(x => x.Value.TeamName))
  102.             {
  103.                 Console.WriteLine(team.Value);
  104.             }
  105.  
  106.             Console.WriteLine("Teams to disband:");
  107.  
  108.             foreach (var team in teams.Where(x => x.Value.Members.Count == 0).OrderBy(x => x.Value.TeamName))
  109.             {
  110.                 Console.WriteLine(team.Value);
  111.             }
  112.         }
  113.     }
  114. }
Add Comment
Please, Sign In to add comment