Advertisement
DennisBoone2020

Untitled

Nov 10th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.06 KB | None | 0 0
  1.  
  2. Assignment directions:Input will be provided in a text file called scores.txt. There will be one line for each game and
  3. the lines will be in the following form:
  4. 2010/11/08,UC Irvine,65,Illinois,79,0,0
  5. 2010/11/12,CSU Northridge,50,UCLA,83,0,0
  6. 2010/11/12,Truman St,59,Mo. Kansas City,71,0,0
  7. where the fields are date, away team, away team's score, home team, home team's score, a
  8. neutral court flag, and the number of overtimes. Fields are separated by commas. The neutral
  9. court flag will be 1 to indicate that the game was played on a neutral court (in which case home
  10. and away teams were determined by which team wore white) and the flag will be 0 otherwise
  11. (when the home team really was playing at home).
  12. Classes
  13. There should be at least three classes in your program.
  14.  
  15. • One class will represent one individual game result (e.g., GameResult). Each object of
  16. this class will be created out of a single line entry in the game database text file.
  17.  
  18. • A second class will hold a list of game result objects in an ArrayList (e.g., GameList).
  19.  
  20. o There should be at least two methods in this second class, namely
  21. computeWinningPercentage (String team) which returns a double value and
  22. getOpponentList(String team) which returns an ArrayList of opponent teams.
  23.  
  24. o The method “double computeWinningPercentage(String team)” will compute
  25. and return the winning percentage. Winning percentage will be computed by
  26. dividing the number of wins by the total number of games played a particular
  27. team. This method will return Double.NaN (look it up in Java API for Double) for
  28. teams that have not played any games.
  29.  
  30. o The method ArrayList<String> getOpponentList(String team) will create and
  31. return a list of all of the opponents for the given team, where opponents that
  32. were played more than once should appear multiple times on the list.
  33.  
  34. Finally, you will design a third class (e.g., GamesDatabase) that will handle user input,
  35. including the name of the input file, what team to get information about, and which
  36. information to get about that team.
  37.  
  38. o There needs to be a method in one of the classes that will compute the winning
  39. percentage of all the teams in the database and write them out in a text file
  40. called winning_pct.txt. When you exit the program by typing QUIT, your
  41. program will compute these percentages, write them to winning_pct.txt and
  42. then quit.
  43.  
  44.  
  45.  
  46. First Class
  47.  
  48. package project2;
  49.  
  50. import java.util.Scanner;
  51.  
  52.  
  53. public class GameResult implements Comparable <GameResult>{
  54.     protected String date;
  55.     protected String awayTeam;
  56.     protected int awayTeamScore;
  57.     protected String homeTeam;
  58.     protected int homeTeamScore;
  59.     protected int neutralCourtFlag;
  60.     protected int overtimes;
  61.     /**
  62.      * Constructor for my instance variables
  63.      *
  64.      *
  65.      */
  66.     public GameResult(String date, String awayTeam, int awayTeamScore, String homeTeam, int homeTeamScore, int neutralCourtFlag, int overtimes) {
  67.         this.date = date;
  68.         this.awayTeam = awayTeam;
  69.         this.awayTeamScore = awayTeamScore;
  70.         this.homeTeam = homeTeam;
  71.         this.homeTeamScore = homeTeamScore;
  72.         this.neutralCourtFlag = neutralCourtFlag;
  73.         this.overtimes = overtimes;
  74.        
  75.     }
  76.    
  77.     /**
  78.      * Getter methods for instance variables
  79.      */
  80.    
  81.     public String getDate(){
  82.         return date;
  83.         }
  84.     public String getAwayTeam() {
  85.         return awayTeam;
  86.     }
  87.     public String getHomeTeam() {
  88.         return homeTeam;
  89.     }
  90.     public int getAwayTeamScore() {
  91.         return awayTeamScore;
  92.     }
  93.     public int getHomeTeamScore() {
  94.         return homeTeamScore;
  95.     }
  96.     public int getNeutralCourtFlag() {
  97.         return neutralCourtFlag;
  98.     }
  99.     public int getOvertimes()
  100.     {
  101.         return overtimes;
  102.     }
  103.    
  104.    
  105.     public int compareTo(GameResult arg0) {
  106.         return 0;
  107.     }
  108.    
  109.    
  110.     /**
  111.      * ToString method for a game's information
  112.      */
  113.    
  114.     public String toString() {
  115.         String gameStats;
  116.         gameStats = "";
  117.         gameStats =    "Date: " + getDate() + "   " + getAwayTeam() + ": " + getAwayTeamScore() + "   " + getHomeTeam() + ": "
  118.                 + getHomeTeamScore() + "  Neutral Court Flags: "  + getNeutralCourtFlag() + "   " + "Overtimes: " + getOvertimes() ;
  119.         return gameStats;
  120.        
  121.     }
  122.    
  123. }
  124.  
  125. ***************************************************************************************************************************************
  126.  
  127. 2nd Class
  128. package project2;
  129.  
  130. import java.util.Scanner;
  131. import java.util.ArrayList;
  132. import java.util.List;
  133. import java.util.StringTokenizer;
  134. import java.io.File;
  135. import java.io.FileOutputStream;
  136. import java.io.FileReader;
  137. import java.io.FileNotFoundException;
  138. import java.io.FileWriter;
  139. import java.io.IOException;
  140. import java.io.BufferedReader;
  141. import java.io.PrintWriter;
  142. import java.io.PrintStream;
  143.  
  144.  
  145. public class GameList  {
  146.     // neutral court flag, and the number of overtimes.
  147.  
  148.     private ArrayList<GameResult> _gameList;
  149.  
  150.     // Default Constructor
  151.  
  152.     public GameList() {
  153.        
  154.         _gameList = new ArrayList<GameResult>();
  155.     }
  156.  
  157.     public void readFromTextFile() {
  158.            String fileName = getInputFileName();
  159.            GameResult objGameInfo;
  160.            try {
  161.            
  162.                 BufferedReader br;
  163.                 FileReader fr;
  164.                 fr = new FileReader("scores.txt");
  165.                 br = new BufferedReader(fr);
  166.                 String oneGameData;
  167.            
  168.                 do {
  169.                     oneGameData = br.readLine();
  170.                     if (oneGameData == null) {
  171.                         break;
  172.                 }
  173.                     else {
  174.                     objGameInfo = processOneGameInfo(oneGameData);
  175.                     _gameList.add(objGameInfo);
  176.                 }
  177.                 while(oneGameData != null);
  178.                 br.close();
  179.                
  180.             //catching filenotfound exception
  181.                
  182.                 }
  183.                 catch(FileNotFoundException fnfe) {
  184.                     System.out.println(fnfe.getMessage());
  185.           //catching IOException
  186.                
  187.            } catch(IOException ioe) {
  188.              System.out.println(ioe.getMessage());
  189.             }
  190.            
  191.         }
  192.     /** Asks user the name of the input file and returns it as a string
  193.      *
  194.      */
  195.     private String getInputFileName() {
  196.         Scanner scan = new Scanner(System.in);
  197.         System.out.println("Please enter the name of the file to read from");
  198.         return scan.nextLine();
  199.     }
  200.     /*
  201.      * Asks user for the name of the output file and returns it as a string
  202.      */
  203.     private String getoutPutFileName() {
  204.         Scanner scan = new Scanner(System.in);
  205.         System.out.println("Please enter the name of the file to write output from");
  206.         return scan.nextLine();
  207.     }
  208.    
  209.     /**
  210.      * Given string representation of one line of info about a basketball game
  211.      * this method parses the info and puts them into the fields of a GameResult object
  212.      *
  213.      * These fields are separated by using StringTokenizer
  214.      * @throws Exception
  215.      *
  216.      */
  217.    
  218.     public GameResult processOneGameInfo(String strGameInfo) throws Exception {
  219.        
  220.         GameResult gameInfo;
  221.         StringTokenizer tokenizer = new StringTokenizer(strGameInfo);
  222.        
  223.         if (tokenizer.countTokens()!= 7) {
  224.             System.out.println(strGameInfo);
  225.             throw new Exception(
  226.                     "Unexpected number of fields in a record: " + tokenizer.countTokens());
  227.         }
  228.        
  229.        
  230.        
  231.         String date = tokenizer.nextToken();
  232.         String awayTeam = tokenizer.nextToken();
  233.         int awayTeamScore = Integer.parseInt(tokenizer.nextToken());
  234.         String homeTeam = tokenizer.nextToken();
  235.         int homeTeamScore = Integer.parseInt(tokenizer.nextToken());
  236.         int neutralCourtFlag = Integer.parseInt(tokenizer.nextToken());
  237.         int overtimes = Integer.parseInt(tokenizer.nextToken());
  238.        
  239.         //constructing a GameResult object using the tokenized values
  240.         gameInfo = new GameResult(date, awayTeam, awayTeamScore, homeTeam,
  241.                 homeTeamScore, neutralCourtFlag, overtimes);
  242.         return gameInfo;
  243.         }
  244.    
  245.         public void displayList() {
  246.             for(GameResult gameInfo: _gameList) {
  247.                 System.out.println(gameInfo);
  248.             }
  249.         }
  250.    
  251.         public double computeWinningPercentage(String team) {
  252.            
  253.             int gameCount = 0;
  254.             int winCount = 0;
  255.             readFromTextFile();
  256.            
  257.             for(GameResult gameInfo: _gameList) {
  258.                    
  259.                
  260.                     if(team.equals(gameInfo.getHomeTeam()) || team.equals(gameInfo.getAwayTeam())) {
  261.                         gameCount ++;
  262.                         if(team.equals(gameInfo.getHomeTeam())
  263.                                 && gameInfo.getHomeTeamScore()  > gameInfo.getAwayTeamScore() ){  //Wouldnt this return nothing since
  264.                                                                                                    //my instance variables are empty?
  265.                                 winCount ++;
  266.                         }
  267.                         else if(team.equals(gameInfo.getAwayTeam()) &&
  268.                                 gameInfo.getAwayTeamScore() > gameInfo.getHomeTeamScore() ) {
  269.                                 winCount ++;
  270.                         }
  271.                            
  272.                     }
  273.            
  274.                     else
  275.                         System.out.println("That team does not exist in the data base.");
  276.                         return Double.NaN;
  277.  
  278.                     }
  279.             double winningPercent = (winCount/gameCount) * 100;
  280.             return winningPercent;     
  281.                
  282.             }
  283.        
  284.     public String getOpponentList(String team){
  285.            
  286.             ArrayList<String> opponentList;
  287.             readFromTextFile();
  288.             for(GameResult gameInfo: _gameList)
  289.             {
  290.                  if (team.hasString(homeTeam))
  291.                  {
  292.                      
  293.                    
  294.                    opponentList.add(awayTeam); //the add functions don't work
  295.                    
  296.                 }
  297.                  
  298.                  else if (team.equals(getAwayTeam())) //this is not defined.. I didnt use my instance variables correctly I think
  299.                  {
  300.                      return (gameInfo.getHomeTeam())
  301.                  }
  302.                  else
  303.                      System.out.println("Team not found");
  304.             }
  305.  
  306. }
  307.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement