package com.jstanier;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.googlecode.jcsv.CSVStrategy;
import com.googlecode.jcsv.reader.CSVReader;
import com.googlecode.jcsv.reader.internal.CSVReaderBuilder;

@Component
public class InputParser {

    @Autowired
    private InputReader inputReader;

    public List<TweetToSchedule> parseInput(String pathToCsvFile) {
        return parseCsvFile(pathToCsvFile);
    }

    private List<TweetToSchedule> parseCsvFile(String pathToCsvFile) {
        List<TweetToSchedule> csvData = null;
        try {
            Reader reader = inputReader.getInputReader(pathToCsvFile);
            CSVReader<TweetToSchedule> csvParser = createCSVReader(reader);
            csvData = csvParser.readAll();
        } catch (FileNotFoundException e) {
            exitWithError("File not found at " + pathToCsvFile);
        } catch (IOException e) {
            exitWithError("IO exception when reading " + pathToCsvFile);
        }
        return csvData;
    }

    private CSVReader<TweetToSchedule> createCSVReader(Reader reader) {
        return (CSVReader<TweetToSchedule>) new CSVReaderBuilder(reader)
                .strategy(CSVStrategy.UK_DEFAULT)
                .entryParser(new TweetToScheduleEntryParser()).build();
    }

    private void exitWithError(String error) {
        System.err.println(error);
        System.exit(1);
    }
}

@@ -20,46 +20,8 @@
 r;%0A%0A
-import java.io.FileNotFoundException;%0A
 impo
@@ -683,26 +683,8 @@
 ll;%0A
-        try %7B%0A    
     
@@ -742,28 +742,24 @@
 ToCsvFile);%0A
-    
         CSVR
@@ -814,24 +814,38 @@
 er(reader);%0A
+        try %7B%0A
             
@@ -896,20 +896,10 @@
 ch (
-FileNotFound
+IO
 Exce
@@ -925,164 +925,35 @@
     
-exitWithError(%22File not found at %22 + pathToCsvFile);%0A        %7D catch (IOException e) %7B%0A            exitWithError(%22IO exception when reading %22 + pathToCsvFil
+throw new RuntimeException(
 e);%0A
@@ -1269,120 +1269,7 @@
    %7D
-%0A%0A    private void exitWithError(String error) %7B%0A        System.err.println(error);%0A        System.exit(1);%0A    %7D
 %0A%7D%0A

Throw RuntimeException instead of System.exit