/* * SimpleGamekeeper.java * * Created on June 19, 2003, 6:50 PM * Modified on June 27, 2003, 1:49 PM */ import java.util.ArrayList; import java.util.Iterator; /** * Simple Implementation of the IRecordkeeper interface. * Keep track of matches that have been played. * * @author Martin Dickey * @version 1.1 */ public class SimpleGamekeeper implements IRecordkeeper { ArrayList contestants; /** * Creates a new instance of SimpleGamekeeper */ public SimpleGamekeeper() { this.contestants = new ArrayList(); } /** * HELPER METHOD * Given a non-null contestant name, find its entry in the contestants list. * * @param contestantName the name of the target entry * @return the entry for the team with this name if it is in the list, * null if the team is not in the list. */ private Contestant findContestant(String contestantName) { String target = contestantName.trim(); Iterator contestantsIter = contestants.iterator(); while (contestantsIter.hasNext()) { Contestant aContestant = (Contestant) contestantsIter.next(); if (target.equals(aContestant.getName())) { return aContestant; } } //didn't find the target return null; } /** * 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. * * @param theWinner name of the winner * @param winnerScore the score of the winner * @param theLoser name of the loser * @param loserScore the score of the loser * @return true if the parameters are valid * false otherwise. The string parameters must be non-null * and must not be equivalent to each other (after white space is trimmed). * Specific to this implementation, empty Strings (i.e. "") should not be accepted a */ public boolean acceptGameResults(final String theWinner, final int winnerScore, final String theLoser, final int loserScore) { // Null String objects should not be accepted if (theWinner == null || theLoser == null) { return false; } // Empty Strings should not be accepted (OPTIONAL) if (theWinner.equals("") || theLoser.equals("")) { return false; } String winnerName = theWinner.trim(); String loserName = theLoser.trim(); // Winning contestant should not equal losing contestant if (winnerName.equals(loserName)) { return false; } // Search for the winning contestant in our ArrayList // If not found, insert our new contestant into the list Contestant winner = findContestant(winnerName); if (winner == null) { winner = new Contestant(winnerName); contestants.add(winner); } // Search for the losing contestant in our ArrayList // If not found, insert our new contestant into the list Contestant loser = findContestant(loserName); if (loser == null) { loser = new Contestant(loserName); contestants.add(loser); } // If the scores are tied, record a draw for each contestant; // otherwise record a win/loss for the winner/loser. if (winnerScore == loserScore) { winner.recordDraw(); loser.recordDraw(); } else { winner.recordWin(); loser.recordLoss(); } return true; } /** * Returns an array of strings giving the standings of all known * contestants, 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. * * @return an array of contestant win/loss standings */ public String[] listContestantResults() { int contestantCount = this.contestants.size(); String[] cString = new String[contestantCount]; for (int cNum = 0; cNum < contestantCount; cNum++) { Contestant oneContestant = (Contestant) this.contestants.get(cNum); cString[cNum] = oneContestant.getName() + " " + oneContestant.getGamesWon() + "-" + oneContestant.getGamesLost(); } return cString; } }