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 TeamworkProjects
- {
- class Program
- {
- static void Main(string[] args)
- {
- int creationCount = int.Parse(Console.ReadLine());
- List<Team> teams = new List<Team>();
- for (int i = 0; i < creationCount; i++)
- {
- string[] teamArgs = Console.ReadLine().Split('-');
- string creator = teamArgs[0];
- string teamName = teamArgs[1];
- CreateTeam(creator, teamName, teams);
- }
- string input = Console.ReadLine();
- while (input != "end of assignment")
- {
- string[] teamJoinArgs = input.Split(new string[] { "->" }, StringSplitOptions.None);
- string joinerName = teamJoinArgs[0];
- string teamToJoin = teamJoinArgs[1];
- JoinTeam(joinerName, teamToJoin, teams);
- input = Console.ReadLine();
- }
- List<Team> populatedTeams = teams.Where(x => x.Members.Count > 1)
- .OrderByDescending(x => x.Members.Count)
- .ThenBy(x => x.TeamName).ToList();
- List<Team> emptyTeams = teams.Where(x => x.Members.Count <= 1).OrderBy(x => x.TeamName).ToList();
- foreach (Team team in populatedTeams)
- {
- Console.WriteLine(team.TeamName);
- string creator = team.Members.First();
- team.Members.Remove(creator);
- Console.WriteLine($"- {creator}");
- foreach (string member in team.Members.OrderBy(x => x))
- {
- Console.WriteLine($"-- {member}");
- }
- }
- Console.WriteLine("Teams to disband:");
- foreach (Team emptyTeam in emptyTeams)
- {
- Console.WriteLine(emptyTeam.TeamName);
- }
- }
- private static void JoinTeam(string joinerName, string teamToJoin, List<Team> teams)
- {
- if (DoesTeamExist(joinerName, teamToJoin, teams))
- {
- if (IsPlayerFree(joinerName, teamToJoin, teams))
- {
- foreach (Team team in teams)
- {
- if (team.TeamName == teamToJoin)
- {
- team.Members.Add(joinerName);
- break;
- }
- }
- }
- }
- }
- private static bool DoesTeamExist(string joinerName, string teamToJoin, List<Team> teams)
- {
- bool teamExistFlag = true;
- foreach (Team team in teams)
- {
- if (team.TeamName != teamToJoin)
- {
- teamExistFlag = false;
- }
- else
- {
- teamExistFlag = true;
- break;
- }
- }
- if (!teamExistFlag)
- {
- Console.WriteLine($"Team {teamToJoin} does not exist!");
- return false;
- }
- return true;
- }
- private static bool IsPlayerFree(string joinerName, string teamToJoin, List<Team> teams)
- {
- foreach (Team team in teams)
- {
- foreach (string teamMember in team.Members)
- {
- if (teamMember == joinerName)
- {
- Console.WriteLine($"Member {joinerName} cannot join team {teamToJoin}!");
- return false;
- }
- }
- }
- return true;
- }
- private static void CreateTeam(string creator, string teamName, List<Team> teams)
- {
- if (ValidateTeamArguments(creator, teamName, teams))
- {
- Console.WriteLine($"Team {teamName} has been created by {creator}!");
- Team team = new Team();
- team.Members = new List<string>();
- team.CreatorName = creator;
- team.TeamName = teamName;
- team.Members.Add(creator);
- teams.Add(team);
- }
- }
- private static bool ValidateTeamArguments(string creator, string teamName, List<Team> teams)
- {
- foreach (Team team in teams)
- {
- if (team.TeamName == teamName)
- {
- Console.WriteLine($"Team {teamName} was already created!");
- return false;
- }
- if (team.CreatorName == creator)
- {
- Console.WriteLine($"{creator} cannot create another team!");
- return false;
- }
- }
- return true;
- }
- }
- class Team
- {
- public string TeamName { get; set; }
- public List<string> Members { get; set; }
- public string CreatorName { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement