Skip to content

Commit 9b9a2a0

Browse files
authored
Merge branch 'code-differently:main' into feat/lesson17
2 parents e83db46 + 319a77c commit 9b9a2a0

File tree

67 files changed

+11683
-5592
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+11683
-5592
lines changed
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
package com.codedifferently.lesson16.deanwalston;
2+
3+
public class CasinoDean {
4+
private final String casinoName = "Casa Dean Casino";
5+
private boolean isOpen = true;
6+
private long annualVisitors = 2_500_000L;
7+
private final float averageBet = 25.5f;
8+
private int playerId;
9+
private double casinoRevenue;
10+
private GameTypes currentGameType;
11+
private double playerBet;
12+
private String[] players;
13+
14+
public enum GameTypes {
15+
POKER,
16+
BLACKJACK,
17+
ROULETTE,
18+
SLOTS,
19+
BACCARAT,
20+
SPADES,
21+
UNO
22+
}
23+
24+
public CasinoDean(GameTypes gameType) {
25+
this.currentGameType = gameType;
26+
}
27+
28+
public CasinoDean(
29+
int playerId,
30+
String[] players,
31+
double casinoRevenue,
32+
boolean isOpen,
33+
GameTypes gameType,
34+
long annualVisitors) {
35+
this.playerId = playerId;
36+
this.players = players;
37+
this.currentGameType = gameType;
38+
this.playerBet = 0.0; // Default 0 bet
39+
this.casinoRevenue = casinoRevenue;
40+
this.isOpen = isOpen;
41+
this.annualVisitors = annualVisitors;
42+
}
43+
44+
public String getCasinoName() {
45+
return casinoName;
46+
}
47+
48+
public boolean isOpen() {
49+
return isOpen;
50+
}
51+
52+
public void setOpen(boolean open) {
53+
isOpen = open;
54+
}
55+
56+
public long getAnnualVisitors() {
57+
return annualVisitors;
58+
}
59+
60+
public void setAnnualVisitors(long annualVisitors) {
61+
this.annualVisitors = annualVisitors;
62+
}
63+
64+
public float getAverageBet() {
65+
return averageBet;
66+
}
67+
68+
public int getPlayerId() {
69+
return playerId;
70+
}
71+
72+
public void setPlayerId(int playerId) {
73+
this.playerId = playerId;
74+
}
75+
76+
public double getCasinoRevenue() {
77+
return casinoRevenue;
78+
}
79+
80+
public void setCasinoRevenue(double casinoRevenue) {
81+
this.casinoRevenue = casinoRevenue;
82+
}
83+
84+
public GameTypes getCurrentGameType() {
85+
return currentGameType;
86+
}
87+
88+
public void setCurrentGameType(GameTypes currentGameType) {
89+
this.currentGameType = currentGameType;
90+
}
91+
92+
public double getPlayerBet() {
93+
return playerBet;
94+
}
95+
96+
public void setPlayerBet(double playerBet) {
97+
this.playerBet = playerBet;
98+
}
99+
100+
public String[] getPlayers() {
101+
return players;
102+
}
103+
104+
public void setPlayers(String[] players) {
105+
this.players = players;
106+
}
107+
108+
/**
109+
* Places a bet for a player
110+
*
111+
* @param playerId The ID of the player
112+
* @param amount The amount to bet
113+
* @return Whether the bet was successful
114+
* @throws CasinoException If the casino is closed, player ID is invalid, or bet amount is invalid
115+
*/
116+
public boolean placeBet(int playerId, double amount) throws CasinoException {
117+
if (!isOpen) {
118+
throw new CasinoException("Casino is closed. Cannot place bets.");
119+
}
120+
if (players == null) {
121+
throw new CasinoException("No player data available for processing bets");
122+
}
123+
if (players.length == 0) {
124+
throw new CasinoException("Player list is empty. Cannot place bets.");
125+
}
126+
if (playerId < 0 || playerId >= players.length) {
127+
throw new CasinoException(
128+
"Invalid player ID: " + playerId + ". Must be between 0 and " + (players.length - 1));
129+
}
130+
if (amount <= 0) {
131+
throw new CasinoException("Invalid bet amount: must be greater than $0.00");
132+
}
133+
if (amount > 10000) {
134+
throw new CasinoException("Invalid bet amount: exceeds maximum allowed ($10,000)");
135+
}
136+
this.playerId = playerId;
137+
this.playerBet = amount;
138+
// Calculate house take (5%)
139+
double houseTake = amount * 0.05;
140+
this.casinoRevenue += houseTake;
141+
return true;
142+
}
143+
144+
/**
145+
* Finds popular games from a list based on a minimum popularity threshold
146+
*
147+
* @param minPopularity Minimum popularity threshold (0-100)
148+
* @return Array of available game types
149+
* @throws CasinoException If popularity threshold is invalid or if game type is null
150+
*/
151+
public GameTypes[] getAvailableGames(int minPopularity) throws CasinoException {
152+
if (minPopularity < 0 || minPopularity > 100) {
153+
throw new CasinoException("Popularity threshold must be between 0 and 100");
154+
}
155+
GameTypes[] allGames = GameTypes.values();
156+
if (allGames.length == 0) {
157+
throw new CasinoException("No game types available");
158+
}
159+
// First count the games that meet the popularity threshold
160+
int count = 0;
161+
try {
162+
for (GameTypes game : allGames) {
163+
if (getGamePopularity(game) >= minPopularity) {
164+
count++;
165+
}
166+
}
167+
if (count == 0) {
168+
throw new CasinoException(
169+
"No games meet the minimum popularity threshold of " + minPopularity);
170+
}
171+
// Create and populate the result array
172+
GameTypes[] availableGames = new GameTypes[count];
173+
int index = 0;
174+
for (GameTypes game : allGames) {
175+
if (getGamePopularity(game) >= minPopularity) {
176+
availableGames[index++] = game;
177+
}
178+
}
179+
return availableGames;
180+
} catch (CasinoException e) {
181+
throw new CasinoException("Error calculating game popularity: " + e.getMessage());
182+
}
183+
}
184+
185+
/**
186+
* Calculate total revenue from all players
187+
*
188+
* @return Total revenue
189+
* @throws CasinoException If player data is not available
190+
*/
191+
public double calculateTotalRevenue() throws CasinoException {
192+
if (players == null) {
193+
throw new CasinoException("Players data is not initialized");
194+
}
195+
if (players.length == 0) {
196+
throw new CasinoException("No players available to calculate revenue");
197+
}
198+
double baseRevenue = casinoRevenue;
199+
double playerRevenue = calculatePlayerBasedRevenue();
200+
return baseRevenue + playerRevenue;
201+
}
202+
203+
/**
204+
* Helper method to calculate revenue from player activity
205+
*
206+
* @return The calculated player-based revenue
207+
*/
208+
private double calculatePlayerBasedRevenue() {
209+
double totalPlayerRevenue = 0;
210+
for (int i = 0; i < players.length; i++) {
211+
// Realistic revenue model based on player index
212+
// Higher index players contribute more revenue
213+
double baseContribution = 100.0;
214+
double variableContribution = i * 50.0;
215+
if (players[i] != null) {
216+
double playerContribution = baseContribution + variableContribution;
217+
totalPlayerRevenue += playerContribution;
218+
}
219+
}
220+
return totalPlayerRevenue;
221+
}
222+
223+
/**
224+
* Helper method to calculate game popularity (for demonstration)
225+
*
226+
* @param gameType The type of game
227+
* @return Popularity score (0-100)
228+
* @throws CasinoException If gameType is null
229+
*/
230+
private int getGamePopularity(GameTypes gameType) throws CasinoException {
231+
if (gameType == null) {
232+
throw new CasinoException("Cannot calculate popularity for null game type");
233+
}
234+
// assign popularity based on ordinal and name length
235+
return ((gameType.ordinal() * 15) + (gameType.name().length() * 5)) % 100;
236+
}
237+
238+
/** Custom checked exception for casino operations that requires explicit handling */
239+
public class CasinoException extends Exception {
240+
/**
241+
* @param Message
242+
*/
243+
public CasinoException(String message) {
244+
super(message);
245+
}
246+
247+
/**
248+
* Constructs a new CasinoException with the specified detail message and cause
249+
*
250+
* @param message The detailed error message
251+
* @param cause The cause of this exception
252+
*/
253+
public CasinoException(String message, Throwable cause) {
254+
super(message, cause);
255+
}
256+
}
257+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.codedifferently.lesson16.devynbenson;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/** Represents an NBA Basketball with player statistics and game data */
7+
public class Basketball {
8+
private String brand;
9+
private double circumference;
10+
private int gamesPlayed;
11+
private boolean isOfficialSize;
12+
private List<String> playersUsed;
13+
private Position primaryPosition;
14+
15+
public Basketball(String brand, double circumference, Position primaryPosition)
16+
throws InvalidStatException {
17+
if (circumference <= 0) {
18+
throw new InvalidStatException("Circumference must be positive");
19+
}
20+
this.brand = brand;
21+
this.circumference = circumference;
22+
this.primaryPosition = primaryPosition;
23+
this.gamesPlayed = 0;
24+
this.isOfficialSize = (circumference >= 29.5 && circumference <= 30.0);
25+
this.playersUsed = new ArrayList<>();
26+
}
27+
28+
public String getPerformanceRating() {
29+
return gamesPlayed > 50 ? "High Usage" : gamesPlayed > 20 ? "Medium Usage" : "Low Usage";
30+
}
31+
32+
public void addPlayerUsage(String playerName) throws InvalidStatException {
33+
if (playerName == null || playerName.trim().isEmpty()) {
34+
throw new InvalidStatException("Player name cannot be null or empty");
35+
}
36+
if (!playersUsed.contains(playerName)) {
37+
playersUsed.add(playerName);
38+
}
39+
}
40+
41+
public String getPlayersReport() {
42+
StringBuilder report = new StringBuilder("Players who used this basketball:\n");
43+
for (int i = 0; i < playersUsed.size(); i++) {
44+
report.append((i + 1)).append(". ").append(playersUsed.get(i)).append("\n");
45+
}
46+
return report.toString();
47+
}
48+
49+
public void incrementGamesPlayed() throws InvalidStatException {
50+
if (gamesPlayed >= 1000) {
51+
throw new InvalidStatException("Basketball has reached maximum game limit");
52+
}
53+
gamesPlayed++;
54+
}
55+
56+
public int getPlayerCount() {
57+
return playersUsed.size();
58+
}
59+
60+
public boolean hasBeenUsedByPlayer(String playerName) {
61+
return playersUsed.contains(playerName);
62+
}
63+
64+
public String getBrand() {
65+
return brand;
66+
}
67+
68+
public void setBrand(String brand) {
69+
this.brand = brand;
70+
}
71+
72+
public double getCircumference() {
73+
return circumference;
74+
}
75+
76+
public void setCircumference(double circumference) throws InvalidStatException {
77+
if (circumference <= 0) {
78+
throw new InvalidStatException("Circumference must be positive");
79+
}
80+
this.circumference = circumference;
81+
this.isOfficialSize = (circumference >= 29.5 && circumference <= 30.0);
82+
}
83+
84+
public int getGamesPlayed() {
85+
return gamesPlayed;
86+
}
87+
88+
public boolean isOfficialSize() {
89+
return isOfficialSize;
90+
}
91+
92+
public List<String> getPlayersUsed() {
93+
return new ArrayList<>(playersUsed);
94+
}
95+
96+
public Position getPrimaryPosition() {
97+
return primaryPosition;
98+
}
99+
100+
public void setPrimaryPosition(Position primaryPosition) {
101+
this.primaryPosition = primaryPosition;
102+
}
103+
104+
@Override
105+
public String toString() {
106+
return String.format(
107+
"Basketball{brand='%s', circumference=%.1f, gamesPlayed=%d, "
108+
+ "isOfficialSize=%s, primaryPosition=%s, playersUsed=%d}",
109+
brand,
110+
circumference,
111+
gamesPlayed,
112+
isOfficialSize,
113+
primaryPosition.getFullName(),
114+
playersUsed.size());
115+
}
116+
}

0 commit comments

Comments
 (0)