diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java index bb4bd92..1ff76fd 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -3,65 +3,134 @@ package ch.zhaw.projekt2.turnierverwaltung; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import java.io.Serializable; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.logging.Logger; + +/** + * Class used to represent a single tournament, participants place and games are placed in it + * For saving purposes it implements Serializable. + */ public class Tournament implements Serializable { private String name; private Type type; - private List participants; - private List places; + private final List participants; + private final List places; private List> gameList; + private static final Logger logger = Logger.getLogger(FileIO.class.getCanonicalName()); + + /** + * Constructor that initializes tournament with the needed parameters name and types, also checks if those + * parameters are valid and throws errors if not + * + * @param name name of the tournament + * @param type type of the Tournament, for example KO or Groupphase + * @throws InvalidNameException If name contains invalid characters + * @throws InvalidTypeException If the Type does not exist + */ public Tournament(String name, Type type) throws InvalidNameException, InvalidTypeException { if (!name.matches("[\\w]{1,20}")) { - throw new InvalidNameException("Invalid Name entered"); //TODO handle en logging. + logger.warning("The entered name is incorrect, Entered name: " + name); + throw new InvalidNameException("Invalid Name entered"); } else if (!Arrays.asList(Type.values()).contains(type)) { - throw new InvalidTypeException("Invalid Type selected"); //TODO handle en logging. + logger.warning("The given Type is invalid and does not exist, Given Type: " + type); + throw new InvalidTypeException("Invalid Type selected"); } + logger.fine("Setting up Tournament with name " + name + " and Type " + type); setName(name); setType(type); + logger.fine("Creating needed Lists for participants, places and games"); participants = new ArrayList<>(); places = new ArrayList<>(); gameList = new ArrayList<>(); + logger.fine("Successfully initialized tournament"); } + /** + * Method used to save new Participant into the tournament, checking if participant already exists and if yes will + * edit the already existing participant + * + * @param newParticipant the Participant to be saved or edited + */ public void saveParticipant(Participant newParticipant) { - for(Participant participant: participants){ - if(participant.equals(newParticipant)){ + logger.fine("Trying to add " + newParticipant + " into the tournament"); + for (Participant participant : participants) { + if (participant.equals(newParticipant)) { + logger.fine("matching participant: " + newParticipant + + "found and instead of creating new one this one will be edited"); participant.change(newParticipant); return; } } + logger.fine("Participant " + newParticipant + " being added to tournament."); participants.add(newParticipant); } + /** + * Method to remove an existing participant throws error if participant could not be found + * + * @param participant + * @throws ParticipantNotExistsException + */ public void removeParticipant(Participant participant) throws ParticipantNotExistsException { + logger.finer("Trying to remove " + participant + " from participants List"); if (!participants.contains(participant)) { - throw new ParticipantNotExistsException("The given Participant is not part of this Tournament"); + logger.warning("Participant: " + participant + " could not be found"); + throw new ParticipantNotExistsException("The given Participant (" + participant + + ")is not part of this Tournament"); } + logger.fine("Matching Participant could be found and is removed, participant name: " + participant); participants.remove(participant); } + /** + * Method to return an Observable List of all current participants + * + * @return ObservableList containing all current participants + */ public ObservableList getParticipants() { + logger.finer("Creates Observable list for participants"); ObservableList participantsObservable = FXCollections.observableArrayList(); + logger.finer("Fills Observable List with all participants"); participantsObservable.addAll(participants); + logger.fine("Returning Observable List containing all participants"); return participantsObservable; } + /** + * Method to add a new Place, also checks if place does already exist. + * + * @param newPlace to be added in the list of all places + * @throws PlaceExistsException if the place already exists + */ public void addPlace(Place newPlace) throws PlaceExistsException { places.removeIf(place -> place.equals(newPlace)); places.add(newPlace); } + /** + * @param place + * @throws PlaceNotExistsException + */ public void removePlace(Place place) throws PlaceNotExistsException { if (!places.contains(place)) { throw new PlaceNotExistsException("The given Place is not part of this Tournament"); } + logger.fine("removing " + place + "from places"); places.remove(place); } + /** + * Method returns an Observable list of all places + * + * @return a list of all places + */ public ObservableList getPlaces() { ObservableList placesObservable = FXCollections.observableArrayList(); placesObservable.addAll(places); @@ -70,70 +139,113 @@ public class Tournament implements Serializable { /* - creates a complete new GameSchedule + Method creates a completely new GameSchedule */ public void createGameSchedule() throws NumberOfParticipantInvalidException { gameList = new ArrayList<>(); if (type == Type.KO) { if (numberOfParticipantValid()) { + logger.fine("initiates a new calculation of the game schedule"); calcGameSchedule(); - //TODO Logging } else { + logger.warning("Invalid number of participants only participants 2^n allowed"); throw new NumberOfParticipantInvalidException("Can not Create Game Schedule for KO Modus"); - //TODO Logging } } else { - //TODO for Type Group + logger.warning("In the prototype the only accessible game schedule is the ko modus"); } } + /** + * Method to check if the number of participants is valid, returns true if yes and false if not + * + * @return boolean that returns if participant is valid + */ private boolean numberOfParticipantValid() { double res = Math.log(participants.size()) / Math.log(2); - return (res * 10) % 10 == 0 && participants.size() >=4; + logger.fine("checks if value is 2^n and returns true if yes"); + return (res * 10) % 10 == 0 && participants.size() >= 4; } + /** + * Method that calculates a new game schedule + */ private void calcGameSchedule() { + logger.finer("uses shuffle of Collections to bring all participants in a random order"); Collections.shuffle(participants); for (int i = 0; i < (Math.log(participants.size()) / Math.log(2)); i++) { List gameRound = new ArrayList<>(); if (i == 0) { for (int j = 0; j < participants.size() - 1; j += 2) { - gameRound.add(new Game(participants.get(j), participants.get(j+1))); + gameRound.add(new Game(participants.get(j), participants.get(j + 1))); } } else { - for (int j = 0; j < (gameList.get(i-1).size()/2); j++) { - gameRound.add(new Game(gameList.get(i-1).get(j*2),gameList.get(i-1).get(j*2+1))); + for (int j = 0; j < (gameList.get(i - 1).size() / 2); j++) { + gameRound.add(new Game(gameList.get(i - 1).get(j * 2), gameList.get(i - 1).get(j * 2 + 1))); } } + logger.fine("Created new game tree"); gameList.add(gameRound); } } - + /** + * Getter Method to return the name of the tournament + * + * @return the name of the tournament + */ public String getName() { + logger.fine("Returning the name of the tournament, Name: " + name); return name; } + /** + * Settermethod for the name of the tournament + * + * @param name the new name that should be set + */ public void setName(String name) { this.name = name; } + /** + * Gettermethod to get the type of the tournament + * + * @return the type of the tournament + */ public Type getType() { + logger.fine("Returning the type of the tournament"); return type; } + /** + * Setter Method to set the type of the tournament + * + * @param type + */ public void setType(Type type) { + logger.fine("Setting the type of the tournament to: " + type); this.type = type; } + /** + * Gettermethod to get the Complete GameList + * + * @return the complete GameList + */ public List> getGameList() { + logger.fine("Returns the complete gameList"); return gameList; } + /** + * Enum for all types of Tournaments + * (In the Prototype only the KO-System has full functionality + */ public enum Type { KO("KO-System"), GROUPS("Gruppenspiele"); @@ -155,6 +267,9 @@ public class Tournament implements Serializable { } } + /** + * Custom exception thrown when an invalid file is chosen + */ public class InvalidTypeException extends Exception { public InvalidTypeException() { super(); @@ -166,17 +281,9 @@ public class Tournament implements Serializable { } - public class ParticipantExistsException extends Exception { - public ParticipantExistsException() { - super(); - } - - public ParticipantExistsException(String errorMessage) { - super(errorMessage); - } - - } - + /** + * Custom Expeption thrown when a Participant does not exist + */ public class ParticipantNotExistsException extends Exception { public ParticipantNotExistsException() { super(); @@ -188,6 +295,9 @@ public class Tournament implements Serializable { } + /** + * Custom Exception thrown when a Place does already exist + */ public class PlaceExistsException extends Exception { public PlaceExistsException() { super(); @@ -199,6 +309,9 @@ public class Tournament implements Serializable { } + /** + * Custom Exception thrown when a Place does not exist + */ public class PlaceNotExistsException extends Exception { public PlaceNotExistsException() { super(); @@ -210,6 +323,9 @@ public class Tournament implements Serializable { } + /** + * Custom Exception thrown when the number of participants is not valid + */ public class NumberOfParticipantInvalidException extends Exception { public NumberOfParticipantInvalidException() { super();