Advertisement
JulianJulianov

13.ObjectAndClasses-Teamwork projects

Mar 5th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.38 KB | None | 0 0
  1. 13.ObjectAndClasses-Teamwork projects
  2. It's time for the teamwork projects and you are responsible for gathering the teams. First you will receive an integer - the count of the teams you will have to register. You will be given a user and a team, separated with “-”.  The user is the creator of the team. For every newly created team you should print a message:
  3. "Team {teamName} has been created by {user}!".
  4. Next, you will receive an user with a team, separated with "->", which means that the user wants to join that team. Upon receiving the command: “end of assignment”, you should print every team, ordered by the count of its members (descending) and then by name (ascending). For each team, you have to print its members sorted by name (ascending). However, there are several rules:
  5. • If an user tries to create a team more than once, a message should be displayed:
  6. -   "Team {teamName} was already created!"
  7. • A creator of a team cannot create another team – the following message should be thrown:
  8. -   "{user} cannot create another team!"
  9. • If an user tries to join a non-existent team, a message should be displayed:
  10. -   "Team {teamName} does not exist!"
  11. • A member of a team cannot join another team – the following message should be thrown:
  12. -   "Member {user} cannot join team {team Name}!"
  13. • In the end, teams with zero members (with only a creator) should disband and you have to print them ordered by name in ascending order.
  14. •  Every valid team should be printed ordered by name (ascending) in the following format:
  15. "{teamName}:
  16. - {creator}
  17. -- {member}…"
  18. Examples
  19. Input                     Output                                           Comments
  20. 2                         Team PowerPuffsCoders has been created by Didi!  Toni created a team, which he attempted to join later and
  21. Didi-PowerPuffsCoders     Team Toni is the best has been created by Toni!  this action resulted in throwing a certain message. Since
  22. Toni-Toni is the best     Member Toni cannot join team Toni is the best!   nobody else tried to join his team, the team had to disband.
  23. Petq->PowerPuffsCoders    PowerPuffsCoders
  24. Toni->Toni is the best    - Didi
  25. end of assignment         -- Petq
  26.                          Teams to disband:
  27.                          Toni is the best  
  28. 3
  29. Tatyana-CloneClub         Team CloneClub has been created by Tatyana!      Note that when a user joins a team, you should first
  30. Helena-CloneClub          Team CloneClub was already created!              check if the team exists and then check
  31. Trifon-AiNaBira           Team AiNaBira has been created by Trifon!        if the user is already in a team:
  32. Pesho->aiNaBira           Team aiNaBira does not exist!                    Tatyana has created CloneClub, then she tried to join
  33. Pesho->AiNaBira           Team Leda does not exist!                        a non-existent team and the concrete message was displayed.
  34. Tatyana->Leda             AiNaBira
  35. PeshO->AiNaBira           - Trifon
  36. Cossima->CloneClub        -- Pesho
  37. end of assignment         -- PeshO
  38.                          CloneClub
  39.                          - Tatyana
  40.                          -- Cossima
  41.                          Teams to disband:
  42.      
  43.  
  44. using System;
  45. using System.Collections.Generic;
  46. using System.Linq;
  47.  
  48. namespace Problem05TeamworkProjects
  49. {
  50.    class Program
  51.    {
  52.        static void Main(string[] args)
  53.        {
  54.            int numberOfTeams = int.Parse(Console.ReadLine());
  55.  
  56.            List<Team> teams = new List<Team>();
  57.  
  58.            for (int i = 1; i <= numberOfTeams; i++)
  59.            {
  60.                string[] line = Console.ReadLine().Split('-');
  61.                var currentTeam = new Team();
  62.  
  63.                string creator = line[0];
  64.                string teamName = line[1];
  65.  
  66.                currentTeam.Creator = creator;
  67.                currentTeam.Name = teamName;
  68.  
  69.                if (teams.Any(x => x.Name == teamName))
  70.                {
  71.                    Console.WriteLine($"Team {teamName} was already created!");
  72.                    continue;
  73.                }
  74.  
  75.                if (teams.Any(x => x.Creator == creator))
  76.                {
  77.                    Console.WriteLine($"{creator} cannot create another team!");
  78.                    continue;
  79.                }
  80.  
  81.                teams.Add(currentTeam);
  82.                Console.WriteLine($"Team {teamName} has been created by {creator}!");
  83.            }
  84.  
  85.            while (true)
  86.            {
  87.                string currentCommand = Console.ReadLine();
  88.  
  89.                if (currentCommand == "end of assignment")
  90.                {
  91.                    break;
  92.                }
  93.  
  94.                string[] line = currentCommand.Split("->");
  95.  
  96.                string user = line[0];
  97.                string team = line[1];
  98.  
  99.                if (!teams.Any(x => x.Name == team))
  100.                {
  101.                    Console.WriteLine($"Team {team} does not exist!");
  102.                    continue;
  103.                }
  104.  
  105.                if (teams.Any(x => x.Creator == user) || teams.Any(x => x.Members.Contains(user)))
  106.                {
  107.                    Console.WriteLine($"Member {user} cannot join team {team}!");
  108.                    continue;
  109.                }
  110.  
  111.                if (teams.Any(x => x.Name == team))
  112.                {
  113.                    var existingTeam = teams.First(x => x.Name == team);
  114.  
  115.                    existingTeam.Members.Add(user);
  116.                }
  117.            }
  118.  
  119.            var teamsDisband = teams.Where(x => x.Members.Count == 0).Select(x => x.Name).ToList();
  120.  
  121.            foreach (var team in teams.OrderByDescending(x => x.Members.Count).ThenBy(x => x.Name))
  122.            {
  123.                if (team.Members.Count == 0)
  124.                {
  125.                    continue;
  126.                }
  127.  
  128.                Console.WriteLine($"{team.Name}");
  129.                Console.WriteLine($"- {team.Creator}");
  130.  
  131.                foreach (var name in team.Members.OrderBy(x => x))
  132.                {
  133.                    Console.WriteLine($"-- {name}");
  134.                }
  135.            }
  136.  
  137.            Console.WriteLine("Teams to disband:");
  138.            foreach (var item in teamsDisband.OrderBy(x => x))
  139.            {
  140.                Console.WriteLine(item);
  141.            }
  142.        }
  143.    }
  144.  
  145.    class Team
  146.    {
  147.        public string Name;
  148.        public string Creator;
  149.        public List<string> Members = new List<string>();
  150.    }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement