tterrag1098

hw1

Sep 22nd, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. package games;
  2.  
  3. import java.util.ArrayList;
  4.  
  5.  
  6. public class Games {
  7.  
  8.     private ArrayList<Game> games;
  9.     private int sum1 = 0, sum2 = 0, maxDiff = 0, newDiff = 0;
  10.    
  11.     private int index = 0;
  12.    
  13.     public Games ()
  14.     {
  15.         games = new ArrayList<Game>();
  16.     }
  17.     public Games addGame(String team1, String team2) {
  18.         for (int i = 0; i < this.games.size(); i++)
  19.         {
  20.             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)))
  21.                     throw new UnsupportedOperationException("Team Already Used!");
  22.         }
  23.         Game game = new Game(team1, team2);
  24.         this.games.add(game);
  25.         return null;
  26.     }
  27.  
  28.     public boolean addRun(String teamName, int inningNum) {
  29.         if (inningNum > 9)
  30.             return false;
  31.         for (int i = 0; i < this.games.size(); i++)
  32.             if ((this.games.get(i).team1).equalsIgnoreCase(teamName) || (this.games.get(i).team2).equalsIgnoreCase(teamName))
  33.                 index = i;
  34.         if (teamName == games.get(index).team1)
  35.         {
  36.             this.games.get(index).scores[0][inningNum - 1]++;
  37.             return true;
  38.         }
  39.         else if (teamName == games.get(index).team2)
  40.         {      
  41.             this.games.get(index).scores[1][inningNum - 1]++;
  42.             return true;
  43.         }
  44.         else return false;
  45.     }
  46.  
  47.     public boolean isPlaying(String teamName) {
  48.         for (int i = 0; i < this.games.size(); i++)
  49.             if ((this.games.get(i).team1).equalsIgnoreCase(teamName) || (this.games.get(i).team2).equalsIgnoreCase(teamName))
  50.                 index = i;
  51.         if (teamName == this.games.get(index).team1 || teamName == this.games.get(index).team2)
  52.             return true;
  53.         return false;
  54.     }
  55.  
  56.     public int getScore(String teamName) {
  57.         int sum = 0;
  58.         for (int i = 0; i < this.games.size(); i++)
  59.         {
  60.             if ((this.games.get(i).team1).equalsIgnoreCase(teamName) || (this.games.get(i).team2).equalsIgnoreCase(teamName))
  61.                 index = i;
  62.             else return sum;
  63.         }
  64.         if (teamName == this.games.get(index).team1)
  65.             for (int j = 0; j < this.games.get(index).scores[0].length; j++)
  66.                 sum += this.games.get(index).scores[0][j];
  67.         else if (teamName == this.games.get(index).team2)
  68.             for (int j = 0; j < this.games.get(index).scores[1].length; j++)
  69.                 sum += this.games.get(index).scores[1][j];
  70.         return sum;
  71.     }
  72.  
  73.     public int getScoreInInning(String teamName, int inningNum) {
  74.         for (int i = 0; i < this.games.size(); i++)
  75.             if ((this.games.get(i).team1).equalsIgnoreCase(teamName) || (this.games.get(i).team2).equalsIgnoreCase(teamName))
  76.                 index = i;
  77.         if (teamName == this.games.get(index).team1)
  78.             return this.games.get(index).scores[0][inningNum - 1];
  79.         else if (teamName == this.games.get(index).team2)
  80.             return this.games.get(index).scores[1][inningNum - 1];
  81.         return 0;
  82.     }
  83.  
  84.     public boolean winner(String teamName) {
  85.         for (int i = 0; i < this.games.size(); i++)
  86.             if ((this.games.get(i).team1).equalsIgnoreCase(teamName) || (this.games.get(i).team2).equalsIgnoreCase(teamName))
  87.                 index = i;
  88.         for (int i = 0; i < this.games.get(index).scores.length; i++)
  89.         {
  90.             sum1 += this.games.get(index).scores[0][i];
  91.         }
  92.         for (int i = 0; i < this.games.get(index).scores.length; i++)
  93.         {
  94.             sum2 += this.games.get(index).scores[1][i];
  95.         }
  96.         if (teamName.equals(this.games.get(index).team1))
  97.         {
  98.             if (sum1 > sum2)
  99.                 return true;
  100.             else return false;
  101.         }
  102.         else if (teamName.equals(this.games.get(index).team2))
  103.         {
  104.             if (sum2 > sum1)
  105.                 return true;
  106.             else return false;
  107.         }
  108.         else
  109.             throw new UnsupportedOperationException("This team isn't playing in this game!");
  110.     }
  111.  
  112.     public String highestMarginOfVictory() {
  113.         if (this.games.get(0) == null)
  114.             return "";
  115.         String highMargin = "";
  116.         for (int i = 0; i < this.games.size(); i++)
  117.         {
  118.             for (int j = 0; j < this.games.get(i).scores[0].length; j++)
  119.             {
  120.                 sum1 += this.games.get(i).scores[0][j];
  121.                 sum2 += this.games.get(i).scores[1][j];
  122.             }
  123.             if (sum1 - sum2 == 0)
  124.                 break;
  125.             newDiff = (Math.abs(sum1 - sum2) > maxDiff) ? Math.abs(sum1 - sum2) : maxDiff;
  126.             highMargin = (newDiff > maxDiff) ? ((sum1 > sum2) ? this.games.get(i).team1 : this.games.get(i).team2) : highMargin;
  127.             maxDiff = (newDiff > maxDiff) ? newDiff : maxDiff;
  128.         }
  129.         return highMargin;
  130.     }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment