-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeam.java
More file actions
66 lines (57 loc) · 1.73 KB
/
Copy pathTeam.java
File metadata and controls
66 lines (57 loc) · 1.73 KB
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
66
package com.gamingroom;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
/**
* A simple class to hold information about a team
* <p>
* Notice the overloaded constructor that requires
* an id and name to be passed when creating.
* Also note that no mutators (setters) defined so
* these values cannot be changed once a team is
* created.
* </p>
* @author coce@snhu.edu
*
*/
//extends Entity to access ID and Name
public class Team extends Entity {
//A list of the players assigned to this team
private List<Player> players = new ArrayList<Player>();
/*
* Constructor with an identifier and name
*/
public Team(long id, String name) {
super(id, name); //Entity now hold ID and 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;
//Use iterator to look for existing player with same name
Iterator<Player> playersIterator = players.iterator();
while (playersIterator.hasNext()) {
Player playerInstance = playersIterator.next();
if (playerInstance.getName().equalsIgnoreCase(name)) {
player = playerInstance;
break;
}
}
//If not found, a new player will be created
if (player == null) {
// Get the singleton instance of GameService to access its ID counters
GameService service = GameService.getInstance();
player = new Player(service.getNextPlayerId(), name);
players.add(player);
}
return player;
}
@Override
public String toString() { //use methods from Entity to access ID and Name
return "Team [id=" + getId() + ", name=" + getName() + "]";
}
}