Skip to content

feat: created FootballTeam object class w/ custom exception #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.footballteamobject;

public class DuplicatePlayerException extends Exception {
public DuplicatePlayerException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.codedifferently.footballteamobject;

import java.util.HashMap;
import java.util.Map;

public class FootballTeam {

public static class DuplicatePlayerException extends Exception {
public DuplicatePlayerException(String message) {
super(message);
}
}

private int playerCount;
private int jerseyNumber50AndOver;
private int jerseyNumberUnder50;
private Map<Integer, String> players = new HashMap<>();
private final String teamName;
private final String location;
private final Conference conference;

public FootballTeam(
String teamName, String location, Conference conference, Map<Integer, String> players) {
this.teamName = teamName;
this.location = location;
this.conference = conference;
this.players = players;
}

public enum Conference {
// Power Five Conferences
ACC,
BIG_12,
BIG_10,
PAC_12,
SEC,
// Group of Five Conferences
AMERICAN,
CONFERENCE_USA,
MAC,
MOUNTAIN_WEST,
SUN_BELT
}

public boolean isPowerFive() {
if (conference == Conference.ACC
|| conference == Conference.BIG_12
|| conference == Conference.BIG_10
|| conference == Conference.PAC_12
|| conference == Conference.SEC) {
return true;
}
return false;
}

public String getTeamName() {
return teamName;
}

public String getLocation() {
return location;
}

public Conference getConference() {
return conference;
}

public int getPlayerCount() {
return playerCount;
}

public Map<Integer, String> getPlayers() {
return players;
}

public void addPlayer(int number, String name) throws DuplicatePlayerException {
if (players.containsKey(number)) {
throw new DuplicatePlayerException("A player with that number already exists.");
}
players.put(number, name);
playerCount++;
}

public void removePlayer(int number) {
players.remove(number);
playerCount--;
}

public void tallyJerseyNumbers() {
for (Map.Entry<Integer, String> entry : players.entrySet()) {
if (entry.getKey() < 50) {
jerseyNumberUnder50++;
} else {
jerseyNumber50AndOver++;
}
}
}

public int getJerseyNumber50AndOver() {
return jerseyNumber50AndOver;
}

public int getJerseyNumberUnder50() {
return jerseyNumberUnder50;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.codedifferently.footballteamobject;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.codedifferently.footballteamobject.FootballTeam.Conference;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class FootballTeamTest {
FootballTeam team;
private Map<Integer, String> players;

@BeforeEach
void setUp() {
team = new FootballTeam("Alabama", "Tuscaloosa", FootballTeam.Conference.SEC, new HashMap<>());
players = new HashMap<>();
players.put(2, "Derrick Henry");
}

@Test
public void testGetTeamName_ReturnsCorrectName() {
assertEquals("Alabama", team.getTeamName());
}

@Test
public void testGetTeamName_ReturnsNonNull() {
assertNotNull(team.getTeamName());
}

@Test
public void testAddPlayer() throws FootballTeam.DuplicatePlayerException {
team.addPlayer(1, "John Doe");
assertEquals(1, team.getPlayerCount());
assertEquals("John Doe", team.getPlayers().get(1));
}

@Test
public void testRemovePlayer() throws FootballTeam.DuplicatePlayerException {
team.addPlayer(1, "John Doe");
team.removePlayer(1);
assertEquals(0, team.getPlayerCount());
assertFalse(team.getPlayers().containsKey(1));
}

@Test
public void testTallyJerseyNumbers_Under50() throws FootballTeam.DuplicatePlayerException {
team.addPlayer(1, "Player 1");
team.tallyJerseyNumbers();
assertEquals(1, team.getJerseyNumberUnder50());
}

@Test
public void testPowerFiveConferences() {
FootballTeam team = new FootballTeam("Team Name", "Location", Conference.ACC, new HashMap<>());
assertTrue(team.isPowerFive());
team = new FootballTeam("Team Name", "Location", Conference.BIG_12, new HashMap<>());
assertTrue(team.isPowerFive());
team = new FootballTeam("Team Name", "Location", Conference.BIG_10, new HashMap<>());
assertTrue(team.isPowerFive());
team = new FootballTeam("Team Name", "Location", Conference.PAC_12, new HashMap<>());
assertTrue(team.isPowerFive());
team = new FootballTeam("Team Name", "Location", Conference.SEC, new HashMap<>());
assertTrue(team.isPowerFive());
}

@Test
public void testGroupOfFiveConferences() {
FootballTeam team =
new FootballTeam("Team Name", "Location", Conference.AMERICAN, new HashMap<>());
assertFalse(team.isPowerFive());
team = new FootballTeam("Team Name", "Location", Conference.CONFERENCE_USA, new HashMap<>());
assertFalse(team.isPowerFive());
team = new FootballTeam("Team Name", "Location", Conference.MAC, new HashMap<>());
assertFalse(team.isPowerFive());
team = new FootballTeam("Team Name", "Location", Conference.MOUNTAIN_WEST, new HashMap<>());
assertFalse(team.isPowerFive());
team = new FootballTeam("Team Name", "Location", Conference.SUN_BELT, new HashMap<>());
assertFalse(team.isPowerFive());
}

@Test
void testDuplicatePlayerException() {
String message = "A player with that number already exists.";
DuplicatePlayerException exception = new DuplicatePlayerException(message);
assertThatThrownBy(
() -> {
throw exception;
})
.isInstanceOf(DuplicatePlayerException.class)
.hasMessage(message);
}
}