-
Notifications
You must be signed in to change notification settings - Fork 25
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
VicenteVigueras
merged 2 commits into
code-differently:main
from
marleomac3:lj-lesson-16
Nov 14, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...ts_app/src/main/java/com/codedifferently/footballteamobject/DuplicatePlayerException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...bjects/objects_app/src/main/java/com/codedifferently/footballteamobject/FootballTeam.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...ts/objects_app/src/test/java/com/codedifferently/footballteamobject/FootballTeamTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.