Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import javax.swing.*;
- import java.io.*;
- import java.text.*;
- import java.lang.*;
- /*************************************************************
- * Simulates a Mega Millions Lottery game.
- *
- * @author Matt Schuch
- * @version 3/12/14
- ************************************************************/
- public class LotteryMachine
- {
- //--------------------------------------------------------------------------
- // Creates the ArrayLists and instances variables needed for the program.
- //--------------------------------------------------------------------------
- public ArrayList <LotteryTicket> tickets;
- private ArrayList <LotteryTicket> majorWinners;
- private ArrayList <LotteryTicket> majorStateWinners;
- private ArrayList <Integer> numbers;
- private ArrayList <Integer> rolls;
- public int roll1, roll2, roll3, roll4, roll5, megaRoll, matches, oldYear = 2014, oldMonth = 12, oldDay = 31;
- private double jackpot = 5000000.00, highWin = -1.00, highStateWin = -1.00;
- private boolean jackpotWon = false;
- public String jackpotResults;
- private LotteryTicket oldest, stateHighest = null, highest = null, jackpotWinner;
- //------------------------------------------------------------------
- // Creates the random number generator needed for the program.
- //------------------------------------------------------------------
- Random rand = new Random();
- //------------------------------------------------------------------
- // Creates the currency format for the program to use.
- //------------------------------------------------------------------
- NumberFormat fmt = NumberFormat.getCurrencyInstance();
- //------------------------------------------------------------------
- // Instantiates the LotteryTicket arraylist.
- //------------------------------------------------------------------
- public LotteryMachine(){
- tickets = new ArrayList <LotteryTicket> ();
- }
- //------------------------------------------------------------------
- // Fills the number array list with numbers from 1-75.
- //------------------------------------------------------------------
- private void fillNumberArray(){
- numbers = new ArrayList <Integer> ();
- for (int i = 1; i <=75; i++){
- numbers.add(i);
- }
- }
- //------------------------------------------------------------------
- // Adds a custom ticket to the LotteryTicket ArrayList.
- //------------------------------------------------------------------
- public void addTicket (LotteryTicket t){
- tickets.add(t);
- }
- //------------------------------------------------------------------
- // Returns the number of tickets in the LottertTicket ArrayList.
- //------------------------------------------------------------------
- public int countTickets(){
- return tickets.size();
- }
- //------------------------------------------------------------------
- // Returns the number of tickets bought in a specific state in the
- // LottertyTicket ArrayList (used specifically for creating reports
- // for specific states.
- //------------------------------------------------------------------
- public int countStateTickets(String st){
- int stateTickets = 0;
- // Checks to see if the ticket was filed in the given state.
- for(LotteryTicket t: tickets){
- // If so, the counter for how many tickets were bought in
- // the entered state is raised by 1.
- if (t.getState().equalsIgnoreCase(st) == true){
- stateTickets++;
- }
- }
- return stateTickets;
- }
- //------------------------------------------------------------------
- // Selects the numbers for a drawing.
- //------------------------------------------------------------------
- public void pickNumbers(){
- // Creates the number array and fills it values from 1 to 75.
- fillNumberArray();
- // Assigns each ball from 1 to 75, and removes each value so it cannot
- // be selected again.
- roll1 = numbers.remove(rand.nextInt(numbers.size()));
- roll2 = numbers.remove(rand.nextInt(numbers.size()));
- roll3 = numbers.remove(rand.nextInt(numbers.size()));
- roll4 = numbers.remove(rand.nextInt(numbers.size()));
- roll5 = numbers.remove(rand.nextInt(numbers.size()));
- // Assigns the Mega Ball a value from 1 to 15.
- megaRoll = rand.nextInt(15) + 1;
- // Orders the rolled numbers in numerical order.
- orderRolls();
- }
- //------------------------------------------------------------------
- // Orders the rolled numbers in numerical order.
- //------------------------------------------------------------------
- private void orderRolls(){
- // Instantiates the ArrayList for the rolls.
- rolls = new ArrayList <Integer> ();
- // Adds the first roll to the ArrayList.
- rolls.add(roll1);
- // Places the second roll in it's numerical spot in the ArrayList.
- if (roll2 < rolls.get(0)){
- rolls.add(0, roll2);
- }else{
- rolls.add(roll2);
- }
- // Places the third roll in it's numerical spot in the ArrayList.
- if (roll3 < rolls.get(0)){
- rolls.add(0, roll3);
- }else if (roll3 < rolls.get(1)){
- rolls.add(1, roll3);
- }else{
- rolls.add(roll3);
- }
- // Places the fourth roll in it's numerical spot in the ArrayList.
- if (roll4 < rolls.get(0)){
- rolls.add(0, roll4);
- }else if (roll4 < rolls.get(1)){
- rolls.add(1, roll4);
- }else if (roll4 < rolls.get(2)){
- rolls.add(2, roll4);
- }else{
- rolls.add(roll4);
- }
- // Places the fifth roll in it's numerical spot in the ArrayList.
- if (roll5 < rolls.get(0)){
- rolls.add(0, roll5);
- }else if (roll5 < rolls.get(1)){
- rolls.add(1, roll5);
- }else if (roll5 < rolls.get(2)){
- rolls.add(2, roll5);
- }else if (roll5 < rolls.get(3)){
- rolls.add(3, roll5);
- }else{
- rolls.add(roll5);
- }
- }
- //------------------------------------------------------------------
- // Counts the number of matches for a given LotteryTicket.
- //------------------------------------------------------------------
- private int countMatches (LotteryTicket t){
- matches = 0;
- // If the first roll matches any of balls selected by the ticket,
- // a match is recorded.
- if (t.ball1 == roll1 || t.ball2 == roll1 || t.ball3 == roll1 || t.ball4 == roll1 || t.ball5 == roll1){
- matches++;
- }
- // If the second roll matches any of balls selected by the ticket,
- // a match is recorded.
- if (t.ball1 == roll2 || t.ball2 == roll2 || t.ball3 == roll2 || t.ball4 == roll2 || t.ball5 == roll2){
- matches++;
- }
- // If the third roll matches any of balls selected by the ticket,
- // a match is recorded.
- if (t.ball1 == roll3 || t.ball2 == roll3 || t.ball3 == roll3 || t.ball4 == roll3 || t.ball5 == roll3){
- matches++;
- }
- // If the fourth roll matches any of balls selected by the ticket,
- // a match is recorded.
- if (t.ball1 == roll4 || t.ball2 == roll4 || t.ball3 == roll4 || t.ball4 == roll4 || t.ball5 == roll4){
- matches++;
- }
- // If the fifth roll matches any of balls selected by the ticket,
- // a match is recorded.
- if (t.ball1 == roll5 || t.ball2 == roll5 || t.ball3 == roll5 || t.ball4 == roll5 || t.ball5 == roll5){
- matches++;
- }
- // Returns the number of matches the ticket had.
- return matches;
- }
- //---------------------------------------------------------------------
- // Distributes the correct prizes to each ticket in the LotteryTicket
- // ArrayList.
- //---------------------------------------------------------------------
- public void makePayouts(){
- for(LotteryTicket t: tickets){
- // Counts the number of matches for the ticket.
- countMatches(t);
- // If only the Mega Ball matches, the ticket wins $1.00.
- if (matches == 0 && t.megaBall == megaRoll){
- t.prize = 1.00;
- }
- // If one ball and the Mega Ball matches, the ticket wins $2.00.
- if (matches == 1 && t.megaBall == megaRoll){
- t.prize = 2.00;
- }
- // If two balls and the Mega Ball matches, the ticket wins $5.00.
- if (matches == 2 && t.megaBall == megaRoll){
- t.prize = 5.00;
- }
- // If three balls match but the Mega Ball does not, the ticket wins $5.00.
- if (matches == 3 && t.megaBall != megaRoll){
- t.prize = 5.00;
- }
- // If three balls and the Mega Ball matches, the ticket wins $50.00.
- if (matches == 3 && t.megaBall == megaRoll){
- t.prize = 50.00;
- }
- // If four balls match but the Mega Ball does not, the ticket wins $500.00.
- if (matches == 4 && t.megaBall != megaRoll){
- t.prize = 500.00;
- }
- // If four balls and the Mega Ball matches, the ticket wins $5,000.00.
- if (matches == 4 && t.megaBall == megaRoll){
- t.prize = 5000.00;
- }
- // If every ball matches but the Mega Ball does not, the ticket wins $1,000,000.00.
- if (matches == 5 && t.megaBall != megaRoll){
- t.prize = 1000000.00;
- }
- // If every ball including the Mega Ball matches, the ticket wins the jackpot (normally $5,000,000.00).
- if (matches == 5 && t.megaBall == megaRoll){
- t.prize = jackpot;
- }
- }
- }
- //-------------------------------------------------------------------------
- // Calculates the average amount won by all participants for this drawing.
- //-------------------------------------------------------------------------
- private double averagePrize(){
- // Creates the variable for the average prize.
- double average = 0.00;
- // Calculates the average prize won by all tickets.
- for(LotteryTicket t: tickets){
- average = average + t.getPrize();
- }
- average = (average / tickets.size());
- // Returns the average prize won by all tickets.
- return average;
- }
- //--------------------------------------------------------------------------
- // Calculates the average amount won by all participants from a given state
- // for this drawing.
- //--------------------------------------------------------------------------
- private double averageStatePrize(String st){
- // Creates the variables for the average prize and the
- // number of tickets for a state found.
- double stateAverage = 0.00;
- int stateCount = 0;
- // Calculates the average prize won by all tickets in the given state.
- // If no tickets were entered for a given state, the average is set to zero.
- for(LotteryTicket t: tickets){
- if (t.getState().equalsIgnoreCase(st) == true){
- stateAverage = stateAverage + t.getPrize();
- stateCount++;
- }
- }
- if (stateCount > 0){
- stateAverage = (stateAverage / stateCount);
- } else{
- stateAverage = 0.00;
- }
- // Returns the average prize won by all tickets in the given state.
- return stateAverage;
- }
- //--------------------------------------------------------------------
- // Returns the rolled numbers for this drawing in a formatted string.
- //--------------------------------------------------------------------
- private String formatNumbers(){
- orderRolls();
- String numberSummary = "Selected Numbers: " + rolls.get(0) + " " + rolls.get(1) + " " + rolls.get(2);
- numberSummary = numberSummary + " " + rolls.get(3) + " " + rolls.get(4) + " " + megaRoll;
- return numberSummary;
- }
- //----------------------------------------------------------------------------
- // Simulates one game, drawing numbers and making payouts to each participant.
- //----------------------------------------------------------------------------
- public void drawTicket(){
- pickNumbers();
- makePayouts();
- }
- //------------------------------------------------------------------
- // Simulates one game with user-chosen nubmers, then making payouts
- // to each paticipant.
- //------------------------------------------------------------------
- public void drawTicket(int b1, int b2, int b3, int b4, int b5, int m){
- roll1 = b1;
- roll2 = b2;
- roll3 = b3;
- roll4 = b4;
- roll5 = b5;
- megaRoll = m;
- makePayouts();
- }
- //------------------------------------------------------------------
- // Allows the user to add their own file of LotteryTicket values
- // to the ArrayList.
- //------------------------------------------------------------------
- public void readTickets(String filename){
- tickets = new ArrayList <LotteryTicket> ();
- try{
- File f = new File(filename);
- Scanner sc = new Scanner(f);
- String info;
- while(sc.hasNext()) {
- info = sc.nextLine();
- LotteryTicket t = new LotteryTicket(info);
- tickets.add(t);
- }
- sc.close();
- }
- catch(IOException e){
- System.out.println("Failed to read the data file: " + filename);
- }
- }
- //------------------------------------------------------------------
- // Returns a custom report for this drawing, showing which numbers
- // were selected, how many tickets were sold, what the average
- // prize was, and who the biggest winner was.
- //------------------------------------------------------------------
- public String createReport(){
- LotteryTicket big = getBiggestWinner();
- String report = "Report for All Sales: \n" + formatNumbers() + "\nTickets Sold: " + tickets.size();
- report = report + "\nAverage Prize: " + fmt.format(averagePrize()) + "\n\nBiggest Winner: ";
- report = report + big;
- return report;
- }
- //------------------------------------------------------------------
- // Returns a custom report for this drawing, showing which numbers
- // were selected, how many tickets were sold, what the average
- // prize was, and who the biggest winner was in a given state.
- //------------------------------------------------------------------
- public String createReport(String st){
- LotteryTicket stBig = getBiggestStateWinner(st);
- String stateReport = "Report for All Sales In " + st + ":\n" + formatNumbers() + "\nTickets Sold: ";
- stateReport = stateReport + countStateTickets(st) + "\nAverage Prize: " + fmt.format(averageStatePrize(st));
- stateReport = stateReport + "\n\nBiggest Winner: " + getBiggestStateWinner(st);
- return stateReport;
- }
- //------------------------------------------------------------------
- // Returns who the oldest participant was in this drawing.
- //------------------------------------------------------------------
- public LotteryTicket getOldestPlayer(){
- for(LotteryTicket t: tickets){
- // Checks to see if the birth year of the current ticket
- // is older than the current oldest birthday.
- if (t.getYear() < oldYear){
- // If the birth year of the current ticket is older than the current
- // oldest birthday, that ticket's birthday becomes the new oldest birthday.
- oldYear = t.getYear();
- oldMonth = t.getMonth();
- oldDay = t.getDay();
- oldest = t;
- }else if(t.getYear() == oldYear){
- // If the birth year of the current ticket is equal to the current oldest
- // birthday, then check to see if the birth month of the current ticket
- // is older than the current oldest birthday.
- if (t.getMonth() < oldMonth){
- // If the birth month of the current ticket is older than the current
- // oldest birthday, that ticket's birthday becomes the new oldest birthday.
- oldYear = t.getYear();
- oldMonth = t.getMonth();
- oldDay = t.getDay();
- oldest = t;
- }else if (t.getMonth() == oldMonth){
- // If the birth month of the current ticket is equal to the current oldest
- // birthday, then check to see if the birth day of the current ticket
- // is older than the current oldest birthday.
- if (t.getDay() < oldDay){
- // If the birth day of the current ticket is older than the current
- // oldest birthday, that ticket's birthday becomes the new oldest birthday.
- oldYear = t.getYear();
- oldMonth = t.getMonth();
- oldDay = t.getDay();
- oldest = t;
- }
- }
- }
- // If, at any point, a birthday is younger than the current oldest birthday,
- // then that birthday is skipped and the next birthday is checked.
- }
- // Returns the oldest ticket filed.
- return oldest;
- }
- //------------------------------------------------------------------
- // Returns who won the most prize money for this drawing.
- //------------------------------------------------------------------
- public LotteryTicket getBiggestWinner(){
- makePayouts();
- highest = null;
- highWin = -1.00;
- for(LotteryTicket t: tickets){
- // Checks to see if the ticket's prize is higher than the
- // current highest prize.
- if (t.getPrize() > highWin){
- // If the ticket's prize is higher than the current highest
- // prize, that ticket becomes the new highest prize winner.
- highWin = t.getPrize();
- highest = t;
- }
- }
- // Returns the highest winning ticket.
- return highest;
- }
- //------------------------------------------------------------------
- // Returns who won the most prize money in a given state for this
- // drawing.
- //------------------------------------------------------------------
- public LotteryTicket getBiggestStateWinner(String st){
- makePayouts();
- stateHighest = null;
- highStateWin = -1.00;
- int stateCounter = 0;
- for(LotteryTicket t: tickets){
- // Checks to see if the ticket was filed in the entered state.
- if (t.getState().equalsIgnoreCase(st) == true){
- // Checks to see if the ticket's prize is higher than the
- // current highest prize.
- if (t.getPrize() > highStateWin){
- // If the ticket's prize is higher than the current highest
- // prize, that ticket becomes the new highest prize winner.
- highStateWin = t.getPrize();
- stateHighest = t;
- }
- // Adds 1 to the number of tickets found in the entered state,
- // whether it was highest or not.
- stateCounter++;
- }
- }
- if (stateCounter == 0){
- // If no tickets were filed for the entered state,
- // the program will return null.
- return null;
- }else{
- // Otherwise, the program will return the highest
- // winning ticket for the entered state.
- return stateHighest;
- }
- }
- //------------------------------------------------------------------
- // Returns each participant for this drawing who won money greater
- // than or equal to a user-given amount.
- //------------------------------------------------------------------
- public ArrayList <LotteryTicket> getMajorWinners(double amount){
- majorWinners = new ArrayList <LotteryTicket> ();
- for(LotteryTicket t: tickets){
- // Checks if the ticket's prize is greater than the entered amount.
- if (t.getPrize() >= amount){
- // If the ticket's pize is greater than the entered amount, the
- // ticket is added to the majorWinners ArrayList.
- majorWinners.add(t);
- }
- }
- // Returns the majorWinners ArrayList.
- return majorWinners;
- }
- //------------------------------------------------------------------
- // Returns each participant for this drawing who won money greater
- // than or equal to a user-given amount within a user-given state.
- //------------------------------------------------------------------
- public ArrayList <LotteryTicket> getMajorStateWinners(double amount, String st){
- majorStateWinners = new ArrayList <LotteryTicket> ();
- for(LotteryTicket t: tickets){
- // Checks if the ticket is in the entered state.
- if (t.getState().equalsIgnoreCase(st) == true){
- // Checks if the ticket's prize is greater than the entered amount.
- if (t.getPrize() >= amount){
- // If the ticket's pize is greater than the entered amount, the
- // ticket is added to the majorStateWinners ArrayList.
- majorStateWinners.add(t);
- }
- }
- }
- // Returns the majorStateWinners ArrayList.
- return majorStateWinners;
- }
- //-------------------------------------------------------------------
- // Simulates multiple games, playing over and over until a jackpot
- // is won, before return a formatted string displaying how long
- // it took to get a winner, who it was, and what they won.
- //-------------------------------------------------------------------
- public String multipleGames (){
- // This creates the variable for how many games happen until a jackpot is won.
- int gameCounter = 0;
- jackpot = 5000000.00;
- while (jackpotWon == false){
- pickNumbers();
- for(LotteryTicket t: tickets){
- countMatches(t);
- //If somebody has won the jackpot, this will make it so that no more games will be played.
- if (matches == 5 && t.megaBall == megaRoll){
- t.prize = jackpot;
- jackpotWon = true;
- jackpotWinner = t;
- }
- }
- //If nobody has won the jackpot this drawing, this will add $1,500,000 to the jackpot value.
- if (jackpotWon == false){
- jackpot = jackpot + 1500000.00;
- }
- //Adds 1 to the number of games that were played.
- gameCounter++;
- }
- // Returns a formatted string for how many games were played
- // and who eventually won the jackpot.
- jackpotResults = "Games until Jackpot: " + gameCounter + "\n\n" + jackpotWinner;
- return jackpotResults;
- }
- //-----------------------------------------------------------------
- // Main method; tests all of the methods above.
- //-----------------------------------------------------------------
- public static void main(String args[]){
- // Creates a LotteryMachine object for testing purposes.
- LotteryMachine test = new LotteryMachine();
- // Imports the "TicketInfo.txt" file for use.
- test.readTickets("TicketInfo.txt");
- // Returns the nubmer of tickets.
- test.countTickets();
- // Returns the number of tickets filed in Michigan.
- test.countStateTickets("MI");
- // Rolls the numbers for this ticket.
- test.drawTicket();
- // Returns the average prize won by all entered tickets this game.
- test.averagePrize();
- // Returns the average prize won by all tickets filed in Michigan.
- test.averageStatePrize("MI");
- // Returns a report for all tickets this game.
- test.createReport();
- // Returns the oldest player for this game.
- test.getOldestPlayer();
- // Returns the biggest winner for this game.
- test.getBiggestWinner();
- // Returns all tickets who won more than $50.00.
- test.getMajorWinners(50.00);
- // Returns a report for all tickets filed in Michigan this game.
- test.createReport("MI");
- // Returns the biggest winner for all tickets in Michigan.
- test.getBiggestStateWinner("MI");
- // Rolls new numbers, 24, 67, 53, 4, 19, and 1 for the Mega Ball.
- test.drawTicket(24, 67, 53, 4, 19, 1);
- // Returns the average prize won by all entered tickets this game.
- test.averagePrize();
- // Runs multiple games with new numbers every time until somebody wins a jackpot.
- test.multipleGames();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement