fixed Exception handling

This commit is contained in:
Johannes Schmelz 2025-05-13 13:01:17 +02:00
parent 136be17443
commit 703097ad4d
2 changed files with 22 additions and 11 deletions

View File

@ -40,14 +40,21 @@ public class ByeGame extends Game{
@Override
public List<Game> getAllGames()
{
List<Game> temp = new ArrayList(Arrays.asList(this.getId()));
List<Game> temp = new ArrayList<>();
temp.add(this);
temp.addAll(ref.getAllGames());
return temp;
}
@Override
public List<String> getRemaningPlayers() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getRemaningPlayers'");
if (winner != null) {
return Arrays.asList(winner);
} else {
List<String> players = new ArrayList<>();
players.add(player1);
players.addAll(ref.getRemaningPlayers());
return players;
}
}
}

View File

@ -4,9 +4,9 @@ import java.util.List;
public abstract class Game {
private static int counter = 0;
private final int id;
protected final int id;
//null nicht notwendig
private String winner = null;
protected String winner = null;
protected Game() {
@ -22,12 +22,16 @@ public abstract class Game {
}
public void setWinner(String winner) {
String player1 = getPlayer1();
System.out.println("Player 1: " + player1);
String player2 = getPlayer2();
System.out.println("Player 2: " + player2);
this.winner = this.winner == null && player1 != null && player2 != null && (winner == player1 || winner == player2) ? this.winner = winner : this.winner;
}
if (this.winner != null)
throw new IllegalStateException("winner already set");
if (getPlayer1() == null || getPlayer2() == null)
throw new IllegalStateException("Opponents are not yet known");
if (getPlayer1().equals(winner) || getPlayer2().equals(winner))
this.winner = winner;
else
throw new IllegalArgumentException("Unknown player " + winner);
}
public abstract String getPlayer1();