views:

32

answers:

1

Hi, again i'm here with my classes, my software is almost done after finishing last two things i will continue to GUI development. Anyway, here is my code:

public class Team
{
    private String clubName;
    private String preName;
    private ArrayList<Branch> branches;

    public Team(String clubName, String preName)
    {
        this.clubName = clubName;
        this.preName = preName;
        branches = new ArrayList<Branch>();
    }

    public Team() {
        // TODO Auto-generated constructor stub
    }

    public String getClubName() { return clubName; }
    public String getPreName() { return preName; }
    public ArrayList<Branch> getBranches() { branches = new ArrayList<Branch>(branches);return branches; }

    public void setClubName(String clubName) { this.clubName = clubName; }
    public void setPreName(String preName) { this.preName = preName; }
    public void setBranches(ArrayList<Branch> branches) { this.branches = new ArrayList<Branch>(branches);  }
}

public class Branch
{
    public ArrayList<Player> players = new ArrayList<Player>();
    String brName;
    public Branch() {}
    public void setBr(String brName){this.brName = brName;}
    public String getBr(){return brName;}
    public ArrayList<Player> getPlayers() {players =new ArrayList<Player>(players); return players; }
    public void setPlayers(ArrayList<Player> players) { this.players =new ArrayList<Player>(players);  }
    public String toString() {
        return "Branches [" + brName  + "]";}
}
public class Player
{
    private String name;
    private String pos;
    private Integer salary;
    private Integer number;

    public Player(String name, String pos, Integer salary, Integer number)
    {
        this.name = name;
        this.pos = pos;
        this.salary = salary;
        this.number = number;
    }

    public Player(){}

    public String getName() { return name; }
    public String getPos() { return pos; }
    public Integer getSalary() { return salary; }
    public Integer getNumber() { return number; }

    public void setName(String name) { this.name = name; }
    public void setPos(String pos) { this.pos = pos; }
    public void setSalary(Integer salary) { this.salary = salary; }
    public void setNumber(Integer number) { this.number = number; }
    public String toString() {
        return "Player [name=" + name + ", number=" + number + ", pos=" + pos
                + ", salary=" + salary + "]";
    }

}

//TEST

String p1,p2;
            int a1,a2;
            String t, br;
            System.out.print("Enter player name : ");
            p1 = input.readLine();
            System.out.print("Enter player position : ");
            p2 = input.readLine();
            System.out.print("Enter player salary : ");
            a1 = Integer.parseInt(input.readLine());
            System.out.print("Enter player number : ");
            a2 = Integer.parseInt(input.readLine());

                players[pCount].setName(p1);
                players[pCount].setPos(p2);
                players[pCount].setSalary(a1);
                players[pCount].setNumber(a2);
                ptmp.add(players[pCount]);
                pCount++;

            System.out.print("Enter the branch of player : ");
            br = input.readLine();
            int fff=0;

            for(int i = 0; i<brCount;i++)
            {
                if(br.equals(myBranch[i].brName)==true){
                    myBranch[i].setPlayers(ptmp);fff=i;}
            }

MY FIRST QUESTION : I'm trying to add a player to my system. When a player added i can easily add it to Branch class too and connect them. But I can't do it for Players' club. I mean i want to display which player plays in which club. But i can't do it.

MY SECOND QUESTION : Deleting a player is problem too. When i delete player it should be deleted everywhere. But couldn't figured that out.

In the test, you can see the display function I tried. It works fine for Branch-Player. And I wanna add Team connection too. Team-Branch-Player should be connected.

+1  A: 

Q1: It depends how efficiently you want to do your searches.. for now, since you don't store back references you have to first search in which branch is your player and then search which is the club that contains your branch.

With good equals method for your Branch and Player class this is trivial:

for (Team t : teamList)
{
  if (t.branches.contains(player))
    return true;
}

return false;

But this won't be efficient since you'll have a O(n*m) complexity where n is the team size and m is the average branch size.

If you want something more efficient I'd suggest you to store backreferences inside your classes, you can have your Player class with two attributes

Branch currentBranch
Team currentTeam

and you can set them while you add the player to a branch/team.

Otherwise you can keep a separate HashMap that maps every player to his branch/team. Less memory efficient but quite straightforward.

Q2: to remove the Player from his branch/team you just have to know in which one he stays.. (using the answer to Q1), then before removing from players you just remove it from the corresponding branch/team:

Branch b = findWhichBranch(player);
Team t = findWhichTeam(player);

b.remove(player);
t.remove(player);

players[index] = null;

Of course if branch is implied by team you will just remove it from the branch, since there's no direct association between a player and a team.

Jack