package com.rjconsultores.ventaboletos.utilerias;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;

public class ValidadorImportacaoEcf {

	public static void main(String[] args) {
		BufferedReader br;
		int linha = 1;

		BigDecimal soma = BigDecimal.ZERO;
		try {
			br = new BufferedReader(new FileReader("c:\\Users\\rjgw\\Downloads\\fiscal_cancel_201604060542.txt"));
			try {
				// int linha = 1;
				String line = br.readLine();
				Integer l1 = null;
				Integer l2 = null;
				try {
				while (line != null) {
					if (line.startsWith("1")) {
						l1 = Integer.valueOf(line.substring(51, 65).trim());
						System.out.println("Linha: " + linha + "   --   " + "Valor: " + l1);
						soma = soma.add(new BigDecimal(l1).divide(new BigDecimal(100)));
						l2 = 0;
					}
					if (line.startsWith("2")) {
						l2 += Integer.valueOf(line.substring(47, 62).trim());
					}
					line = br.readLine();
					if (line.startsWith("1") && l1 != null && l2 != null) {
						if (l1.compareTo(l2) != 0) {
							System.out.println("Linha: " + linha);
						}
					}
					linha++;
				}
				} catch (NullPointerException e) {
				}

				System.out.println("Soma: " + soma);

			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				System.out.println("Linha: " + linha);
				e.printStackTrace();
			} finally {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} catch (FileNotFoundException e) {
		}
	}

}
