From 4cb743a21aef931f31c35b4c5821f85ed7e41dc0 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 1 May 2022 17:00:09 +0200 Subject: [PATCH] implemented functionality to save participants also in file. --- .../projekt2/turnierverwaltung/Factory.java | 1 + .../projekt2/turnierverwaltung/Person.java | 14 ++++++++++--- .../projekt2/turnierverwaltung/Player.java | 3 +-- .../zhaw/projekt2/turnierverwaltung/Team.java | 7 ++++++- .../TournamentDecorator.java | 20 ++++++++++++++++--- .../turnierverwaltung/main/MainWindow.java | 2 +- 6 files changed, 37 insertions(+), 10 deletions(-) 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 e59d7d5..7b329f4 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java @@ -36,6 +36,7 @@ public class Factory { } public void loadTournamentList(BorderPane pane, FactoryDecorator factoryDecorator){ + tournamentDecorator.setTournament(null); TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml"), factoryDecorator); } 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 ec70ee9..9cf624f 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Person.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Person.java @@ -3,16 +3,19 @@ package ch.zhaw.projekt2.turnierverwaltung; import java.io.Serializable; public class Person implements Serializable { + private final String NAME_MATCHING_REGEX = "[a-zA-Z]{1,20}"; + private final String PHONE_MATCHING_REGEX = "[\\+0-9]+"; + private String name; private String firstName; private 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}$")){ + if(!firstName.matches(NAME_MATCHING_REGEX)){ throw new InvalidNameException("The First name is Invalid."); - } else if(!name.matches("^[a-zA-Z][a-zA-Z][-',.][a-zA-Z]{1,20}$")){ + } else if(!name.matches(NAME_MATCHING_REGEX)){ throw new InvalidNameException("The Last name is Invalid"); - } else if(!phoneNumber.matches("[\\+0-9]+")){ + } else if(!phoneNumber.matches(PHONE_MATCHING_REGEX)){ throw new InvalidPhoneNumberException("The entered Phone Number is invalid."); } setFirstName(firstName); @@ -55,4 +58,9 @@ public class Person implements Serializable { } + @Override + public String toString(){ + return firstName + " " + name; + } + } 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 6f78215..e1573a9 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java @@ -22,7 +22,6 @@ public class Player extends Person implements Participant{ @Override public boolean equals(Participant participant) { - return getClass().equals(participant.getClass()) && getName().equals(participant.getName()) && - getFirstName().equals(((Player) participant).getFirstName()); + return getClass().equals(participant.getClass()) && toString().toLowerCase().equals(participant.toString().toLowerCase()); } } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Team.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Team.java index 23ca660..e815a16 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Team.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Team.java @@ -29,7 +29,7 @@ public class Team implements Participant { @Override public boolean equals(Participant participant) { - return getClass().equals(participant.getClass()) && name.equals(participant.getName()); + return getClass().equals(participant.getClass()) && toString().toLowerCase().equals(participant.toString().toLowerCase()); } public Person getContactPerson() { @@ -39,4 +39,9 @@ public class Team implements Participant { public void setContactPerson(Person contactPerson) { this.contactPerson = contactPerson; } + + @Override + public String toString(){ + return name; + } } 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 b9a1f24..47708ca 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -10,9 +10,16 @@ public class TournamentDecorator implements IsObservable{ private FileIO fileIO; private List listener = new ArrayList<>(); - public TournamentDecorator(FileIO fileIO, Tournament tournament){ - setTournament(tournament); + public TournamentDecorator(FileIO fileIO){ setFileIO(fileIO); + addListener(new IsObserver() { + @Override + public void update() { + if(tournament != null){ + saveTournament(); + } + } + }); } public void setFileIO(FileIO fileIO) { @@ -37,6 +44,14 @@ public class TournamentDecorator implements IsObservable{ listener.remove(observer); } + public void saveTournament(){ + try { + fileIO.saveTournament(tournament); + } catch (IOException e) { + e.printStackTrace(); //TODO handle and logging + } + } + public void createTournament(String name, Tournament.Type type){ if(fileIO.tournamentExists(name)){ System.out.println("Tournament with same name exists already."); @@ -77,7 +92,6 @@ public class TournamentDecorator implements IsObservable{ } } - public void informListener() { for(IsObserver observer : listener) { observer.update(); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindow.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindow.java index 14348bd..290b1e6 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindow.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindow.java @@ -16,7 +16,7 @@ import java.io.IOException; public class MainWindow extends Application { private FileIO fileIO = new FileIO(System.getProperty("user.dir") + "/tournierverwaltung_angrynerds"); - private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO, null); + private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO); private Factory factory = new Factory(fileIO, tournamentDecorator); //TODO make it private! private FactoryDecorator factoryDecorator;