YORDAN2347

SumArrayElements

Jan 21st, 2021 (edited)
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _05._TeamworkProjects
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // 1. Read Input
  12.             int teamsCount = int.Parse(Console.ReadLine());
  13.  
  14.             // 2. Create [teamsCount] teams
  15.             List<string> teamsMembers = new List<string>();
  16.             List<Team> teams = CreateTeams(teamsCount, teamsMembers);
  17.             List<string> teamsToDisban = new List<string>();
  18.  
  19.             // 3. Add Members to teams
  20.             AddMembers(teams, teamsMembers);
  21.  
  22.             // 4. Sort Teams
  23.             teams = teams.OrderBy(order => order.Members.Count).ToList();
  24.             teams.Reverse();
  25.  
  26.             // 5. print valid teams
  27.             foreach (Team team in teams)
  28.             {
  29.                 if (team.Members.Count > 0)
  30.                 {
  31.                     team.Print();
  32.                 }
  33.                 else
  34.                 {
  35.                     teamsToDisban.Add(team.TeamName);
  36.                 }
  37.             }
  38.  
  39.             Console.WriteLine("Teams to disband:");
  40.             foreach (var team in teamsToDisban)
  41.             {
  42.                 Console.WriteLine(team);
  43.             }
  44.         }
  45.  
  46.         private static void AddMembers(List<Team> teams, List<string> teamsMembers)
  47.         {
  48.             string input = Console.ReadLine();
  49.             while (input != "end of assignment")
  50.             {
  51.                 string[] memberInfo = input.Split("->", StringSplitOptions.RemoveEmptyEntries);
  52.                 string user = memberInfo[0];
  53.                 string team = memberInfo[1];
  54.  
  55.                 bool isTeamContains = IsTeamExists(teams, team);
  56.                 if (!isTeamContains)
  57.                 {
  58.                     Console.WriteLine($"Team {team} does not exist!");
  59.                 }
  60.                 else
  61.                 {
  62.                     if (teamsMembers.Contains(user))
  63.                     {
  64.                         Console.WriteLine($"Member {user} cannot join team {team}!");
  65.                     }
  66.                     else
  67.                     {
  68.                         teamsMembers.Add(user);
  69.                         teams = AddMember(teams, user, team);
  70.                     }
  71.                 }
  72.  
  73.                 input = Console.ReadLine();
  74.             }
  75.         }
  76.  
  77.         private static List<Team> AddMember(List<Team> teams, string user, string team)
  78.         {
  79.             for (int i = 0; i < teams.Count; i++)
  80.             {
  81.                 if (teams[i].TeamName == team)
  82.                 {
  83.                     teams[i].Members.Add(user);
  84.                 }
  85.             }
  86.  
  87.             return teams;
  88.         }
  89.  
  90.         private static List<Team> CreateTeams(int teamsCount, List<string> TeamsMembers)
  91.         {
  92.             List<Team> teams = new List<Team>(teamsCount);
  93.  
  94.             for (int i = 0; i < teamsCount; i++)
  95.             {
  96.                 // 2.1 Read input
  97.                 string[] input = Console.ReadLine()
  98.                 .Split("-", StringSplitOptions.RemoveEmptyEntries);
  99.                 string creator = input[0];
  100.                 string teamName = input[1];
  101.  
  102.                 // 2.2 Create Team
  103.                 bool isTeamContains = IsTeamExists(teams, teamName);
  104.                 if (isTeamContains)
  105.                 {
  106.                     Console.WriteLine($"Team {teamName} was already created!");
  107.                 }
  108.                 else
  109.                 {
  110.                     bool isCreatorContains = IsCreatorExists(teams, creator);
  111.                     if (isCreatorContains)
  112.                     {
  113.                         Console.WriteLine($"{creator} cannot create another team!");
  114.                     }
  115.                     else
  116.                     {
  117.                         Team newTeam = new Team(creator, teamName);
  118.                         // 2.3 Add Team to list
  119.                         teams.Add(newTeam);
  120.                         TeamsMembers.Add(creator);
  121.  
  122.                         // 2.4 Print "Team {teamName} has been created by {user}!".
  123.                         Console.WriteLine($"Team {newTeam.TeamName} has been created by {input[0]}!");
  124.                     }              
  125.                 }                
  126.             }
  127.  
  128.             return teams;
  129.         }
  130.  
  131.         private static bool IsCreatorExists(List<Team> teams, string creator)
  132.         {
  133.             bool isContains = false;
  134.             for (int i = 0; i < teams.Count; i++)
  135.             {
  136.                 if (creator == teams[i].Creator)
  137.                 {
  138.                     isContains = true;
  139.                     break;
  140.                 }
  141.             }
  142.  
  143.             return isContains;
  144.         }
  145.  
  146.         private static bool IsTeamExists(List<Team> teams, string teamName)
  147.         {
  148.             bool isContains = false;
  149.             for (int i = 0; i < teams.Count; i++)
  150.             {
  151.                 if (teamName == teams[i].TeamName)
  152.                 {
  153.                     isContains = true;
  154.                     break;
  155.                 }
  156.             }
  157.  
  158.             return isContains;
  159.         }
  160.     }
  161.  
  162.     class Team
  163.     {
  164.         public Team(string creator, string name)
  165.         {
  166.             Creator = creator;
  167.             TeamName = name;
  168.             Members = new List<string>();
  169.         }
  170.         public string TeamName { get; set; }
  171.         public string Creator { get; set; }
  172.         public List<string> Members { get; set; }
  173.  
  174.         public void Print()
  175.         {
  176.             Console.WriteLine($"{TeamName}");
  177.             Console.WriteLine($"- {Creator}");
  178.             Members.Sort();
  179.             foreach (var member in Members)
  180.             {
  181.                 Console.WriteLine($"-- {member}");
  182.             }
  183.         }
  184.     }
  185. }
  186.  
Add Comment
Please, Sign In to add comment