From ff6f2bd94edc662015b0b09e820b0e696fedf2fa Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 1 May 2022 15:18:30 +0200 Subject: [PATCH] created Methods to show and add Players. --- .../projekt2/turnierverwaltung/Factory.java | 2 +- .../zhaw/projekt2/turnierverwaltung/Game.java | 4 ++- .../InvalidNameException.java | 12 +++++++++ .../turnierverwaltung/Participant.java | 4 ++- .../projekt2/turnierverwaltung/Person.java | 25 +++++++++++++++++-- .../projekt2/turnierverwaltung/Player.java | 13 +++++----- .../turnierverwaltung/Tournament.java | 20 ++++++--------- .../TournamentDecorator.java | 20 ++++++++++++++- .../ParticipantFormularController.java | 7 +++--- .../turnierverwaltung/FileIOTest.java | 2 +- 10 files changed, 81 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/InvalidNameException.java diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java index c05347b..e59d7d5 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java @@ -21,7 +21,7 @@ public class Factory { } public void setTournament(Tournament tournament) { - this.tournamentDecorator = tournamentDecorator; + this.tournamentDecorator.setTournament(tournament); } public BorderPane loadMainWindow(){ diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Game.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Game.java index bab3390..3df9735 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Game.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Game.java @@ -1,6 +1,8 @@ package ch.zhaw.projekt2.turnierverwaltung; -public class Game { +import java.io.Serializable; + +public class Game implements Serializable { private Participant participant1, Participant2; private int points1, points2; private Location location; diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/InvalidNameException.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/InvalidNameException.java new file mode 100644 index 0000000..d3be2ec --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/InvalidNameException.java @@ -0,0 +1,12 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +public class InvalidNameException extends Exception { + public InvalidNameException() { + super(); + } + + public InvalidNameException(String errorMessage) { + super(errorMessage); + } + +} \ No newline at end of file diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Participant.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Participant.java index 31c2a39..6f85290 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Participant.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Participant.java @@ -1,6 +1,8 @@ package ch.zhaw.projekt2.turnierverwaltung; -public interface Participant { +import java.io.Serializable; + +public interface Participant extends Serializable { String getName(); void setName(String name); boolean equals(Participant participant); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Person.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Person.java index 1165e7d..ec70ee9 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Person.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Person.java @@ -1,11 +1,20 @@ package ch.zhaw.projekt2.turnierverwaltung; -public class Person { +import java.io.Serializable; + +public class Person implements Serializable { private String name; private String firstName; private String phoneNumber; - public Person(String firstName, String name, String phoneNumber){ + public Person(String firstName, String name, String phoneNumber) throws InvalidNameException, InvalidPhoneNumberException { + if(!firstName.matches("^[a-zA-Z][a-zA-Z][-',.][a-zA-Z]{1,20}$")){ + throw new InvalidNameException("The First name is Invalid."); + } else if(!name.matches("^[a-zA-Z][a-zA-Z][-',.][a-zA-Z]{1,20}$")){ + throw new InvalidNameException("The Last name is Invalid"); + } else if(!phoneNumber.matches("[\\+0-9]+")){ + throw new InvalidPhoneNumberException("The entered Phone Number is invalid."); + } setFirstName(firstName); setName(name); setPhoneNumber(phoneNumber); @@ -34,4 +43,16 @@ public class Person { public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } + + public class InvalidPhoneNumberException extends Exception { + public InvalidPhoneNumberException() { + super(); + } + + public InvalidPhoneNumberException(String errorMessage) { + super(errorMessage); + } + + } + } 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 f922512..6f78215 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java @@ -1,22 +1,23 @@ package ch.zhaw.projekt2.turnierverwaltung; -import java.util.Date; +import java.util.GregorianCalendar; public class Player extends Person implements Participant{ - private Date dateOfBirth; + private GregorianCalendar dateOfBirth; - public Player(String firstName, String name, String phoneNumber, Date dateOfBirth){ + public Player(String firstName, String name, String phoneNumber, String dateOfBirth) throws InvalidNameException, InvalidPhoneNumberException { super(firstName, name, phoneNumber); setDateOfBirth(dateOfBirth); } - public Date getDateOfBirth() { + public GregorianCalendar getDateOfBirth() { return dateOfBirth; } - public void setDateOfBirth(Date dateOfBirth) { - this.dateOfBirth = dateOfBirth; + public void setDateOfBirth(String dateOfBirth) { + String[] date = dateOfBirth.split("\\."); + this.dateOfBirth = new GregorianCalendar(Integer.valueOf(date[2]), Integer.valueOf(date[1]), Integer.valueOf(date[0])); } @Override 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 878985f..d684b1f 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -3,6 +3,7 @@ package ch.zhaw.projekt2.turnierverwaltung; import javafx.collections.FXCollections; import javafx.collections.ObservableList; +import java.io.File; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; @@ -17,7 +18,7 @@ public class Tournament implements Serializable { public Tournament(String name, Type type) throws InvalidNameException, InvalidTypeException { if(!name.matches("[\\w]{1,20}")){ - throw new Tournament.InvalidNameException("Invalid Name entered"); //TODO handle en logging. + throw new InvalidNameException("Invalid Name entered"); //TODO handle en logging. } else if(!Arrays.asList(Type.values()).contains(type)){ throw new InvalidTypeException("Invalid Type selected"); //TODO handle en logging. } @@ -42,6 +43,12 @@ public class Tournament implements Serializable { } } + public ObservableList getParticipants() { + ObservableList participantsObservable = FXCollections.observableArrayList(); + participantsObservable.addAll(participants); + return participantsObservable; + } + public String getName() { return name; } @@ -79,17 +86,6 @@ public class Tournament implements Serializable { } } - public class InvalidNameException extends Exception { - public InvalidNameException() { - super(); - } - - public InvalidNameException(String errorMessage) { - super(errorMessage); - } - - } - public class InvalidTypeException extends Exception { public InvalidTypeException() { 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 a848a4a..b9a1f24 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -2,6 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung; import java.io.IOException; import java.util.ArrayList; +import java.util.Date; import java.util.List; public class TournamentDecorator implements IsObservable{ @@ -22,6 +23,10 @@ public class TournamentDecorator implements IsObservable{ this.tournament = tournament; } + public Tournament getTournament() { + return tournament; + } + @Override public void addListener(IsObserver observer) { listener.add(observer); @@ -41,7 +46,7 @@ public class TournamentDecorator implements IsObservable{ Tournament tournament = new Tournament(name, type); fileIO.saveTournament(tournament); informListener(); - } catch (Tournament.InvalidNameException e) { + } catch (InvalidNameException e) { e.printStackTrace(); //TODO handle and logging } catch (Tournament.InvalidTypeException e) { e.printStackTrace(); //TODO handle and logging @@ -59,6 +64,19 @@ public class TournamentDecorator implements IsObservable{ } } + public void addPlayer(String firstName, String name, String phoneNumber, String dateOfBirth){ + try { + tournament.addParticipant(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) { + e.printStackTrace(); //TODO handle and logging + } + } + public void informListener() { for(IsObserver observer : listener) { diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/participantAddFormular/ParticipantFormularController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/participantAddFormular/ParticipantFormularController.java index 08877c1..a6e2c4f 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/participantAddFormular/ParticipantFormularController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/participantAddFormular/ParticipantFormularController.java @@ -1,6 +1,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main.participantAddFormular; import ch.zhaw.projekt2.turnierverwaltung.FXController; +import ch.zhaw.projekt2.turnierverwaltung.Participant; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; @@ -44,7 +45,7 @@ public class ParticipantFormularController extends FXController { private Label participantListTitle; @FXML - private ListView participantListView; + private ListView participantListView; @FXML private Label participantNameLabel; @@ -63,7 +64,7 @@ public class ParticipantFormularController extends FXController { @FXML void addParticipant(ActionEvent event) { - + getTournamentDecorator().addPlayer(firstNameTextField.getText(), participantNameTextField.getText(), phoneNumberTextField.getText(), birthDateTextField.getText()); } @FXML @@ -78,6 +79,6 @@ public class ParticipantFormularController extends FXController { @Override public void loadContent() { - + participantListView.setItems(getTournamentDecorator().getTournament().getParticipants()); } } diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java index c8b8060..09eeef6 100644 --- a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java @@ -108,7 +108,7 @@ class FileIOTest { } @Test - void saveTournament() throws IOException, Tournament.InvalidNameException, Tournament.InvalidTypeException { + void saveTournament() throws IOException, InvalidNameException, Tournament.InvalidTypeException { Tournament tournament = null; tournament = new Tournament("test1", Tournament.Type.KO); io.saveTournament(tournament);