implemented Functionality to save, edit and delete participants.

This commit is contained in:
schrom01 2022-05-01 21:26:32 +02:00
parent 90fa3a9183
commit 0d482bfcac
5 changed files with 69 additions and 32 deletions

View File

@ -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

View File

@ -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<Participant> getParticipants() {

View File

@ -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();

View File

@ -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) {
}

View File

@ -1,23 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<HBox alignment="CENTER" VBox.vgrow="ALWAYS" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.participantAddFormular.ParticipantFormularController">
<HBox alignment="CENTER" VBox.vgrow="ALWAYS" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.participantAddFormular.ParticipantFormularController">
<children>
<VBox alignment="TOP_CENTER" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS">
<children>
@ -29,19 +17,23 @@
<Insets bottom="20.0" />
</VBox.margin>
</Label>
<ListView fx:id="participantListView" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<ListView fx:id="participantListView" onMouseClicked="#changedSelection" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets />
</VBox.margin>
</ListView>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<children>
<Button fx:id="saveBtn" mnemonicParsing="false" onAction="#save" text="Save">
<Button fx:id="closeBtn" mnemonicParsing="false" onAction="#close" text="Schliessen">
<HBox.margin>
<Insets right="40.0" />
</HBox.margin>
</Button>
<Button fx:id="deleteBtn" mnemonicParsing="false" onAction="#delete" text="Löschen">
<HBox.margin>
<Insets right="40.0" />
</HBox.margin>
</Button>
<Button fx:id="openBtn" mnemonicParsing="false" text="Bearbeiten" />
</children>
</HBox>
</children>
@ -56,7 +48,7 @@
</Separator>
<VBox fx:id="changeBtn" alignment="TOP_CENTER" onDragDetected="#changeParticipant" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS">
<children>
<Label fx:id="newParticipantFormularTitle" text="Neuer Teilnehmer">
<Label fx:id="newParticipantFormularTitle" text="Teilnehmer ändern/erstellen">
<font>
<Font name="System Bold" size="21.0" />
</font>
@ -100,7 +92,7 @@
</children>
</GridPane>
<Separator prefWidth="200.0" />
<Button fx:id="addBtn" alignment="TOP_LEFT" mnemonicParsing="false" onAction="#addParticipant" text="Erstellen" VBox.vgrow="ALWAYS">
<Button fx:id="saveBtn" alignment="TOP_LEFT" mnemonicParsing="false" onAction="#saveParticipant" text="Speichern" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets bottom="10.0" top="30.0" />
</VBox.margin>