implemented functionality to add new Tournament

This commit is contained in:
schrom01 2022-05-01 10:27:21 +02:00
parent fd2e775c94
commit 171cf7ef4e
6 changed files with 105 additions and 36 deletions

View File

@ -1,6 +1,9 @@
package ch.zhaw.projekt2.turnierverwaltung; package ch.zhaw.projekt2.turnierverwaltung;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.io.*; import java.io.*;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
@ -34,13 +37,13 @@ public class FileIO {
} }
} }
public List<TournamentFile> getList() { public ObservableList<TournamentFile> getList() {
logger.fine("Creating a List out of all Files in the save directory and returning it"); logger.fine("Creating a List out of all Files in the save directory and returning it");
List<TournamentFile> tournaments = new ArrayList<>(); ObservableList<TournamentFile> tournamentFiles = FXCollections.observableArrayList();
for(File tournament : saves.listFiles()){ for(File tournament : saves.listFiles()){
tournaments.add(new TournamentFile(tournament.toURI())); tournamentFiles.add(new TournamentFile(tournament.toURI()));
} }
return tournaments; return tournamentFiles;
} }
/** /**
@ -86,7 +89,7 @@ public class FileIO {
return tournament; return tournament;
} }
public void saveTournament(Tournament tournament) { public void saveTournament(Tournament tournament) throws IOException {
if (tournament == null) { if (tournament == null) {
logger.warning("Given tournament file is empty"); logger.warning("Given tournament file is empty");
throw new IllegalArgumentException("Null tournament received"); throw new IllegalArgumentException("Null tournament received");

View File

@ -1,12 +1,22 @@
package ch.zhaw.projekt2.turnierverwaltung; package ch.zhaw.projekt2.turnierverwaltung;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.io.Serializable; import java.io.Serializable;
public class Tournament implements Serializable { public class Tournament implements Serializable {
private String name; private String name;
private Type type;
public Tournament(String name){
public Tournament(String name, Type type) throws InvalidNameException {
if(!name.matches("[\\w]{1,20}")){
throw new Tournament.InvalidNameException("Invalid Name entered"); //TODO handle en logging.
}
setName(name); setName(name);
setType(type);
} }
public String getName() { public String getName() {
@ -16,4 +26,46 @@ public class Tournament implements Serializable {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public enum Type {
KO("KO-System"), GROUPS("Gruppenspiele");
private String name;
private Type(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
public static ObservableList<Type> getObservableList(){
ObservableList<Type> items = FXCollections.observableArrayList();
items.addAll(values());
return items;
}
}
public class InvalidNameException extends Exception {
public InvalidNameException() {
super();
}
public InvalidNameException(String errorMessage) {
super(errorMessage);
}
}
} }

View File

@ -1,14 +1,13 @@
package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList; package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList;
import ch.zhaw.projekt2.turnierverwaltung.FXController; import ch.zhaw.projekt2.turnierverwaltung.FXController;
import ch.zhaw.projekt2.turnierverwaltung.FileIO;
import ch.zhaw.projekt2.turnierverwaltung.Tournament;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.*;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
@ -17,6 +16,11 @@ import java.io.IOException;
public class TournamentListController extends FXController { public class TournamentListController extends FXController {
@FXML
public void initialize(){
modusChoiceBox.setItems(Tournament.Type.getObservableList());
}
@FXML @FXML
private Button createBtn; private Button createBtn;
@ -24,7 +28,7 @@ public class TournamentListController extends FXController {
private GridPane grid; private GridPane grid;
@FXML @FXML
private ChoiceBox<?> modusChoiceBox; private ChoiceBox<Tournament.Type> modusChoiceBox;
@FXML @FXML
private Label newTournamentFormularTitle; private Label newTournamentFormularTitle;
@ -36,7 +40,7 @@ public class TournamentListController extends FXController {
private Label tournamentListTitle; private Label tournamentListTitle;
@FXML @FXML
private ListView<File> tournamentListView; private ListView<FileIO.TournamentFile> tournamentListView;
@FXML @FXML
private Label tournamentModLabel; private Label tournamentModLabel;
@ -44,8 +48,26 @@ public class TournamentListController extends FXController {
@FXML @FXML
private Label tournamentNameLabel; private Label tournamentNameLabel;
@FXML
private TextField tournamentNameField;
@FXML @FXML
void createTournament(ActionEvent event) { void createTournament(ActionEvent event) {
for(FileIO.TournamentFile file : getFileIO().getList()) {
if(file.toString().equals(tournamentNameField.getText())){
return; //TODO handle and logging
// Tournament with same name exists already.
}
}
try {
Tournament tournament = new Tournament(tournamentNameField.getText(), modusChoiceBox.getValue());
getFileIO().saveTournament(tournament);
loadContent();
} catch (Tournament.InvalidNameException e) {
e.printStackTrace(); //TODO handle and logging
} catch (IOException e) {
e.printStackTrace(); //TODO handle and logging
}
} }
@ -54,21 +76,17 @@ public class TournamentListController extends FXController {
try { try {
File tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0); File tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0);
getFactory().setTournament(getFileIO().loadTournament(tournamentFile)); getFactory().setTournament(getFileIO().loadTournament(tournamentFile));
getFactory().loadParticipantFormular((BorderPane) getPane()); getFactory().loadParticipantFormular((BorderPane) getPane()); //TODO load TournamentView instead of ParticipantFormular?
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} } //TODO handle and logging
} }
@Override @Override
public void loadContent() { public void loadContent() {
ObservableList<File> tournamentFiles = FXCollections.observableArrayList(); tournamentListView.setItems(getFileIO().getList());
for(File tournament : getFileIO().getList()){
tournamentFiles.add(tournament);
}
tournamentListView.setItems(tournamentFiles);
} }
} }

View File

@ -1,20 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.*?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.*?>
<?import javafx.scene.control.ChoiceBox?> <?import javafx.scene.layout.*?>
<?import javafx.scene.control.Label?> <?import javafx.scene.text.*?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?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="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController"> <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.tournamentList.TournamentListController">
<children> <children>
<VBox alignment="TOP_CENTER" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS"> <VBox alignment="TOP_CENTER" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS">
<children> <children>
@ -66,12 +57,12 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints> </rowConstraints>
<children> <children>
<Label fx:id="turnierNameLabel" styleClass="lableGrid" text="Turnier Name:"> <Label fx:id="tournamentNameLabel" styleClass="lableGrid" text="Turnier Name:">
<GridPane.margin> <GridPane.margin>
<Insets /> <Insets />
</GridPane.margin> </GridPane.margin>
</Label> </Label>
<TextField styleClass="inputGrid" GridPane.columnIndex="1"> <TextField fx:id="tournamentNameField" styleClass="inputGrid" GridPane.columnIndex="1">
<GridPane.margin> <GridPane.margin>
<Insets /> <Insets />
</GridPane.margin> </GridPane.margin>

View File

@ -109,7 +109,12 @@ class FileIOTest {
@Test @Test
void saveTournament() throws IOException { void saveTournament() throws IOException {
Tournament tournament = new Tournament("test1"); Tournament tournament = null;
try {
tournament = new Tournament("test1", Tournament.Type.KO);
} catch (Tournament.InvalidNameException e) {
e.printStackTrace();
}
io.saveTournament(tournament); io.saveTournament(tournament);
File file = new File(mainDir + "/saves/test1.txt"); File file = new File(mainDir + "/saves/test1.txt");
if(file.exists()){ if(file.exists()){