public final class Table {
private final Client[] clients = new Client[7];
public boolean contains(Client client) {
for (Client c : clients) {
if (c == client) {
return true;
}
}
return false;
}
public int indexOf(Client client) {
if (!contains(client)) {
return -1;
}
else {
for (int i = 0; i < clients.length; i++) {
if (clients[i] == client) {
return i;
}
}
}
}
public void add(Client client) throws IllegalArgumentException,
FullTableException {
if (client == null) {
throw new IllegalArgumentException("client must not be null");
}
if (contains(client)) {
throw new IllegalArgumentException(
"cannot add client to the table more than once");
}
if (available() > 0) {
clients[next()] = client;
// NOTE: You might want to announce to the other players that a new
// player has joined the game.
}
else {
throw new FullTableException();
}
}
public void remove(Client client) throws IllegalArgumentException {
if (!contains(client)) {
throw new IllegalArgumentException("table does not contain client");
}
clients[indexOf(client)] = null;
// NOTE: Announce to the other players that the player has left.
}
private int next() {
for (int i = 0; i < clients.length; i++) {
if (clients[i] == null) {
return i;
}
}
return -1;
}
}