-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeamAndPlayers.java
113 lines (89 loc) · 2.38 KB
/
TeamAndPlayers.java
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
import java.util.ArrayList;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author kdost
*/
public class Team {
private String name;
private ArrayList<Player> members;
private int maxSize;
public Team(String name) {
this.name = name;
this.members = new ArrayList<Player>();
this.maxSize = 16;
}
public String getName() {
return this.name;
}
public void addPlayer(Player player) {
if (this.members.size() < this.maxSize) {
this.members.add(player);
}
}
public void printPlayers() {
for (Player dude : this.members) {
System.out.println(dude + ", goals " + dude.goals());
}
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
public int size() {
return this.members.size();
}
public int goals() {
int teamGoals = 0;
for (Player dude : this.members) {
teamGoals += dude.goals();
}
return teamGoals;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author kdost
*/
public class Player {
private String name;
private int goals;
public Player(String name, int goals) {
this.name = name;
this.goals = goals;
}
public Player(String name) {
this.name = name;
this.goals = 0;
}
public String getName() {
return this.name;
}
public int goals() {
return this.goals;
}
@Override
public String toString() {
return this.name + ", goals " + this.goals;
}
}
public class Main {
public static void main(String[] args) {
// test your code here
Team barcelona = new Team("FC Barcelona");
Player brian = new Player("Brian");
Player pekka = new Player("Pekka", 39);
barcelona.addPlayer(brian);
barcelona.addPlayer(pekka);
barcelona.addPlayer(new Player("Mikael", 1)); // works similarly as the above
System.out.println("Total goals: " + barcelona.goals());
}
}