In: Computer Science
(Implementing Interfaces) Implement the following
interface and classes:
a. Interface IsCompetition, which has no constants
and two methods (see the classes below for details about what these
methods should do):
i. getSummary(), ii. getCompetitors()
b. Class Game: A game object is associated with
a home team, away team, home score, away score, and a date of
competition. It should only be created using the following
constructor:
Game(String HTeam, String ATeam, int HScore, int AScore,
LocalDate date).
A game is also a competition, with getSummary printing
which team beat the other and by how many points (you can assume no
ties) and getCompetitors printing the two team names
separated by a comma and followed by a newline.
c. Class Season: A season consists of one or
more games. It’s up to you what data structure you want to hold the
games (an ArrayList should be fine). It has a
zero-parameter constructor, and thus games must be added with the
mutator below:
addGame(Game newGame): adds newGame to the existing season
games.
A season is also a competition, with getSummary printing
the following details:
i. The earliest and latest dates (games may be added out of
chronological order!)
ii. The lowest and highest scores of the season
Likewise, getCompetitors should print a list of all of the
teams who competed in the season (duplicates allowed), separated by
a comma, followed by a newline.
Make sure you include simple comments for each class and method definition.
For the Season class, let getSummary
also print the name of the teams that won the most and fewest
games, alongside their win/loss totals.
For the Season class, force
getCompetitors to only include each unique team in the
list of printed names once and only once.
Please find the complete code below,
IsCompetition.java
public interface IsCompetition {
public void getSummary();
public void getCompetitors();
}
Game.java
import java.time.LocalDate;
public class Game implements IsCompetition {
private String HTeam;
private String ATeam;
private int HScore;
private int AScore;
private LocalDate date;
// Constructor as required
public Game(String HTeam, String ATeam, int HScore, int AScore, LocalDate date) {
this.HTeam = HTeam;
this.ATeam = ATeam;
this.HScore = HScore;
this.AScore = AScore;
this.date = date;
}
@Override
public void getSummary() {
if(HScore < AScore) System.out.println(ATeam +" beats "+ HTeam+" by "+(AScore-HScore)+" points");
else System.out.println(HTeam +" beats "+ ATeam+" by "+(HScore-AScore)+" points");
}
@Override
public void getCompetitors() {
System.out.println(HTeam+" , "+ATeam);
}
// Getter and setter method to access private variables out of the class
public String getHTeam() {
return HTeam;
}
public void setHTeam(String HTeam) {
this.HTeam = HTeam;
}
public String getATeam() {
return ATeam;
}
public void setATeam(String ATeam) {
this.ATeam = ATeam;
}
public int getHScore() {
return HScore;
}
public void setHScore(int HScore) {
this.HScore = HScore;
}
public int getAScore() {
return AScore;
}
public void setAScore(int AScore) {
this.AScore = AScore;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
}
Season.java
import java.time.LocalDate;
import java.util.*;
public class Season implements IsCompetition{
private final List<Game> games;
public Season(){
games = new ArrayList<>();
}
public void addGame(Game newGame){
games.add(newGame);
}
@Override
public void getSummary() {
if(games.isEmpty()){
System.out.println("No games played till now.");
return;
}
LocalDate earliestDate = null, latestDate = null;
int lowestScore = Integer.MAX_VALUE, highestScore = Integer.MIN_VALUE;
for(Game game : games){
if(earliestDate == null) earliestDate = game.getDate();
else if(game.getDate().isBefore(earliestDate)) earliestDate = game.getDate();
if(latestDate == null) latestDate = game.getDate();
else if(game.getDate().isAfter(latestDate)) latestDate = game.getDate();
lowestScore = Math.min(lowestScore, Math.min(game.getAScore(), game.getHScore()));
highestScore = Math.max(highestScore, Math.max(game.getAScore(), game.getHScore()));
}
System.out.println("Earliest Date : "+ earliestDate.toString());
System.out.println("Latest Date : "+ latestDate.toString());
System.out.println("Lowest Score : "+ lowestScore);
System.out.println("Highest Score : "+ highestScore);
Map<String, WinLoss> winsAndLoses = new HashMap<>();
for(Game game : games){
WinLoss teamA = winsAndLoses.getOrDefault(game.getATeam(), new WinLoss());
WinLoss teamH = winsAndLoses.getOrDefault(game.getHTeam(), new WinLoss());
if(game.getHScore() < game.getAScore()){
teamA.setWins(teamA.getWins() + 1);
teamH.setLoss(teamH.getLoss() + 1);
}
else {
teamH.setWins(teamH.getWins() + 1);
teamA.setLoss(teamA.getLoss() + 1);
}
winsAndLoses.put(game.getATeam(), teamA);
winsAndLoses.put(game.getHTeam(), teamH);
}
String mostWinTeamName="", mostLossTeamName="";
int mostWins = 0, mostLoss = 0;
for(Map.Entry<String, WinLoss> entry : winsAndLoses.entrySet()){
WinLoss winLoss = entry.getValue();
if(winLoss.getWins() > mostWins){
mostWins = winLoss.getWins();
mostWinTeamName = entry.getKey();
}
if(winLoss.getLoss() > mostLoss){
mostLoss = winLoss.getLoss();
mostLossTeamName = entry.getKey();
}
}
System.out.println("TeamName with most number of wins : "+mostWinTeamName+". Won "+mostWins+" games.");
System.out.println("TeamName with most number of loss : "+mostLossTeamName+". Lost "+mostLoss+" games.");
}
@Override
public void getCompetitors() {
Set<String> includedTeams = new HashSet<>();
for(Game game : games){
if(includedTeams.contains(game.getATeam()) || includedTeams.contains(game.getHTeam())) continue;
game.getCompetitors();
includedTeams.add(game.getATeam());
includedTeams.add(game.getHTeam());
}
}
private static class WinLoss{
private int wins;
private int loss;
public WinLoss(){
wins = loss = 0;
}
public void setLoss(int loss) {
this.loss = loss;
}
public int getLoss() {
return loss;
}
public void setWins(int wins) {
this.wins = wins;
}
public int getWins() {
return wins;
}
}
}
If you have any doubts ask in the comments. Don't forget to upvote the solution.