jdalbey

Golf Pool method implementation

Jan 24th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.59 KB | None | 0 0
  1. import java.io.*;
  2. /**
  3.  * GolfPool is an application to find the winner of a golf pool.
  4.  *
  5.  * @author  J. Dalbey
  6.  * @version 0.1
  7.  */
  8. public final class GolfPool
  9. {
  10.  
  11.     /**
  12.      *  Entry point for the application.  Read the data file
  13.      *  name from the command line.
  14.      *  
  15.      *  @param args command line arguments, first argument is
  16.      *  name of data file.
  17.      */
  18.     public static void main(String[] args)
  19.     {
  20.         // Create instance of GolfPool
  21.         GolfPool app = new GolfPool();
  22.         // IF no argument given THEN
  23.         if (args.length == 0)
  24.         {
  25.             // PRINT missing data file message
  26.             System.out.println("Please provide the name of the golf scores file.");
  27.         }
  28.         // ELSE
  29.         else
  30.         {
  31.             // CALL run with filename
  32.             app.run(args[0]);
  33.         } // END IF        
  34.     }
  35.  
  36.     /** Read data file and compute winner and display results.
  37.     @param filename the name of the data file to be processed
  38.      */
  39.     public void run(String filename)
  40.     {
  41.         // OPEN File for filename
  42.         File datafile = new File(filename);
  43.         Reader rdr = null;
  44.         // TRY
  45.         try
  46.         {
  47.             // CREATE Reader from File
  48.             rdr = new FileReader(datafile);
  49.  
  50.             // CALL readGolfScores with Reader
  51.             AverageList averages = new AverageList();
  52.             averages.readGolfScores(rdr);
  53.             // IF doesWinnerExist THEN
  54.             if (averages.doesWinnerExist())
  55.             {
  56.                 // CALL computeAverages
  57.                 averages.computeAverages();
  58.                 // CALL determineWinner
  59.                 averages.determineWinner();
  60.                 // CALL getWinner
  61.                 Golfer winner = averages.getWinner();
  62.                 // PRINT winner's name, average, rounds
  63.                 System.out.println("Winning golfer: " + winner.name);
  64.                 System.out.println("Average score: " + winner.getAverage());
  65.                 System.out.println("Number of rounds: " + winner.getRounds());
  66.             }
  67.             // ELSE
  68.             else
  69.             {
  70.                 // PRINT no eligible winner
  71.                 System.out.println("No eligible winner");
  72.             } // END IF        
  73.         }
  74.         // CATCH File not found
  75.         catch (FileNotFoundException ex)
  76.         {
  77.             // PRINT incorrect filename message
  78.             System.out.println("Sorry, a file named " + filename +
  79.                 " could not be found in the current directory. " +
  80.                 " Check the spelling and try again.");
  81.         } // END TRY
  82.     }
  83. }
  84. import java.util.*;
  85. import java.io.*;
  86. /**
  87.  * AverageList is a list of golfers and their average score for
  88.  * all rounds played.
  89.  *
  90.  * @author  J. Dalbey
  91.  * @version 0.1
  92.  */
  93. public final class AverageList
  94. {
  95.     List<Golfer> list = new ArrayList<Golfer>();
  96.     Golfer winner;
  97.     /**
  98.      * Constructor for objects of class AverageList
  99.      */
  100.     public AverageList()
  101.     {
  102.     }
  103.  
  104.     /** Read golf scores from specified Reader, and accumulate
  105.      * the total for each golfer.  Also count the number of scores read
  106.      * for each golfer.
  107.      * @param rdr Reader from which scores will be obtained
  108.      */
  109.     public void readGolfScores(Reader rdr)
  110.     {
  111.         // CREATE scanner from rdr
  112.         Scanner scan = new Scanner(rdr);
  113.         // WHILE scanner has more lines LOOP
  114.         while (scan.hasNextLine())
  115.         {
  116.             // READ next line from scanner
  117.             String line = scan.nextLine();
  118.             // SPLIT line into fields using ; as delimiter
  119.             String[] splitLine = line.split(";");
  120.             // CREATE a golfer from first field
  121.             Golfer golfer = new Golfer(splitLine[0]);
  122.             int score = Integer.parseInt(splitLine[2]);
  123.             // IF list doesn't contain golfer THEN
  124.             if (!list.contains(golfer))
  125.             {
  126.                 // CALL addScore with third field
  127.                 golfer.addScore(score);
  128.                 // ADD golfer to list
  129.                 list.add(golfer);
  130.             }
  131.             // ELSE
  132.             else
  133.             {
  134.                 // Get golfer from list
  135.                 int index = list.indexOf(golfer);
  136.                 golfer = list.get(index);
  137.                 // CALL addScore with third field
  138.                 golfer.addScore(score);
  139.             } // ENDIF    
  140.         }// END LOOP            
  141.     }
  142.  
  143.     /** Calculate average for each golfer
  144.      */
  145.     public void computeAverages()
  146.     {
  147.         // FOR each golfer in list LOOP
  148.         for (Golfer golfer: list)
  149.         {
  150.             // CALL calculateAverage
  151.             golfer.calculateAverage();
  152.         } // END LOOP            
  153.     }
  154.  
  155.     /** Find lowest average in the list.
  156.      * Assumes computerAverages() has been called.
  157.      */
  158.     public void determineWinner()
  159.     {
  160.         // SET winner to a golfer with average of infinity
  161.         winner = new Golfer("");
  162.         winner.addScore(Integer.MAX_VALUE);
  163.         winner.calculateAverage();
  164.         // FOR each golfer in list LOOP
  165.         for (Golfer golfer: list)
  166.         {
  167.             // IF golfer >= 3 rounds THEN
  168.             if (golfer.getRounds() >= 3)
  169.             {
  170.                 // compare golfer to winner
  171.                 // IF golfer < winner THEN
  172.                 if (golfer.compareTo(winner) < 0)
  173.                 {
  174.                     // SET winner to golfer
  175.                     winner = golfer;
  176.                 } // END IF
  177.             } // END IF
  178.         } // END LOOP        
  179.     }
  180.  
  181.     /** Return the winning golfer.
  182.      * Assumes doesWinnerExist is true
  183.      * Assumes determineWinner has been called.
  184.      * @return golfer who won
  185.      */
  186.     public Golfer getWinner()
  187.     {
  188.         return winner;
  189.     }
  190.  
  191.     /** Determine if there is an eligible winner.
  192.      * @return false if no golfer has played three rounds, true otherwise
  193.      */
  194.     public boolean doesWinnerExist()
  195.     {
  196.         // SET result to false
  197.         boolean result = false;
  198.         // FOR each golfer in list LOOP
  199.         for (Golfer golfer: list)
  200.         {
  201.             // IF golfer >= 3 rounds THEN
  202.             if (golfer.getRounds() >= 3)
  203.             {
  204.                 // SET result to true
  205.                 result = true;
  206.             } // END IF
  207.         } // END LOOP
  208.         // return result;        
  209.         return result;
  210.     }
  211.  
  212.     /** For unit testing: accessor to list size */
  213.     int size()
  214.     {
  215.         return list.size();
  216.     }
  217. }
  218.  
  219. /**
  220.  * Golfer is a record of the statistics for a golfer for a month.
  221.  *
  222.  * @author  J. Dalbey
  223.  * @version 0.1
  224.  */
  225. public final class Golfer implements Comparable<Golfer>
  226. {
  227.     final String name;
  228.     private int numberRounds;
  229.     private long totalScore;
  230.     private double averageScore;
  231.  
  232.     /**
  233.      * Constructor for objects of class Golfer
  234.      */
  235.     public Golfer(String name)
  236.     {
  237.         this.name = name;
  238.     }
  239.  
  240.     /**
  241.      * Add one score to total
  242.      * @param roundScore the score to be added to the total
  243.      * @pre roundScore > 0
  244.      */
  245.     public void addScore(int roundScore)
  246.     {    
  247.         // Increment totalScore by roundScore
  248.         totalScore += roundScore;
  249.         // Increment numberRounds by 1  
  250.         numberRounds += 1;
  251.     }
  252.  
  253.     /**
  254.      * Calculate average score.
  255.      * @pre addScore has been called at least once.
  256.      */
  257.     public void calculateAverage()
  258.     {
  259.         // Compute averageScore as totalScore / numberRounds        
  260.         averageScore = (double) totalScore / numberRounds;
  261.     }
  262.  
  263.     /**
  264.      * Compare two golfers.  Lowest average wins,
  265.      * ties go to player with most rounds.
  266.      * @param other golfer to be compared to this one.
  267.      * @return 0 if the two golfers are equal, -1 if this golfer is smaller,
  268.      * +1 if this golfer is larger.
  269.      */
  270.     public int compareTo(Golfer other)
  271.     {
  272.         // SET result to 0
  273.         int result = 0;
  274.         // IF averageScore < other's THEN
  275.         if (averageScore < other.averageScore)
  276.         {
  277.             // SET result to -1
  278.             result = -1;
  279.         }
  280.         // ELSE
  281.         else
  282.         {
  283.             // IF averageScore > other's THEN
  284.             if (averageScore > other.averageScore)
  285.             {
  286.                 // SET result to 1
  287.                 result = 1;
  288.             }
  289.             // ELSE
  290.             else
  291.             {                
  292.                 // IF numberRounds > other's THEN
  293.                 if (numberRounds > other.numberRounds)
  294.                 {
  295.                     // SET result to -1; he's the winner
  296.                     result = -1;
  297.                 }
  298.                 // ELSE
  299.                 else
  300.                 {
  301.                     // IF numberRounds > other's THEN
  302.                     if (numberRounds < other.numberRounds)
  303.                     {
  304.                         // SET result to 1
  305.                         result = 1;
  306.                     }// ENDIF
  307.                 }// ENDIF
  308.             }// ENDIF
  309.         }// ENDIF
  310.         // RETURN result        
  311.         return result;
  312.     }
  313.  
  314.     /** Accessors */
  315.     public int getRounds()
  316.     {
  317.         return numberRounds;
  318.     }
  319.  
  320.     public double getAverage()
  321.     {
  322.         return averageScore;
  323.     }
  324.  
  325.     /** Compare two golfers for equality.  
  326.      * @return true if they have the same name.
  327.      */
  328.     @Override
  329.     public boolean equals(Object aThat)
  330.     {
  331.         if ( this == aThat ) return true;
  332.         if ( !(aThat instanceof Golfer) ) return false;
  333.         Golfer that = (Golfer)aThat;
  334.         return this.name.equals(that.name);
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment