added Type checking in Tournament.java

This commit is contained in:
schrom01 2022-05-01 12:33:14 +02:00
parent 80005d214d
commit 50f98b4ac1
3 changed files with 25 additions and 12 deletions

View File

@ -4,6 +4,7 @@ import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays;
public class Tournament implements Serializable { public class Tournament implements Serializable {
private String name; private String name;
@ -11,10 +12,13 @@ public class Tournament implements Serializable {
public Tournament(String name, Type type) throws InvalidNameException { public Tournament(String name, Type type) throws InvalidNameException, InvalidTypeException {
if(!name.matches("[\\w]{1,20}")){ if(!name.matches("[\\w]{1,20}")){
throw new Tournament.InvalidNameException("Invalid Name entered"); //TODO handle en logging. throw new Tournament.InvalidNameException("Invalid Name entered"); //TODO handle en logging.
} else if(!Arrays.asList(Type.values()).contains(type)){
throw new InvalidTypeException("Invalid Type selected"); //TODO handle en logging.
} }
setName(name); setName(name);
setType(type); setType(type);
} }
@ -67,5 +71,16 @@ public class Tournament implements Serializable {
} }
public class InvalidTypeException extends Exception {
public InvalidTypeException() {
super();
}
public InvalidTypeException(String errorMessage) {
super(errorMessage);
}
}
} }

View File

@ -64,8 +64,11 @@ public class TournamentListController extends FXController {
Tournament tournament = new Tournament(tournamentNameField.getText(), modusChoiceBox.getValue()); Tournament tournament = new Tournament(tournamentNameField.getText(), modusChoiceBox.getValue());
getFileIO().saveTournament(tournament); getFileIO().saveTournament(tournament);
loadContent(); loadContent();
tournamentNameField.clear();
} catch (Tournament.InvalidNameException e) { } catch (Tournament.InvalidNameException e) {
e.printStackTrace(); //TODO handle and logging e.printStackTrace(); //TODO handle and logging
} catch (Tournament.InvalidTypeException e) {
e.printStackTrace(); //TODO handle and logging
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); //TODO handle and logging e.printStackTrace(); //TODO handle and logging
} }
@ -92,6 +95,7 @@ public class TournamentListController extends FXController {
if(alert.showAndGetResult()){ if(alert.showAndGetResult()){
try { try {
getFileIO().deleteTournament(tournamentFile); getFileIO().deleteTournament(tournamentFile);
loadContent();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); //TODO handle and logging e.printStackTrace(); //TODO handle and logging
} }

View File

@ -108,20 +108,14 @@ class FileIOTest {
} }
@Test @Test
void saveTournament() throws IOException { void saveTournament() throws IOException, Tournament.InvalidNameException, Tournament.InvalidTypeException {
Tournament tournament = null; Tournament tournament = null;
try { tournament = new Tournament("test1", Tournament.Type.KO);
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()){ assertTrue(file.exists());
file.delete(); assertTrue(file.delete());
} else { assertFalse(file.exists());
fail();
}
} }
@Test @Test