From 0d482bfcac7170e3cff52266599d903a7f7eb124 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 1 May 2022 21:26:32 +0200 Subject: [PATCH] implemented Functionality to save, edit and delete participants. --- .../projekt2/turnierverwaltung/Player.java | 21 ++++++++--- .../turnierverwaltung/Tournament.java | 3 +- .../TournamentDecorator.java | 11 +++++- .../ParticipantFormularController.java | 30 +++++++++++++--- .../participantFormular.fxml | 36 ++++++++----------- 5 files changed, 69 insertions(+), 32 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 e1573a9..3165364 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Player.java @@ -1,23 +1,36 @@ package ch.zhaw.projekt2.turnierverwaltung; -import java.util.GregorianCalendar; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; public class Player extends Person implements Participant{ - private GregorianCalendar dateOfBirth; + private LocalDate dateOfBirth; public Player(String firstName, String name, String phoneNumber, String dateOfBirth) throws InvalidNameException, InvalidPhoneNumberException { super(firstName, name, phoneNumber); setDateOfBirth(dateOfBirth); } - public GregorianCalendar getDateOfBirth() { + public LocalDate getDateOfBirth() { return dateOfBirth; } + public String getFormatedDateOfBirth(){ + try{ + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + return dateOfBirth.format(formatter); + } catch (Exception e) { + //todo handle and logging + return ""; + } + } + 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])); + this.dateOfBirth = LocalDate.of(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 d684b1f..744ba05 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -31,7 +31,7 @@ public class Tournament implements Serializable { public void addParticipant(Participant newParticipant) throws ParticipantExistsException { for(Participant participant : participants){ if(participant.equals(newParticipant)){ - throw new ParticipantExistsException("A Participant with the same Name exists already."); + participants.remove(participant); } } participants.add(newParticipant); @@ -41,6 +41,7 @@ public class Tournament implements Serializable { if(!participants.contains(participant)){ throw new ParticipantNotExistsException("The given Participant is not part of this Tournament"); } + participants.remove(participant); } public ObservableList getParticipants() { 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 47708ca..d284219 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -79,7 +79,7 @@ public class TournamentDecorator implements IsObservable{ } } - public void addPlayer(String firstName, String name, String phoneNumber, String dateOfBirth){ + public void savePlayer(String firstName, String name, String phoneNumber, String dateOfBirth){ try { tournament.addParticipant(new Player(firstName, name, phoneNumber, dateOfBirth)); informListener(); @@ -92,6 +92,15 @@ public class TournamentDecorator implements IsObservable{ } } + public void deleteParticipant(Participant participant){ + try { + tournament.removeParticipant(participant); + informListener(); + } catch (Tournament.ParticipantNotExistsException e) { + e.printStackTrace(); //TODO handle and logging + } + } + public void informListener() { for(IsObserver observer : listener) { observer.update(); 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 a6e2c4f..521334f 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 @@ -2,6 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main.participantAddFormular; import ch.zhaw.projekt2.turnierverwaltung.FXController; import ch.zhaw.projekt2.turnierverwaltung.Participant; +import ch.zhaw.projekt2.turnierverwaltung.Player; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; @@ -39,7 +40,11 @@ public class ParticipantFormularController extends FXController { private Label newParticipantFormularTitle; @FXML - private Button openBtn; + private Button closeBtn; + + @FXML + private Button deleteBtn; + @FXML private Label participantListTitle; @@ -63,8 +68,12 @@ public class ParticipantFormularController extends FXController { private Button saveBtn; @FXML - void addParticipant(ActionEvent event) { - getTournamentDecorator().addPlayer(firstNameTextField.getText(), participantNameTextField.getText(), phoneNumberTextField.getText(), birthDateTextField.getText()); + void changedSelection(MouseEvent event){ + Player participant = (Player) participantListView.getSelectionModel().getSelectedItems().get(0); + participantNameTextField.setText(participant.getName()); + firstNameTextField.setText(participant.getFirstName()); + phoneNumberTextField.setText(participant.getPhoneNumber()); + birthDateTextField.setText(participant.getFormatedDateOfBirth()); } @FXML @@ -73,7 +82,20 @@ public class ParticipantFormularController extends FXController { } @FXML - void save(ActionEvent event) { + void saveParticipant(ActionEvent event) { + getTournamentDecorator().savePlayer(firstNameTextField.getText(), participantNameTextField.getText(), phoneNumberTextField.getText(), birthDateTextField.getText()); + } + + + @FXML + void delete(ActionEvent event) { + System.out.println("delete"); + Participant participant = participantListView.getSelectionModel().getSelectedItems().get(0); + getTournamentDecorator().deleteParticipant(participant); + } + + @FXML + void close(ActionEvent event) { } diff --git a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/participantAddFormular/participantFormular.fxml b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/participantAddFormular/participantFormular.fxml index caa919e..c925573 100644 --- a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/participantAddFormular/participantFormular.fxml +++ b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/participantAddFormular/participantFormular.fxml @@ -1,23 +1,11 @@ - - - - - - - - - + + + + - - - - - - - - + @@ -29,19 +17,23 @@ - + - + -