I have three classes. The Player Class:
public class Player
{
private String playerName;
private int playerWorth;
private int playerSpent;
// A list of player prize objects.
private ArrayList<Prize> playerPrizeList;
public void Player()
{
playerPrizeList = new ArrayList <Prize>();
playerName = "";
playerWorth = 0;
playerSpent = 0;
}
public void playerDetails(String playerName, int playerWorth, int playerSpent)
{
setPlayerName(playerName);
setPlayerWorth(playerWorth);
setPlayerSpent(playerSpent);
}
public void prize(String prizeName, int prizeWorth, int prizeCost)
{
populatePrize(prizeName, prizeWorth, prizeCost);
}
private void populatePrize(String prizeName, int prizeWorth, int prizeCost)
{
playerPrizeList.add(new Prize(prizeName, prizeWorth, prizeCost));
}
The other class is PlayerList class from within which I am trying to add a new player object to an arrayList which works but then I also have a prizeList Array of (Prize Objects) inside this Player class. Once I add the player to the ArrayList how do I retrieve that object and make changes to (my last block of code). I hope I explained that properly.
public class PlayerList
{
private ArrayList<Player> playerAList;
public PlayerList()
{
playerAList = new ArrayList<Player>();
}
public ArrayList<Player> addPlayer(String playerName, int playerWorth, int playerSpent)
{
Player newPlayer = new Player();
newPlayer.setPlayerName(playerName);
newPlayer.setPlayerWorth(playerWorth);
newPlayer.setPlayerSpent(playerSpent);
playerAList.add(newPlayer);
return playerAList;
}
/**
* @i is the index of ArrayList for Player Prizes
*/
public void setPlayerPrizeList(int i, String prizeName, int prizeWorth, int prizeCost)
{
playerAList.get(i) = currentPlayer;
currentPlayer.populatePrize(prizeName, prizeWorth, prizeCost);
playerAList.set(i, currentPlayer);
return playerAList;
}
playerAList.get(i) = currentPlayer;Well, have a look at my answer below.