implemented functionality to save participants also in file.

This commit is contained in:
schrom01 2022-05-01 17:00:09 +02:00
parent ff6f2bd94e
commit 4cb743a21a
6 changed files with 37 additions and 10 deletions

View File

@ -36,6 +36,7 @@ public class Factory {
}
public void loadTournamentList(BorderPane pane, FactoryDecorator factoryDecorator){
tournamentDecorator.setTournament(null);
TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml"), factoryDecorator);
}

View File

@ -3,16 +3,19 @@ package ch.zhaw.projekt2.turnierverwaltung;
import java.io.Serializable;
public class Person implements Serializable {
private final String NAME_MATCHING_REGEX = "[a-zA-Z]{1,20}";
private final String PHONE_MATCHING_REGEX = "[\\+0-9]+";
private String name;
private String firstName;
private String phoneNumber;
public Person(String firstName, String name, String phoneNumber) throws InvalidNameException, InvalidPhoneNumberException {
if(!firstName.matches("^[a-zA-Z][a-zA-Z][-',.][a-zA-Z]{1,20}$")){
if(!firstName.matches(NAME_MATCHING_REGEX)){
throw new InvalidNameException("The First name is Invalid.");
} else if(!name.matches("^[a-zA-Z][a-zA-Z][-',.][a-zA-Z]{1,20}$")){
} else if(!name.matches(NAME_MATCHING_REGEX)){
throw new InvalidNameException("The Last name is Invalid");
} else if(!phoneNumber.matches("[\\+0-9]+")){
} else if(!phoneNumber.matches(PHONE_MATCHING_REGEX)){
throw new InvalidPhoneNumberException("The entered Phone Number is invalid.");
}
setFirstName(firstName);
@ -55,4 +58,9 @@ public class Person implements Serializable {
}
@Override
public String toString(){
return firstName + " " + name;
}
}

View File

@ -22,7 +22,6 @@ public class Player extends Person implements Participant{
@Override
public boolean equals(Participant participant) {
return getClass().equals(participant.getClass()) && getName().equals(participant.getName()) &&
getFirstName().equals(((Player) participant).getFirstName());
return getClass().equals(participant.getClass()) && toString().toLowerCase().equals(participant.toString().toLowerCase());
}
}

View File

@ -29,7 +29,7 @@ public class Team implements Participant {
@Override
public boolean equals(Participant participant) {
return getClass().equals(participant.getClass()) && name.equals(participant.getName());
return getClass().equals(participant.getClass()) && toString().toLowerCase().equals(participant.toString().toLowerCase());
}
public Person getContactPerson() {
@ -39,4 +39,9 @@ public class Team implements Participant {
public void setContactPerson(Person contactPerson) {
this.contactPerson = contactPerson;
}
@Override
public String toString(){
return name;
}
}

View File

@ -10,9 +10,16 @@ public class TournamentDecorator implements IsObservable{
private FileIO fileIO;
private List<IsObserver> listener = new ArrayList<>();
public TournamentDecorator(FileIO fileIO, Tournament tournament){
setTournament(tournament);
public TournamentDecorator(FileIO fileIO){
setFileIO(fileIO);
addListener(new IsObserver() {
@Override
public void update() {
if(tournament != null){
saveTournament();
}
}
});
}
public void setFileIO(FileIO fileIO) {
@ -37,6 +44,14 @@ public class TournamentDecorator implements IsObservable{
listener.remove(observer);
}
public void saveTournament(){
try {
fileIO.saveTournament(tournament);
} catch (IOException e) {
e.printStackTrace(); //TODO handle and logging
}
}
public void createTournament(String name, Tournament.Type type){
if(fileIO.tournamentExists(name)){
System.out.println("Tournament with same name exists already.");
@ -77,7 +92,6 @@ public class TournamentDecorator implements IsObservable{
}
}
public void informListener() {
for(IsObserver observer : listener) {
observer.update();

View File

@ -16,7 +16,7 @@ import java.io.IOException;
public class MainWindow extends Application {
private FileIO fileIO = new FileIO(System.getProperty("user.dir") + "/tournierverwaltung_angrynerds");
private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO, null);
private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO);
private Factory factory = new Factory(fileIO, tournamentDecorator); //TODO make it private!
private FactoryDecorator factoryDecorator;