/* * BaseballGamekeeper.java * * Created on June 27, 2003, 11:41 PM */ package gamekeeper2a; /** A baseball keeper is aware of the fact that in baseball: * 1. A high score wins * 2. No ties are possible (keeper's upset over last * year's All-Star Game) * 3. Scores should be positive * * @author dickey */ public class BaseballGamekeeper extends SimpleGamekeeper { /** Creates a new instance of BaseballGamekeeper */ public BaseballGamekeeper() { super(); } /** Remember the results of a game * The contestant with the LARGER score is the winner. * Ties are not allowed. * Negative scores are not allowed. * String must be non-null. * 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, true); return acceptGameResults(gresult); } else { return false; } } /** Remember the results of a match. * The contestant with the LARGER score is the winner. * Ties are not permitted. * Negative scores are not allowed. * Strings must be non-null. * 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, true /*high 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) { if (gresult.isDraw() || gresult.getWinnerScore() < 0 || gresult.getLoserScore() < 0) { //no draws nor negative scores are legal in this game return false; } assert gresult.getWinnerScore() > gresult.getLoserScore(); 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 BaseballGamekeeper(); System.out.println(g1); g1.acceptGameResults("Tiger Wood 60, Tanya Harding 102"); g1.acceptGameResults("Tanya Harding", 80, "Tiger Woods", 78); GameUtilities.printArray(g1.listContestants()); GameUtilities.printArray(g1.listContestantResults()); System.out.println(g1); } }