Skip to content

Commit f395439

Browse files
authored
feat: created FootballTeam object class w/ custom exception (#538)
* feat: created footballteam object class w/ custom exception * fix: ran gradle commadn to fix violations
1 parent 448b52e commit f395439

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.footballteamobject;
2+
3+
public class DuplicatePlayerException extends Exception {
4+
public DuplicatePlayerException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.codedifferently.footballteamobject;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class FootballTeam {
7+
8+
public static class DuplicatePlayerException extends Exception {
9+
public DuplicatePlayerException(String message) {
10+
super(message);
11+
}
12+
}
13+
14+
private int playerCount;
15+
private int jerseyNumber50AndOver;
16+
private int jerseyNumberUnder50;
17+
private Map<Integer, String> players = new HashMap<>();
18+
private final String teamName;
19+
private final String location;
20+
private final Conference conference;
21+
22+
public FootballTeam(
23+
String teamName, String location, Conference conference, Map<Integer, String> players) {
24+
this.teamName = teamName;
25+
this.location = location;
26+
this.conference = conference;
27+
this.players = players;
28+
}
29+
30+
public enum Conference {
31+
// Power Five Conferences
32+
ACC,
33+
BIG_12,
34+
BIG_10,
35+
PAC_12,
36+
SEC,
37+
// Group of Five Conferences
38+
AMERICAN,
39+
CONFERENCE_USA,
40+
MAC,
41+
MOUNTAIN_WEST,
42+
SUN_BELT
43+
}
44+
45+
public boolean isPowerFive() {
46+
if (conference == Conference.ACC
47+
|| conference == Conference.BIG_12
48+
|| conference == Conference.BIG_10
49+
|| conference == Conference.PAC_12
50+
|| conference == Conference.SEC) {
51+
return true;
52+
}
53+
return false;
54+
}
55+
56+
public String getTeamName() {
57+
return teamName;
58+
}
59+
60+
public String getLocation() {
61+
return location;
62+
}
63+
64+
public Conference getConference() {
65+
return conference;
66+
}
67+
68+
public int getPlayerCount() {
69+
return playerCount;
70+
}
71+
72+
public Map<Integer, String> getPlayers() {
73+
return players;
74+
}
75+
76+
public void addPlayer(int number, String name) throws DuplicatePlayerException {
77+
if (players.containsKey(number)) {
78+
throw new DuplicatePlayerException("A player with that number already exists.");
79+
}
80+
players.put(number, name);
81+
playerCount++;
82+
}
83+
84+
public void removePlayer(int number) {
85+
players.remove(number);
86+
playerCount--;
87+
}
88+
89+
public void tallyJerseyNumbers() {
90+
for (Map.Entry<Integer, String> entry : players.entrySet()) {
91+
if (entry.getKey() < 50) {
92+
jerseyNumberUnder50++;
93+
} else {
94+
jerseyNumber50AndOver++;
95+
}
96+
}
97+
}
98+
99+
public int getJerseyNumber50AndOver() {
100+
return jerseyNumber50AndOver;
101+
}
102+
103+
public int getJerseyNumberUnder50() {
104+
return jerseyNumberUnder50;
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.codedifferently.footballteamobject;
2+
3+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
import com.codedifferently.footballteamobject.FootballTeam.Conference;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
public class FootballTeamTest {
16+
FootballTeam team;
17+
private Map<Integer, String> players;
18+
19+
@BeforeEach
20+
void setUp() {
21+
team = new FootballTeam("Alabama", "Tuscaloosa", FootballTeam.Conference.SEC, new HashMap<>());
22+
players = new HashMap<>();
23+
players.put(2, "Derrick Henry");
24+
}
25+
26+
@Test
27+
public void testGetTeamName_ReturnsCorrectName() {
28+
assertEquals("Alabama", team.getTeamName());
29+
}
30+
31+
@Test
32+
public void testGetTeamName_ReturnsNonNull() {
33+
assertNotNull(team.getTeamName());
34+
}
35+
36+
@Test
37+
public void testAddPlayer() throws FootballTeam.DuplicatePlayerException {
38+
team.addPlayer(1, "John Doe");
39+
assertEquals(1, team.getPlayerCount());
40+
assertEquals("John Doe", team.getPlayers().get(1));
41+
}
42+
43+
@Test
44+
public void testRemovePlayer() throws FootballTeam.DuplicatePlayerException {
45+
team.addPlayer(1, "John Doe");
46+
team.removePlayer(1);
47+
assertEquals(0, team.getPlayerCount());
48+
assertFalse(team.getPlayers().containsKey(1));
49+
}
50+
51+
@Test
52+
public void testTallyJerseyNumbers_Under50() throws FootballTeam.DuplicatePlayerException {
53+
team.addPlayer(1, "Player 1");
54+
team.tallyJerseyNumbers();
55+
assertEquals(1, team.getJerseyNumberUnder50());
56+
}
57+
58+
@Test
59+
public void testPowerFiveConferences() {
60+
FootballTeam team = new FootballTeam("Team Name", "Location", Conference.ACC, new HashMap<>());
61+
assertTrue(team.isPowerFive());
62+
team = new FootballTeam("Team Name", "Location", Conference.BIG_12, new HashMap<>());
63+
assertTrue(team.isPowerFive());
64+
team = new FootballTeam("Team Name", "Location", Conference.BIG_10, new HashMap<>());
65+
assertTrue(team.isPowerFive());
66+
team = new FootballTeam("Team Name", "Location", Conference.PAC_12, new HashMap<>());
67+
assertTrue(team.isPowerFive());
68+
team = new FootballTeam("Team Name", "Location", Conference.SEC, new HashMap<>());
69+
assertTrue(team.isPowerFive());
70+
}
71+
72+
@Test
73+
public void testGroupOfFiveConferences() {
74+
FootballTeam team =
75+
new FootballTeam("Team Name", "Location", Conference.AMERICAN, new HashMap<>());
76+
assertFalse(team.isPowerFive());
77+
team = new FootballTeam("Team Name", "Location", Conference.CONFERENCE_USA, new HashMap<>());
78+
assertFalse(team.isPowerFive());
79+
team = new FootballTeam("Team Name", "Location", Conference.MAC, new HashMap<>());
80+
assertFalse(team.isPowerFive());
81+
team = new FootballTeam("Team Name", "Location", Conference.MOUNTAIN_WEST, new HashMap<>());
82+
assertFalse(team.isPowerFive());
83+
team = new FootballTeam("Team Name", "Location", Conference.SUN_BELT, new HashMap<>());
84+
assertFalse(team.isPowerFive());
85+
}
86+
87+
@Test
88+
void testDuplicatePlayerException() {
89+
String message = "A player with that number already exists.";
90+
DuplicatePlayerException exception = new DuplicatePlayerException(message);
91+
assertThatThrownBy(
92+
() -> {
93+
throw exception;
94+
})
95+
.isInstanceOf(DuplicatePlayerException.class)
96+
.hasMessage(message);
97+
}
98+
}

0 commit comments

Comments
 (0)