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() { 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..648776f 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 | IndexOutOfBoundsException 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/Tournament.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java index 1ff76fd..bb4bd92 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -3,134 +3,65 @@ package ch.zhaw.projekt2.turnierverwaltung; import javafx.collections.FXCollections; import javafx.collections.ObservableList; 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; +import java.util.*; - -/** - * 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 final List participants; - private final List places; + private List participants; + private 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}")) { - logger.warning("The entered name is incorrect, Entered name: " + name); - throw new InvalidNameException("Invalid Name entered"); + throw new InvalidNameException("Invalid Name entered"); //TODO handle en logging. } else if (!Arrays.asList(Type.values()).contains(type)) { - logger.warning("The given Type is invalid and does not exist, Given Type: " + type); - throw new InvalidTypeException("Invalid Type selected"); + throw new InvalidTypeException("Invalid Type selected"); //TODO handle en logging. } - 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"); + for(Participant participant: participants){ + if(participant.equals(newParticipant)){ 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)) { - logger.warning("Participant: " + participant + " could not be found"); - throw new ParticipantNotExistsException("The given Participant (" + participant - + ")is not part of this Tournament"); + throw new ParticipantNotExistsException("The given 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); @@ -139,113 +70,70 @@ public class Tournament implements Serializable { /* - Method creates a completely new GameSchedule + creates a complete 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 { - logger.warning("In the prototype the only accessible game schedule is the ko modus"); + //TODO for Type Group } } - /** - * 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; + 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"); @@ -267,9 +155,6 @@ public class Tournament implements Serializable { } } - /** - * Custom exception thrown when an invalid file is chosen - */ public class InvalidTypeException extends Exception { public InvalidTypeException() { super(); @@ -281,9 +166,17 @@ public class Tournament implements Serializable { } - /** - * Custom Expeption thrown when a Participant does not exist - */ + public class ParticipantExistsException extends Exception { + public ParticipantExistsException() { + super(); + } + + public ParticipantExistsException(String errorMessage) { + super(errorMessage); + } + + } + public class ParticipantNotExistsException extends Exception { public ParticipantNotExistsException() { super(); @@ -295,9 +188,6 @@ public class Tournament implements Serializable { } - /** - * Custom Exception thrown when a Place does already exist - */ public class PlaceExistsException extends Exception { public PlaceExistsException() { super(); @@ -309,9 +199,6 @@ public class Tournament implements Serializable { } - /** - * Custom Exception thrown when a Place does not exist - */ public class PlaceNotExistsException extends Exception { public PlaceNotExistsException() { super(); @@ -323,9 +210,6 @@ 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 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); } }