/* * Contestant.java * * Created on June 19, 2003, 6:52 PM */ package gamekeeper2a; /** Information about a single contestant. * Unless the subclass specifies otherwise, assume that contestant * names are case sensitive; and that leading and trailing spaces * (but not internal spaces) are ignored and trimmed. */ public class Contestant implements IContestant { /** Name of this contestant. */ final private String name; /** Number of games won. */ private int gamesWon = 0; /** Number of games lost. */ private int gamesLost = 0; /** Number of games drawn. */ private int gamesDrawn = 0; /** Creates a new instance of contestant, with initial no games recorded. */ public Contestant(String contestant) { this.name = contestant.trim(); } public int getGamesLost() { return this.gamesLost; } public int getGamesWon() { return this.gamesWon; } public int getGamesDrawn() { return this.gamesDrawn; } /** Tells the name of this contestant. Spaces are trimmed. */ public String getName() { return this.name; } /** Remember that a game was lost. */ public void recordLoss() { this.gamesLost++; } /** Remember that a game was won. */ public void recordWin() { this.gamesWon++; } public void recordDraw() { this.gamesDrawn++; } /** Return a number which summarizes how well the contestant has done * so far. Higher values are considered better. * @return games won /(games lost + games drawn). If no games have * been played, return 0.0. */ public double getWinRatio() { int totalGames = this.gamesWon + this.gamesLost + this.gamesDrawn; if (totalGames > 0) { return (double) this.gamesWon / totalGames; } else { return 0.0; } } /** Format a string, without line feeds or surrounding whitespace, with * basic contestant information. */ public String toString() { String info = this.getName() + " W" + getGamesWon() + " L" + getGamesLost(); return info; } }