/* * Entry.java * * Created on March 14, 2006, 9:32 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package powerball; import java.util.Random; import java.util.Arrays; import java.util.GregorianCalendar; import java.util.Calendar; /** * * @author Mike */ public class Entry { public static final int BALLS = 5; // number of regular balls public static final int INVALIDTEST = -1; // private String soldLocation = "unknown"; private GregorianCalendar drawingDate; private int[] numbers; // array of regular numbers 1-55 private int powerball; // powerball 1-42 private boolean hasBeenChecked = false; // winner doesn't mean anything unless entry has been checked private boolean winner = false; private int numBallsMatch = 0; private boolean powerballMatches = false; private static Random generator = new Random(); // uses time as a seed /** * Service method for generating a power ball entry. One flaw - doesn't check for duplicate numbers */ public void genRandEntry () { // instantiate array numbers = new int[BALLS]; int rand; for (int cnt = 0; cnt < BALLS; ) { // we control the counter rand = generator.nextInt(55); // generate an int from 0-54 then add 1 to get 1-55 // if already been generated - don't accept duplicate number boolean duplicate = false; for (int prev = 0; prev < cnt; prev++){ if (numbers[prev] == (rand + 1) ) { // dup duplicate = true; } } if (!duplicate) { numbers[cnt] = rand + 1; cnt++; } } // put the numbers in order Arrays.sort(numbers); powerball = generator.nextInt(42); // generate an int from 0-41 then add 1 to get 1-42 } /** Creates a new instance of Entry - random */ public Entry() { this.genRandEntry(); // drawing date by default is "today" drawingDate = new GregorianCalendar(); } /** * Creates a new instance of Entry, random numbers - given location name */ public Entry(String nam) { this.genRandEntry(); this.soldLocation = nam; // drawing date by default is "today" drawingDate = new GregorianCalendar(); } /** * Creates a new instance of Entry, random numbers - given location name and drawing date */ public Entry(String nam, int mon, int day, int yr) { this.genRandEntry(); this.soldLocation = nam; // drawing date - really should validate - the sets below should fail if bad inputs drawingDate = new GregorianCalendar(); drawingDate.set(Calendar.MONTH, mon - 1); // stored as 0-11 drawingDate.set(Calendar.DAY_OF_MONTH, day); drawingDate.set(Calendar.YEAR, yr); } /** * Creates a new instance of Entry, passed numbers - given location name and drawing date */ public Entry(String nam, int mon, int day, int yr, int[] nums, int pb) { this(nam, mon,day,yr); numbers = nums; powerball = pb; } /** * Creates a new instance of Entry, passed numbers - given location name - assume todays date */ public Entry(String nam, int mon, int[] nums, int pb) { this(nam); numbers = nums; powerball = pb; } ///////////////////////////////////////////////////////////////////////// /// inspectors ///////////////////////////////////////////////////////////////////////// /** * report sold location */ public String getSoldLocation() { return soldLocation; } /** * report drawing date - as GregorianCalendar */ public GregorianCalendar getDrawingDate () { return drawingDate; } /** * report drawing date as String */ public String getDrawingDateAsString () { String res = ""; res += (1 + drawingDate.get(Calendar.MONTH)) + "/"; // add 1 because month stored 0-11 res += drawingDate.get(Calendar.DAY_OF_MONTH) + "/"; res += drawingDate.get(Calendar.YEAR); return res; } /** * report regular numbers - question whether this should be public */ public int[] getNumbers () { return numbers; } /** * report powerball - - question whether this should be public */ public int getPowerball () { return powerball; } /** * report whether the entry has been checked against winning numbers */ public boolean getHasBeenChecked () { return hasBeenChecked; } /** * report whether entry is a winner - if hasn't been checked can't be a winner */ public boolean getWinner () { if (!hasBeenChecked) { return false; } else { return winner; } } /** * report how many balls match - if hasn't been checked - return invalid indicator */ public int getNumBallsMatch () { if (!hasBeenChecked ) { return INVALIDTEST; } else { return numBallsMatch; } } /** * report whether entry powerball matches - if hasn't been checked can't be a winner */ public boolean getPowerballMatches () { if (!hasBeenChecked) { return false; } else { return powerballMatches; } } /////////////////////////////////////////////////////////////////////////// /// mutators /////////////////////////////////////////////////////////////////////////// /** * change sold location */ public boolean setSoldLocation (String loc) { soldLocation = loc; return true; // don't know how to validate } /** * change drawing date - validation needs more work */ public boolean setDrawingDate (int mon, int day, int yr) { if ((mon < 1) || (mon > 12)) { return false; } if ((day < 1) || (day > 31)) { return false; } if ((day < 1970) || (day > 2100)) { return false; } mon--; // stored as 0-11 drawingDate.set(yr,mon,day); // return true; } /** * change numbers - question whether this should be public. Really ougth to validate */ public boolean setNumbers (int [] nums) { numbers = nums; return true; } /** * change powerball - question whether this should be public */ public boolean setPowerball (int pb) { if (( pb < 1) || (pb > 42) ) { return false; } powerball = pb; return true; } /** * change winner - question - should this mean that the entry has been checked? */ public boolean setWinner (boolean win) { winner = win; return true; } /** * change whether the entry has been checked */ public boolean setHasBeenChecked (boolean check) { hasBeenChecked = check; return true; } /** * change whether the entry has been checked - and whether the entry is a winner */ public boolean setHasBeenChecked (boolean check, boolean win) { if (check) { // entry has been checked winner = win; hasBeenChecked = true; } else { // if hasn't been checked, set to not a winner hasBeenChecked = false; winner = false; numBallsMatch = 0; powerballMatches = false; } return true; } ////////////////////////////////////////////////////////////////////////// /// other ////////////////////////////////////////////////////////////////////////// /** * check entry against actual balls - return whether winner */ public boolean checkEntry (int[] winning, int pb) { numBallsMatch = 0; hasBeenChecked = true; winner = false; for (int wcnt = 0; wcnt < BALLS; wcnt++ ) { for (int ecnt = 0; ecnt < BALLS; ecnt++) { if (numbers[ecnt] == winning[wcnt]) { numBallsMatch++; } } } if (pb == powerball) { powerballMatches = true; } if ((numBallsMatch == BALLS) && powerballMatches) { winner = true; } return winner; } /////////////////////////////////////////////////////////////////////////// /// override OBject versions /////////////////////////////////////////////////////////////////////////// /** * check if two Entries have same contents - need policy decision - migth want to check to see if * all of the numbers are the same - in which could use it to check if a winner. * But for now, check all contents */ public boolean equals(Object obj) { if (obj instanceof Entry ){ Entry toCompare = (Entry) obj; boolean matches = true; // default that it matches if (! this.soldLocation.equals(toCompare.soldLocation)) { matches = false; } if (! this.drawingDate.equals(toCompare.drawingDate)) { matches = false; } for (int cnt = 0; cnt < BALLS; cnt++) { if (this.numbers[cnt] != toCompare.numbers[cnt]) { matches = false; } } if (this.powerball != toCompare.powerball) { matches = false; } if (this.winner != toCompare.winner) { matches = false; } if (this.hasBeenChecked != toCompare.hasBeenChecked) { matches = false; } if (this.numBallsMatch != toCompare.numBallsMatch) { matches = false; } if (this.powerballMatches != toCompare.powerballMatches) { matches = false; } return matches; } else { return false; // can't match if not even the same type } } /** * return a string representation for the Entry */ public String toString () { String res = ""; res += "Drawing Date: " + this.getDrawingDateAsString(); res += " Sold At: " + this.getSoldLocation(); res += " Numbers: "; for (int cnt = 0; cnt < BALLS; cnt++) { res += " " + this.numbers[cnt]; } res += " PB: " + powerball; if (!hasBeenChecked) { res += " Not Checked Yet"; } else { res += " Matches: " + this.numBallsMatch; res += " PB: "; if (powerballMatches) { res += "MATCH"; } else { res += "no match"; } if (winner) { res += " WINNER"; } else { res += " no win"; } } return res; } }