/* * GolfGameKeeper.java * * Created on June 27, 2003, 11:41 PM */ package gamekeeper2a; /** A Golf game keeper is aware of the fact that: * 1. A low score wins * 2. Ties are possible * 3. No score lower than 18 is allowed * * @author dickey */ public class GolfGamekeeper extends SimpleGamekeeper { /** According to the rules, no score lower than this is possible. */ private final int MIN_POSSIBLE_SCORE = 18; /** Creates a new instance of GolfGameKeeper */ public GolfGamekeeper() { super(); //not actually necessary } /** Remember the results of a match. * The contestant with the SMALLER score is the winner. * White space at the begining and ending of the winner or loser should be ignored. * For example, "Tyler", " Tyler", and " Tyler " * should all be considered the same contestant. * The two contestants must not be the same (after trimming spaces). * @return true if the results are all valid; false otherwise. */ public boolean acceptGameResults(String gameString) { if(gameString != null) { IGameResult gresult = new GameResult(gameString, false); return acceptGameResults(gresult); } else { return false; } } /** Remember the results of a match. * The contestant with the SMALLER score is the winner. * The score must be within a legal range. * White space at the begining and ending of the winner or loser should be ignored. * For example, "Tyler", " Tyler", and " Tyler " * should all be considered the same contestant. * The two contestants must not be the same (after trimming spaces). * @return true if the results are all valid; false otherwise. */ public boolean acceptGameResults(String contestant1, int contestant1Score, String contestant2, int contestant2Score) { if (contestant1 != null && contestant2 != null) { boolean status; IGameResult gresult = new GameResult( contestant1, contestant1Score, contestant2, contestant2Score, false /*low score wins*/); return acceptGameResults(gresult); } else { return false; } } /** Private method to check the situation before passing it along to * the superclass to accept the result. * @return true if all is OK, false otherwise. */ protected boolean acceptGameResults(IGameResult gresult) { assert (gresult.getWinnerScore() <= gresult.getLoserScore()); if (gresult.getWinnerScore() < MIN_POSSIBLE_SCORE) { return false; } if (gresult.getLoserScore() < MIN_POSSIBLE_SCORE) { return false; } boolean status = super.acceptGameResults(gresult); return status; } /** Purely for testing. * @param args the command line arguments */ public static void main(String[] args) { IRecordkeeper g1 = new GolfGamekeeper(); System.out.println(g1); System.out.println(g1.acceptGameResults("Tiger Wood 30, Tanya Harding 29")); System.out.println(g1.acceptGameResults("Tanya Harding", 8, "Tiger Woods", 8)); GameUtilities.printArray(g1.listContestants()); GameUtilities.printArray(g1.listContestantResults()); System.out.println(g1); } }