Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FootbalTeamGenerator;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws IllegalArgumentException {
- Scanner scanner = new Scanner(System.in);
- String[] tokens = scanner.nextLine().split(";");
- Map<String, Team> teams = new LinkedHashMap<>();
- Team team;
- Player player;
- while (!tokens[0].equals("END")) {
- String command = tokens[0];
- String teamName = tokens[1];
- try {
- if (command.equals("Team")) {
- createNewTeamInTeams(teams, teamName);
- } else if (command.equals("Add")) {
- player = createNewPlayer(tokens);
- if (teams.containsKey(teamName)) {
- teams.get(teamName).addPlayer(player);
- } else {
- throw new IllegalArgumentException
- ("Team " + teamName + " does not exist.");
- }
- } else if (command.equals("Remove")) {
- String playerName = tokens[2];
- if (teams.containsKey(teamName)) {
- removePlayer(teams, teamName, playerName);
- }else {
- throw new IllegalArgumentException
- ("Team " + teamName + " does not exist.");
- }
- } else if (command.equals("Rating")) {
- if (teams.containsKey(teamName)) {
- teams.get(teamName).getRating();
- } else {
- throw new IllegalArgumentException
- ("Team " + teamName + " does not exist.");
- }
- }
- } catch (IllegalArgumentException ex) {
- System.out.println(ex.getMessage());
- }
- tokens = scanner.nextLine().split(";");
- }
- if (!teams.isEmpty()) {
- teams.entrySet()
- .forEach(teamEntry -> {
- System.out.println(teamEntry.getValue().toString());
- });
- }
- }
- private static void removePlayer(Map<String, Team> teams, String teamName, String playerName) {
- if (teams.containsKey(teamName)) {
- teams.get(teamName).removePlayer(playerName);
- } else {
- throw new IllegalArgumentException
- ("Team " + teamName + " does not exist.");
- }
- }
- private static Player createNewPlayer(String[] tokens) {
- Player player;
- String playerName = tokens[2];
- int endurance = Integer.parseInt(tokens[3]);
- int sprint = Integer.parseInt(tokens[4]);
- int dribble = Integer.parseInt(tokens[5]);
- int passing = Integer.parseInt(tokens[6]);
- int shooting = Integer.parseInt(tokens[7]);
- player = new Player
- (playerName, endurance, sprint, dribble, passing, shooting);
- return player;
- }
- private static void createNewTeamInTeams(Map<String, Team> teams, String teamName) {
- Team team;
- team = new Team(teamName);
- teams.putIfAbsent(teamName, new Team(teamName));
- }
- }
Add Comment
Please, Sign In to add comment