Chessmaster

Untitled

Nov 9th, 2018
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Numerics;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. namespace OOPBasics
  8. {
  9.     class Program
  10.     {
  11.         public static void Project(int n)
  12.         {
  13.             List<Team> teams = new List<Team>();
  14.             for (int i = 0; i < n; i++)
  15.             {
  16.                 string[] tokens = Console.ReadLine().Split('-');
  17.  
  18.                 string creator = tokens[0];
  19.                 string teamName = tokens[1];
  20.  
  21.                 bool isTeamMoreThanOnce = teams.Any(t => t.TeamName == teamName);
  22.  
  23.                 if (isTeamMoreThanOnce)
  24.                 {
  25.                     Console.WriteLine($"Team {teamName} was already created!");
  26.                     continue;
  27.                 }
  28.  
  29.                 bool isUserHasATeam = teams.Any(c => c.Creator == creator);
  30.                 if (isUserHasATeam)
  31.                 {
  32.                     Console.WriteLine($"{creator} cannot create another team!");
  33.                     continue;
  34.                 }
  35.  
  36.  
  37.                 Team team = new Team(creator, teamName);
  38.                 teams.Add(team);
  39.  
  40.                 Console.WriteLine($"Team {teamName} has been created by {creator}!");
  41.             }
  42.  
  43.             while (true)
  44.             {
  45.                 string[] tokens = Console.ReadLine().Split("->");
  46.  
  47.                 if (tokens[0] == "end of assignment")
  48.                 {
  49.                     break;
  50.                 }
  51.  
  52.                 string member = tokens[0];
  53.                 string teamName = tokens[1];
  54.  
  55.                 bool isTeamExists = teams.Any(t => t.TeamName == teamName);
  56.  
  57.                 if (isTeamExists == false)
  58.                 {
  59.                     Console.WriteLine($"Team {teamName} does not exist!");
  60.                     continue;
  61.                 }
  62.                 bool isAlreadyInATeam = teams.Any(m => m.Members.Contains(member) || m.Creator == member);
  63.                 if (isAlreadyInATeam)
  64.                 {
  65.                     Console.WriteLine($"Member {member} cannot join team {teamName}!");
  66.                     continue;
  67.                 }
  68.  
  69.                 foreach (var item in teams)
  70.                 {
  71.                     if (item.TeamName == teamName)
  72.                     {
  73.                         item.Members.Add(member);
  74.                     }
  75.                 }
  76.             }
  77.             List<Team> disbandedTeams = teams.Where(x => x.Members.Count == 0).OrderBy(x => x.TeamName).ToList(); // no
  78.  
  79.             var sorted = teams.Where(x => x.Members.Count > 0).OrderByDescending(x => x.Members.Count).ThenBy(x => x.TeamName) // no
  80.                .ToList();
  81.  
  82.  
  83.             foreach (var item in sorted)
  84.             {
  85.                 Console.WriteLine(item.TeamName);
  86.                 Console.WriteLine("- {0}", item.Creator);
  87.                 foreach (var member in item.Members.OrderBy(m => m))
  88.                 {
  89.                     Console.WriteLine("-- {0}", member);
  90.                 }
  91.             }
  92.  
  93.             Console.WriteLine("Teams to disband:");
  94.             foreach (var team in disbandedTeams)
  95.             {
  96.                 Console.WriteLine(team.TeamName);
  97.             }
  98.         }
  99.  
  100.         static void Main()
  101.         {
  102.             int n = int.Parse(Console.ReadLine());
  103.  
  104.             Project(n);
  105.         }
  106.     }
  107.  
  108.     public class Team
  109.     {
  110.         public Team(string creator, string teamName)
  111.         {
  112.             this.Creator = creator;
  113.             this.TeamName = teamName;
  114.             this.Members = new List<string>();
  115.         }
  116.  
  117.         public string TeamName { get; set; }
  118.  
  119.         public string Creator { get; set; }
  120.  
  121.         public List<string> Members { get; set; }
  122.     }
  123. } // .
Advertisement
Add Comment
Please, Sign In to add comment