Advertisement
shadowsofme

Untitled

Mar 24th, 2014
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.67 KB | None | 0 0
  1. import java.util.*;
  2. import javax.swing.*;
  3. import java.io.*;
  4. import java.text.*;
  5. import java.lang.*;
  6.  
  7. /*************************************************************
  8. * Simulates a Mega Millions Lottery game.
  9. *
  10. * @author Matt Schuch
  11. * @version 3/12/14
  12. ************************************************************/
  13. public class LotteryMachine
  14. {
  15. //--------------------------------------------------------------------------
  16. // Creates the ArrayLists and instances variables needed for the program.
  17. //--------------------------------------------------------------------------
  18. public ArrayList <LotteryTicket> tickets;
  19. private ArrayList <LotteryTicket> majorWinners;
  20. private ArrayList <LotteryTicket> majorStateWinners;
  21. private ArrayList <Integer> numbers;
  22. private ArrayList <Integer> rolls;
  23. public int roll1, roll2, roll3, roll4, roll5, megaRoll, matches, oldYear = 2014, oldMonth = 12, oldDay = 31;
  24. private double jackpot = 5000000.00, highWin = -1.00, highStateWin = -1.00;
  25. private boolean jackpotWon = false;
  26. public String jackpotResults;
  27. private LotteryTicket oldest, stateHighest = null, highest = null, jackpotWinner;
  28.  
  29. //------------------------------------------------------------------
  30. // Creates the random number generator needed for the program.
  31. //------------------------------------------------------------------
  32. Random rand = new Random();
  33.  
  34. //------------------------------------------------------------------
  35. // Creates the currency format for the program to use.
  36. //------------------------------------------------------------------
  37. NumberFormat fmt = NumberFormat.getCurrencyInstance();
  38.  
  39. //------------------------------------------------------------------
  40. // Instantiates the LotteryTicket arraylist.
  41. //------------------------------------------------------------------
  42. public LotteryMachine(){
  43. tickets = new ArrayList <LotteryTicket> ();
  44. }
  45.  
  46. //------------------------------------------------------------------
  47. // Fills the number array list with numbers from 1-75.
  48. //------------------------------------------------------------------
  49. private void fillNumberArray(){
  50. numbers = new ArrayList <Integer> ();
  51.  
  52. for (int i = 1; i <=75; i++){
  53. numbers.add(i);
  54. }
  55. }
  56.  
  57. //------------------------------------------------------------------
  58. // Adds a custom ticket to the LotteryTicket ArrayList.
  59. //------------------------------------------------------------------
  60. public void addTicket (LotteryTicket t){
  61. tickets.add(t);
  62. }
  63.  
  64. //------------------------------------------------------------------
  65. // Returns the number of tickets in the LottertTicket ArrayList.
  66. //------------------------------------------------------------------
  67. public int countTickets(){
  68. return tickets.size();
  69. }
  70.  
  71. //------------------------------------------------------------------
  72. // Returns the number of tickets bought in a specific state in the
  73. // LottertyTicket ArrayList (used specifically for creating reports
  74. // for specific states.
  75. //------------------------------------------------------------------
  76. public int countStateTickets(String st){
  77. int stateTickets = 0;
  78.  
  79. // Checks to see if the ticket was filed in the given state.
  80. for(LotteryTicket t: tickets){
  81. // If so, the counter for how many tickets were bought in
  82. // the entered state is raised by 1.
  83. if (t.getState().equalsIgnoreCase(st) == true){
  84. stateTickets++;
  85. }
  86. }
  87.  
  88. return stateTickets;
  89. }
  90.  
  91. //------------------------------------------------------------------
  92. // Selects the numbers for a drawing.
  93. //------------------------------------------------------------------
  94. public void pickNumbers(){
  95. // Creates the number array and fills it values from 1 to 75.
  96. fillNumberArray();
  97.  
  98. // Assigns each ball from 1 to 75, and removes each value so it cannot
  99. // be selected again.
  100. roll1 = numbers.remove(rand.nextInt(numbers.size()));
  101. roll2 = numbers.remove(rand.nextInt(numbers.size()));
  102. roll3 = numbers.remove(rand.nextInt(numbers.size()));
  103. roll4 = numbers.remove(rand.nextInt(numbers.size()));
  104. roll5 = numbers.remove(rand.nextInt(numbers.size()));
  105.  
  106. // Assigns the Mega Ball a value from 1 to 15.
  107. megaRoll = rand.nextInt(15) + 1;
  108.  
  109. // Orders the rolled numbers in numerical order.
  110. orderRolls();
  111. }
  112.  
  113. //------------------------------------------------------------------
  114. // Orders the rolled numbers in numerical order.
  115. //------------------------------------------------------------------
  116. private void orderRolls(){
  117. // Instantiates the ArrayList for the rolls.
  118. rolls = new ArrayList <Integer> ();
  119.  
  120. // Adds the first roll to the ArrayList.
  121. rolls.add(roll1);
  122.  
  123. // Places the second roll in it's numerical spot in the ArrayList.
  124. if (roll2 < rolls.get(0)){
  125. rolls.add(0, roll2);
  126. }else{
  127. rolls.add(roll2);
  128. }
  129. // Places the third roll in it's numerical spot in the ArrayList.
  130. if (roll3 < rolls.get(0)){
  131. rolls.add(0, roll3);
  132. }else if (roll3 < rolls.get(1)){
  133. rolls.add(1, roll3);
  134. }else{
  135. rolls.add(roll3);
  136. }
  137. // Places the fourth roll in it's numerical spot in the ArrayList.
  138. if (roll4 < rolls.get(0)){
  139. rolls.add(0, roll4);
  140. }else if (roll4 < rolls.get(1)){
  141. rolls.add(1, roll4);
  142. }else if (roll4 < rolls.get(2)){
  143. rolls.add(2, roll4);
  144. }else{
  145. rolls.add(roll4);
  146. }
  147. // Places the fifth roll in it's numerical spot in the ArrayList.
  148. if (roll5 < rolls.get(0)){
  149. rolls.add(0, roll5);
  150. }else if (roll5 < rolls.get(1)){
  151. rolls.add(1, roll5);
  152. }else if (roll5 < rolls.get(2)){
  153. rolls.add(2, roll5);
  154. }else if (roll5 < rolls.get(3)){
  155. rolls.add(3, roll5);
  156. }else{
  157. rolls.add(roll5);
  158. }
  159. }
  160.  
  161. //------------------------------------------------------------------
  162. // Counts the number of matches for a given LotteryTicket.
  163. //------------------------------------------------------------------
  164. private int countMatches (LotteryTicket t){
  165. matches = 0;
  166.  
  167. // If the first roll matches any of balls selected by the ticket,
  168. // a match is recorded.
  169. if (t.ball1 == roll1 || t.ball2 == roll1 || t.ball3 == roll1 || t.ball4 == roll1 || t.ball5 == roll1){
  170. matches++;
  171. }
  172.  
  173. // If the second roll matches any of balls selected by the ticket,
  174. // a match is recorded.
  175. if (t.ball1 == roll2 || t.ball2 == roll2 || t.ball3 == roll2 || t.ball4 == roll2 || t.ball5 == roll2){
  176. matches++;
  177. }
  178.  
  179. // If the third roll matches any of balls selected by the ticket,
  180. // a match is recorded.
  181. if (t.ball1 == roll3 || t.ball2 == roll3 || t.ball3 == roll3 || t.ball4 == roll3 || t.ball5 == roll3){
  182. matches++;
  183. }
  184.  
  185. // If the fourth roll matches any of balls selected by the ticket,
  186. // a match is recorded.
  187. if (t.ball1 == roll4 || t.ball2 == roll4 || t.ball3 == roll4 || t.ball4 == roll4 || t.ball5 == roll4){
  188. matches++;
  189. }
  190.  
  191. // If the fifth roll matches any of balls selected by the ticket,
  192. // a match is recorded.
  193. if (t.ball1 == roll5 || t.ball2 == roll5 || t.ball3 == roll5 || t.ball4 == roll5 || t.ball5 == roll5){
  194. matches++;
  195. }
  196.  
  197. // Returns the number of matches the ticket had.
  198. return matches;
  199. }
  200.  
  201. //---------------------------------------------------------------------
  202. // Distributes the correct prizes to each ticket in the LotteryTicket
  203. // ArrayList.
  204. //---------------------------------------------------------------------
  205. public void makePayouts(){
  206. for(LotteryTicket t: tickets){
  207. // Counts the number of matches for the ticket.
  208. countMatches(t);
  209.  
  210. // If only the Mega Ball matches, the ticket wins $1.00.
  211. if (matches == 0 && t.megaBall == megaRoll){
  212. t.prize = 1.00;
  213. }
  214.  
  215. // If one ball and the Mega Ball matches, the ticket wins $2.00.
  216. if (matches == 1 && t.megaBall == megaRoll){
  217. t.prize = 2.00;
  218. }
  219.  
  220. // If two balls and the Mega Ball matches, the ticket wins $5.00.
  221. if (matches == 2 && t.megaBall == megaRoll){
  222. t.prize = 5.00;
  223. }
  224.  
  225. // If three balls match but the Mega Ball does not, the ticket wins $5.00.
  226. if (matches == 3 && t.megaBall != megaRoll){
  227. t.prize = 5.00;
  228. }
  229.  
  230. // If three balls and the Mega Ball matches, the ticket wins $50.00.
  231. if (matches == 3 && t.megaBall == megaRoll){
  232. t.prize = 50.00;
  233. }
  234.  
  235. // If four balls match but the Mega Ball does not, the ticket wins $500.00.
  236. if (matches == 4 && t.megaBall != megaRoll){
  237. t.prize = 500.00;
  238. }
  239.  
  240. // If four balls and the Mega Ball matches, the ticket wins $5,000.00.
  241. if (matches == 4 && t.megaBall == megaRoll){
  242. t.prize = 5000.00;
  243. }
  244.  
  245. // If every ball matches but the Mega Ball does not, the ticket wins $1,000,000.00.
  246. if (matches == 5 && t.megaBall != megaRoll){
  247. t.prize = 1000000.00;
  248. }
  249.  
  250. // If every ball including the Mega Ball matches, the ticket wins the jackpot (normally $5,000,000.00).
  251. if (matches == 5 && t.megaBall == megaRoll){
  252. t.prize = jackpot;
  253. }
  254. }
  255. }
  256.  
  257. //-------------------------------------------------------------------------
  258. // Calculates the average amount won by all participants for this drawing.
  259. //-------------------------------------------------------------------------
  260. private double averagePrize(){
  261. // Creates the variable for the average prize.
  262. double average = 0.00;
  263.  
  264. // Calculates the average prize won by all tickets.
  265. for(LotteryTicket t: tickets){
  266. average = average + t.getPrize();
  267. }
  268. average = (average / tickets.size());
  269.  
  270. // Returns the average prize won by all tickets.
  271. return average;
  272. }
  273.  
  274. //--------------------------------------------------------------------------
  275. // Calculates the average amount won by all participants from a given state
  276. // for this drawing.
  277. //--------------------------------------------------------------------------
  278. private double averageStatePrize(String st){
  279. // Creates the variables for the average prize and the
  280. // number of tickets for a state found.
  281. double stateAverage = 0.00;
  282. int stateCount = 0;
  283.  
  284. // Calculates the average prize won by all tickets in the given state.
  285. // If no tickets were entered for a given state, the average is set to zero.
  286. for(LotteryTicket t: tickets){
  287. if (t.getState().equalsIgnoreCase(st) == true){
  288. stateAverage = stateAverage + t.getPrize();
  289. stateCount++;
  290. }
  291. }
  292. if (stateCount > 0){
  293. stateAverage = (stateAverage / stateCount);
  294. } else{
  295. stateAverage = 0.00;
  296. }
  297.  
  298. // Returns the average prize won by all tickets in the given state.
  299. return stateAverage;
  300. }
  301.  
  302. //--------------------------------------------------------------------
  303. // Returns the rolled numbers for this drawing in a formatted string.
  304. //--------------------------------------------------------------------
  305. private String formatNumbers(){
  306. orderRolls();
  307. String numberSummary = "Selected Numbers: " + rolls.get(0) + " " + rolls.get(1) + " " + rolls.get(2);
  308. numberSummary = numberSummary + " " + rolls.get(3) + " " + rolls.get(4) + " " + megaRoll;
  309. return numberSummary;
  310. }
  311.  
  312. //----------------------------------------------------------------------------
  313. // Simulates one game, drawing numbers and making payouts to each participant.
  314. //----------------------------------------------------------------------------
  315. public void drawTicket(){
  316. pickNumbers();
  317. makePayouts();
  318. }
  319.  
  320. //------------------------------------------------------------------
  321. // Simulates one game with user-chosen nubmers, then making payouts
  322. // to each paticipant.
  323. //------------------------------------------------------------------
  324. public void drawTicket(int b1, int b2, int b3, int b4, int b5, int m){
  325. roll1 = b1;
  326. roll2 = b2;
  327. roll3 = b3;
  328. roll4 = b4;
  329. roll5 = b5;
  330. megaRoll = m;
  331.  
  332. makePayouts();
  333. }
  334.  
  335. //------------------------------------------------------------------
  336. // Allows the user to add their own file of LotteryTicket values
  337. // to the ArrayList.
  338. //------------------------------------------------------------------
  339. public void readTickets(String filename){
  340. tickets = new ArrayList <LotteryTicket> ();
  341.  
  342. try{
  343. File f = new File(filename);
  344. Scanner sc = new Scanner(f);
  345. String info;
  346.  
  347. while(sc.hasNext()) {
  348. info = sc.nextLine();
  349.  
  350. LotteryTicket t = new LotteryTicket(info);
  351. tickets.add(t);
  352. }
  353. sc.close();
  354. }
  355. catch(IOException e){
  356. System.out.println("Failed to read the data file: " + filename);
  357. }
  358. }
  359.  
  360. //------------------------------------------------------------------
  361. // Returns a custom report for this drawing, showing which numbers
  362. // were selected, how many tickets were sold, what the average
  363. // prize was, and who the biggest winner was.
  364. //------------------------------------------------------------------
  365. public String createReport(){
  366. LotteryTicket big = getBiggestWinner();
  367.  
  368. String report = "Report for All Sales: \n" + formatNumbers() + "\nTickets Sold: " + tickets.size();
  369. report = report + "\nAverage Prize: " + fmt.format(averagePrize()) + "\n\nBiggest Winner: ";
  370. report = report + big;
  371. return report;
  372. }
  373.  
  374. //------------------------------------------------------------------
  375. // Returns a custom report for this drawing, showing which numbers
  376. // were selected, how many tickets were sold, what the average
  377. // prize was, and who the biggest winner was in a given state.
  378. //------------------------------------------------------------------
  379. public String createReport(String st){
  380. LotteryTicket stBig = getBiggestStateWinner(st);
  381.  
  382. String stateReport = "Report for All Sales In " + st + ":\n" + formatNumbers() + "\nTickets Sold: ";
  383. stateReport = stateReport + countStateTickets(st) + "\nAverage Prize: " + fmt.format(averageStatePrize(st));
  384. stateReport = stateReport + "\n\nBiggest Winner: " + getBiggestStateWinner(st);
  385. return stateReport;
  386. }
  387.  
  388. //------------------------------------------------------------------
  389. // Returns who the oldest participant was in this drawing.
  390. //------------------------------------------------------------------
  391. public LotteryTicket getOldestPlayer(){
  392. for(LotteryTicket t: tickets){
  393. // Checks to see if the birth year of the current ticket
  394. // is older than the current oldest birthday.
  395. if (t.getYear() < oldYear){
  396. // If the birth year of the current ticket is older than the current
  397. // oldest birthday, that ticket's birthday becomes the new oldest birthday.
  398. oldYear = t.getYear();
  399. oldMonth = t.getMonth();
  400. oldDay = t.getDay();
  401. oldest = t;
  402. }else if(t.getYear() == oldYear){
  403. // If the birth year of the current ticket is equal to the current oldest
  404. // birthday, then check to see if the birth month of the current ticket
  405. // is older than the current oldest birthday.
  406. if (t.getMonth() < oldMonth){
  407. // If the birth month of the current ticket is older than the current
  408. // oldest birthday, that ticket's birthday becomes the new oldest birthday.
  409. oldYear = t.getYear();
  410. oldMonth = t.getMonth();
  411. oldDay = t.getDay();
  412. oldest = t;
  413. }else if (t.getMonth() == oldMonth){
  414. // If the birth month of the current ticket is equal to the current oldest
  415. // birthday, then check to see if the birth day of the current ticket
  416. // is older than the current oldest birthday.
  417. if (t.getDay() < oldDay){
  418. // If the birth day of the current ticket is older than the current
  419. // oldest birthday, that ticket's birthday becomes the new oldest birthday.
  420. oldYear = t.getYear();
  421. oldMonth = t.getMonth();
  422. oldDay = t.getDay();
  423. oldest = t;
  424. }
  425. }
  426. }
  427.  
  428. // If, at any point, a birthday is younger than the current oldest birthday,
  429. // then that birthday is skipped and the next birthday is checked.
  430. }
  431.  
  432. // Returns the oldest ticket filed.
  433. return oldest;
  434. }
  435.  
  436. //------------------------------------------------------------------
  437. // Returns who won the most prize money for this drawing.
  438. //------------------------------------------------------------------
  439. public LotteryTicket getBiggestWinner(){
  440. makePayouts();
  441. highest = null;
  442. highWin = -1.00;
  443.  
  444. for(LotteryTicket t: tickets){
  445. // Checks to see if the ticket's prize is higher than the
  446. // current highest prize.
  447. if (t.getPrize() > highWin){
  448. // If the ticket's prize is higher than the current highest
  449. // prize, that ticket becomes the new highest prize winner.
  450. highWin = t.getPrize();
  451. highest = t;
  452. }
  453. }
  454.  
  455. // Returns the highest winning ticket.
  456. return highest;
  457. }
  458.  
  459. //------------------------------------------------------------------
  460. // Returns who won the most prize money in a given state for this
  461. // drawing.
  462. //------------------------------------------------------------------
  463. public LotteryTicket getBiggestStateWinner(String st){
  464. makePayouts();
  465. stateHighest = null;
  466. highStateWin = -1.00;
  467.  
  468. int stateCounter = 0;
  469. for(LotteryTicket t: tickets){
  470. // Checks to see if the ticket was filed in the entered state.
  471. if (t.getState().equalsIgnoreCase(st) == true){
  472. // Checks to see if the ticket's prize is higher than the
  473. // current highest prize.
  474. if (t.getPrize() > highStateWin){
  475. // If the ticket's prize is higher than the current highest
  476. // prize, that ticket becomes the new highest prize winner.
  477. highStateWin = t.getPrize();
  478. stateHighest = t;
  479. }
  480. // Adds 1 to the number of tickets found in the entered state,
  481. // whether it was highest or not.
  482. stateCounter++;
  483. }
  484. }
  485.  
  486. if (stateCounter == 0){
  487. // If no tickets were filed for the entered state,
  488. // the program will return null.
  489. return null;
  490. }else{
  491. // Otherwise, the program will return the highest
  492. // winning ticket for the entered state.
  493. return stateHighest;
  494. }
  495. }
  496.  
  497. //------------------------------------------------------------------
  498. // Returns each participant for this drawing who won money greater
  499. // than or equal to a user-given amount.
  500. //------------------------------------------------------------------
  501. public ArrayList <LotteryTicket> getMajorWinners(double amount){
  502. majorWinners = new ArrayList <LotteryTicket> ();
  503. for(LotteryTicket t: tickets){
  504. // Checks if the ticket's prize is greater than the entered amount.
  505. if (t.getPrize() >= amount){
  506. // If the ticket's pize is greater than the entered amount, the
  507. // ticket is added to the majorWinners ArrayList.
  508. majorWinners.add(t);
  509. }
  510. }
  511.  
  512. // Returns the majorWinners ArrayList.
  513. return majorWinners;
  514. }
  515.  
  516. //------------------------------------------------------------------
  517. // Returns each participant for this drawing who won money greater
  518. // than or equal to a user-given amount within a user-given state.
  519. //------------------------------------------------------------------
  520. public ArrayList <LotteryTicket> getMajorStateWinners(double amount, String st){
  521. majorStateWinners = new ArrayList <LotteryTicket> ();
  522. for(LotteryTicket t: tickets){
  523. // Checks if the ticket is in the entered state.
  524. if (t.getState().equalsIgnoreCase(st) == true){
  525. // Checks if the ticket's prize is greater than the entered amount.
  526. if (t.getPrize() >= amount){
  527. // If the ticket's pize is greater than the entered amount, the
  528. // ticket is added to the majorStateWinners ArrayList.
  529. majorStateWinners.add(t);
  530. }
  531. }
  532. }
  533.  
  534. // Returns the majorStateWinners ArrayList.
  535. return majorStateWinners;
  536. }
  537.  
  538. //-------------------------------------------------------------------
  539. // Simulates multiple games, playing over and over until a jackpot
  540. // is won, before return a formatted string displaying how long
  541. // it took to get a winner, who it was, and what they won.
  542. //-------------------------------------------------------------------
  543. public String multipleGames (){
  544. // This creates the variable for how many games happen until a jackpot is won.
  545. int gameCounter = 0;
  546. jackpot = 5000000.00;
  547. while (jackpotWon == false){
  548. pickNumbers();
  549. for(LotteryTicket t: tickets){
  550. countMatches(t);
  551. //If somebody has won the jackpot, this will make it so that no more games will be played.
  552. if (matches == 5 && t.megaBall == megaRoll){
  553. t.prize = jackpot;
  554. jackpotWon = true;
  555. jackpotWinner = t;
  556. }
  557. }
  558. //If nobody has won the jackpot this drawing, this will add $1,500,000 to the jackpot value.
  559. if (jackpotWon == false){
  560. jackpot = jackpot + 1500000.00;
  561. }
  562. //Adds 1 to the number of games that were played.
  563. gameCounter++;
  564. }
  565.  
  566. // Returns a formatted string for how many games were played
  567. // and who eventually won the jackpot.
  568. jackpotResults = "Games until Jackpot: " + gameCounter + "\n\n" + jackpotWinner;
  569. return jackpotResults;
  570. }
  571.  
  572. //-----------------------------------------------------------------
  573. // Main method; tests all of the methods above.
  574. //-----------------------------------------------------------------
  575. public static void main(String args[]){
  576. // Creates a LotteryMachine object for testing purposes.
  577. LotteryMachine test = new LotteryMachine();
  578. // Imports the "TicketInfo.txt" file for use.
  579. test.readTickets("TicketInfo.txt");
  580. // Returns the nubmer of tickets.
  581. test.countTickets();
  582. // Returns the number of tickets filed in Michigan.
  583. test.countStateTickets("MI");
  584. // Rolls the numbers for this ticket.
  585. test.drawTicket();
  586. // Returns the average prize won by all entered tickets this game.
  587. test.averagePrize();
  588. // Returns the average prize won by all tickets filed in Michigan.
  589. test.averageStatePrize("MI");
  590. // Returns a report for all tickets this game.
  591. test.createReport();
  592. // Returns the oldest player for this game.
  593. test.getOldestPlayer();
  594. // Returns the biggest winner for this game.
  595. test.getBiggestWinner();
  596. // Returns all tickets who won more than $50.00.
  597. test.getMajorWinners(50.00);
  598. // Returns a report for all tickets filed in Michigan this game.
  599. test.createReport("MI");
  600. // Returns the biggest winner for all tickets in Michigan.
  601. test.getBiggestStateWinner("MI");
  602. // Rolls new numbers, 24, 67, 53, 4, 19, and 1 for the Mega Ball.
  603. test.drawTicket(24, 67, 53, 4, 19, 1);
  604. // Returns the average prize won by all entered tickets this game.
  605. test.averagePrize();
  606. // Runs multiple games with new numbers every time until somebody wins a jackpot.
  607. test.multipleGames();
  608. }
  609. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement