Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace task_24
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- List<Team> AllTeams = new List<Team>();
- TwoListsOfTeams TwoLists = new TwoListsOfTeams();
- // Adding new teams with name, creator name and 1 member (the creator):
- for (int i = 0; i < n; i++)
- {
- Input inputLine = ReadInputLine();
- Team NewTeam = GetNewTeam(inputLine.input);
- bool newUniqueTeam = true;
- bool FirstTeamForCreator = true;
- foreach (Team item in AllTeams)
- {
- if (item.Name == NewTeam.Name)
- {
- newUniqueTeam = false;
- Console.WriteLine($"Team {NewTeam.Name} was already created!");
- break;
- }
- }
- foreach (Team item in AllTeams)
- {
- if (item.CreatorName == NewTeam.CreatorName)
- {
- FirstTeamForCreator = false;
- Console.WriteLine($"{NewTeam.CreatorName} cannot create another team!");
- break;
- }
- }
- if (newUniqueTeam && FirstTeamForCreator)
- {
- AllTeams.Add(NewTeam);
- Console.WriteLine($"Team {NewTeam.Name} has been created by {NewTeam.CreatorName}!");
- }
- }
- // Adding members to teams:
- for (int i = 0; ; i++)
- {
- Input inputLine = ReadInputLine();
- if (inputLine.CheckKeyWord)
- {
- TwoLists = SepareteTeams(AllTeams);
- List<Team> ValidList = new List<Team>();
- List<Team> ZeroList = new List<Team>();
- ValidList = TwoLists.ValidList;
- ZeroList = TwoLists.ZeroList;
- ValidList = OrderListOfTeams(ValidList);
- ZeroList = OrderListOfTeams(ZeroList);
- PrintValidTeams(ValidList);
- PrintZeroTeams(ZeroList);
- break;
- }
- AllTeams = JoinUserToTeam(AllTeams, inputLine.input);
- }
- }
- static void PrintZeroTeams(List<Team> ZeroListOfTeams)
- {
- Console.WriteLine("Teams to disband:");
- foreach (Team team in ZeroListOfTeams)
- {
- Console.WriteLine(team.Name);
- }
- }
- static void PrintValidTeams(List<Team> ValidListOfTeams)
- {
- foreach (Team team in ValidListOfTeams)
- {
- Console.WriteLine(team.Name);
- Console.WriteLine($"- {team.CreatorName}");
- List<string> orderedList = new List<string>();
- orderedList = OrderListOfMembers(team.Members);
- foreach (string item in orderedList)
- {
- Console.WriteLine($"-- {item}");
- }
- }
- }
- static List<string> OrderListOfMembers(List<string> ListOfMembers)
- {
- List<string> OrderedListOfMembers = new List<string>();
- OrderedListOfMembers = ListOfMembers.OrderBy(o => o).ToList();
- return OrderedListOfMembers;
- }
- static List<Team> OrderListOfTeams(List<Team> ListToOrder)
- {
- List<Team> OrderedListOfTeams = new List<Team>();
- OrderedListOfTeams = ListToOrder.OrderByDescending(o => o.Members.Count).ThenBy(o => o.Name).ToList();
- return OrderedListOfTeams;
- }
- static List<Team> OrderZeroList(List<Team> ZeroListToOrder)
- {
- List<Team> OrderedZeroList = new List<Team>();
- OrderedZeroList = ZeroListToOrder.OrderBy(o => o.Name).ToList();
- return OrderedZeroList;
- }
- static TwoListsOfTeams SepareteTeams(List<Team> ListToCheck)
- {
- List<Team> ValidList = new List<Team>();
- List<Team> ZeroList = new List<Team>();
- TwoListsOfTeams TwoLists = new TwoListsOfTeams();
- for (int i = 0; i < ListToCheck.Count; i++)
- {
- if (ListToCheck.ElementAt(i).Members.Count == 0)
- {
- ZeroList.Add(ListToCheck.ElementAt(i));
- }
- else
- {
- ValidList.Add(ListToCheck.ElementAt(i));
- }
- }
- TwoLists.ValidList = ValidList;
- TwoLists.ZeroList = ZeroList;
- return TwoLists;
- }
- static List<Team> JoinUserToTeam(List<Team> ListOfInterest, string inputLine)
- {
- string[] input = inputLine.Split(new string[] { "->" }, StringSplitOptions.None).ToArray();
- if (CheckIfTeamExists(ListOfInterest, input[1]))
- {
- List<string> ListOfMembers = new List<string>();
- if (CheckIfMemberExistsInAnotherTeam(ListOfInterest, input[0]))
- {
- Console.WriteLine($"Member {input[0]} cannot join team {input[1]}!");
- return ListOfInterest;
- }
- for (int i = 0; i < ListOfInterest.Count; i++)
- {
- if (ListOfInterest.ElementAt(i).Name == input[1])
- {
- ListOfMembers = ListOfInterest.ElementAt(i).Members;
- ListOfMembers.Add(input[0]);
- ListOfInterest.ElementAt(i).Members = ListOfMembers;
- return ListOfInterest;
- }
- }
- }
- return ListOfInterest;
- }
- static bool CheckIfTeamExists(List<Team> ListOfTeams, string team)
- {
- bool CheckTeam = false;
- foreach (Team item in ListOfTeams)
- {
- if (item.Name == team)
- {
- CheckTeam = true;
- return CheckTeam;
- }
- }
- Console.WriteLine($"Team {team} does not exist!");
- return CheckTeam;
- }
- static bool CheckIfMemberExistsInAnotherTeam(List<Team> ListOfTeams, string Member)
- {
- bool Check = false;
- foreach (Team team in ListOfTeams)
- {
- if (team.Members.Contains(Member) || team.CreatorName == Member)
- {
- Check = true;
- return Check;
- }
- }
- return Check;
- }
- static Team GetNewTeam(string inputLine)
- {
- string[] input = inputLine.Split('-').ToArray();
- Team NewTeam = new Team();
- NewTeam.Name = input[1];
- NewTeam.CreatorName = input[0];
- List<string> Members = new List<string>();
- NewTeam.Members = Members;
- return NewTeam;
- }
- static Input ReadInputLine()
- {
- Input inputLogic = new Input();
- string input = Console.ReadLine();
- bool WordForBreak = false;
- if (input == "end of assignment")
- {
- WordForBreak = true;
- }
- inputLogic.input = input;
- inputLogic.CheckKeyWord = WordForBreak;
- return inputLogic;
- }
- }
- public class Team
- {
- public string Name { get; set; }
- public List<string> Members { get; set; }
- public string CreatorName { get; set; }
- }
- public class Input
- {
- public bool CheckKeyWord { get; set; }
- public string input { get; set; }
- }
- public class TwoListsOfTeams
- {
- public List<Team> ValidList { get; set; }
- public List<Team> ZeroList { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement