/* * ScoreBoard.java * * Created on April 4, 2002, 1:17 PM * Modified based on what was done in class on 4/8 and 4/10 */ /** * * @author Dr Michael Redmond * @version 1.1 */ public class ScoreBoard extends java.lang.Object { public static final int SECSINPERIOD = 1200; private String homeTeamName = "La Salle"; private int homeTeamPoints = 0; private String awayTeamName = "Visitor"; private int awayTeamPoints = 0; private int period = 1; private int secondsLeft = SECSINPERIOD; /** Creates new ScoreBoard */ public ScoreBoard() { } //////////////////////////////// // Accessor methods //////////////////////////////// /********************************** * Report home teams name */ public String getHomeName() { return homeTeamName; } /********************************** * Report away teams name */ public String getAwayName() { return awayTeamName; } /********************************** * Report home teams score */ public int getHomeScore() { return homeTeamPoints; } /********************************** * Report away teams score */ public int getAwayScore() { return awayTeamPoints; } /********************************** * Report number of seconds left in period **/ public int getTimeLeft() { return secondsLeft; } /********************************* * Report time left in period in a friendly string format **/ public String getTimeLeftString () { int mins = secondsLeft / 60; int secs = secondsLeft % 60; String result = mins + ":"; if (secs < 10) { result = result + "0" + secs; } else { result = result + secs; } return result; } /********************************** * Report who is leading */ public String getLeadingTeam() { if (homeTeamPoints > awayTeamPoints) { return homeTeamName; } else if (homeTeamPoints < awayTeamPoints) { return awayTeamName; } else { return "Tie"; } } /************************************************************* * return how much the leading team is leading by */ public int getCurrMargin() { int margin = homeTeamPoints - awayTeamPoints; // if home team is losing - reverse if (margin < 0) { margin = 0 - margin; } return margin; } /********************************** * Report which period it is */ public int getPeriod() { return period; } /*********************************** * reports whether the period is over (clock is zero) **/ public boolean endPeriod() { if (secondsLeft == 0) { return true; } else { return false; } } /********************************** * Prepare for nice output of score board **/ public String toString() { String res = "Period: " + period + " " + getTimeLeftString() + " Score --> " + homeTeamName + ": " + homeTeamPoints + " " + awayTeamName + ": " + awayTeamPoints; return res; } /*********************************** * Determine if two scoreboards should be considered equal * I think probably we want all instance variables to be equal * in order to consider the objects equal **/ public boolean equals (ScoreBoard toCompare) { if (!homeTeamName.equals(toCompare.homeTeamName)) { return false; } if (!awayTeamName.equals(toCompare.awayTeamName)) { return false; } if (homeTeamPoints != toCompare.homeTeamPoints) { return false; } if (awayTeamPoints != toCompare.awayTeamPoints) { return false; } if (period != toCompare.period) { return false; } if (secondsLeft != toCompare.secondsLeft) { return false; } // if we got here (and didn't return anywhere) then all must match return true; } //////////////////////////////// // Mutator methods //////////////////////////////// /********************************** * Establish a bunch of things appropriate to the beginning of a game **/ public void beginGame(String home, String away) { period = 1; homeTeamPoints = 0; awayTeamPoints = 0; homeTeamName = home; awayTeamName = away; secondsLeft = SECSINPERIOD; } /********************************** * Establish a new value for home teams name * - should probably only be used at very beginningwhich period it is */ public void setHomeName(String name) { homeTeamName = name; } /********************************** * Establish a new value for away teams name * - should probably only be used at very beginning */ public void setAwayName(String name) { awayTeamName = name; } /********************************** * Establish a new value for home teams score * - should probably only be used at very beginning */ public void setHomeScore(int score) { homeTeamPoints = score; } /********************************** * Establish a new value for away teams score * - should probably only be used at very beginning */ public void setAwayScore(int score) { awayTeamPoints = score; } /********************************** * Establish a new value for period in game * - should probably only be used at very beginning */ public void setPeriod(int per) { period = per; } /********************************** * Establish a new value for seconds left in a period * - should probably only be used at very beginning */ public void setTimeLeft(int secs) { secondsLeft = secs; } /********************************** * Change the value for seconds left in a period * - pass negative number if want to decrease (take time off the clock) * pass a positive number to add time */ public void adjustClock(int secs) { secondsLeft = secondsLeft + secs; // don't let time go negative if (secondsLeft < 0) { secondsLeft = 0; } // don't let time go too high if (secondsLeft > SECSINPERIOD) { secondsLeft = SECSINPERIOD; } } /******************************************************* * helper function for all scoring * * Only used internally - main should go through specific types of scoring */ private int addPoints (String team, int points) { // ensure not negative points if (points <= 0) { System.out.println("ERROR - cannot add negative or zero points"); return -9; } if (team.equals(homeTeamName) || team.equalsIgnoreCase("home") ) { homeTeamPoints += points; return homeTeamPoints; } else if (team.equals(awayTeamName) || team.equalsIgnoreCase("away") ) { awayTeamPoints += points; return awayTeamPoints; } else { System.out.println("ERROR - team not found"); return -1; } } /******************************************************* * score a foul shot (1 point) for a given team **/ public int makeFoulShot(String team) { int newScore = addPoints(team,1); return newScore; } /******************************************************* * score a two point shot (regular field goal) for a given team **/ public int makeTwoPoint(String team) { int newScore = addPoints(team,2); return newScore; } /******************************************************* * score a three point shot (long field goal) for a given team **/ public int makeThreePoint(String team) { int newScore = addPoints(team,3); return newScore; } /******************************************************* * advance the period number to the next period **/ public void nextPeriod( ) { period++; } /*********************************** * Make a score board a copy of a passed scoreboard * copy all instance variables **/ public void copyOf (ScoreBoard toCopy) { homeTeamName = toCopy.homeTeamName; awayTeamName = toCopy.awayTeamName; homeTeamPoints = toCopy.homeTeamPoints; awayTeamPoints = toCopy.awayTeamPoints; period = toCopy.period; secondsLeft = toCopy.secondsLeft; } /*********************************** * Make a passed score board a copy of the current (calling) score board * Not necessary because we have the above - but done to illustrate * that object parameters are called by reference - so they can be * changed inside the method (unlike primitive parameters such as ints) * copy all instance variables **/ public void copyMe (ScoreBoard toChange) { toChange.homeTeamName = homeTeamName; toChange.awayTeamName = awayTeamName; toChange.homeTeamPoints = homeTeamPoints; toChange.awayTeamPoints = awayTeamPoints; toChange.period = period; toChange.secondsLeft = secondsLeft; } }