At the moment inside the class I have ArrayList which the objects are stored in when they are added to the club. I also have a integer which would be auto incremented once a object has been added to the ArrayList. However, I am unsure how I would be able to add the registration ID with the player object in the ArrayList.
4 Answers
One solution is to use a Map, which associates the individual ID with the particular player, as a key-value pair, instead of a List:
public class Club
{
private String clubName;
private int registrationID;
private Map<Player, Integer> players;
/**
* Create a club with given club name.
*/
public Club(String clubName)
{
players = new HashMap<Player, Integer>();
this.clubName = clubName;
registrationID = 1;
}
public void registerPlayer(Player p)
{
// check if player is already in the club:
if (!players.containsKey(p)) {
players.put(p, new Integer(registrationID));
// increment ID counter:
registrationID++;
}
}
public void listAll ()
{
for (Player p : players.keySet()) {
System.out.println(p);
}
}
}
Comments
Try creating another class:
public class Registration() {
private Player player;
private String registrationId;
public Registration(Player p) {
// Assign p to player
// Generate the registration ID
}
}
Then just have ArrayList<Registration> registrations to hold all these. Your listAll() method would then need to reference i.getNext().getPlayer() to perform the same action.
Playerclass look like? If I can make an assumption thatPlayer.setRegID(int)is defined, then it would be straightforward.Playerscan be part of multipleClubs, in which case it doesn't make sense to store the registration ID in thePlayerclass, unless the method is something likePlayer.setRegId(Club c, int id).Players can leave aClub, then you'll need a separate structure to keep track ofPlayerswho are no longer valid (removing them from the list would change all the ID numbers).