Test class #42
|
@ -2,55 +2,113 @@ 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<Participant> participants;
|
||||
private List<Place> places;
|
||||
private final List<Participant> participants;
|
||||
private final List<Place> places;
|
||||
private List<List<Game>> 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) {
|
||||
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<Participant> getParticipants() {
|
||||
logger.finer("Creates Observable list for participants");
|
||||
ObservableList<Participant> 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) {
|
||||
for (Place place : places) {
|
||||
if (place.equals(newPlace)) {
|
||||
|
@ -61,13 +119,23 @@ public class Tournament implements Serializable {
|
|||
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<Place> getPlaces() {
|
||||
ObservableList<Place> placesObservable = FXCollections.observableArrayList();
|
||||
placesObservable.addAll(places);
|
||||
|
@ -76,31 +144,41 @@ 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);
|
||||
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++) {
|
||||
|
@ -114,32 +192,65 @@ public class Tournament implements Serializable {
|
|||
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<List<Game>> 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");
|
||||
|
||||
|
@ -161,6 +272,9 @@ public class Tournament implements Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom exception thrown when an invalid file is chosen
|
||||
*/
|
||||
public class InvalidTypeException extends Exception {
|
||||
public InvalidTypeException() {
|
||||
super();
|
||||
|
@ -172,17 +286,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();
|
||||
|
@ -194,6 +300,9 @@ public class Tournament implements Serializable {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Exception thrown when a Place does already exist
|
||||
*/
|
||||
public class PlaceExistsException extends Exception {
|
||||
public PlaceExistsException() {
|
||||
super();
|
||||
|
@ -205,6 +314,9 @@ public class Tournament implements Serializable {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Exception thrown when a Place does not exist
|
||||
*/
|
||||
public class PlaceNotExistsException extends Exception {
|
||||
public PlaceNotExistsException() {
|
||||
super();
|
||||
|
@ -216,6 +328,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();
|
||||
|
|
Loading…
Reference in New Issue