/* * SimpleGamekeeper.java * * Created on June 19, 2003, 6:50 PM */ package gamekeeper2a; import java.util.ArrayList; import java.util.Iterator; /** Keep track of matches that have been played. * * @author dickey */ abstract public class SimpleGamekeeper implements IRecordkeeper { ArrayList teams; /** Creates a new instance of SimpleGamekeeper. At this point, no games are recorded, and no contestants are known. */ public SimpleGamekeeper() { this.teams = new ArrayList(); } /** Given a contestant name, find the information about it. * PRECONDITION: rawInfoString is not null * @param contestantName the name to search for; surrounding spaces are ignored. * @return the entry for the team with this name, if it is known, * null if the team is not known. */ public IContestant findContestant(String contestantName) { assert contestantName != null; String target = contestantName.trim(); Iterator teamIter = teams.iterator(); while (teamIter.hasNext()) { Contestant aContestant = (Contestant) teamIter.next(); if (target.equals(aContestant.getName())) { return aContestant; } else { //System.out.println("Match not found for " + contestantName + ", instead found " + aContestant.getName()); } } //didn't find the target return null; } /** Remember the results of a match. * Information about the match is in a string with the following format: * , * The comma is required, and must immediately follow the first score (no intervening spaces). * White space at the begining and ending of the winner or loser should be ignored. * The two contestants must not be the same (after trimming spaces). * String should be non-null. * @return true if there were no errors detected and the information has * been recorded; false if there were errors (in which case the information * is not remembered.) */ public abstract boolean acceptGameResults(String gameString); /** Remember the results of a match. * 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). * Strings must be non-null. * @return true iff all parameters are valid; return false if there is any * error. In case of an error, nothing should be remembered about this * case; for example, if one of the contestants was hitherto unknown -- * it remains unknown. */ abstract public boolean acceptGameResults(String contestant1, int contestant1Score, String contestant2, int contestant2Score); /** Remember the results of a match. * The contestant with the larger 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). * Strings must be non-null. */ public boolean acceptGameResults(String contestant1, int contestant1Score, String contestant2, int contestant2Score, boolean highScoreWins) { if (contestant1 != null && contestant2 != null) { boolean status; IGameResult gresult = new GameResult( contestant1, contestant1Score, contestant2, contestant2Score, highScoreWins); status = setGameResults(gresult); return status; } else { return false; } } public boolean acceptGameResults(String resultsString, boolean highScoreWins) { IGameResult mresult = new GameResult(resultsString, highScoreWins); boolean setStatus = setGameResults(mresult); return setStatus; } protected boolean acceptGameResults(IGameResult mresult) { boolean setStatus = setGameResults(mresult); return setStatus; } /** Remember the results of a match. * Winner and loser must not be the same (after trimming spaces). * If the scores are the same, the game is considered a draw. * @return true if the parameters are valid; false otherwise. */ protected boolean setGameResults(IGameResult gresult) { String winnerName = gresult.getWinnerName().trim(); String loserName = gresult.getLoserName().trim(); if (winnerName.equals(loserName)) { return false; } IContestant winner = findContestant(winnerName); if (winner == null) { winner = new Contestant(winnerName); teams.add(winner); } IContestant loser = findContestant(loserName); if (loser == null) { loser = new Contestant(loserName); teams.add(loser); } if (gresult.isDraw()) { winner.recordDraw(); loser.recordDraw(); } else { winner.recordWin(); loser.recordLoss(); } return true; } public java.util.ArrayList listContestants() { int contestantCount = this.teams.size(); java.util.ArrayList newCList = new ArrayList(); for (int cNum = 0; cNum < contestantCount; cNum++) { Contestant oneContestant = (Contestant) this.teams.get(cNum); newCList.add(oneContestant); //not good -- should clone } return newCList; } /** Returns a string array giving information about all known contestants, * exactly one contestant per array entry, * in no particular order. * The array has exactly one line (string) per contestant, with the * contestant name at the beginning of the line, a space, followed by the * number of games won, a dash, then the number of games lost. * The line should be suitable for displaying on a console without * any changes. */ public String[] listContestantResults() { int contestantCount = this.teams.size(); String[] cString = new String[contestantCount]; for (int cNum = 0; cNum < contestantCount; cNum++) { Contestant oneContestant = (Contestant) this.teams.get(cNum); cString[cNum] = oneContestant.getName() + " " + oneContestant.getGamesWon() + "-" + oneContestant.getGamesLost(); } return cString; } public String toString() { String info = this.getClass().getName(); info += ": " + this.teams.size() + " teams known."; return info; } //end class }