|
| 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 | +} |
0 commit comments