Compare commits

...
7 Commits
6 changed files with 123 additions and 35 deletions
@@ -11,14 +11,20 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
/**
* Class in Charge of Reading and Writing files
*/
public class FileIO { public class FileIO {
private File mainDir; private File mainDir;
private File saves; private File saves;
private static final Logger logger = Logger.getLogger(FileIO.class.getName()); private static final Logger logger = Logger.getLogger(FileIO.class.getName());
/**
* Constructor initiates the Directory and creates a save folder if not already existing.
*
* @param saveLocation the directory in which the save Files will be saved
*/
public FileIO(String saveLocation) { public FileIO(String saveLocation) {
this.mainDir = new File(saveLocation); this.mainDir = new File(saveLocation);
if (!mainDir.exists()) { if (!mainDir.exists()) {
@@ -37,6 +43,11 @@ public class FileIO {
} }
} }
/**
* Returns a list with all save Files that are located inside the save folder.
*
* @return List with all containing save Files
*/
public ObservableList<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");
ObservableList<TournamentFile> tournamentFiles = FXCollections.observableArrayList(); ObservableList<TournamentFile> tournamentFiles = FXCollections.observableArrayList();
@@ -56,12 +67,15 @@ public class FileIO {
} }
/** /**
* @param tournamentFile * Loads and returns a tournament from a given File which contains the serialiazed tournament.
* @return *
* @throws ClassNotFoundException * @param tournamentFile The tournament file where the data should be read from.
* @throws IOException File not found or not readable. * @return Tournament that is returned when succefully being read from the file
* @throws ClassNotFoundException No definition for the class with the specified name could be found
* @throws IOException File not readable
* @throws FileNotFoundException File not found
*/ */
public Tournament loadTournament(File tournamentFile) throws IOException, ClassNotFoundException { public Tournament loadTournament(File tournamentFile) throws IOException, ClassNotFoundException, FileNotFoundException {
if (tournamentFile == null) { if (tournamentFile == null) {
logger.warning("Given tournament file is empty"); logger.warning("Given tournament file is empty");
throw new IllegalArgumentException("Tournament File is null"); throw new IllegalArgumentException("Tournament File is null");
@@ -80,10 +94,10 @@ public class FileIO {
throw e; throw e;
} catch (IOException e) { } catch (IOException e) {
logger.severe("Failed to read File" + tournamentFile.getName()); logger.severe("Failed to read File" + tournamentFile.getName());
throw new IOException("Error while reading File",e); throw new IOException("Error while reading File", e);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
logger.severe("No definition for the class with the specified name could be found"); logger.severe("No definition for the class with the specified name could be found");
throw new ClassNotFoundException("No definition for the class with the specified name could be found",e); throw new ClassNotFoundException("No definition for the class with the specified name could be found", e);
} finally { } finally {
if (in != null) { if (in != null) {
try { try {
@@ -91,37 +105,49 @@ public class FileIO {
in.close(); in.close();
} catch (IOException e) { } catch (IOException e) {
logger.severe("Failed to close input stream"); logger.severe("Failed to close input stream");
throw new IOException("Error while closing input stream",e); throw new IOException("Error while closing input stream", e);
} }
} }
} }
return tournament; return tournament;
} }
public void saveTournament(Tournament tournament) throws IOException { /**
* Serializables and saves the receiving tournament file to a txt file.
*
* @param tournament the receiving tournament.
* @throws IOException File not readable
* @throws FileNotFoundException File not found
*/
public void saveTournament(Tournament tournament) throws IOException, FileNotFoundException {
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");
} }
logger.fine("Creates the Object for the path of the save file");
File newSave = new File(saves, tournament.getName() + ".txt"); File newSave = new File(saves, tournament.getName() + ".txt");
ObjectOutputStream out = null; ObjectOutputStream out = null;
try { try {
boolean newFile = newSave.createNewFile(); logger.fine("creates or overwrites the new save FILE");
newSave.createNewFile();
out = new ObjectOutputStream(new FileOutputStream(newSave)); out = new ObjectOutputStream(new FileOutputStream(newSave));
out.writeObject(tournament); out.writeObject(tournament);
System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath()); System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath());
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new RuntimeException(e); logger.severe("Could not find tournament File");
throw e;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); logger.severe("Failed to write File" + tournament.getName());
throw new IOException("Error while writing File", e);
} finally { } finally {
if (out != null) { if (out != null) {
try { try {
out.close(); out.close();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); logger.severe("Failed to close output stream");
throw new IOException("Error while closing output stream", e);
} }
} }
@@ -129,6 +155,24 @@ public class FileIO {
} }
/**
* Deletes the requested File in the directory
*
* @param tournamentFile the file that should be deleted
* @throws FileNotFoundException File could not be deleted
* @throws IllegalArgumentException File is null
*/
public void deleteTournament(File tournamentFile) throws FileNotFoundException {
if (tournamentFile == null) {
throw new IllegalArgumentException("Receiving file is null");
}
if (tournamentFile.delete()) {
logger.fine("deleted requested File");
} else {
logger.warning("Failed to delete requested File");
throw new FileNotFoundException("File deletion unsuccessful");
}
public class TournamentFile extends File{ public class TournamentFile extends File{
public TournamentFile(URI uri) { public TournamentFile(URI uri) {
@@ -141,8 +185,4 @@ public class FileIO {
} }
public void deleteTournament(File tournamentFile) throws IOException {
throw new UnsupportedOperationException("Method deleteTournament not implemented yet.");
}
} }
@@ -1,23 +1,36 @@
package ch.zhaw.projekt2.turnierverwaltung; 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{ 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 { public Player(String firstName, String name, String phoneNumber, String dateOfBirth) throws InvalidNameException, InvalidPhoneNumberException {
super(firstName, name, phoneNumber); super(firstName, name, phoneNumber);
setDateOfBirth(dateOfBirth); setDateOfBirth(dateOfBirth);
} }
public GregorianCalendar getDateOfBirth() { public LocalDate getDateOfBirth() {
return dateOfBirth; 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) { public void setDateOfBirth(String dateOfBirth) {
String[] date = dateOfBirth.split("\\."); 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 @Override
@@ -31,7 +31,7 @@ public class Tournament implements Serializable {
public void addParticipant(Participant newParticipant) throws ParticipantExistsException { public void addParticipant(Participant newParticipant) throws ParticipantExistsException {
for(Participant participant : participants){ for(Participant participant : participants){
if(participant.equals(newParticipant)){ if(participant.equals(newParticipant)){
throw new ParticipantExistsException("A Participant with the same Name exists already."); participants.remove(participant);
} }
} }
participants.add(newParticipant); participants.add(newParticipant);
@@ -41,6 +41,7 @@ public class Tournament implements Serializable {
if(!participants.contains(participant)){ if(!participants.contains(participant)){
throw new ParticipantNotExistsException("The given Participant is not part of this Tournament"); throw new ParticipantNotExistsException("The given Participant is not part of this Tournament");
} }
participants.remove(participant);
} }
public ObservableList<Participant> getParticipants() { public ObservableList<Participant> getParticipants() {
@@ -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 { try {
tournament.addParticipant(new Player(firstName, name, phoneNumber, dateOfBirth)); tournament.addParticipant(new Player(firstName, name, phoneNumber, dateOfBirth));
informListener(); 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() { public void informListener() {
for(IsObserver observer : listener) { for(IsObserver observer : listener) {
observer.update(); observer.update();
@@ -2,6 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main.participantAddFormular;
import ch.zhaw.projekt2.turnierverwaltung.FXController; import ch.zhaw.projekt2.turnierverwaltung.FXController;
import ch.zhaw.projekt2.turnierverwaltung.Participant; import ch.zhaw.projekt2.turnierverwaltung.Participant;
import ch.zhaw.projekt2.turnierverwaltung.Player;
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.Button;
@@ -39,7 +40,11 @@ public class ParticipantFormularController extends FXController {
private Label newParticipantFormularTitle; private Label newParticipantFormularTitle;
@FXML @FXML
private Button openBtn; private Button closeBtn;
@FXML
private Button deleteBtn;
@FXML @FXML
private Label participantListTitle; private Label participantListTitle;
@@ -63,8 +68,12 @@ public class ParticipantFormularController extends FXController {
private Button saveBtn; private Button saveBtn;
@FXML @FXML
void addParticipant(ActionEvent event) { void changedSelection(MouseEvent event){
getTournamentDecorator().addPlayer(firstNameTextField.getText(), participantNameTextField.getText(), phoneNumberTextField.getText(), birthDateTextField.getText()); 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 @FXML
@@ -73,7 +82,20 @@ public class ParticipantFormularController extends FXController {
} }
@FXML @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) {
} }
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
@@ -25,19 +24,23 @@
<Insets bottom="20.0" /> <Insets bottom="20.0" />
</VBox.margin> </VBox.margin>
</Label> </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> <VBox.margin>
<Insets /> <Insets />
</VBox.margin> </VBox.margin>
</ListView> </ListView>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS"> <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<children> <children>
<Button fx:id="saveBtn" mnemonicParsing="false" onAction="#save" text="Speichern"> <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> <HBox.margin>
<Insets right="40.0" /> <Insets right="40.0" />
</HBox.margin> </HBox.margin>
</Button> </Button>
<Button fx:id="openBtn" mnemonicParsing="false" text="Bearbeiten" />
</children> </children>
</HBox> </HBox>
</children> </children>
@@ -52,7 +55,7 @@
</Separator> </Separator>
<VBox fx:id="changeBtn" alignment="TOP_CENTER" onDragDetected="#changeParticipant" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS"> <VBox fx:id="changeBtn" alignment="TOP_CENTER" onDragDetected="#changeParticipant" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS">
<children> <children>
<Label fx:id="newParticipantFormularTitle" text="Neuer Teilnehmer"> <Label fx:id="newParticipantFormularTitle" text="Teilnehmer ändern/erstellen">
<font> <font>
<Font name="System Bold" size="21.0" /> <Font name="System Bold" size="21.0" />
</font> </font>
@@ -96,7 +99,7 @@
</children> </children>
</GridPane> </GridPane>
<Separator prefWidth="200.0" /> <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> <VBox.margin>
<Insets bottom="10.0" top="30.0" /> <Insets bottom="10.0" top="30.0" />
</VBox.margin> </VBox.margin>