?? match.java
字號(hào):
package ope.football;
import java.util.ArrayList;
/**
* The class <CODE>Match</CODE> represents match results in a
* football match statistics program. A match is played
* between teams from two clubs: a home club and an away club.
* Goals scored by players from either team can be added
* to the match object with the method <CODE>addGoal</CODE>.
* The class is expected to be used so that a match object is
* initially creates as a real-life match starts and goals are
* added incrementally as the match progresses.
*/
public class Match {
private Club homeClub; // fixed value
private ArrayList<Player> homeScorers; // container: players who have scored for the home team
private Club awayClub; // fixed value
private ArrayList<Player> awayScorers; // container: players who have scored for the away team
/**
* Constructs a match between teams of the given clubs,
* with no goals scored (yet).
*
* @param homeClub the club whose team plays at home in the match
* @param awayClub the club whose team plays away in the match
*/
public Match(Club homeClub, Club awayClub) {
this.homeClub = homeClub;
this.awayClub = awayClub;
this.homeScorers = new ArrayList<Player>();
this.awayScorers = new ArrayList<Player>();
}
/**
* Returns the club who hosts the match.
*
* @return home club
*/
public Club getHomeClub() {
return this.homeClub;
}
/**
* Returns the club who is visiting the home club to play the match.
*
* @return away club
*/
public Club getAwayClub() {
return this.awayClub;
}
/**
* Records a goal as having been scored in the match.
* Whether the goal is a home goal or an away goal is
* determined by checking the employer of the given
* goalscorer. (If the given scorer is not employed by
* either club, the method does nothing.)
*
* @param scorer the player who scored the goal
*/
public void addGoal(Player scorer) {
if(scorer.getEmployer() == this.awayClub)
this.awayScorers.add(scorer);
if (scorer.getEmployer() == this.homeClub)
this.homeScorers.add(scorer);
}
/**
* Returns the number of goals scored by the home team.
*
* @return home team's goal count
*/
public int getHomeGoals() {
return this.homeScorers.size();
}
/**
* Returns the number of goals scored by the away team.
*
* @return away team's goal count
*/
public int getAwayGoals() {
return this.awayScorers.size();
}
/**
* Returns the total number of goals scored by
* the two teams.
*
* @return total goal count
*/
public int getTotalGoals() {
return this.getHomeGoals() + this.getAwayGoals();
}
/**
* Returns the goal difference of the match. If the home
* team won, a positive integer indicating the win margin
* is returned. Similarly, a negative integer indicates an
* away win. A tied match has a goal difference of zero.
*
* @return goal difference
*/
public int getGoalDifference() {
return this.getHomeGoals() - this.getAwayGoals();
}
/**
* Returns the club whose team won the match (or is about to
* win it, assuming the current result stands). That is,
* returns the club who has scored more goals than the other
* one.
*
* @return winning team or <CODE>null</CODE> if the game is tied
*/
public Club getWinner() {
if (this.getHomeGoals() < this.getAwayGoals())
return this.awayClub;
else if
(this.getHomeGoals() > this.getAwayGoals())
return this.homeClub;
else
return null;
}
/**
* Returns the player who scored the so-called "winning goal".
* The winning goal of a match is the first goal for the winning
* team scoring which was "necessary" in light of the match scoreline.
* For instance, if the score is 4-2 to the home team, then the
* third goal scored by the home team is the "winning goal".
* A tied match has no winning goal or winning goal scorer.
*
* @return winning goal scorer, or <CODE>null</CODE> in case of a tie
*/
public Player getWinningGoalScorer() {
int goalDifference = this.getGoalDifference();
if (goalDifference < 0)
return this.awayScorers.get(this.homeScorers.size());
else if
(goalDifference > 0)
return this.homeScorers.get(this.awayScorers.size());
else
return null;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -