Added Java Doc and Logger to Person

This commit is contained in:
Leonardo Brandenberger 2022-05-13 16:12:55 +02:00
parent 7267fe590b
commit 726410f677
1 changed files with 64 additions and 4 deletions

View File

@ -1,7 +1,11 @@
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]*";
@ -10,43 +14,94 @@ public class Person implements Serializable {
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 {
logger.finer("Trying to initialize a new person with name: " + name + ", first name: "
+ firstName + ", Phone number: " + phoneNumber);
if (!firstName.matches(NAME_MATCHING_REGEX)) { 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."); throw new InvalidNameException("The First name is Invalid.");
} else if (!name.matches(NAME_MATCHING_REGEX)) { } else if (!name.matches(NAME_MATCHING_REGEX)) {
logger.warning("Name: " + name + ", is not in a valid format");
throw new InvalidNameException("The Last name is Invalid"); throw new InvalidNameException("The Last name is Invalid");
} else if (!phoneNumber.matches(PHONE_MATCHING_REGEX)) { } else 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."); 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;
} }
/**
* Method to set the name of a Person
*
* @param name of the person to be set
*/
public void setName(String name) { public void setName(String name) {
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;
} }
/**
* Method to set the first name of a Person
*
* @param firstName the person to be set
*/
public void setFirstName(String firstName) { public void setFirstName(String firstName) {
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;
} }
/**
* Method used to set the Phone number of a Person
* @param phoneNumber to be set
*/
public void setPhoneNumber(String phoneNumber) { public void setPhoneNumber(String phoneNumber) {
logger.fine("Setting new phone number of " + this + " with: " + phoneNumber);
this.phoneNumber = phoneNumber; this.phoneNumber = phoneNumber;
} }
/**
* Exception, that is used when a Phone number is in an invalid format.
*/
public class InvalidPhoneNumberException extends Exception { public class InvalidPhoneNumberException extends Exception {
public InvalidPhoneNumberException() { public InvalidPhoneNumberException() {
super(); super();
@ -58,6 +113,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;