title: Team and players objects Java difficulties
body: <p>Basically I'm trying to get my head around creating a java program that manages teams and players.</p>
  
  <p>From my understanding I would have a team and a player class. In the team class there would be the get and set methods, as well as some form of collection to store the players in right, such as an array list? Then in the player class the relevant get and set methods.</p>
  
  <p>This setup would be because one team have one to many players right? </p>
  
  <p>I've had no end of trouble trying to get this working. One particular problem I've encountered is that every time I create a team object, and add a player object to it, then create another team object and another player but if I list the players for that new team it shows the previous player added to the first team as well as the new player.</p>
  
  <p>So I figured it was back to the drawing board and wondered if someone could offer some general advice about how they would structure this?</p>
  
  <p>Many thanks,</p>
  
  <pre><code>import java.util.Iterator;
  
  public class test {
  public test() {
  }
  
  //Method to show the team and its players
  
  public static void showTeamPlayers(Team aTeam) {
      Player players;
      System.out.println(aTeam.getTeamName());
      Iterator e = aTeam.getPlayerList().iterator();
      while (e.hasNext()) {
          players = (Player)e.next();
          System.out.println("\t" + players.getPlayerNumber() + " " + players.getPlayerName());
      }
      System.out.println("");
  }
  
  public static void main(String[] args) {
      int teamID;
      String teamName = "";
  
      //First create a divison/league
      League DivisionOne = new League("Division One");
  
      //Create a new team object
      Team team = new Team(teamName);
  
      //Asks the user to enter a team name and stores the input
      UserInput.print("Enter team name:");
      teamName = UserInput.readString();
  
      team.setTeamName(teamName);
  
      //Add the team
      DivisionOne.addTeam(new Team(teamName));
  
      Player player = new Player(0, "Dave");
      Player player1 = new Player(1, "Dennis");
      Player player2 = new Player(2, "Peter");
  
      //Add to team
      team.addPlayer(player);
      team.addPlayer(player1);
      team.addPlayer(player2);
  
      test.showTeamPlayers(team);
  
      //Asks the user to enter a team name and stores the input
      UserInput.print("Enter team name:");
      teamName = UserInput.readString();
  
      team.setTeamName(teamName);
  
      //Add the team
      DivisionOne.addTeam(new Team(teamName));
  
      Player player3 = new Player(3, "Creamer");
      Player player4 = new Player(4, "Matt");
      Player player5 = new Player(5, "John");
  
      //Add to team 1
      team.addPlayer(player3);
      team.addPlayer(player4);
      team.addPlayer(player5);
  
      test.showTeamPlayers(team);
  }
  }
  </code></pre>