From 3ebf9c5072d243117fba73c7ba7348c041df663e Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 12 May 2022 22:41:18 +0200 Subject: [PATCH 1/4] added Exception for Invalid Date --- .../projekt2/turnierverwaltung/Player.java | 24 ++++++++++++++++--- .../TournamentDecorator.java | 3 +++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java index 29bb2c6..20c78d0 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java @@ -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,10 +28,16 @@ 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("\\."); - this.dateOfBirth = LocalDate.of(Integer.valueOf(date[2]), Integer.valueOf(date[1]), Integer.valueOf(date[0])); + try{ + this.dateOfBirth = LocalDate.of(Integer.valueOf(date[2]), Integer.valueOf(date[1]), Integer.valueOf(date[0])); + } catch (NumberFormatException e) { + e.printStackTrace(); + throw new InvalidDateException("Date invalid"); + } + } else { dateOfBirth = null; } @@ -52,4 +58,16 @@ public class Player extends Person implements Participant{ setDateOfBirth(player.getDateOfBirth()); setPhoneNumber(player.getPhoneNumber()); } + + public class InvalidDateException extends Exception { + public InvalidDateException() { + super(); + } + + public InvalidDateException(String errorMessage) { + super(errorMessage); + } + + } + } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java index 28d6bac..55dc2d7 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -128,6 +128,9 @@ public class TournamentDecorator implements IsObservable{ } 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); } } -- 2.40.1 From ddfbb9bbd0b8917345c54566ea2e499f44f15441 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 12 May 2022 22:45:10 +0200 Subject: [PATCH 2/4] added error Message for file reading error --- .../zhaw/projekt2/turnierverwaltung/FactoryDecorator.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java index e993fad..0026069 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java @@ -51,11 +51,11 @@ public class FactoryDecorator implements IsObservable{ try { factory.setTournament(fileIO.loadTournament(tournamentFile)); openScheduleView(); - } catch (IOException e) { + clearMessage(false); + } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } //TODO handle and logging + printMessageToFooter("Fehler beim lesen der Datei.", true); + } //TODO handle and logging } public void openTournamentList() { -- 2.40.1 From e77d6cbc21cb046b9085537e5e049b5e8d125132 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Thu, 12 May 2022 22:51:05 +0200 Subject: [PATCH 3/4] java doc and logging added to class Tournament --- .../turnierverwaltung/Tournament.java | 178 +++++++++++++++--- .../TournamentDecorator.java | 2 - 2 files changed, 147 insertions(+), 33 deletions(-) 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 73976c3..ac983c6 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -3,67 +3,136 @@ 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.io.Serializable; +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"); } - public void saveParticipant(Participant newParticipant) throws ParticipantExistsException { - for(Participant participant: participants){ - if(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 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); @@ -72,70 +141,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"); @@ -157,6 +269,9 @@ public class Tournament implements Serializable { } } + /** + * Custom exception thrown when an invalid file is chosen + */ public class InvalidTypeException extends Exception { public InvalidTypeException() { super(); @@ -168,17 +283,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(); @@ -190,6 +297,9 @@ public class Tournament implements Serializable { } + /** + * Custom Exception thrown when a Place does already exist + */ public class PlaceExistsException extends Exception { public PlaceExistsException() { super(); @@ -201,6 +311,9 @@ public class Tournament implements Serializable { } + /** + * Custom Exception thrown when a Place does not exist + */ public class PlaceNotExistsException extends Exception { public PlaceNotExistsException() { super(); @@ -212,6 +325,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(); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java index b9af3ad..6019e8e 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -91,8 +91,6 @@ public class TournamentDecorator implements IsObservable{ try { tournament.saveParticipant(new Player(firstName, name, phoneNumber, dateOfBirth)); informListener(); - } catch (Tournament.ParticipantExistsException e) { - e.printStackTrace(); //TODO handle and logging } catch (InvalidNameException e) { e.printStackTrace(); //TODO handle and logging } catch (Person.InvalidPhoneNumberException e) { -- 2.40.1 From 280d2ebda38b9f92eccbe4028ed6a3c7e7ad2230 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 12 May 2022 23:21:17 +0200 Subject: [PATCH 4/4] fixed Error when IndexOutOfBoundsException in setDateofBirtch in Player.java --- .../main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java index 20c78d0..648776f 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java @@ -33,7 +33,7 @@ public class Player extends Person implements Participant{ String[] date = dateOfBirth.split("\\."); try{ this.dateOfBirth = LocalDate.of(Integer.valueOf(date[2]), Integer.valueOf(date[1]), Integer.valueOf(date[0])); - } catch (NumberFormatException e) { + } catch (NumberFormatException | IndexOutOfBoundsException e) { e.printStackTrace(); throw new InvalidDateException("Date invalid"); } -- 2.40.1