From 3ebf9c5072d243117fba73c7ba7348c041df663e Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 12 May 2022 22:41:18 +0200 Subject: [PATCH] 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); } }