From 703097ad4d22ce5d9578fe4b2447a091be897609 Mon Sep 17 00:00:00 2001 From: Peet Date: Tue, 13 May 2025 13:01:17 +0200 Subject: [PATCH] fixed Exception handling --- uebung04/tournament/ByeGame.java | 13 ++++++++++--- uebung04/tournament/Game.java | 20 ++++++++++++-------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/uebung04/tournament/ByeGame.java b/uebung04/tournament/ByeGame.java index 1647206..c8d59a6 100644 --- a/uebung04/tournament/ByeGame.java +++ b/uebung04/tournament/ByeGame.java @@ -40,14 +40,21 @@ public class ByeGame extends Game{ @Override public List getAllGames() { - List temp = new ArrayList(Arrays.asList(this.getId())); + List temp = new ArrayList<>(); + temp.add(this); temp.addAll(ref.getAllGames()); return temp; } @Override public List getRemaningPlayers() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getRemaningPlayers'"); + if (winner != null) { + return Arrays.asList(winner); + } else { + List players = new ArrayList<>(); + players.add(player1); + players.addAll(ref.getRemaningPlayers()); + return players; + } } } diff --git a/uebung04/tournament/Game.java b/uebung04/tournament/Game.java index 48fb1c3..47e6170 100644 --- a/uebung04/tournament/Game.java +++ b/uebung04/tournament/Game.java @@ -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();