04. Teamwork projects
04. Teamwork projects https://judge.softuni.bg/Contests/Practice/Index/1328#3
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}…" |
Examples
Input |
Output |
Comments |
2 Didi-PowerPuffsCoders Toni-Toni is the best Petq->PowerPuffsCoders Toni->Toni is the best end of assignment
|
Team PowerPuffsCoders has been created by Didi! Team Toni is the best has been created by Toni! Member Toni cannot join team Toni is the best! PowerPuffsCoders - Didi -- Petq Teams to disband: Toni is the best
|
Toni created a team in which he tried later to join. So message was shown. Since there is no one other who is trying to join his team the team have to disband. |
3 Tatyana-CloneClub Helena-CloneClub Trifon-AiNaBira Pesho->aiNaBira Pesho->AiNaBira Tatyana->Leda PeshO->AiNaBira Cossima->CloneClub end of assignment |
Team CloneClub has been created by Tatyana! Team CloneClub was already created! Team AiNaBira has been created by Trifon! Team aiNaBira does not exist! Team Leda does not exist! AiNaBira - Trifon -- Pesho -- PeshO CloneClub - Tatyana -- Cossima Teams to disband: |
Note that when you join a team you should check first if it exists, then check if the user is already in a team:
Tatyana has created CloneClub, then she tries to join a non-existing team – so message for non-existing team is shown. |
package Teamworkprojects; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; class Member { private final String user; private final String team; private int count; public Member(String user, String team) { this.user = user; this.team = team; this.count = 1; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public String getUser() { return user; } public String getTeam() { return team; } @Override public String toString() { return String.format(" %s", user); } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int reg = Integer.parseInt(scanner.nextLine()); List<Member> teamsList = new ArrayList<>(); List<Member> memberList = new ArrayList<>(); String read = scanner.nextLine(); boolean create = reg > 0; while (!"end of assignment".equals(read)) { String[] token = read.split("-"); String user = token[0]; String teamName = token[1]; String join = token[1].substring(0, 1); teamName = teamName.replace(">", ""); Member teamCurrent = new Member(user, teamName); Member people = new Member(user, teamName); if (">".equals(join)) { if (isListIsEmpty(teamsList)) { printNotExist(teamName); } else { boolean isIt = true; for (Member member : memberList) { if (checkIsInList(member.getUser(), (user))) { isIt = false; break; } } int count = 0; for (Member member : memberList) { if (checkIsInList(member.getTeam(), teamName) && isIt) { memberList.add(people); break; } else if (checkIsInList(member.getTeam(), teamName) && !isIt) { System.out.println("Member " + user + " cannot join team " + teamName + "!"); break; } else { count++; } } if (count == memberList.size()) { printNotExist(teamName); } } } else { if (isListIsEmpty(teamsList)) { teamsList.add(teamCurrent); memberList.add(people); printCreateTeam(teamName, user); reg--; read = scanner.nextLine(); continue; } for (Member teams : teamsList) { if (!checkIsInList(teams.getTeam(), teamName)) { if (teams.getUser().equals(user)) { System.out.println(user + " cannot create another team!"); } else { if (create) { teamsList.add(teamCurrent); printCreateTeam(teamName, user); memberList.add(people); break; } } } else { System.out.println("Team " + teamName + " was already created!"); } } } reg--; read = scanner.nextLine(); } setTeamsList(memberList, teamsList); printTeams(memberList, teamsList); printDishbanTeams(teamsList); } private static void printDishbanTeams(List<Member> teamsList) { System.out.println("Teams to disband:"); for (Member member : teamsList) { if (member.getCount() == 1) { System.out.println(member.getTeam()); } } } private static void printTeams(List<Member> memberList, List<Member> teamsList) { for (Member member : teamsList) { if (member.getCount() > 1) { System.out.println(member.getTeam()); System.out.println("- " + member.getUser()); for (Member member1 : memberList) { if (checkIsInList(member.getTeam(), member1.getTeam())) { if (1 == member1.getCount()) { System.out.println("--" + member1.toString()); } } } } } } private static void setTeamsList(List<Member> memberList, List<Member> teamsList) { for (Member member : teamsList) { int count = 0; int countFirstIndex = 0; for (Member member1 : memberList) { if (member.getTeam().equals(member1.getTeam())) { count++; } if (member.getUser().equals(member1.getUser())) { countFirstIndex++; if (countFirstIndex == 1) { member1.setCount(0); } } } member.setCount(count); } teamsList.sort(Comparator.comparing(Member::getCount).reversed()); memberList.sort(Comparator.comparing(Member::getUser)); } private static boolean checkIsInList(String user, String user1) { return user.equals(user1); } private static boolean isListIsEmpty(List<Member> list) { return list.isEmpty(); } private static void printNotExist(String teamName) { System.out.println("Team " + teamName + " does not exist!"); } public static void printCreateTeam(String team, String user) { System.out.println("Team " + team + " has been created by " + user + "!"); } }
Докарвам го на IJ ама като го пусна в Judge свеятят 5 от 6 часовника и един грешен тест ( последен). Пробвах да я кача в няколко варянта и като зип, но резултата е един и същ.
Може ли за съдействие.