File size: 3,317 Bytes
1749291
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Let's continue the implementation by implementing a class that models one team.

So, write a (non-public) class Team with the following properties:
Attribute 'name' (string)
Some attribute to store the players in
A constructor that takes the name as a parameter
Set and get methods for the name
Method addPlayer(Player p), which adds one player to the team
The method printPlayers(), which prints the players in the order of addition as shown in the example below (first the number, then the name):

Example output of players:
7. Pele
8. Ronaldo





================



import java.util.Random;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;

public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        
        
        
       System.out.println("Testing class Team...");
       
       Team t = new Team("FC Turku");
       
       System.out.println("Team object created!");
       
       try {
           Field name = t.getClass().getDeclaredField("name");
           boolean namePrivate = name.getModifiers() == 2;
           System.out.println("Name is private: " + namePrivate);
       } catch (Exception e) {
           System.out.println("Attribute name is not defined");
       }
              
       System.out.println("Testing get methods...");
       System.out.println("Name: " + t.getName());
       
       System.out.println("Testing set methods...");
       t.setName("University Football Club");
       System.out.println("Name: " + t.getName());
      
       System.out.println("Adding players to the team...");
       Player p1 = new Player(7, "Ronaldo");
       Player p2 = new Player(10, "Pele");
       
       
       t.addPlayer(p1);
       t.addPlayer(p2);
       
       System.out.println("Players added!");
       System.out.println("Printing players:");
       t.printPlayers();
       
       Player p3 = new Player(34, "Brad Baller");
       t.addPlayer(p3);
       System.out.println("Players added!");
       System.out.println("Printing players:");
       t.printPlayers();
       
    }
}


//ADD
class Team {
    // attributes
    private String name;
    private ArrayList<Player> listPlayers;


    //constructor
    public Team(String name) {
        this.name = name;
        // need to initialise empty array, otherwise 'NullPointerException'
        this.listPlayers = new ArrayList<>();
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addPlayer(Player p) {
        this.listPlayers.add(p);
    }

    public void printPlayers() {
        // int counter = 0;
        
        for (Player p: listPlayers) {
            // counter+=1;
            // System.out.println(counter + ". " + p.getName());
            // a player has their own number, dont use 'counter'
            System.out.println(p.getNumber() + ". " + p.getName());
        }
    }

    
}




Testing class Team...
Team object created!
Name is private: true
Testing get methods...
Name: FC Turku
Testing set methods...
Name: University Football Club
Adding players to the team...
Players added!
Printing players:
7. Ronaldo
10. Pele
Players added!
Printing players:
7. Ronaldo
10. Pele
34. Brad Baller