Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.52 KB | None | 0 0
  1. /*******************************************************************************************************************
  2. * Lab 4
  3. * Author: Jocelyn Smith
  4. * CIS 129 - Programming and Problem Solving I
  5. ******************************************************************************************************************/
  6. import java.util.*;
  7. import java.util.Scanner;
  8. public class CIS129_JocelynSmith_Lab4 {
  9.  
  10. static Scanner keyboard = new Scanner(System.in);
  11.  
  12.  
  13. //Declare global constants for winnings
  14. private static final int FIVE_NUMBERS_PLUS_POWERBALL = 100000000;
  15. private static final int FIVE_NUMBERS = 1000000;
  16. private static final int FOUR_NUMBERS_PLUS_POWERBALL = 50000;
  17. private static final int FOUR_NUMBERS = 100;
  18. private static final int THREE_NUMBERS_PLUS_POWERBALL = 100;
  19. private static final int THREE_NUMBERS = 7;
  20. private static final int TWO_NUMBERS_PLUS_POWERBALL = 7;
  21. private static final int TWO_NUMBERS = 4;
  22. private static final int ONE_NUMBER_PLUS_POWERBALL = 4;
  23. private static final int POWERBALL_ONLY = 4;
  24. //Declare global constant to be able to cheat
  25. private static final int WINNING_NUMBER = -267549;
  26. //Declare global constants for lottery ticket price and amount of starting money
  27. private static final int LOTTERY_TICKET_PRICE = 2;
  28. private static final int START_MONEY = 1000;
  29. //Declare global constant for size of array
  30. private static final int SIZE = 5;
  31. //Declare global constants for ranges of lottery ticket numbers
  32. private static final int LOW = 1;
  33. private static final int HIGH_LOTTERY = 69;
  34. private static final int HIGH_POWERBALL = 26;
  35.  
  36. //Main
  37. public static void main(String[] args) {
  38. //Declare variable to hold actual amount of money player has
  39. int ticketsSold = 1;
  40. int playerPowerballNumber = 0;
  41. int totalMoney = START_MONEY;
  42. //Display welcome message to explain how to win
  43. displayWelcomeMessage();
  44. //Display total amount of money user has to start with
  45. displayStartingTotal();
  46. //Prompt user for ticket sales and return amount
  47. ticketsSold = getTicketsSold(totalMoney);
  48. do {
  49. //Create array to hold winning lottery ticket numbers
  50. int [] winningLotteryNum = new int [SIZE];
  51. //Initialize winning lottery ticket array with unique values
  52. initializeArrayWithUniqueNumbers(winningLotteryNum);
  53. //Generate winning Powerball Number
  54. int winPowerballNumber = IR4.getRandomNumber(LOW, HIGH_POWERBALL);
  55. //Sort winning ticket array into ascending order
  56. sortWinningTicket(winningLotteryNum);
  57. //Display winning lottery tickets followed by winning Powerball Number
  58. displayWinningNumbers(winningLotteryNum, winPowerballNumber);
  59.  
  60. //Create array to hold player's lottery ticket numbers
  61. displayLotteryMsg();
  62. //For-loop to loop initializing and displaying tickets for amount of tickets sold
  63. for (int i = 0; i < ticketsSold; i++) {
  64. int[] playerLotteryNum = new int[SIZE];
  65. //Initialize player's ticket lottery with unique random numbers
  66. initializeTicketWithUniqueNumbers(playerLotteryNum);
  67. //Generate player's Powerball Number
  68. playerPowerballNumber = IR4.getRandomNumber(LOW, HIGH_POWERBALL);
  69. //If secret number is entered, make one ticket a winning ticket
  70. if (ticketsSold == WINNING_NUMBER) {
  71. for (int x = 0; x < winningLotteryNum.length; x++) {
  72. for (int y = 0; y < playerLotteryNum.length; y++) {
  73. playerLotteryNum[y] = winningLotteryNum[x];
  74. }
  75. }
  76. playerPowerballNumber = winPowerballNumber;
  77. }
  78. //Sort player's ticket into ascending order
  79. sortPlayerTicket(playerLotteryNum);
  80. //Display player's lottery tickets followed by winning Powerball Number and amount won
  81. displayPlayerTicket(winningLotteryNum, playerLotteryNum, playerPowerballNumber, winPowerballNumber);
  82. //Return count of matching lottery ticket numbers
  83. int count = getMatchingNumberAmount(winningLotteryNum, playerLotteryNum);
  84. //Return count of matching powerball numbers
  85. int pbCount = getMatchingPowerballAmount(playerPowerballNumber, winPowerballNumber);
  86. //Call function to determine amount of money won per ticket and adds to total money
  87. int tempSum = getWinningTicketAmount(count, pbCount);
  88. totalMoney = totalMoney + tempSum;
  89. } //loops through amount of tickets
  90. //Call function to calculate new total
  91. totalMoney = getNewTotal(totalMoney, ticketsSold);
  92. //Display new total to user and ask if they'd like to buy more tickets
  93. System.out.println("You have $" + totalMoney);
  94. ticketsSold = getTicketsSold(totalMoney);
  95.  
  96. } while (ticketsSold != 0); //exits program once player is done purchasing tickets
  97.  
  98. System.out.println("Goodbye!");
  99. }//end main
  100.  
  101. //Module to display welcome message with winnings
  102. private static void displayWelcomeMessage() {
  103. System.out.println("****************************************************************************************************");
  104. System.err.println(" Let's play Powerball! ");
  105. System.out.println("****************************************************************************************************");
  106. System.out.println("5 numbers correct plus powerball = $"+ String.format("%,d", FIVE_NUMBERS_PLUS_POWERBALL)); //add formatter to display commas with integer
  107. System.out.println("5 numbers correct, no powerball = $" + String.format("%,d", FIVE_NUMBERS));
  108. System.out.println("4 numbers correct plus powerball = $" + String.format("%,d", FOUR_NUMBERS_PLUS_POWERBALL));
  109. System.out.println("4 numbers correct, no powerball = $" + FOUR_NUMBERS);
  110. System.out.println("3 numbers correct plus powerball = $" + THREE_NUMBERS_PLUS_POWERBALL);
  111. System.out.println("3 numbers correct, no powerball = $" + THREE_NUMBERS);
  112. System.out.println("2 numbers correct plus powerball = $" + TWO_NUMBERS_PLUS_POWERBALL);
  113. System.out.println("2 numbers correct, no powerball = $" + TWO_NUMBERS);
  114. System.out.println("1 number correct plus powerball = $" + ONE_NUMBER_PLUS_POWERBALL);
  115. System.out.println("0 numbers correct plus powerball = $" + POWERBALL_ONLY);
  116. System.out.println("****************************************************************************************************");
  117. }//end module
  118.  
  119. //Module to display starting amount of money player has
  120. private static void displayStartingTotal() {
  121.  
  122. System.out.println("You have $" + START_MONEY);
  123. }//end module
  124.  
  125. //Function to sell tickets and decline purchases that surpass player's total money
  126. private static int getTicketsSold (int totalMoney) {
  127. int ticketsSold = IR4.getInteger ("How many $" + LOTTERY_TICKET_PRICE + " lottery cards do you want to purchase?");
  128. int ticketTotal = ticketsSold * LOTTERY_TICKET_PRICE;
  129. //While-statement to loop with error message when player tries to buy more tickets than they can afford
  130. if (ticketTotal > totalMoney) {
  131. System.err.println("You don't have enough money to purchase that many tickets! You have $" + totalMoney + ".");
  132. ticketsSold = IR4.getInteger("Please try again. How many $" + LOTTERY_TICKET_PRICE + " lottery cards do you want to purchase?");
  133. } else if (ticketsSold < 0 && ticketsSold != WINNING_NUMBER) {
  134. System.err.println("You can't buy negative tickets!");
  135. ticketsSold = IR4.getInteger("Please try again. How many $" + LOTTERY_TICKET_PRICE + " lottery cards do you want to purchase?");
  136. } else if (ticketsSold == WINNING_NUMBER) {
  137. System.out.println("Input accepted.");
  138. }
  139. //Return total amount of money sold on tickets and amount of tickets sold
  140. return ticketsSold;
  141. }//end function
  142.  
  143. //Initialize winning ticket array
  144. private static void initializeArrayWithUniqueNumbers (int [] winningLotteryNum) {
  145. for (int x = 0; x < winningLotteryNum.length; x++) {
  146. winningLotteryNum[x] = getUniqueRandomNumber(winningLotteryNum);
  147. }
  148. }//end module
  149.  
  150. //Function to check if number is unique before adding to winning lottery ticket
  151. public static int getUniqueRandomNumber (int [] winningLotteryNum) {
  152. int randomNbr = IR4.getRandomNumber(LOW, HIGH_LOTTERY);
  153. while (isDuplicate(winningLotteryNum, randomNbr)) {
  154. randomNbr = IR4.getRandomNumber(LOW, HIGH_LOTTERY);
  155. }
  156. return randomNbr; //return unique number
  157. }//end function
  158.  
  159. //Create boolean to check if random number is unique to lottery ticket
  160. public static boolean isDuplicate(int [] winningLotteryNum, int valueToCheck){
  161. for (int x = 0; x < winningLotteryNum.length; x++) {
  162. if (valueToCheck == winningLotteryNum[x]) {
  163. return true; //it's a duplicate!
  164. }
  165. }
  166. return false; //the number is unique!
  167. }//end boolean
  168.  
  169. //Module to sort winning ticket in ascending order
  170. private static void sortWinningTicket (int [] winningLotteryNum) {
  171. int tempNum; //variable to hold number while being compared
  172. //Create for-loop to loop and sort through all of the winning lottery ticket numbers until they've all been compared
  173. for (int i = 0; i < winningLotteryNum.length; i++) {
  174. for (int j = i + 1; j < winningLotteryNum.length; j++) {
  175. if (winningLotteryNum[i] > winningLotteryNum[j]) {
  176. tempNum = winningLotteryNum[i];
  177. winningLotteryNum[i] = winningLotteryNum[j];
  178. winningLotteryNum[j] = tempNum;
  179. }
  180. }
  181. }
  182. }//end module
  183.  
  184. //Module to display winning lottery tickets followed by Powerball Number
  185. private static void displayWinningNumbers(int [] winningLotteryNum, int winPowerballNumber) {
  186. System.out.println("The winning lottery numbers, followed by the Powerball Number, are:");
  187. for (int i = 0; i < winningLotteryNum.length; i++) {
  188. System.out.print(winningLotteryNum[i] + " ");
  189. }
  190. System.err.print(winPowerballNumber);
  191. }//end module
  192.  
  193.  
  194. //Initialize player ticket array with random unique values
  195. private static void initializeTicketWithUniqueNumbers (int [] playerLotteryNum) {
  196. for (int x = 0; x < playerLotteryNum.length; x++) {
  197. playerLotteryNum[x] = getUniqueRandomTicket(playerLotteryNum);
  198. }
  199. }//end module
  200.  
  201. //Function to check if number is unique before adding to player lottery ticket
  202. public static int getUniqueRandomTicket (int [] playerLotteryNum) {
  203. int randomNbr = IR4.getRandomNumber(LOW, HIGH_LOTTERY);
  204. while (isDuplicateTicket(playerLotteryNum, randomNbr)) {
  205. randomNbr = IR4.getRandomNumber(LOW, HIGH_LOTTERY);
  206. }
  207. return randomNbr; //return unique number
  208. }//end function
  209.  
  210. //Create boolean to check if random number is unique to player's lottery ticket
  211. public static boolean isDuplicateTicket(int [] playerLotteryNum, int valueToCheck){
  212. for (int x = 0; x < playerLotteryNum.length; x++) {
  213. if (valueToCheck == playerLotteryNum[x]) {
  214. return true; //it's a duplicate!
  215. }
  216. }
  217. return false; //the number is unique!
  218. }//end boolean
  219.  
  220. //Module to sort player's ticket in ascending order
  221. private static void sortPlayerTicket (int [] playerLotteryNum) {
  222. int tempNum; //variable to hold number while being compared
  223. //Create for-loop to loop and sort through all of the winning lottery ticket numbers until they've all been compared
  224. for (int i = 0; i < playerLotteryNum.length; i++) {
  225. for (int j = i + 1; j < playerLotteryNum.length; j++) {
  226. if (playerLotteryNum[i] > playerLotteryNum[j]) {
  227. tempNum = playerLotteryNum[i];
  228. playerLotteryNum[i] = playerLotteryNum[j];
  229. playerLotteryNum[j] = tempNum;
  230. }
  231. }
  232. }
  233. }//end module
  234.  
  235. //Module to display winning lottery tickets followed by Powerball Number
  236. private static void displayPlayerTicket(int [] winningLotteryNum, int[] playerLotteryNum, int playerPowerballNumber, int winPowerballNumber) {
  237. int count = 0;
  238. int pbCount = 0;
  239. for (int i = 0; i < playerLotteryNum.length; i++){
  240. for (int j = 0; j < winningLotteryNum.length; j++){
  241. if (playerLotteryNum[i] == winningLotteryNum[j]) {
  242. System.err.print(playerLotteryNum[i] + " ");
  243. j = winningLotteryNum.length + 1; // exit loop once matching number is found
  244. count++;
  245. }
  246. else if (j == winningLotteryNum.length - 1) {
  247. System.out.print(playerLotteryNum[i] + " ");
  248. }
  249. }
  250. }
  251. if (playerPowerballNumber == winPowerballNumber) {
  252. System.err.print(playerPowerballNumber + " ");
  253. pbCount++;
  254. }
  255. else {
  256. System.out.print(playerPowerballNumber + " ");
  257.  
  258.  
  259. } //If-else statements to display amount of money won
  260. if (count == 0 && pbCount == 1) {
  261. System.out.print("You won $" + POWERBALL_ONLY + "!");
  262. } else if (count == 1 && pbCount == 1) {
  263. System.out.print("You won $" + ONE_NUMBER_PLUS_POWERBALL + "!");
  264. } else if (count == 2 && pbCount == 1) {
  265. System.out.print("You won $" + TWO_NUMBERS_PLUS_POWERBALL + "!");
  266. } else if (count == 3 && pbCount == 0) {
  267. System.out.print("You won $" + THREE_NUMBERS + "!");
  268. } else if (count == 3 && pbCount == 1) {
  269. System.out.print("You won $" + THREE_NUMBERS_PLUS_POWERBALL + "!");
  270. } else if (count == 4 && pbCount == 0) {
  271. System.out.print("You won $" + FOUR_NUMBERS + "!");
  272. } else if (count == 4 && pbCount == 1) {
  273. System.out.print("You won $" + String.format("%,d", FOUR_NUMBERS_PLUS_POWERBALL + "!"));
  274. } else if (count == 5 && pbCount == 0) {
  275. System.out.print("You won $" + String.format("%,d", FIVE_NUMBERS + "!"));
  276. } else if (count == 5 && pbCount == 1) {
  277. System.out.print("You won $" + String.format("%,d", FIVE_NUMBERS_PLUS_POWERBALL + "!"));
  278. }//end if
  279. System.out.println("");
  280. }//end module
  281.  
  282.  
  283. //Module to display message before player's ticket numbers
  284. private static void displayLotteryMsg() {
  285. System.out.println("");
  286. System.out.println("Your lottery numbers, followed by the Powerball Number, are: ");
  287. }//end module
  288.  
  289. //Create function to count and return number of matching lottery ticket numbers
  290. private static int getMatchingNumberAmount(int [] winningLotteryNum, int[] playerLotteryNum) {
  291. int count = 0;
  292. for (int i = 0; i < playerLotteryNum.length; i++){
  293. for (int j = 0; j < winningLotteryNum.length; j++){
  294. if (playerLotteryNum[i] == winningLotteryNum[j]) {
  295. count++;
  296. }
  297. }
  298. }
  299. return count;
  300. }//end function
  301.  
  302. //Create function to count and return number of matching powerball tickets
  303. private static int getMatchingPowerballAmount(int playerPowerballNumber, int winPowerballNumber) {
  304. int pbCount = 0;
  305. if (playerPowerballNumber == winPowerballNumber) {
  306. pbCount++;
  307. }
  308. return pbCount;
  309. }//end function
  310.  
  311. //Create function to return value of winning ticket
  312. private static int getWinningTicketAmount(int count, int pbCount) {
  313. int sum = 0;
  314. //Give new value to sum when right parameters are met
  315. if (count == 0 && pbCount == 1) {
  316. sum = POWERBALL_ONLY;
  317. } else if (count == 1 && pbCount == 1) {
  318. sum = ONE_NUMBER_PLUS_POWERBALL;
  319. } else if (count == 2 && pbCount == 1) {
  320. sum = TWO_NUMBERS_PLUS_POWERBALL;
  321. } else if (count == 3 && pbCount == 0) {
  322. sum = THREE_NUMBERS;
  323. } else if (count == 3 && pbCount == 1) {
  324. sum = THREE_NUMBERS_PLUS_POWERBALL;
  325. } else if (count == 4 && pbCount == 0) {
  326. sum = FOUR_NUMBERS;
  327. } else if (count == 4 && pbCount == 1) {
  328. sum = FOUR_NUMBERS_PLUS_POWERBALL;
  329. } else if (count == 5 && pbCount == 0) {
  330. sum = FIVE_NUMBERS;
  331. } else if (count == 5 && pbCount == 1) {
  332. sum = FIVE_NUMBERS_PLUS_POWERBALL;
  333. } //end if
  334. return sum;
  335. }//end function
  336.  
  337. //Function to calculate and return player's new total amount of money
  338. private static int getNewTotal(int totalMoney, int ticketsSold) {
  339. int newTotal = totalMoney - (ticketsSold * LOTTERY_TICKET_PRICE);
  340. return newTotal;
  341. }//end function
  342.  
  343. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement