Permalink
Browse files

Inner loop - Green: Implement statement formatter

  • Loading branch information...
CarlosMChica committed May 2, 2016
1 parent 1ed7495 commit 8a262bf34a4f50d900996b22ac1465c24a219ffc
@@ -6,6 +6,44 @@
public String runningBalance;
public ViewStatementLine(String date, String amount, String runningBalance) {
+ this.date = date;
+ this.amount = amount;
+ this.runningBalance = runningBalance;
+ }
+
+ @Override public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ ViewStatementLine that = (ViewStatementLine) o;
+
+ if (date != null ? !date.equals(that.date) : that.date != null) {
+ return false;
+ }
+ if (amount != null ? !amount.equals(that.amount) : that.amount != null) {
+ return false;
+ }
+ return runningBalance != null
+ ? runningBalance.equals(that.runningBalance)
+ : that.runningBalance == null;
+ }
+
+ @Override public int hashCode() {
+ int result = date != null ? date.hashCode() : 0;
+ result = 31 * result + (amount != null ? amount.hashCode() : 0);
+ result = 31 * result + (runningBalance != null ? runningBalance.hashCode() : 0);
+ return result;
+ }
+ @Override public String toString() {
+ return "ViewStatementLine{" +
+ "date='" + date + '\'' +
+ ", amount='" + amount + '\'' +
+ ", runningBalance='" + runningBalance + '\'' +
+ '}';
}
}
@@ -1,10 +1,38 @@
package me.panavtec.androidbankkata.account.statement;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Comparator;
import java.util.List;
+import java.util.Locale;
import me.panavtec.androidbankkata.ViewStatementLine;
+import static java.lang.String.valueOf;
+import static java.util.Collections.reverseOrder;
+import static java.util.Collections.sort;
+
public class StatementFormatter {
+
+ private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy", Locale.UK);
+ private static final Comparator<StatementLine> STATEMENT_LINE_DATE_COMPARATOR =
+ new Comparator<StatementLine>() {
+ @Override public int compare(StatementLine lhs, StatementLine rhs) {
+ return lhs.date().compareTo(rhs.date());
+ }
+ };
+
public List<ViewStatementLine> format(Statement statement) {
- return null;
+ return statementLinesFrom(statement);
+ }
+
+ private List<ViewStatementLine> statementLinesFrom(Statement statement) {
+ List<StatementLine> orderedLines = new ArrayList<>(statement.lines());
+ sort(orderedLines, reverseOrder(STATEMENT_LINE_DATE_COMPARATOR));
+ List<ViewStatementLine> viewStatementLines = new ArrayList<>();
+ for (StatementLine statementLine : orderedLines) {
+ viewStatementLines.add(new ViewStatementLine(DATE_FORMAT.format(statementLine.date()),
+ valueOf(statementLine.amount()), valueOf(statementLine.runningBalance())));
+ }
+ return viewStatementLines;
}
}

0 comments on commit 8a262bf

Please sign in to comment.