package ca.jonathanfritz.budgey.importer;

import ca.jonathanfritz.budgey.Transaction;
import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class ScotiabankCSVParser implements CSVParser {

	// date format is 9/1/15
	private final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
	        .appendMonthOfYear(1)
	        .appendLiteral('/')
	        .appendDayOfMonth(1)
	        .appendLiteral('/')
	        .appendTwoDigitYear(2000)
	        .toFormatter();

	private final NumberFormat cadFormat = NumberFormat.getNumberInstance(Locale.CANADA);

	private final String accountNumber;

	public ScotiabankCSVParser(final String accountNumber) {
		this.accountNumber = accountNumber;
	}

	@Override
	public Transaction parse(final String[] fields) {

		Money amount = Money.zero(CurrencyUnit.CAD);
		try {
			if (fields.length > 2 && StringUtils.isNotBlank(fields[2])) {
				final BigDecimal cad = new BigDecimal(cadFormat.parse(fields[2]).toString());
				amount = Money.of(CurrencyUnit.CAD, cad);
			}
		} catch (final ParseException e) {
			throw new RuntimeException("Failed to parse transaction [" + Joiner.on(",").join(fields) + "]", e);
		}

		return Transaction.newBuilder()
				.setAccountNumber(this.accountNumber)
		        .setTransactionDate(formatter.parseDateTime(fields[0]))
		        .setDescription(fields[1].replaceAll("^\"|\"$", ""))
		        .setAmount(amount)
		        .build();
	}
}

@@ -609,24 +609,18 @@
 lder()%0A%09
-        
+%09%09
 .appendM
@@ -630,32 +630,26 @@
 hOfYear(1)%0A%09
-        
+%09%09
 .appendLiter
@@ -653,32 +653,26 @@
 teral('/')%0A%09
-        
+%09%09
 .appendDayOf
@@ -681,24 +681,18 @@
 nth(1)%0A%09
-        
+%09%09
 .appendL
@@ -704,24 +704,18 @@
 l('/')%0A%09
-        
+%09%09
 .appendT
@@ -733,24 +733,18 @@
 (2000)%0A%09
-        
+%09%09
 .toForma
@@ -1513,32 +1513,26 @@
 ntNumber)%0A%09%09
-        
+%09%09
 .setTransact
@@ -1573,32 +1573,26 @@
 elds%5B0%5D))%0A%09%09
-        
+%09%09
 .setDescript
@@ -1634,24 +1634,18 @@
  %22%22))%0A%09%09
-        
+%09%09
 .setAmou
@@ -1661,16 +1661,10 @@
 )%0A%09%09
-        
+%09%09
 .bui

Fix whitespace