Logging and docs added to classes Person, Place and Player #27 #28 #50

Merged
brandleo merged 10 commits from logging_and_docs into main 2022-05-13 17:49:39 +02:00
7 changed files with 335 additions and 82 deletions

View File

@ -2,9 +2,38 @@ package ch.zhaw.projekt2.turnierverwaltung;
import java.io.Serializable; import java.io.Serializable;
/**
* Interface that defines the common functionality of a Participant, used by Players and Teams
*/
public interface Participant extends Serializable { public interface Participant extends Serializable {
/**
* Method that will be used to get
*
* @return the name
*/
String getName(); String getName();
void setName(String name);
/**
* Method to set a participants name
*
* @param name to be set
* @throws InvalidNameException if the name does not follow the correct format
*/
void setName(String name) throws InvalidNameException;
/**
* Method to compare two participants with each other
*
* @param participant to be compared to the current instance
* @return true if equal, false if not
*/
boolean equals(Participant participant); boolean equals(Participant participant);
void change(Participant participant);
/**
* Method to change out participants
*
* @param participant to be exchanged
* @throws Person.InvalidPhoneNumberException if phone number does not follow the correct formatting
*/
void change(Participant participant) throws Person.InvalidPhoneNumberException;
} }

View File

@ -1,56 +1,113 @@
package ch.zhaw.projekt2.turnierverwaltung; package ch.zhaw.projekt2.turnierverwaltung;
import java.io.Serializable; import java.io.Serializable;
import java.util.logging.Logger;
/**
* Class that represents a person and holds its attributes
*/
public class Person implements Serializable { public class Person implements Serializable {
private final String NAME_MATCHING_REGEX = "[a-zA-Z]{1,20}"; 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 name;
private String firstName; private String firstName;
private String phoneNumber; private String phoneNumber;
private static final Logger logger = Logger.getLogger(FileIO.class.getCanonicalName());
/**
* Constructor to initialize a person, also in charge of checking if name and phone number follow a valid format
*
* @param firstName of the new person
* @param name of the new person
* @param phoneNumber of the new person
* @throws InvalidNameException thrown when the name or firstname does not follow the valid format
* @throws InvalidPhoneNumberException thrown when the number does not follow a valid format
*/
public Person(String firstName, String name, String phoneNumber) throws InvalidNameException, InvalidPhoneNumberException { public Person(String firstName, String name, String phoneNumber) throws InvalidNameException, InvalidPhoneNumberException {
if(!firstName.matches(NAME_MATCHING_REGEX)){ logger.finer("Trying to initialize a new person with name: " + name + ", first name: "
throw new InvalidNameException("The First name is Invalid."); + firstName + ", Phone number: " + phoneNumber);
} else if(!name.matches(NAME_MATCHING_REGEX)){
throw new InvalidNameException("The Last name is Invalid");
} else if(!phoneNumber.matches(PHONE_MATCHING_REGEX)){
throw new InvalidPhoneNumberException("The entered Phone Number is invalid.");
}
setFirstName(firstName); setFirstName(firstName);
setName(name); setName(name);
setPhoneNumber(phoneNumber); setPhoneNumber(phoneNumber);
logger.fine("Successfully created a new Person with Name: " + name + ", Firstname: "
+ firstName + ", Phone number: " + phoneNumber);
} }
/**
* Method to return the name of a Person
*
* @return the name as String
*/
public String getName() { public String getName() {
logger.fine("Returning name of:" + name);
return name; return name;
} }
public void setName(String name) { /**
* Method to set the name of a Person
*
* @param name of the person to be set
*/
public void setName(String name) throws InvalidNameException {
if (!name.matches(NAME_MATCHING_REGEX)) {
logger.warning("Name: " + name + ", is not in a valid format");
throw new InvalidNameException("The Last name is Invalid");
}
logger.fine("Setting new name of " + this + " with: " + name);
this.name = name; this.name = name;
} }
public String getFirstName() { public String getFirstName() {
logger.fine("Returning firstname of " + this + "that is: " + firstName);
return firstName; return firstName;
} }
public void setFirstName(String firstName) { /**
* Method to set the first name of a Person
*
* @param firstName the person to be set
*/
public void setFirstName(String firstName) throws InvalidNameException {
if (!firstName.matches(NAME_MATCHING_REGEX)) {
logger.warning("First name: " + firstName + ", is not in a valid format");
throw new InvalidNameException("The First name is Invalid.");
}
logger.fine("Setting new first name of " + this + " with: " + firstName);
this.firstName = firstName; this.firstName = firstName;
} }
/**
* Method used to get the Phone number of a Person
*
* @return phone number of the person
*/
public String getPhoneNumber() { public String getPhoneNumber() {
logger.fine("returning phone number of " + this + "that is: " + phoneNumber);
return phoneNumber; return phoneNumber;
} }
public void setPhoneNumber(String phoneNumber) { /**
* Method used to set the Phone number of a Person
*
* @param phoneNumber to be set
*/
public void setPhoneNumber(String phoneNumber) throws InvalidPhoneNumberException {
if (!phoneNumber.matches(PHONE_MATCHING_REGEX)) {
logger.warning("Phone number: " + phoneNumber + ", is not in a valid format");
throw new InvalidPhoneNumberException("The entered Phone Number is invalid.");
}
logger.fine("Setting new phone number of " + this + " with: " + phoneNumber);
this.phoneNumber = phoneNumber; this.phoneNumber = phoneNumber;
} }
public class InvalidPhoneNumberException extends Exception { /**
public InvalidPhoneNumberException() { * Exception, that is used when a Phone number is in an invalid format.
super(); */
} public static class InvalidPhoneNumberException extends Exception {
public InvalidPhoneNumberException(String errorMessage) { public InvalidPhoneNumberException(String errorMessage) {
super(errorMessage); super(errorMessage);
@ -58,6 +115,11 @@ public class Person implements Serializable {
} }
/**
* Overridden toString method to be Return the name in Format "Firstname Name".
*
* @return Returning the Name in format "Firstname Name" as String
*/
@Override @Override
public String toString() { public String toString() {
return firstName + " " + name; return firstName + " " + name;

View File

@ -1,36 +1,78 @@
package ch.zhaw.projekt2.turnierverwaltung; package ch.zhaw.projekt2.turnierverwaltung;
import java.io.Serializable; import java.io.Serializable;
import java.util.logging.Logger;
/**
* Class Representing a place that can be set for a game
*/
public class Place implements Serializable { public class Place implements Serializable {
private final String NAME_MATCHING_REGEX = "[a-zA-Z0-9]{1,20}"; private final String NAME_MATCHING_REGEX = "[a-zA-Z0-9]{1,20}";
private String name; private String name;
private static final Logger logger = Logger.getLogger(FileIO.class.getCanonicalName());
/**
* Constructor of a place initializes it and checks if name is in valid format
*
* @param name to be set
* @throws InvalidNameException If name is invalid
*/
public Place(String name) throws InvalidNameException { public Place(String name) throws InvalidNameException {
if (!name.matches(NAME_MATCHING_REGEX)) { if (!name.matches(NAME_MATCHING_REGEX)) {
throw new InvalidNameException("The Name is Invalid."); logger.warning("Name: " + name + " does not follow the valid format");
throw new InvalidNameException("The Name: " + name + " is in an invalid Format");
} }
logger.fine("Name: " + name + " is valid and will be set for the Place");
setName(name); setName(name);
} }
/**
* Method to get the Name of the Place as String
*
* @return name of place as String
*/
public String getName() { public String getName() {
logger.finer("returning the name: " + name);
return name; return name;
} }
public void setName(String name) { /**
* Method to set the name of a place
*
* @param name the name to be set
*/
private void setName(String name) {
logger.finer("Setting " + name + " as name of place");
this.name = name; this.name = name;
} }
/**
* Override of toString to return the name as String when needed
*
* @return the name of the place as String
*/
@Override @Override
public String toString() { public String toString() {
return name; return name;
} }
/**
* Override of equals tom compare places with each other
*
* @param place to be compared with this instance
* @return true if equals and false if not equal
*/
public boolean equals(Place place) { public boolean equals(Place place) {
logger.fine("Comparing " + place + " with " + this);
return name.equals(place.getName()); return name.equals(place.getName());
} }
/**
* Functionality to save more details than the name about a place is not implemented in this prototype,
* so no functionality is set yet to change the place.
* @param place to be changed
*/
public void change(Place place) { public void change(Place place) {
//TODO: If Place gets more developed in future releases
} }
} }

View File

@ -1,69 +1,127 @@
package ch.zhaw.projekt2.turnierverwaltung; package ch.zhaw.projekt2.turnierverwaltung;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.logging.Logger;
/**
* Class Representing a single Player
*/
public class Player extends Person implements Participant { public class Player extends Person implements Participant {
private LocalDate dateOfBirth; private LocalDate dateOfBirth;
private static final Logger logger = Logger.getLogger(FileIO.class.getCanonicalName());
/**
* Constructor of player initializes a new player setting sever attributes like firstname, name birthdate usw.
* and checking if the format is valid
*
* @param firstName the firstname of the player
* @param name the name of the player
* @param phoneNumber the phone number of the player
* @param dateOfBirth the birthdate of the player
* @throws InvalidNameException thrown when the name or firstname are not in a valid format
* @throws InvalidPhoneNumberException thrown when the phone number is not in a valid format
*/
public Player(String firstName, String name, String phoneNumber, String dateOfBirth) throws InvalidNameException, InvalidPhoneNumberException, InvalidDateException { public Player(String firstName, String name, String phoneNumber, String dateOfBirth) throws InvalidNameException, InvalidPhoneNumberException, InvalidDateException {
super(firstName, name, phoneNumber); super(firstName, name, phoneNumber);
logger.fine("initialized super of Player with firstname, name and phone number");
logger.finer("Setting the date of birth as:" + dateOfBirth);
setDateOfBirth(dateOfBirth); setDateOfBirth(dateOfBirth);
logger.finer("finished initializing the Player:" + name);
} }
/**
* Method to get the birthday of a player
*
* @return the birthday of a player
*/
public LocalDate getDateOfBirth() { public LocalDate getDateOfBirth() {
logger.fine("returning the birthday of " + getName());
return dateOfBirth; return dateOfBirth;
} }
public String getFormatedDateOfBirth(){ /**
* Method that returns the date of birth formatted in eu way dd.MM.YYYY and as String
*
* @return String of formatted date
*/
public String getFormattedDateOfBirth() {
logger.finer("Trying to get the formatted date");
try { try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
logger.fine("Returning the formatted birthdate of player: " + getName());
return dateOfBirth.format(formatter); return dateOfBirth.format(formatter);
} catch (Exception e) { } catch (Exception e) {
//todo handle and logging logger.warning("Failed to get formatted date for player " + getName() + "Error: " + e);
//TODO handle
return ""; return "";
} }
} }
/**
* Method to set the date of Birth with a String provided.
*
* @param dateOfBirth to be as a String
*/
public void setDateOfBirth(String dateOfBirth) throws InvalidDateException { public void setDateOfBirth(String dateOfBirth) throws InvalidDateException {
logger.finer("Trying to set of birth with the String " + dateOfBirth + " provided");
if (dateOfBirth.length() > 0) { if (dateOfBirth.length() > 0) {
String[] date = dateOfBirth.split("\\."); String[] date = dateOfBirth.split("\\.");
try { try {
this.dateOfBirth = LocalDate.of(Integer.valueOf(date[2]), Integer.valueOf(date[1]), Integer.valueOf(date[0])); this.dateOfBirth = LocalDate.of(Integer.parseInt(date[2]), Integer.parseInt(date[1]), Integer.parseInt(date[0]));
logger.fine("Date of birth of" + getName() + " has been set to " + dateOfBirth);
} catch (NumberFormatException | IndexOutOfBoundsException e) { } catch (NumberFormatException | IndexOutOfBoundsException e) {
e.printStackTrace(); e.printStackTrace();
logger.warning("Date of birth of " + getName() + " could not be set, leaving at null");
throw new InvalidDateException("Date invalid"); throw new InvalidDateException("Date invalid");
} }
} else { } else {
dateOfBirth = null; logger.warning("Date of birth of " + getName() + " could not be set, leaving at null");
} }
} }
/**
* Method that sets date of Birth with a LocalDate provided
*
* @param dateOfBirth parameter given as LocalDate
*/
public void setDateOfBirth(LocalDate dateOfBirth) { public void setDateOfBirth(LocalDate dateOfBirth) {
logger.fine("Setting date of birth of" + getName() + "with provided LocalDate: " + dateOfBirth);
this.dateOfBirth = dateOfBirth; this.dateOfBirth = dateOfBirth;
} }
/**
* Override of equals method to compare participants against each other
*
* @param participant to be compared with this instance
* @return true if equals, false if not
*/
@Override @Override
public boolean equals(Participant participant) { public boolean equals(Participant participant) {
return getClass().equals(participant.getClass()) && toString().toLowerCase().equals(participant.toString().toLowerCase()); logger.fine("Comparing " + participant + " with " + this);
return getClass().equals(participant.getClass()) && toString().equalsIgnoreCase(participant.toString());
} }
/**
* Override of the change method to change a Participant with a new one
*
* @param participant the new participant to be added
*/
@Override @Override
public void change(Participant participant) { public void change(Participant participant) throws InvalidPhoneNumberException {
logger.fine("changing the current Player " + this + "with " + participant.getName());
Player player = (Player) participant; Player player = (Player) participant;
setDateOfBirth(player.getDateOfBirth()); setDateOfBirth(player.getDateOfBirth());
setPhoneNumber(player.getPhoneNumber()); setPhoneNumber(player.getPhoneNumber());
} }
public class InvalidDateException extends Exception { public static class InvalidDateException extends Exception {
public InvalidDateException() {
super();
}
public InvalidDateException(String errorMessage) { public InvalidDateException(String errorMessage) {
super(errorMessage); super(errorMessage);
} }

View File

@ -2,57 +2,127 @@ package ch.zhaw.projekt2.turnierverwaltung;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger;
/**
* Class Team represents a team that can be added to a tournament
* (in the prototype there is no functionality for the team)
*/
public class Team implements Participant { public class Team implements Participant {
private String name; private String name;
private List<Player> players; private List<Player> players;
private Person contactPerson; private Person contactPerson;
private static final Logger logger = Logger.getLogger(FileIO.class.getCanonicalName());
/**
* Constructor to initiate a team, sets its name
*
* @param name the new name to be set
*/
public Team(String name) { public Team(String name) {
logger.fine("Setting the new name of the team as: " + name);
setName(name); setName(name);
players = new ArrayList<>(); players = new ArrayList<>();
} }
/**
* Method to add a new player to a team
*
* @param player new player to be added to a team
*/
public void addPlayer(Player player) { public void addPlayer(Player player) {
logger.fine("Adding new Player named: " + player + "into the team");
players.add(player); players.add(player);
} }
/**
* Method to return the name of the team
*
* @return the name of the team
*/
@Override @Override
public String getName() { public String getName() {
logger.fine("Returning the name of the team:" + name);
return name; return name;
} }
/**
* Method to set the name of a team
*
* @param name of the team
*/
@Override @Override
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
/**
* Method that sets a complete List of all players as players of the team
*
* @param players List of Players to be set
*/
public void setPlayers(List<Player> players) { public void setPlayers(List<Player> players) {
logger.fine("Inserts Player");
this.players = players; this.players = players;
} }
/**
* Method to get the List of all Players in a team
*
* @return List of all players in team
*/
public List<Player> getPlayers() { public List<Player> getPlayers() {
logger.fine("Returning List of all players in " + getName());
return players; return players;
} }
/**
* Override of equals Method to compare teams with each other
*
* @param participant to be compared with this instance
* @return true if teams are the same false if not.
*/
@Override @Override
public boolean equals(Participant participant) { public boolean equals(Participant participant) {
return getClass().equals(participant.getClass()) && toString().toLowerCase().equals(participant.toString().toLowerCase()); return getClass().equals(participant.getClass()) && toString().equalsIgnoreCase(participant.toString());
} }
/**
* Functionality to save a team is not implemented in this prototype, so no functionality is set yet to change the team
*
* @param participant date object containing new data
*/
@Override @Override
public void change(Participant participant) { public void change(Participant participant) {
return;
} }
/**
* Method to get the current contact person of a team
*
* @return the current contact person
*/
public Person getContactPerson() { public Person getContactPerson() {
logger.fine("Returning contact Person of team " + getName());
return contactPerson; return contactPerson;
} }
/**
* Method to set a Person as a contact person of a team
*
* @param contactPerson to be set
*/
public void setContactPerson(Person contactPerson) { public void setContactPerson(Person contactPerson) {
logger.fine("Setting " + contactPerson + " as a contact Person for team " + getName());
this.contactPerson = contactPerson; this.contactPerson = contactPerson;
} }
/**
* Override of toString method, that returns the current name
*
* @return name in form of a String
*/
@Override @Override
public String toString() { public String toString() {
return name; return name;

View File

@ -59,7 +59,7 @@ public class Tournament implements Serializable {
* *
* @param newParticipant the Participant to be saved or edited * @param newParticipant the Participant to be saved or edited
*/ */
public void saveParticipant(Participant newParticipant) { public void saveParticipant(Participant newParticipant) throws Person.InvalidPhoneNumberException {
logger.fine("Trying to add " + newParticipant + " into the tournament"); logger.fine("Trying to add " + newParticipant + " into the tournament");
for (Participant participant : participants) { for (Participant participant : participants) {
if (participant.equals(newParticipant)) { if (participant.equals(newParticipant)) {
@ -76,8 +76,8 @@ public class Tournament implements Serializable {
/** /**
* Method to remove an existing participant throws error if participant could not be found * Method to remove an existing participant throws error if participant could not be found
* *
* @param participant * @param participant to be removed
* @throws ParticipantNotExistsException * @throws ParticipantNotExistsException thrown when the participant could not be found
*/ */
public void removeParticipant(Participant participant) throws ParticipantNotExistsException { public void removeParticipant(Participant participant) throws ParticipantNotExistsException {
logger.finer("Trying to remove " + participant + " from participants List"); logger.finer("Trying to remove " + participant + " from participants List");
@ -120,8 +120,9 @@ public class Tournament implements Serializable {
} }
/** /**
* @param place * Method that removes a place from the List, also checks if place is in the list
* @throws PlaceNotExistsException * @param place to be removed
* @throws PlaceNotExistsException thrown when the place could not be found
*/ */
public void removePlace(Place place) throws PlaceNotExistsException { public void removePlace(Place place) throws PlaceNotExistsException {
if (!places.contains(place)) { if (!places.contains(place)) {
@ -156,8 +157,6 @@ public class Tournament implements Serializable {
logger.warning("Invalid number of participants only participants 2^n allowed"); logger.warning("Invalid number of participants only participants 2^n allowed");
throw new NumberOfParticipantInvalidException("Can not Create Game Schedule for KO Modus"); throw new NumberOfParticipantInvalidException("Can not Create Game Schedule for KO Modus");
} }
} else { } else {
logger.warning("In the prototype the only accessible game schedule is the ko modus"); logger.warning("In the prototype the only accessible game schedule is the ko modus");
} }
@ -230,7 +229,7 @@ public class Tournament implements Serializable {
/** /**
* Setter Method to set the type of the tournament * Setter Method to set the type of the tournament
* *
* @param type * @param type the type to be set
*/ */
public void setType(Type type) { public void setType(Type type) {
logger.fine("Setting the type of the tournament to: " + type); logger.fine("Setting the type of the tournament to: " + type);
@ -254,17 +253,26 @@ public class Tournament implements Serializable {
public enum Type { public enum Type {
KO("KO-System"); //GROUPS("Gruppenspiele"); //Type GROUPS is not implemented in this prototype. KO("KO-System"); //GROUPS("Gruppenspiele"); //Type GROUPS is not implemented in this prototype.
private String name; private final String name;
private Type(String name) { Type(String name) {
this.name = name; this.name = name;
} }
/**
* Overridden toString method to return the name
* @return the name of the type
*/
@Override @Override
public String toString() { public String toString() {
return name; return name;
} }
/**
* Method to return an Observable List containing all types
*
* @return the Observable list with all types
*/
public static ObservableList<Type> getObservableList() { public static ObservableList<Type> getObservableList() {
ObservableList<Type> items = FXCollections.observableArrayList(); ObservableList<Type> items = FXCollections.observableArrayList();
items.addAll(values()); items.addAll(values());
@ -275,11 +283,7 @@ public class Tournament implements Serializable {
/** /**
* Custom exception thrown when an invalid file is chosen * Custom exception thrown when an invalid file is chosen
*/ */
public class InvalidTypeException extends Exception { public static class InvalidTypeException extends Exception {
public InvalidTypeException() {
super();
}
public InvalidTypeException(String errorMessage) { public InvalidTypeException(String errorMessage) {
super(errorMessage); super(errorMessage);
} }
@ -287,13 +291,9 @@ public class Tournament implements Serializable {
} }
/** /**
* Custom Expeption thrown when a Participant does not exist * Custom Exception thrown when a Participant does not exist
*/ */
public class ParticipantNotExistsException extends Exception { public static class ParticipantNotExistsException extends Exception {
public ParticipantNotExistsException() {
super();
}
public ParticipantNotExistsException(String errorMessage) { public ParticipantNotExistsException(String errorMessage) {
super(errorMessage); super(errorMessage);
} }
@ -303,11 +303,7 @@ public class Tournament implements Serializable {
/** /**
* Custom Exception thrown when a Place does not exist * Custom Exception thrown when a Place does not exist
*/ */
public class PlaceNotExistsException extends Exception { public static class PlaceNotExistsException extends Exception {
public PlaceNotExistsException() {
super();
}
public PlaceNotExistsException(String errorMessage) { public PlaceNotExistsException(String errorMessage) {
super(errorMessage); super(errorMessage);
} }
@ -317,11 +313,7 @@ public class Tournament implements Serializable {
/** /**
* Custom Exception thrown when the number of participants is not valid * Custom Exception thrown when the number of participants is not valid
*/ */
public class NumberOfParticipantInvalidException extends Exception { public static class NumberOfParticipantInvalidException extends Exception {
public NumberOfParticipantInvalidException() {
super();
}
public NumberOfParticipantInvalidException(String msg) { public NumberOfParticipantInvalidException(String msg) {
super(msg); super(msg);
} }

View File

@ -74,7 +74,7 @@ public class ParticipantFormularController extends FXController {
participantNameTextField.setText(participant.getName()); participantNameTextField.setText(participant.getName());
firstNameTextField.setText(participant.getFirstName()); firstNameTextField.setText(participant.getFirstName());
phoneNumberTextField.setText(participant.getPhoneNumber()); phoneNumberTextField.setText(participant.getPhoneNumber());
birthDateTextField.setText(participant.getFormatedDateOfBirth()); birthDateTextField.setText(participant.getFormattedDateOfBirth());
} }
@FXML @FXML