Objects and classes Exercise- 05. Teamwork Projects
Здравейте колеги. Дава ми 50 точки на джъджа
Условието е следното:
5. Teamwork projects
It's time for teamwork projects and you are responsible for making 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 that team.
For every newly created team you should print a message: "Team {team Name} has been created by
{user}!".
Next you will receive user with 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
(disband teams as well), you have to print its members sorted by name (ascending). However, there
are several rules:
If user tries to create a team more than once a message should be displayed:
- "Team {teamName} was already created!"
Creator of a team cannot create another team - message should be thrown:
- "{user} cannot create another team!"
If user tries to join currently non-existing team a message should be displayed:
- "Team {teamName} does not exist!"
Member of a team cannot join another team - message should be thrown:
- "Member {user} cannot join team {team Name}!"
In the end (after teams' report) teams with zero members (with only a creator) should
disband ordered by name in ascending other.
Every valid team should be printed ordered by name (ascending) in this format:
"{teamName}:
- {creator}
-- {member}…"
КОДЪТ МИ Е СЛЕДНИЯ:
using System;
using System.Collections.Generic;
using System.Linq;
//using System.Text;
public class Team
{
public Team (string name, string creatorName)
{
this.TeamName = name;
this.CreatorName = creatorName;
Members = new List<string>();
}
public string TeamName {get; set;}
public string CreatorName {get; set;}
public List<string> Members {get; set;}
}
public class Program
{
public static void Main()
{
int teamCount = int.Parse(Console.ReadLine());
List<Team> allTeam = new List<Team>();
for (int i =0; i < teamCount; i++)
{
string[] inputForTeam = Console.ReadLine()
.Split(new[]{'-'}, StringSplitOptions.RemoveEmptyEntries);
string currentCreator = inputForTeam[0];
string currentTeamNeme = inputForTeam[1];
// проверка дали тиймът вече не съществува.
// Съобщение, ако вече съществува
bool isTeamNameExist = allTeam
.Select(x => x.TeamName).Contains(currentTeamNeme);
bool isCreatorExist = allTeam
.Any(x => x.CreatorName == currentTeamNeme);
// проверка дали Инициаторът на Тийма вече не е създал тийм.
// Съобщение, ако вече е създал
// if-ови конструкции
if (isTeamNameExist == false && isCreatorExist == false)
{
Team currentTeam = new Team(currentTeamNeme, currentCreator);
allTeam.Add(currentTeam);
Console.WriteLine("Team {0} has been created by {1}!", currentTeamNeme, currentCreator);
}
else if (isTeamNameExist)
{
Console.WriteLine("Team {0} was already created!", currentTeamNeme);
}
else if (isCreatorExist)
{
Console.WriteLine("{0} cannot create another team!", currentCreator);
}
} // end for-circle
while (true)
{
string fensForTeam = Console.ReadLine();
if (fensForTeam == "end of assignment")
{
break;
}
string[] inputAssignment = fensForTeam
.Split(new[]{'-', '>'},StringSplitOptions.RemoveEmptyEntries);
string fen = inputAssignment[0];
string ofFensTeam = inputAssignment[1];
// Първа проверка за това, че Тиймът съществува.
// Единствената положителна проверка в алгоритъма на задачата.
bool isTeamExist = allTeam.Any(x => x.TeamName == ofFensTeam);
// Втора проверка за това дали записващия се не инициатор
// Продължение на втората проверка -дали записващия се
// не се е записал вече в друг тийм.
bool isCreatorCheating = allTeam.Any(x => x.CreatorName == fen);
bool isAlreadyFen = allTeam.Any(x => x.Members.Contains(fen));
if (isTeamExist && isCreatorCheating == false && isAlreadyFen == false)
{
int indexOfTeam = allTeam
.FindIndex(x => x.TeamName == ofFensTeam);
allTeam[indexOfTeam].Members.Add(fen);
}
else if (isTeamExist == false)
{
Console.WriteLine("Team {0} does not exist!", ofFensTeam);
}
else if (isAlreadyFen || isCreatorCheating)
{
Console.WriteLine("Member {0} cannot join team {1}!", fen, ofFensTeam);
}
} // END WHILE CIRCLE
// Изписване на резултатите
// тиймовете с поне един член се подреждат по азбучен ред
// и се изписват по дадения в задачата ред.
List<Team> teamWithMember = allTeam
.Where(x => x.Members.Count > 0)
.OrderByDescending(x => x.Members.Count)
.ThenBy(x => x.TeamName)
.ToList();
List<Team> notValidTeam = allTeam
.Where(x => x.Members.Count == 0)
.OrderBy(x => x.TeamName)
.ToList();
foreach (var team in teamWithMember)
{
Console.WriteLine(team.TeamName);
Console.WriteLine("- " + team.CreatorName);
Console.WriteLine(string.Join(Environment.NewLine, team.Members.Select(x => "-- " + x)));
}
Console.WriteLine("Teams to disband:");
foreach (var team in notValidTeam)
{
Console.WriteLine(team.TeamName);
}
}
}