Skip to content

Commit 07be639

Browse files
committed
feat: created footballteam object class w/ custom exception
1 parent 2e32759 commit 07be639

File tree

3 files changed

+209
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)