This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Team.java
65 lines (56 loc) · 1.54 KB
/
Team.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.gamingroom;
import java.util.ArrayList;
import java.util.List;
/**
* A simple class to hold information about a team
*
* @author eric.slutz@snuh.edu
*
*/
public class Team extends Entity {
private final List<Player> players = new ArrayList<>();
/**
* Constructor with an identifier and name
*/
public Team(long id, String name) {
super(id, name);
}
/**
* Construct a new player instance
*
* @param name the unique name of the player
* @return the player instance (new or existing)
*/
public Player addPlayer(String name) {
// a local player instance
Player player = null;
/*
Iterator Pattern Purpose and Characteristics
The purpose of the iterator pattern is to be able to go through each element of a list.
In this case, a for loop is being used basically saying, for each existingPlayer in the
list players, execute the code inside the loop.
*/
// if found, simply return the existing instance
for (Player existingPlayer : players) {
if (existingPlayer.getName().equals(name)) {
player = existingPlayer;
}
}
// if not found, make a new player instance and add to list of players
if (player == null) {
player = new Player(GameService.getInstance().getNextPlayerId(), name);
players.add(player);
}
// return the new/existing player instance to the caller
return player;
}
/**
* Returns team id and name in a formatted string
*
* @return string of id and name
*/
@Override
public String toString() {
return "Team [id=" + getId() + ", name=" + getName() + "]";
}
}