Guest User

Untitled

a guest
Jun 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. package resultGenerator;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.UnsupportedEncodingException;
  8. import java.text.ParseException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12.  
  13. public class Sorter {
  14.     private BufferedReader startReader;
  15.     private BufferedReader endReader;
  16.  
  17.     /**
  18.      * Constructor of Sorter which constructs BufferedReaders of the startReader and the endReader.
  19.      * @param startFile
  20.      *            The file that contains the startTimes
  21.      * @param endFile
  22.      *            The file that contains the endTimes
  23.      * @param resultFile
  24.      *            The file that result will be written to
  25.      * @throws FileNotFoundException if either startFile or endFile do not exist
  26.      * @throws UnsupportedEncodingException if either startFile or endFile is not UTF-8
  27.      */
  28.     public Sorter(String startFile, String endFile) throws FileNotFoundException, UnsupportedEncodingException {
  29.         startReader = FileHandler.readFromFile(startFile);
  30.         endReader = FileHandler.readFromFile(endFile);
  31.     }
  32.  
  33.     /**
  34.      * Reads lines from a file specified by the BufferedReader and creates a list with said lines as RaceTimes.
  35.      * @param br which is a BufferedReader including the file to be read and parsed.
  36.      * @return a ArrayList<RaceTime> of the RaceTimes which exist in the file which BufferedReader reads from.
  37.      * @throws ParseException if a line has a bad format.
  38.      * @throws IOException if the file can't be read.
  39.      */
  40.     private ArrayList<RaceTime> readResultsList(BufferedReader br) throws IOException, ParseException {
  41.         ArrayList<RaceTime> list = new ArrayList<RaceTime>();
  42.         String line = null;
  43.         while ((line = br.readLine()) != null) {
  44.             list.add(new RaceTime(line));
  45.         }
  46.         return list;
  47.     }
  48.  
  49.     /**
  50.      * ReadStartTimes return an ArrayList<RaceTime> of all startTimes.
  51.      * @return ArrayList<RaceTime> of all startTimes.
  52.      * @throws IOException if a file can't be read.
  53.      * @throws ParseException if a line can't be parsed.
  54.      */
  55.  
  56.     public ArrayList<RaceTime> readStartTimes() throws IOException, ParseException {
  57.         return readResultsList(startReader);
  58.     }
  59.  
  60.     /**
  61.      * ReadStartTimes return an ArrayList<RaceTime> of all endTimes.
  62.      * @return ArrayList<RaceTime> of all endTimes.
  63.      * @throws IOException if a file can't be read.
  64.      * @throws ParseException if a line can't be parsed.
  65.      */
  66.     public ArrayList<RaceTime> readEndTimes() throws IOException, ParseException {
  67.         return readResultsList(endReader);
  68.     }
  69.    
  70.     /**
  71.      * This method reads start, end and contestant (name) files,
  72.      * and produces a results file.
  73.      * @param startFile The file containing start times.
  74.      * @param endFile The file containing end times
  75.      * @param nameFile The file containing the contestant list.
  76.      * @param resultsFile The target file for writing results.
  77.      */
  78.     private void processFiles(String startFile, String endFile, String nameFile, String resultsFile) {
  79.         try {
  80.            
  81.             Sorter sorter1 = new Sorter(startFile, endFile);
  82.             StartAndEndTimes times = new StartAndEndTimes();
  83.            
  84.             times.addAllStartTimes(sorter1.readStartTimes());
  85.             times.addAllEndTimes(sorter1.readEndTimes());
  86.            
  87.             List<RaceResult> results = times.getResults();
  88.            
  89.             ContestantList contestants = new ContestantList();
  90.             contestants.readContestantsFile(nameFile);
  91.            
  92.             StartAndEndTimes.writeToResultsFile(resultsFile, contestants, results);
  93.            
  94.         } catch (IOException e) {
  95.             e.printStackTrace();
  96.         } catch (ParseException e) {
  97.             e.printStackTrace();
  98.         } catch (DuplicateContestantNumberException e) {
  99.             e.printStackTrace();
  100.         }
  101.     }
  102.    
  103.     /**
  104.      * This is the main method of the Sorter application.
  105.      * @param args The arguments of the application: {start time file, end time file, contestant file, results file}
  106.      * @throws FileNotFoundException
  107.      */
  108.     public Sorter(String[] args) throws FileNotFoundException{
  109.  
  110.         // Handles fallback argument values
  111.         if (args.length != 4) {
  112.             args = new String[4];
  113.             args[0] = "./defaultStart.txt";
  114.             args[1] = "./defaultStop.txt";
  115.             args[2] = "./namnfil.txt";
  116.             args[3] = "./resultat.txt";
  117.            
  118.             // Note: does NOT process args[3], so the results file is not needed.
  119.             for (int i = 0; i <= 2; i++) {
  120.                 File f = new File(args[i]);
  121.                 if(!f.exists()){
  122.                     throw new FileNotFoundException();
  123.                 }
  124.             }
  125.         }
  126.        
  127.         processFiles(args[0], args[1], args[2], args[3]);
  128.        
  129.     }
  130.    
  131.     /**
  132.      * This is the main method of the Sorter application.
  133.      * @param args The arguments of the application: {start time file, end time file, contestant file, results file}
  134.      */
  135.     public static void main(String[] args){
  136.         try {
  137.             new Sorter(args);
  138.         } catch (FileNotFoundException ie){
  139.             ie.printStackTrace();
  140.             System.exit(1);
  141.         }
  142.                
  143.     }
  144.    
  145. }
Add Comment
Please, Sign In to add comment