package com.theaccountant.accountabilityhack.processor;

import com.theaccountant.accountabilityhack.data.Address;
import com.theaccountant.accountabilityhack.data.SchoolEntry;
import com.theaccountant.accountabilityhack.data.SchoolRegistry;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public final class ProcessSchoolLocations implements Processor {

    private static final java.lang.String COMMA = ";";

    @Override
    public void process(final SchoolRegistry registry) throws IOException {
        for (final SchoolEntry entry : processInputFile()) {
            registry.addSchool(entry);
        }
    }

    public static List<SchoolEntry> processInputFile() throws IOException {
        List<SchoolEntry> inputList = new ArrayList<SchoolEntry>();
        InputStream inputFS = ProcessSchoolLocations.class.getClassLoader().getResourceAsStream("01.-hoofdvestigingen-basisonderwijs-2017.csv");
        BufferedReader br = new BufferedReader(new InputStreamReader(inputFS));

        // skip the header of the csv
        inputList = br.lines().skip(1).map(mapToItem).collect(Collectors.toList());
        br.close();
        System.out.println("Done processing addresses");
        return inputList;
    }

    private static Function<String, SchoolEntry> mapToItem = (line) -> {

        String[] p = line.split(COMMA);// a CSV has comma separated lines

        // School adres
        Address adres = Address.builder().
                streetname(p[7]).
                streetNr(p[4]).
                zipcode(p[5]).
                city(p[6]).
                build();

        // SchoolEntry data
        SchoolEntry item = SchoolEntry.builder().
                brin(p[2]).
                bevoegdGezag(Integer.valueOf(p[1])).
                name(p[3]).
                address(adres).
                build();

        return item;
    };
}
@@ -706,355 +706,45 @@
    f
-or (final SchoolEntry entry : processInputFile()) %7B%0A            registry.addSchool(entry);%0A        %7D%0A    %7D%0A%0A    public static List%3CSchoolEntry%3E processInputFile() throws IOException %7B%0A        List%3CSchoolEntry%3E inputList = new ArrayList%3CSchoolEntry%3E();%0A        InputStream inputFS = ProcessSchoolLocations.class.getClassLoader().getResourceAsStream
+inal CsvReader reader = new CsvReader
 (%2201
@@ -789,24 +789,25 @@
 .csv%22);%0A
+%0A
         
 Buffered
@@ -802,89 +802,105 @@
     
-BufferedReader
+while (reader.next()) %7B%0A            final String
  br
+in
  = 
-new BufferedReader(new InputStreamReader(inputFS)
+reader.getString(%22BRIN_NUMMER%22
 );%0A
-%0A
         
 // s
@@ -899,429 +899,317 @@
     
-// skip the header of the csv%0A        inputList = br.lines().skip(1).map(mapToItem).collect(Collectors.toList());%0A        br.close();%0A        System.out.println(%22Done processing addresses%22);%0A        return inputList;
+    final SchoolEntry entry;%0A            if (registry.isSchoolPresent(brin)) %7B%0A                entry = registry.getSchoolByBrin(brin);%0A            %7D else %7B
 %0A    
-%7D%0A%0A
     
-private static Function%3CString, SchoolEntry%3E mapToItem = (line) -%3E %7B%0A%0A        String%5B%5D p = line.split(COMMA);// a CSV has comma separated lines%0A%0A        // School adres%0A        Address adres = 
+        entry = SchoolEntry.builder().brin(brin).build();%0A                registry.addSchool(entry);%0A            %7D%0A%0A            entry.setAddress(
 Addr
@@ -1213,34 +1213,37 @@
 ddress.builder()
-.
 %0A
+    
                 
@@ -1238,24 +1238,25 @@
             
+.
 streetname(p
@@ -1258,14 +1258,39 @@
 ame(
-p%5B7%5D).
+reader.getString(%22STRAATNAAM%22))
 %0A   
@@ -1302,16 +1302,21 @@
         
+    .
 streetNr
@@ -1320,14 +1320,50 @@
 tNr(
-p%5B4%5D).
+reader.getString(%22HUISNUMMER-TOEVOEGING%22))
 %0A   
@@ -1379,16 +1379,21 @@
     
+    .
 zipcode(
 p%5B5%5D
@@ -1392,15 +1392,39 @@
 ode(
-p%5B5%5D).%0A
+reader.getString(%22POSTCODE%22))%0A 
     
@@ -1439,123 +1439,74 @@
     
+   .
 city(
-p%5B6%5D).%0A                build();%0A%0A        // SchoolEntry data%0A        SchoolEntry item = SchoolEntry
+reader.getString(%22CITY%22))%0A                    
 .build
-er().
+());%0A
 %0A   
@@ -1518,136 +1518,133 @@
     
-    brin(p%5B2%5D).%0A                bevoegdGezag(Integer.valueOf(p%5B1%5D)).%0A                name(p%5B3%5D).%0A                address(adres).
+entry.setBevoegdGezag(reader.getInt(%22BEVOEGD GEZAG NUMMER%22));%0A            entry.setName(reader.getString(%22INSTELLINGSNAAM%22));
 %0A   
@@ -1648,24 +1648,26 @@
         
+%7D%0A
         
 build();
@@ -1662,43 +1662,61 @@
     
-build();%0A%0A        return item
+System.out.println(%22Done processing addresses%22)
 ;%0A    %7D
-;
 %0A%7D
+%0A

Reimplement using standard format.