Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package games;
- import java.util.ArrayList;
- public class Games {
- private ArrayList<Game> games;
- private int sum1 = 0, sum2 = 0, maxDiff = 0, newDiff = 0;
- private int index = 0;
- public Games ()
- {
- games = new ArrayList<Game>();
- }
- public Games addGame(String team1, String team2) {
- for (int i = 0; i < this.games.size(); i++)
- {
- if ((team1.equalsIgnoreCase(this.games.get(i).team1) || team1.equalsIgnoreCase(this.games.get(i).team2)) || (team2.equalsIgnoreCase(this.games.get(i).team1) || team2.equalsIgnoreCase(this.games.get(i).team2)))
- throw new UnsupportedOperationException("Team Already Used!");
- }
- Game game = new Game(team1, team2);
- this.games.add(game);
- return null;
- }
- public boolean addRun(String teamName, int inningNum) {
- if (inningNum > 9)
- return false;
- for (int i = 0; i < this.games.size(); i++)
- if ((this.games.get(i).team1).equalsIgnoreCase(teamName) || (this.games.get(i).team2).equalsIgnoreCase(teamName))
- index = i;
- if (teamName == games.get(index).team1)
- {
- this.games.get(index).scores[0][inningNum - 1]++;
- return true;
- }
- else if (teamName == games.get(index).team2)
- {
- this.games.get(index).scores[1][inningNum - 1]++;
- return true;
- }
- else return false;
- }
- public boolean isPlaying(String teamName) {
- for (int i = 0; i < this.games.size(); i++)
- if ((this.games.get(i).team1).equalsIgnoreCase(teamName) || (this.games.get(i).team2).equalsIgnoreCase(teamName))
- index = i;
- if (teamName == this.games.get(index).team1 || teamName == this.games.get(index).team2)
- return true;
- return false;
- }
- public int getScore(String teamName) {
- int sum = 0;
- for (int i = 0; i < this.games.size(); i++)
- {
- if ((this.games.get(i).team1).equalsIgnoreCase(teamName) || (this.games.get(i).team2).equalsIgnoreCase(teamName))
- index = i;
- else return sum;
- }
- if (teamName == this.games.get(index).team1)
- for (int j = 0; j < this.games.get(index).scores[0].length; j++)
- sum += this.games.get(index).scores[0][j];
- else if (teamName == this.games.get(index).team2)
- for (int j = 0; j < this.games.get(index).scores[1].length; j++)
- sum += this.games.get(index).scores[1][j];
- return sum;
- }
- public int getScoreInInning(String teamName, int inningNum) {
- for (int i = 0; i < this.games.size(); i++)
- if ((this.games.get(i).team1).equalsIgnoreCase(teamName) || (this.games.get(i).team2).equalsIgnoreCase(teamName))
- index = i;
- if (teamName == this.games.get(index).team1)
- return this.games.get(index).scores[0][inningNum - 1];
- else if (teamName == this.games.get(index).team2)
- return this.games.get(index).scores[1][inningNum - 1];
- return 0;
- }
- public boolean winner(String teamName) {
- for (int i = 0; i < this.games.size(); i++)
- if ((this.games.get(i).team1).equalsIgnoreCase(teamName) || (this.games.get(i).team2).equalsIgnoreCase(teamName))
- index = i;
- for (int i = 0; i < this.games.get(index).scores.length; i++)
- {
- sum1 += this.games.get(index).scores[0][i];
- }
- for (int i = 0; i < this.games.get(index).scores.length; i++)
- {
- sum2 += this.games.get(index).scores[1][i];
- }
- if (teamName.equals(this.games.get(index).team1))
- {
- if (sum1 > sum2)
- return true;
- else return false;
- }
- else if (teamName.equals(this.games.get(index).team2))
- {
- if (sum2 > sum1)
- return true;
- else return false;
- }
- else
- throw new UnsupportedOperationException("This team isn't playing in this game!");
- }
- public String highestMarginOfVictory() {
- if (this.games.get(0) == null)
- return "";
- String highMargin = "";
- for (int i = 0; i < this.games.size(); i++)
- {
- for (int j = 0; j < this.games.get(i).scores[0].length; j++)
- {
- sum1 += this.games.get(i).scores[0][j];
- sum2 += this.games.get(i).scores[1][j];
- }
- if (sum1 - sum2 == 0)
- break;
- newDiff = (Math.abs(sum1 - sum2) > maxDiff) ? Math.abs(sum1 - sum2) : maxDiff;
- highMargin = (newDiff > maxDiff) ? ((sum1 > sum2) ? this.games.get(i).team1 : this.games.get(i).team2) : highMargin;
- maxDiff = (newDiff > maxDiff) ? newDiff : maxDiff;
- }
- return highMargin;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment