Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package resultGenerator;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.List;
- public class Sorter {
- private BufferedReader startReader;
- private BufferedReader endReader;
- /**
- * Constructor of Sorter which constructs BufferedReaders of the startReader and the endReader.
- * @param startFile
- * The file that contains the startTimes
- * @param endFile
- * The file that contains the endTimes
- * @param resultFile
- * The file that result will be written to
- * @throws FileNotFoundException if either startFile or endFile do not exist
- * @throws UnsupportedEncodingException if either startFile or endFile is not UTF-8
- */
- public Sorter(String startFile, String endFile) throws FileNotFoundException, UnsupportedEncodingException {
- startReader = FileHandler.readFromFile(startFile);
- endReader = FileHandler.readFromFile(endFile);
- }
- /**
- * Reads lines from a file specified by the BufferedReader and creates a list with said lines as RaceTimes.
- * @param br which is a BufferedReader including the file to be read and parsed.
- * @return a ArrayList<RaceTime> of the RaceTimes which exist in the file which BufferedReader reads from.
- * @throws ParseException if a line has a bad format.
- * @throws IOException if the file can't be read.
- */
- private ArrayList<RaceTime> readResultsList(BufferedReader br) throws IOException, ParseException {
- ArrayList<RaceTime> list = new ArrayList<RaceTime>();
- String line = null;
- while ((line = br.readLine()) != null) {
- list.add(new RaceTime(line));
- }
- return list;
- }
- /**
- * ReadStartTimes return an ArrayList<RaceTime> of all startTimes.
- * @return ArrayList<RaceTime> of all startTimes.
- * @throws IOException if a file can't be read.
- * @throws ParseException if a line can't be parsed.
- */
- public ArrayList<RaceTime> readStartTimes() throws IOException, ParseException {
- return readResultsList(startReader);
- }
- /**
- * ReadStartTimes return an ArrayList<RaceTime> of all endTimes.
- * @return ArrayList<RaceTime> of all endTimes.
- * @throws IOException if a file can't be read.
- * @throws ParseException if a line can't be parsed.
- */
- public ArrayList<RaceTime> readEndTimes() throws IOException, ParseException {
- return readResultsList(endReader);
- }
- /**
- * This method reads start, end and contestant (name) files,
- * and produces a results file.
- * @param startFile The file containing start times.
- * @param endFile The file containing end times
- * @param nameFile The file containing the contestant list.
- * @param resultsFile The target file for writing results.
- */
- private void processFiles(String startFile, String endFile, String nameFile, String resultsFile) {
- try {
- Sorter sorter1 = new Sorter(startFile, endFile);
- StartAndEndTimes times = new StartAndEndTimes();
- times.addAllStartTimes(sorter1.readStartTimes());
- times.addAllEndTimes(sorter1.readEndTimes());
- List<RaceResult> results = times.getResults();
- ContestantList contestants = new ContestantList();
- contestants.readContestantsFile(nameFile);
- StartAndEndTimes.writeToResultsFile(resultsFile, contestants, results);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- } catch (DuplicateContestantNumberException e) {
- e.printStackTrace();
- }
- }
- /**
- * This is the main method of the Sorter application.
- * @param args The arguments of the application: {start time file, end time file, contestant file, results file}
- * @throws FileNotFoundException
- */
- public Sorter(String[] args) throws FileNotFoundException{
- // Handles fallback argument values
- if (args.length != 4) {
- args = new String[4];
- args[0] = "./defaultStart.txt";
- args[1] = "./defaultStop.txt";
- args[2] = "./namnfil.txt";
- args[3] = "./resultat.txt";
- // Note: does NOT process args[3], so the results file is not needed.
- for (int i = 0; i <= 2; i++) {
- File f = new File(args[i]);
- if(!f.exists()){
- throw new FileNotFoundException();
- }
- }
- }
- processFiles(args[0], args[1], args[2], args[3]);
- }
- /**
- * This is the main method of the Sorter application.
- * @param args The arguments of the application: {start time file, end time file, contestant file, results file}
- */
- public static void main(String[] args){
- try {
- new Sorter(args);
- } catch (FileNotFoundException ie){
- ie.printStackTrace();
- System.exit(1);
- }
- }
- }
Add Comment
Please, Sign In to add comment