Compare commits
25
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bdb34fde31 | ||
|
|
20903f6a69 | ||
|
|
e9d42b719b | ||
|
|
1d6fcce13f | ||
|
|
b4da0b2fef | ||
|
|
6c761c944f | ||
|
|
127e152637 | ||
|
|
40cfd69436 | ||
|
|
03c9da13a5 | ||
|
|
f81b031555 | ||
|
|
92d121890c | ||
|
|
280d2ebda3 | ||
|
|
68080c36ff | ||
|
|
3315b7f9c1 | ||
|
|
1031c27650 | ||
|
|
e77d6cbc21 | ||
|
|
ddfbb9bbd0 | ||
|
|
3ebf9c5072 | ||
|
|
f8e4dadcfb | ||
|
|
f885092259 | ||
|
|
2784cd67fa | ||
|
|
298d61eeca | ||
|
|
b0aa0ab1ad | ||
|
|
4b74a8d285 | ||
|
|
3d13dbcb45 |
+5
-1
@@ -26,6 +26,8 @@ dependencies {
|
||||
// Use JUnit Jupiter for testing.
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
|
||||
|
||||
testImplementation 'org.mockito:mockito-core:4.3.+'
|
||||
|
||||
// This dependency is used by the application.
|
||||
implementation 'com.google.guava:guava:30.1.1-jre'
|
||||
|
||||
@@ -37,7 +39,9 @@ application {
|
||||
mainClass = 'ch.zhaw.projekt2.turnierverwaltung.App'
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
test {
|
||||
// Use JUnit Platform for unit tests.
|
||||
useJUnitPlatform()
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -37,11 +37,6 @@ public abstract class FXController {
|
||||
factoryDecorator.addListener(listener);
|
||||
}
|
||||
|
||||
protected void removeListener(){
|
||||
tournamentDecorator.removeListener(listener);
|
||||
factoryDecorator.removeListener(listener);
|
||||
}
|
||||
|
||||
protected TournamentDecorator getTournamentDecorator() {
|
||||
return tournamentDecorator;
|
||||
}
|
||||
|
||||
@@ -2,22 +2,23 @@ package ch.zhaw.projekt2.turnierverwaltung;
|
||||
|
||||
import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameController;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameDecorator;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.layout.*;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.text.Font;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
public class Factory {
|
||||
private TournamentDecorator tournamentDecorator;
|
||||
private FileIO fileIO;
|
||||
//TODO save views instead of recreate them.
|
||||
|
||||
public Factory(FileIO fileIO, TournamentDecorator tournamentDecorator) {
|
||||
this.fileIO = fileIO;
|
||||
this.tournamentDecorator = tournamentDecorator;
|
||||
|
||||
}
|
||||
|
||||
public TournamentDecorator getTournamentDecorator() {
|
||||
@@ -39,25 +40,43 @@ public class Factory {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void loadTournamentList(BorderPane pane, FactoryDecorator factoryDecorator){
|
||||
public void loadAllViews(FactoryDecorator factoryDecorator, BorderPane pane){
|
||||
for(View view : View.values()){
|
||||
try {
|
||||
view.loadView(tournamentDecorator, fileIO, factoryDecorator, pane);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
//TODO Handle and logging.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void showTournamentList(BorderPane pane, FactoryDecorator factoryDecorator) {
|
||||
tournamentDecorator.setTournament(null);
|
||||
TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml"), factoryDecorator);
|
||||
setCenterOfBorderPane(pane, View.tournamentList);
|
||||
}
|
||||
|
||||
//Can be used to Open new Scene in same Stage.
|
||||
//This way possible to later give object to Controller
|
||||
public void loadParticipantFormular(BorderPane pane, FactoryDecorator factoryDecorator) {
|
||||
setCenterOfBorderPane(pane, getClass().getResource("participantAddFormular/participantFormular.fxml"), factoryDecorator);
|
||||
public void showParticipantFormular(BorderPane pane, FactoryDecorator factoryDecorator) {
|
||||
setCenterOfBorderPane(pane, View.participantFormular);
|
||||
}
|
||||
|
||||
public void loadPlacesFormular(BorderPane pane, FactoryDecorator factoryDecorator) {
|
||||
setCenterOfBorderPane(pane, getClass().getResource("placesAddFormular/PlacesFormular.fxml"), factoryDecorator);
|
||||
public void showPlacesFormular(BorderPane pane, FactoryDecorator factoryDecorator) {
|
||||
setCenterOfBorderPane(pane, View.placesFormular);
|
||||
}
|
||||
|
||||
public void loadGameScheduler(BorderPane pane, FactoryDecorator factoryDecorator) {
|
||||
setCenterOfBorderPane(pane, getClass().getResource("gameScheduleView/GameSchedule.fxml"), factoryDecorator);
|
||||
public void showGameScheduler(BorderPane pane, FactoryDecorator factoryDecorator) {
|
||||
setCenterOfBorderPane(pane, View.gameScheduler);
|
||||
}
|
||||
|
||||
private void setCenterOfBorderPane(BorderPane pane, View view) {
|
||||
FXController controller = null;
|
||||
pane.setCenter(view.getPane());
|
||||
resetFooter(pane, true);
|
||||
}
|
||||
|
||||
|
||||
public GameController loadGameView(VBox box, GameDecorator gameDecorator, FactoryDecorator factoryDecorator) {
|
||||
try {
|
||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("gameScheduleView/Game.fxml"));
|
||||
@@ -72,21 +91,69 @@ public class Factory {
|
||||
return null;
|
||||
}
|
||||
|
||||
private FXController setCenterOfBorderPane(BorderPane pane, URL location, FactoryDecorator factoryDecorator) {
|
||||
FXController controller = null;
|
||||
try {
|
||||
FXMLLoader loader = new FXMLLoader(location);
|
||||
pane.setCenter(loader.load());
|
||||
controller = loader.getController();
|
||||
controller.setup(tournamentDecorator, fileIO, factoryDecorator, pane);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
//TODO handle and logging?
|
||||
}
|
||||
return controller;
|
||||
}
|
||||
public void printMessageToFooter(BorderPane pane, String msg, boolean error) {
|
||||
VBox bottom = (VBox) pane.getBottom();
|
||||
Label label = new Label();
|
||||
VBox innerVbox;
|
||||
label.setText(msg);
|
||||
|
||||
if (error) {
|
||||
innerVbox = (VBox) bottom.getChildren().get(0);
|
||||
resetFooter(pane, true);
|
||||
label.setTextFill(Color.RED);
|
||||
label.setFont(new Font(20));
|
||||
innerVbox.setBorder(new Border(new BorderStroke(Color.valueOf("#FF0000"), BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
|
||||
innerVbox.getChildren().add(label);
|
||||
|
||||
} else {
|
||||
innerVbox = (VBox) bottom.getChildren().get(1);
|
||||
resetFooter(pane,false);
|
||||
label.setTextFill(Color.GREEN);
|
||||
label.setFont(new Font(10));
|
||||
label.setPadding(new Insets(0,10,0,0));
|
||||
innerVbox.getChildren().add(label);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void resetFooter(BorderPane pane,boolean error) {
|
||||
VBox bottom = (VBox) pane.getBottom();
|
||||
VBox vBox;
|
||||
if (error) {
|
||||
vBox = (VBox) bottom.getChildren().get(0);
|
||||
} else {
|
||||
vBox = (VBox) bottom.getChildren().get(1);
|
||||
}
|
||||
vBox.getChildren().clear();
|
||||
vBox.setBorder(null);
|
||||
}
|
||||
|
||||
public enum View {
|
||||
tournamentList("tournamentList/tournamentList.fxml"),
|
||||
participantFormular("participantAddFormular/participantFormular.fxml"),
|
||||
placesFormular("placesAddFormular/PlacesFormular.fxml"),
|
||||
gameScheduler("gameScheduleView/GameSchedule.fxml");
|
||||
|
||||
private String fxmlFileName;
|
||||
private Pane pane;
|
||||
|
||||
private View(String fxmlFileName) {
|
||||
this.fxmlFileName = fxmlFileName;
|
||||
}
|
||||
|
||||
public Pane getPane() {
|
||||
return pane;
|
||||
}
|
||||
|
||||
public void loadView(TournamentDecorator tournamentDecorator, FileIO fileIO, FactoryDecorator factoryDecorator, BorderPane borderPane) throws IOException {
|
||||
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFileName));
|
||||
this.pane = loader.load();
|
||||
FXController controller = loader.getController();
|
||||
controller.setup(tournamentDecorator, fileIO, factoryDecorator, borderPane);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package ch.zhaw.projekt2.turnierverwaltung;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameController;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameDecorator;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.Pane;
|
||||
@@ -52,36 +51,40 @@ public class FactoryDecorator implements IsObservable{
|
||||
try {
|
||||
factory.setTournament(fileIO.loadTournament(tournamentFile));
|
||||
openScheduleView();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
clearMessage(false);
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
printMessageToFooter("Fehler beim lesen der Datei.", true);
|
||||
} //TODO handle and logging
|
||||
}
|
||||
|
||||
public void openTournamentList() {
|
||||
factory.loadTournamentList((BorderPane) pane, this);
|
||||
factory.showTournamentList((BorderPane) pane, this);
|
||||
informListener();
|
||||
}
|
||||
|
||||
public void openParticipantFormular() {
|
||||
factory.loadParticipantFormular((BorderPane) pane, this);
|
||||
factory.showParticipantFormular((BorderPane) pane, this);
|
||||
informListener();
|
||||
}
|
||||
|
||||
public void openPlacesFormular() {
|
||||
factory.loadPlacesFormular((BorderPane) pane, this);
|
||||
factory.showPlacesFormular((BorderPane) pane, this);
|
||||
informListener();
|
||||
}
|
||||
|
||||
public void openScheduleView() {
|
||||
factory.loadGameScheduler((BorderPane) pane, this);
|
||||
factory.showGameScheduler((BorderPane) pane, this);
|
||||
informListener();
|
||||
}
|
||||
|
||||
public void loadGameList(HBox hBoxCenter, TournamentDecorator tournamentDecorator, boolean treeView) {
|
||||
hBoxCenter.getChildren().clear();
|
||||
|
||||
if(tournamentDecorator.getTournament() == null){
|
||||
return;
|
||||
}
|
||||
|
||||
List<List<Game>> gameList = tournamentDecorator.getTournament().getGameList();
|
||||
|
||||
List<GameDecorator> gameDecoratorsList = new ArrayList<>();
|
||||
@@ -158,6 +161,13 @@ public class FactoryDecorator implements IsObservable{
|
||||
return factory.loadGameView(vBox ,gameDecorator, this);
|
||||
}
|
||||
|
||||
public void printMessageToFooter(String msg, boolean error) {
|
||||
factory.printMessageToFooter((BorderPane) pane,msg,error);
|
||||
}
|
||||
|
||||
public void clearMessage(boolean error){
|
||||
factory.resetFooter((BorderPane) pane, error);
|
||||
}
|
||||
|
||||
public void informListener() {
|
||||
for(IsObserver observer : listener) {
|
||||
|
||||
@@ -6,4 +6,5 @@ public interface Participant extends Serializable {
|
||||
String getName();
|
||||
void setName(String name);
|
||||
boolean equals(Participant participant);
|
||||
void change(Participant participant);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.io.Serializable;
|
||||
|
||||
public class Person implements Serializable {
|
||||
private final String NAME_MATCHING_REGEX = "[a-zA-Z]{1,20}";
|
||||
private final String PHONE_MATCHING_REGEX = "[\\+0-9]+";
|
||||
private final String PHONE_MATCHING_REGEX = "[\\+]?[0-9]*";
|
||||
|
||||
private String name;
|
||||
private String firstName;
|
||||
|
||||
@@ -29,4 +29,8 @@ public class Place implements Serializable {
|
||||
public boolean equals(Place place){
|
||||
return name.equals(place.getName());
|
||||
}
|
||||
|
||||
public void change(Place place) {
|
||||
//TODO: If Place gets more developed in future releases
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ public class Player extends Person implements Participant{
|
||||
|
||||
private LocalDate dateOfBirth;
|
||||
|
||||
public Player(String firstName, String name, String phoneNumber, String dateOfBirth) throws InvalidNameException, InvalidPhoneNumberException {
|
||||
public Player(String firstName, String name, String phoneNumber, String dateOfBirth) throws InvalidNameException, InvalidPhoneNumberException, InvalidDateException {
|
||||
super(firstName, name, phoneNumber);
|
||||
setDateOfBirth(dateOfBirth);
|
||||
}
|
||||
@@ -28,13 +28,46 @@ public class Player extends Person implements Participant{
|
||||
}
|
||||
}
|
||||
|
||||
public void setDateOfBirth(String dateOfBirth) {
|
||||
public void setDateOfBirth(String dateOfBirth) throws InvalidDateException {
|
||||
if(dateOfBirth.length() > 0) {
|
||||
String[] date = dateOfBirth.split("\\.");
|
||||
try{
|
||||
this.dateOfBirth = LocalDate.of(Integer.valueOf(date[2]), Integer.valueOf(date[1]), Integer.valueOf(date[0]));
|
||||
} catch (NumberFormatException | IndexOutOfBoundsException e) {
|
||||
e.printStackTrace();
|
||||
throw new InvalidDateException("Date invalid");
|
||||
}
|
||||
|
||||
} else {
|
||||
dateOfBirth = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setDateOfBirth(LocalDate dateOfBirth) {
|
||||
this.dateOfBirth = dateOfBirth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Participant participant) {
|
||||
return getClass().equals(participant.getClass()) && toString().toLowerCase().equals(participant.toString().toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void change(Participant participant) {
|
||||
Player player = (Player) participant;
|
||||
setDateOfBirth(player.getDateOfBirth());
|
||||
setPhoneNumber(player.getPhoneNumber());
|
||||
}
|
||||
|
||||
public class InvalidDateException extends Exception {
|
||||
public InvalidDateException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public InvalidDateException(String errorMessage) {
|
||||
super(errorMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,11 +27,24 @@ public class Team implements Participant {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setPlayers(List<Player> players) {
|
||||
this.players = players;
|
||||
}
|
||||
|
||||
public List<Player> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Participant participant) {
|
||||
return getClass().equals(participant.getClass()) && toString().toLowerCase().equals(participant.toString().toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void change(Participant participant) {
|
||||
return;
|
||||
}
|
||||
|
||||
public Person getContactPerson() {
|
||||
return contactPerson;
|
||||
}
|
||||
|
||||
@@ -3,62 +3,139 @@ package ch.zhaw.projekt2.turnierverwaltung;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
|
||||
import java.io.File;
|
||||
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");
|
||||
}
|
||||
|
||||
public void addParticipant(Participant newParticipant) throws ParticipantExistsException {
|
||||
participants.removeIf(participant -> participant.equals(newParticipant));
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
public void addPlace(Place newPlace) throws PlaceExistsException {
|
||||
places.removeIf(place -> place.equals(newPlace));
|
||||
/**
|
||||
* Method to add a new Place, also checks if place does already exist.
|
||||
*
|
||||
* @param newPlace to be added in the list of all places
|
||||
*/
|
||||
public void savePlace(Place newPlace) {
|
||||
for (Place place : places) {
|
||||
if (place.equals(newPlace)) {
|
||||
place.change(newPlace);
|
||||
return;
|
||||
}
|
||||
}
|
||||
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);
|
||||
@@ -67,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++) {
|
||||
@@ -105,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");
|
||||
|
||||
@@ -152,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();
|
||||
@@ -163,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();
|
||||
@@ -185,17 +300,9 @@ public class Tournament implements Serializable {
|
||||
|
||||
}
|
||||
|
||||
public class PlaceExistsException extends Exception {
|
||||
public PlaceExistsException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public PlaceExistsException(String errorMessage) {
|
||||
super(errorMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Exception thrown when a Place does not exist
|
||||
*/
|
||||
public class PlaceNotExistsException extends Exception {
|
||||
public PlaceNotExistsException() {
|
||||
super();
|
||||
@@ -207,6 +314,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();
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
package ch.zhaw.projekt2.turnierverwaltung;
|
||||
|
||||
import javafx.application.Platform;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TournamentDecorator implements IsObservable{
|
||||
private Tournament tournament;
|
||||
private FileIO fileIO;
|
||||
private List<IsObserver> listener = new ArrayList<>();
|
||||
private ExecutorService executorService;
|
||||
private FactoryDecorator factoryDecorator;
|
||||
|
||||
public TournamentDecorator(FileIO fileIO){
|
||||
setFileIO(fileIO);
|
||||
@@ -24,7 +28,11 @@ public class TournamentDecorator implements IsObservable{
|
||||
}
|
||||
}
|
||||
});
|
||||
executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
executorService = Executors.newFixedThreadPool(1);
|
||||
}
|
||||
|
||||
public void setFactoryDecorator(FactoryDecorator factoryDecorator) {
|
||||
this.factoryDecorator = factoryDecorator;
|
||||
}
|
||||
|
||||
public void setFileIO(FileIO fileIO) {
|
||||
@@ -52,81 +60,112 @@ public class TournamentDecorator implements IsObservable{
|
||||
|
||||
public void saveTournament(){
|
||||
executorService.execute(new saveTask());
|
||||
factoryDecorator.clearMessage(false);
|
||||
factoryDecorator.printMessageToFooter("Zuletzt gespeichert: " + new Date().toString(),false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void createTournament(String name, Tournament.Type type){
|
||||
if(fileIO.tournamentExists(name)){
|
||||
System.out.println("Tournament with same name exists already.");
|
||||
return; //TODO handle and logging
|
||||
}
|
||||
|
||||
try {
|
||||
if(fileIO.tournamentExists(name)){
|
||||
//TODO:Logger
|
||||
factoryDecorator.printMessageToFooter("Turniername existiert bereits",true);
|
||||
return;
|
||||
}
|
||||
Tournament tournament = new Tournament(name, type);
|
||||
fileIO.saveTournament(tournament);
|
||||
factoryDecorator.clearMessage(true);
|
||||
informListener();
|
||||
} catch (InvalidNameException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
e.printStackTrace();
|
||||
//TODO: Logger
|
||||
factoryDecorator.printMessageToFooter("Invalider Turniername",true);
|
||||
} catch (Tournament.InvalidTypeException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
e.printStackTrace();
|
||||
//TODO: Logger
|
||||
factoryDecorator.printMessageToFooter("Turniermodus nicht möglich",true);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
e.printStackTrace();
|
||||
//TODO: Logger
|
||||
factoryDecorator.printMessageToFooter("Fehler bei Dateizugriff",true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void deleteTournament(FileIO.TournamentFile tournamentFile){
|
||||
try {
|
||||
fileIO.deleteTournament(tournamentFile);
|
||||
factoryDecorator.clearMessage(true);
|
||||
informListener();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
factoryDecorator.printMessageToFooter("Fehler bei Dateizugriff",true);
|
||||
}
|
||||
}
|
||||
|
||||
public void createNewGameSchedule() throws Tournament.NumberOfParticipantInvalidException {
|
||||
public void createNewGameSchedule() {
|
||||
//TODO: logging
|
||||
try {
|
||||
tournament.createGameSchedule();
|
||||
factoryDecorator.clearMessage(true);
|
||||
} catch (Tournament.NumberOfParticipantInvalidException e) {
|
||||
e.printStackTrace();
|
||||
factoryDecorator.printMessageToFooter("Anzahl Teilnehmer nicht vielfaches von 2",true);
|
||||
}
|
||||
informListener();
|
||||
}
|
||||
|
||||
public void savePlayer(String firstName, String name, String phoneNumber, String dateOfBirth){
|
||||
try {
|
||||
tournament.addParticipant(new Player(firstName, name, phoneNumber, dateOfBirth));
|
||||
tournament.saveParticipant(new Player(firstName, name, phoneNumber, dateOfBirth));
|
||||
factoryDecorator.clearMessage(true);
|
||||
informListener();
|
||||
} catch (Tournament.ParticipantExistsException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
} catch (InvalidNameException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
factoryDecorator.printMessageToFooter("Invalider Teilnehmername",true);
|
||||
} catch (Person.InvalidPhoneNumberException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
factoryDecorator.printMessageToFooter("Invalide Telefonnummer",true);
|
||||
} catch (Player.InvalidDateException e) {
|
||||
e.printStackTrace();
|
||||
factoryDecorator.printMessageToFooter("Ungültiges Geburtsdatum", true);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteParticipant(Participant participant){
|
||||
try {
|
||||
tournament.removeParticipant(participant);
|
||||
factoryDecorator.clearMessage(true);
|
||||
informListener();
|
||||
} catch (Tournament.ParticipantNotExistsException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
factoryDecorator.printMessageToFooter("Teilnehmer existiert nicht",true);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void savePlace(String name){
|
||||
try {
|
||||
tournament.addPlace(new Place(name));
|
||||
tournament.savePlace(new Place(name));
|
||||
factoryDecorator.clearMessage(true);
|
||||
informListener();
|
||||
} catch (Tournament.PlaceExistsException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
} catch (InvalidNameException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
factoryDecorator.printMessageToFooter("Invalider Ortsname",true);
|
||||
}
|
||||
}
|
||||
|
||||
public void deletePlace(Place place){
|
||||
try {
|
||||
tournament.removePlace(place);
|
||||
factoryDecorator.clearMessage(true);
|
||||
informListener();
|
||||
} catch (Tournament.PlaceNotExistsException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
factoryDecorator.printMessageToFooter("Ort existiert nicht",true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,6 +176,11 @@ public class TournamentDecorator implements IsObservable{
|
||||
}
|
||||
}
|
||||
|
||||
public void closeApplication() throws InterruptedException {
|
||||
executorService.shutdown();
|
||||
executorService.awaitTermination(2000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
private class saveTask implements Runnable {
|
||||
|
||||
@Override
|
||||
@@ -144,7 +188,10 @@ public class TournamentDecorator implements IsObservable{
|
||||
try {
|
||||
fileIO.saveTournament(tournament);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); //TODO handle and logging
|
||||
e.printStackTrace();
|
||||
Platform.runLater(() -> {
|
||||
factoryDecorator.printMessageToFooter("Datenzugriff nicht moeglich",true);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,29 +5,31 @@ import ch.zhaw.projekt2.turnierverwaltung.FactoryDecorator;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.FileIO;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.TournamentDecorator;
|
||||
import javafx.application.Application;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.Pane;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.stage.WindowEvent;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MainWindow extends Application {
|
||||
private FileIO fileIO = new FileIO(System.getProperty("user.dir") + System.getProperty("file.separator") + "tournierverwaltung_angrynerds");
|
||||
private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO);
|
||||
private Factory factory = new Factory(fileIO, tournamentDecorator); //TODO make it private!
|
||||
private FactoryDecorator factoryDecorator;
|
||||
private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO);
|
||||
private Factory factory = new Factory(fileIO, tournamentDecorator);
|
||||
private static final Logger logger = Logger.getLogger(FileIO.class.getCanonicalName());
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
|
||||
BorderPane pane = factory.loadMainWindow();
|
||||
factoryDecorator = new FactoryDecorator(fileIO, factory, pane);
|
||||
factory.loadAllViews(factoryDecorator, pane);
|
||||
tournamentDecorator.setFactoryDecorator(factoryDecorator);
|
||||
factoryDecorator.openTournamentList();
|
||||
|
||||
|
||||
Scene scene = new Scene(pane);
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.setMaximized(true);
|
||||
@@ -36,5 +38,13 @@ public class MainWindow extends Application {
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
try {
|
||||
super.stop();
|
||||
tournamentDecorator.closeApplication();
|
||||
} catch (Exception e) {
|
||||
logger.warning("Exception while closing application");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package ch.zhaw.projekt2.turnierverwaltung.main;
|
||||
|
||||
import ch.zhaw.projekt2.turnierverwaltung.FXController;
|
||||
import javafx.application.Platform;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
|
||||
@@ -13,7 +14,7 @@ public class MainWindowController extends FXController {
|
||||
|
||||
@FXML
|
||||
void closeApplication(ActionEvent event) {
|
||||
|
||||
Platform.exit();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
-8
@@ -30,29 +30,21 @@ public class GameScheduleController extends FXController {
|
||||
|
||||
@FXML
|
||||
void createNewSchedule(ActionEvent event) {
|
||||
try {
|
||||
getTournamentDecorator().createNewGameSchedule();
|
||||
} catch (Tournament.NumberOfParticipantInvalidException e) {
|
||||
//TODO Method in FactoryDecorater to show msg
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
void openPlacesFormular(ActionEvent event) {
|
||||
removeListener();
|
||||
getFactoryDecorator().openPlacesFormular();
|
||||
}
|
||||
|
||||
@FXML
|
||||
void openParticipantFormular(ActionEvent event) {
|
||||
removeListener();
|
||||
getFactoryDecorator().openParticipantFormular();
|
||||
}
|
||||
|
||||
@FXML
|
||||
void closeTournament(ActionEvent event) {
|
||||
removeListener();
|
||||
getFactoryDecorator().openTournamentList();
|
||||
}
|
||||
|
||||
|
||||
+15
-2
@@ -3,6 +3,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main.participantAddFormular;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.FXController;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.Participant;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.Player;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.Tournament;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
@@ -79,6 +80,14 @@ public class ParticipantFormularController extends FXController {
|
||||
@FXML
|
||||
void saveParticipant(ActionEvent event) {
|
||||
getTournamentDecorator().savePlayer(firstNameTextField.getText(), participantNameTextField.getText(), phoneNumberTextField.getText(), birthDateTextField.getText());
|
||||
clearFormular();
|
||||
}
|
||||
|
||||
private void clearFormular() {
|
||||
firstNameTextField.clear();
|
||||
participantNameTextField.clear();
|
||||
phoneNumberTextField.clear();
|
||||
birthDateTextField.clear();
|
||||
}
|
||||
|
||||
@FXML
|
||||
@@ -89,12 +98,16 @@ public class ParticipantFormularController extends FXController {
|
||||
|
||||
@FXML
|
||||
void close(ActionEvent event) {
|
||||
removeListener();
|
||||
getFactoryDecorator().openScheduleView();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadContent() {
|
||||
participantListView.setItems(getTournamentDecorator().getTournament().getParticipants());
|
||||
Tournament tournament = getTournamentDecorator().getTournament();
|
||||
if(tournament != null){
|
||||
participantListView.setItems(tournament.getParticipants());
|
||||
} else {
|
||||
participantListView.getItems().clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-2
@@ -3,6 +3,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main.placesAddFormular;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.FXController;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.Place;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.Player;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.Tournament;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
@@ -58,6 +59,11 @@ public class PlacesFormularController extends FXController {
|
||||
@FXML
|
||||
void savePlace(ActionEvent event) {
|
||||
getTournamentDecorator().savePlace(placeNameTextField.getText());
|
||||
clearFormular();
|
||||
}
|
||||
|
||||
private void clearFormular() {
|
||||
placeNameTextField.clear();
|
||||
}
|
||||
|
||||
@FXML
|
||||
@@ -68,12 +74,16 @@ public class PlacesFormularController extends FXController {
|
||||
|
||||
@FXML
|
||||
void close(ActionEvent event) {
|
||||
removeListener();
|
||||
getFactoryDecorator().openScheduleView();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadContent() {
|
||||
placeListView.setItems(getTournamentDecorator().getTournament().getPlaces());
|
||||
Tournament tournament = getTournamentDecorator().getTournament();
|
||||
if(tournament != null){
|
||||
placeListView.setItems(tournament.getPlaces());
|
||||
} else {
|
||||
placeListView.getItems().clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,6 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList;
|
||||
|
||||
import ch.zhaw.projekt2.turnierverwaltung.FXController;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.FileIO;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.InvalidNameException;
|
||||
import ch.zhaw.projekt2.turnierverwaltung.Tournament;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
@@ -61,7 +62,6 @@ public class TournamentListController extends FXController {
|
||||
|
||||
@FXML
|
||||
void openTournament(ActionEvent event) {
|
||||
removeListener();
|
||||
FileIO.TournamentFile tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0);
|
||||
getFactoryDecorator().openTournament(tournamentFile);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<?import javafx.scene.text.Font?>
|
||||
|
||||
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.MainWindowController">
|
||||
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.MainWindowController">
|
||||
<top>
|
||||
<VBox alignment="TOP_CENTER" prefHeight="86.0" prefWidth="600.0" BorderPane.alignment="CENTER">
|
||||
<children>
|
||||
@@ -34,4 +34,12 @@
|
||||
</children>
|
||||
</VBox>
|
||||
</top>
|
||||
<bottom>
|
||||
<VBox prefHeight="49.0" prefWidth="600.0" BorderPane.alignment="CENTER">
|
||||
<children>
|
||||
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" />
|
||||
<VBox alignment="CENTER_RIGHT" prefHeight="200.0" prefWidth="100.0" />
|
||||
</children>
|
||||
</VBox>
|
||||
</bottom>
|
||||
</BorderPane>
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package ch.zhaw.projekt2.turnierverwaltung;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@DisplayName("TournamentTest")
|
||||
public class TournamentTest {
|
||||
|
||||
@Nested
|
||||
@DisplayName("KOMode")
|
||||
public class KOMode {
|
||||
private Tournament tournament;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
try {
|
||||
tournament = new Tournament("Name", Tournament.Type.KO);
|
||||
} catch (InvalidNameException e) {
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
} catch (Tournament.InvalidTypeException e) {
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test invalid instance")
|
||||
void makeInvalidInstance() {
|
||||
assertThrows(InvalidNameException.class, () -> new Tournament(".", Tournament.Type.KO));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Save Participant")
|
||||
void saveParticipantTest() {
|
||||
//Checks if one Participant gets added
|
||||
Participant participantOne = Mockito.mock(Player.class);
|
||||
when(participantOne.equals(any(Participant.class))).thenReturn(false);
|
||||
|
||||
assertEquals(0, tournament.getParticipants().size());
|
||||
tournament.saveParticipant(participantOne);
|
||||
assertEquals(1, tournament.getParticipants().size());
|
||||
|
||||
//Checks if a second Participant gets added
|
||||
Participant participantTwo = Mockito.mock(Player.class);
|
||||
tournament.saveParticipant(participantTwo);
|
||||
assertEquals(2, tournament.getParticipants().size());
|
||||
|
||||
//Checks if a allready added Particpant does not get added
|
||||
when(participantOne.equals(any(Participant.class))).thenReturn(true);
|
||||
tournament.saveParticipant(participantTwo);
|
||||
assertEquals(2, tournament.getParticipants().size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Remove Participant")
|
||||
void removeParticipantTest() {
|
||||
Participant participant = Mockito.mock(Player.class);
|
||||
|
||||
//Checks if Error is thrown if Participant not in list
|
||||
assertThrows(Tournament.ParticipantNotExistsException.class, () -> tournament.removeParticipant(participant));
|
||||
|
||||
//Checks if participant gets removed
|
||||
tournament.saveParticipant(participant);
|
||||
assertEquals(1, tournament.getParticipants().size());
|
||||
try {
|
||||
tournament.removeParticipant(participant);
|
||||
assertEquals(0, tournament.getParticipants().size());
|
||||
} catch (Tournament.ParticipantNotExistsException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Add Place")
|
||||
void addPlaceTest() {
|
||||
Place place = mock(Place.class);
|
||||
when(place.equals(any(Place.class))).thenReturn(false).thenReturn(true);
|
||||
|
||||
assertEquals(0, tournament.getPlaces().size());
|
||||
tournament.savePlace(place);
|
||||
assertEquals(1, tournament.getPlaces().size());
|
||||
tournament.savePlace(place);
|
||||
assertEquals(2, tournament.getPlaces().size());
|
||||
tournament.savePlace(place);
|
||||
assertEquals(2, tournament.getPlaces().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Remove Place")
|
||||
void removePlaceTest() {
|
||||
Place place = mock(Place.class);
|
||||
|
||||
assertThrows(Tournament.PlaceNotExistsException.class, () -> tournament.removePlace(place));
|
||||
|
||||
tournament.savePlace(place);
|
||||
assertEquals(1, tournament.getPlaces().size());
|
||||
try {
|
||||
tournament.removePlace(place);
|
||||
assertEquals(0, tournament.getPlaces().size());
|
||||
} catch (Tournament.PlaceNotExistsException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test gameschedule calculation")
|
||||
void calcGameSchedule() {
|
||||
Participant participant = mock(Player.class);
|
||||
when(participant.equals(any(Participant.class))).thenReturn(false);
|
||||
|
||||
//Checks if invalid number of Participants throws error
|
||||
assertThrows(Tournament.NumberOfParticipantInvalidException.class, () -> tournament.createGameSchedule());
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if (i % 4 == 0 && i > 0) {
|
||||
try {
|
||||
tournament.createGameSchedule();
|
||||
assertEquals(2, tournament.getGameList().size());
|
||||
tournament.saveParticipant(participant);
|
||||
} catch (Tournament.NumberOfParticipantInvalidException e) {
|
||||
fail();
|
||||
}
|
||||
|
||||
} else {
|
||||
assertThrows(Tournament.NumberOfParticipantInvalidException.class, () -> tournament.createGameSchedule());
|
||||
tournament.saveParticipant(participant);
|
||||
}
|
||||
}
|
||||
try {
|
||||
tournament.createGameSchedule();
|
||||
} catch (Tournament.NumberOfParticipantInvalidException e) {
|
||||
fail();
|
||||
}
|
||||
assertEquals(3, tournament.getGameList().size());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user