Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. package tom;
  2. import javax.swing.*;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Random;
  7. public class BettingManager {
  8. // This gets the variable
  9. private static JFrame jFrame = new JFrame();
  10. // JFrame displays a graphical window for the user
  11. private static List<bettingPlayer> bettingPlayers = new ArrayList<>();
  12. // This creates an Array list for the bettingPlayer class
  13. private static List<bet> bets = new ArrayList<>();
  14. // This creates an Array list for the bet class
  15. private static String[] options = {
  16. "Bet Under",
  17. "Bet Seven",
  18. "Bet Over",
  19. "Go back"};
  20. // This lists all the options for the available bets
  21. public static void main(String[] args)
  22. {
  23. openName();
  24.  
  25. }
  26.  
  27. private static void openName()
  28. {
  29. String name = JOptionPane.showInputDialog(jFrame, "Enter your name");
  30. // This displays the enter your name message
  31. bettingPlayer bettingPlayer = getBettingPlayer(name);
  32. // This gets the players name
  33. openOption(bettingPlayer);
  34. }
  35.  
  36. private static void openOption(bettingPlayer bettingPlayer)
  37. {
  38. int option = JOptionPane.showOptionDialog(jFrame, "Hello " + bettingPlayer.getName() + ". You have " + bettingPlayer.getTriesLeft() + " tries left and $" + bettingPlayer.getMoney() + ".",
  39. // This displays the message the main screen message that contains their name and how much money they have left and how many tries
  40. null,
  41. JOptionPane.YES_NO_CANCEL_OPTION,
  42. JOptionPane.QUESTION_MESSAGE,
  43. null,
  44. options,
  45. options[3]);
  46.  
  47. if (option == 3)
  48. {
  49. openName();
  50.  
  51. } else
  52. {
  53. openBetAmount(bettingPlayer, option);
  54. }
  55. }
  56.  
  57. private static void openBetAmount(bettingPlayer bettingPlayer, int option)
  58. {
  59. String value = JOptionPane.showInputDialog("How much do you want to bet?");
  60. // This displays the message asking the player how much do they want to bet
  61. if (value == null)
  62. {
  63. openOption(bettingPlayer);
  64. // End of if statement
  65. } else
  66. {
  67. int amount = Integer.parseInt(value);
  68.  
  69. if (amount > bettingPlayer.getMoney())
  70. // End of if statement
  71. //
  72. {
  73. JOptionPane.showMessageDialog(jFrame,
  74. "You only have $" + bettingPlayer.getMoney(),
  75. "Error",
  76. JOptionPane.ERROR_MESSAGE);
  77. openBetAmount(bettingPlayer, option);
  78. // End of if statement
  79. } else
  80. {
  81. runBet(bettingPlayer, option, amount);
  82. }
  83. }
  84. }
  85.  
  86.  
  87. private static void runBet(bettingPlayer bettingPlayer, int option, int amount)
  88. {
  89. bet bet = new bet(bettingPlayer.getName(), amount);
  90. bets.add(bet);
  91. // This will run a new bet
  92.  
  93. int roll = bet.rollDice();
  94. // This will roll the dice for the new bet
  95.  
  96. if (option == getConditionID(roll))
  97. {
  98. int amountWon = 0;
  99. if (option == 0)
  100. {
  101. // End of the if statement
  102. amountWon = bet.getBetAmount() * 2;
  103. // Times the amount won from the bet by 2
  104. } else if (option == 1)
  105. {
  106. // End of the if else statement
  107. amountWon = bet.getBetAmount() * 5;
  108. // Times the amount won from the bet by 5
  109. } else if (option == 2)
  110. {
  111. amountWon = bet.getBetAmount() * 2;
  112. // Times the amount won from the bet by 2
  113. }
  114. // End of the if else statement
  115. JOptionPane.showMessageDialog(jFrame,
  116. "Computer rolled " + roll + ". You won $" + amountWon,
  117. "Congratulations",
  118. JOptionPane.PLAIN_MESSAGE);
  119. bettingPlayer.setMoney(bettingPlayer.getMoney() + amount);
  120. } else
  121. {
  122. JOptionPane.showMessageDialog(jFrame,
  123. "Computer rolled " + roll + ". You lost!",
  124. "Better luck next time!",
  125. JOptionPane.PLAIN_MESSAGE);
  126. bettingPlayer.setMoney(bettingPlayer.getMoney() - bet.getBetAmount());
  127. }
  128. bettingPlayer.setTriesLeft(bettingPlayer.getTriesLeft() - 1);
  129. // End of else statement
  130. if (bettingPlayer.getTriesLeft() == 0)
  131. {
  132. JOptionPane.showMessageDialog(jFrame,
  133. "You have no more tries! You have a total of $" + bettingPlayer.getMoney(),
  134. "Finished",
  135. JOptionPane.PLAIN_MESSAGE);
  136. // End of if statement
  137. } else
  138. {
  139. openOption(bettingPlayer);
  140. }
  141. }
  142.  
  143.  
  144. private static bettingPlayer getBettingPlayer(String name)
  145. {
  146.  
  147. for (bettingPlayer bettingPlayer : bettingPlayers)
  148. {
  149. if (bettingPlayer.getName().equalsIgnoreCase(name))
  150. {
  151. return bettingPlayer;
  152. }
  153. }
  154.  
  155. bettingPlayer bettingPlayer = new bettingPlayer();
  156. bettingPlayers.add(bettingPlayer);
  157. return bettingPlayer;
  158. }
  159.  
  160. private static int getConditionID(int roll)
  161. {
  162. if (roll < 7)
  163. {
  164. // This is the if statement for if the roll is under 7
  165. return 0;
  166. } else if (roll == 7)
  167. // This is the if statement for if the roll is equal to 7
  168. {
  169. return 1;
  170. } else
  171. {
  172. return 2;
  173. }
  174. }
  175.  
  176. }
  177. // This is the end of the class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement