Skip to content

Commit 58c1c5d

Browse files
authored
Merge branch 'code-differently:main' into lesson_16
2 parents 5202fed + f395439 commit 58c1c5d

File tree

9 files changed

+619
-0
lines changed

9 files changed

+619
-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+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.codedifferently.lesson16.hummadtanweer;
2+
3+
import java.util.ArrayList;
4+
5+
public class Person {
6+
private final String name;
7+
private final int age;
8+
private final Gender gender;
9+
private final ArrayList<String> hobbies;
10+
private final String email;
11+
private final int MAX_HOBBIES = 2;
12+
13+
public class HobbyLimitExceededException extends Exception {
14+
public HobbyLimitExceededException(String message) {
15+
super(message);
16+
}
17+
}
18+
19+
enum Gender {
20+
MALE,
21+
FEMALE,
22+
OTHER
23+
}
24+
25+
public Person(String name, int age, Gender gender, String email) {
26+
this.name = name;
27+
this.age = age;
28+
this.gender = gender;
29+
this.email = email;
30+
this.hobbies = new ArrayList<>();
31+
}
32+
33+
public void addHobby(String hobby) throws HobbyLimitExceededException {
34+
if (hobbies.size() >= MAX_HOBBIES) {
35+
throw new HobbyLimitExceededException("Cannot add more than " + MAX_HOBBIES + " hobbies.");
36+
}
37+
hobbies.add(hobby);
38+
}
39+
40+
public ArrayList<String> getHobbies() {
41+
return hobbies;
42+
}
43+
44+
public boolean isAdult() {
45+
return age >= 18;
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public String getEmail() {
53+
return email;
54+
}
55+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.shawndunsmore;
2+
3+
public enum BuyType {
4+
BONUS_BUY,
5+
DOUBLE_CHANCE,
6+
NORMAL_BUY,
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codedifferently.lesson16.shawndunsmore;
2+
3+
public class InvalidPayAmountException extends Exception {
4+
5+
public InvalidPayAmountException(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.codedifferently.lesson16.shawndunsmore;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
6+
public class SlotMachine {
7+
private int numOfSlots;
8+
private int payAmount;
9+
private String name;
10+
private BuyType buyType;
11+
private ArrayList<String> iconList;
12+
private int moneyNeeded;
13+
14+
public SlotMachine(
15+
int numOfSlots,
16+
int payAmount,
17+
String name,
18+
BuyType buyType,
19+
ArrayList<String> iconList,
20+
int moneyNeeded) {
21+
this.numOfSlots = numOfSlots;
22+
this.payAmount = payAmount;
23+
this.name = name;
24+
this.buyType = buyType;
25+
this.iconList = iconList;
26+
this.moneyNeeded = moneyNeeded;
27+
}
28+
29+
public int getNumOfSlots() {
30+
return numOfSlots;
31+
}
32+
33+
public int getPayAmount() {
34+
return payAmount;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public BuyType getBuyType() {
42+
return buyType;
43+
}
44+
45+
public ArrayList<String> getIconList() {
46+
return iconList;
47+
}
48+
49+
public int payOut() {
50+
if (buyType.equals(BuyType.DOUBLE_CHANCE)) {
51+
return payAmount * 2;
52+
}
53+
54+
if (buyType.equals(BuyType.BONUS_BUY)) {
55+
return payAmount * 3;
56+
}
57+
return payAmount;
58+
}
59+
60+
public ArrayList<String> spin(int money) throws InvalidPayAmountException {
61+
if (money < moneyNeeded) {
62+
throw new InvalidPayAmountException("Amount inavalid");
63+
}
64+
Collections.shuffle(iconList);
65+
return iconList;
66+
}
67+
68+
public ArrayList<String> spin(int money, int numOfSpins) throws InvalidPayAmountException {
69+
if (money < moneyNeeded) {
70+
throw new InvalidPayAmountException("Amount inavalid");
71+
}
72+
for (int i = 0; i < numOfSpins; i++) {
73+
Collections.shuffle(iconList);
74+
}
75+
76+
return iconList;
77+
}
78+
79+
public int getMoneyNeeded() {
80+
return moneyNeeded;
81+
}
82+
83+
public void setNumOfSlots(int numOfSlots) {
84+
this.numOfSlots = numOfSlots;
85+
}
86+
87+
public void setPayAmount(int payAmount) {
88+
this.payAmount = payAmount;
89+
}
90+
91+
public void setName(String name) {
92+
this.name = name;
93+
}
94+
95+
public void setBuyType(BuyType buyType) {
96+
this.buyType = buyType;
97+
}
98+
99+
public void setIconList(ArrayList<String> iconList) {
100+
this.iconList = iconList;
101+
}
102+
103+
public void setMoneyNeeded(int moneyNeeded) {
104+
this.moneyNeeded = moneyNeeded;
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)