Advertisement
Guest User

Untitled

a guest
Jan 13th, 2024
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Numerics;
  7.  
  8. namespace ConsoleApp7
  9. {
  10.     class Program
  11.     {
  12.         static void Main()
  13.         {
  14.             int numTeams = int.Parse(Console.ReadLine());
  15.             List<Teams> teams = new List<Teams>();
  16.             var sb = new StringBuilder();
  17.             var sb2 = new StringBuilder();
  18.             sb2.AppendLine("Teams to disband:");
  19.  
  20.             for (int i = 0; i < numTeams; i++)
  21.             {
  22.                 string[] input = Console.ReadLine().Split('-');
  23.                 string teamCreator = input[0];
  24.                 string teamName = input[1];
  25.  
  26.                 if (teams.Any(x => x.Name == teamName) ) {
  27.                     sb.AppendLine($"Team {teamName} was already created!");
  28.                 }
  29.                 else if (teams.Any(x => x.Creator == teamCreator))
  30.                 {
  31.                     sb.AppendLine($"{teamCreator} cannot create another team!");
  32.                 }
  33.                 else
  34.                 {
  35.                     Teams team = new Teams(teamName, teamCreator);
  36.                     teams.Add(team);
  37.                     sb.AppendLine($"Team {teamName} has been created by {teamCreator}!");
  38.                 }
  39.             }
  40.  
  41.             string input01;
  42.             while ((input01 = Console.ReadLine()) != "end of assignment")
  43.             {
  44.                 string[] inputData = input01.Split(new char[] { '-', '>' }, StringSplitOptions.RemoveEmptyEntries);
  45.                 string memberName = inputData[0];
  46.                 string teamToJoin = inputData[1];
  47.  
  48.                 if (!teams.Any(x => x.Name == teamToJoin))
  49.                 {
  50.                     sb.AppendLine($"Team {teamToJoin} does not exist!");      
  51.                 }
  52.                 else if (teams.Any(team => team.Members.Contains(memberName) || team.Creator == memberName))
  53.                 {
  54.                     sb.AppendLine($"Member {memberName} cannot join team {teamToJoin}!");
  55.                 }
  56.                 else
  57.                 {
  58.                     Teams team = teams.First(t => t.Name == teamToJoin);
  59.                     team.AddMember(memberName);
  60.                 }
  61.             }
  62.             teams.Where(x => x.Members.Count == 0).OrderBy(x => x.Name).ToList().ForEach(x => sb2.AppendLine(x.Name));
  63.  
  64.             foreach (var team in teams.Where(x => x.Members.Count != 0).OrderByDescending(x => x.Members.Count).ThenBy(team => team.Name))
  65.             {
  66.                 sb.AppendLine(team.Name);
  67.                 sb.AppendLine($"- {team.Creator}");
  68.  
  69.                 foreach (string member in team.Members.OrderBy(x => x))
  70.                 {
  71.                     sb.AppendLine($"-- {member}");
  72.                 }
  73.             }
  74.  
  75.             Console.WriteLine(sb.ToString().Trim());
  76.             Console.WriteLine(sb2.ToString().Trim());
  77.         }
  78.     }
  79.  
  80.     class Teams
  81.     {
  82.         public string Name;
  83.         public string Creator;
  84.         public List<string> Members = new List<string>();
  85.  
  86.         public Teams(string name, string creator)
  87.         {
  88.             this.Name = name;
  89.             this.Creator = creator;
  90.         }
  91.  
  92.         public void AddMember(string member)
  93.         {
  94.             Members.Add(member);
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement