StatementPrinter, test for print method green
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 4 Dec 2016 20:09:46 +0000 (21:09 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 4 Dec 2016 22:30:44 +0000 (23:30 +0100)
TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/StatementPrinter.java
TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Transaction.java
TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/StatementPrinterShould.java

index e55d7a7..7304ad9 100644 (file)
@@ -1,11 +1,15 @@
 package org.craftedsw.feature;
 
+import java.text.DecimalFormat;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
 
 public class StatementPrinter {
 
        private static final String STATEMENT_HEADER = "DATE | AMOUNT | BALANCE";
-       
+
        private final Console console;
 
        public StatementPrinter(Console console) {
@@ -14,6 +18,22 @@ public class StatementPrinter {
 
        public void print(List<Transaction> transactions) {
                console.printLine(STATEMENT_HEADER);
+               AtomicInteger runningBalance = new AtomicInteger(0);
+               transactions.stream()
+                                       .map(transaction -> statementLine(transaction, runningBalance))
+                                       .collect(Collectors.toCollection(LinkedList::new))
+                                       .descendingIterator()
+                                       .forEachRemaining(console::printLine);
+       }
+
+       private String statementLine(Transaction transaction, AtomicInteger runningBalance) {
+               DecimalFormat decimalFormatter = new DecimalFormat("#.00");
+
+               return transaction.date()
+                               + " | "
+                               + decimalFormatter.format(transaction.ammount())
+                               + " | "
+                               + decimalFormatter.format(runningBalance.addAndGet(transaction.ammount()));
        }
 
 }
index 8ae726c..f5f09da 100644 (file)
@@ -10,6 +10,14 @@ public class Transaction {
                this.amount = amount;
        }
 
+       public String date() {
+               return this.date;
+       }
+
+       public int ammount() {
+               return this.amount;
+       }
+       
        @Override
        public int hashCode() {
                final int prime = 31;
index 84eea6a..de914f6 100644 (file)
@@ -41,9 +41,9 @@ public class StatementPrinterShould {
                
                InOrder inOrder = inOrder(console);
                inOrder.verify(console).printLine("DATE | AMOUNT | BALANCE");
-               inOrder.verify(console).printLine("10/04/2014 | 500.00 | 1400.00");
-               inOrder.verify(console).printLine("02/04/2014 | -100.00 | 900.00");
-               inOrder.verify(console).printLine("01/04/2014 | 1000.00 | 1000.00");
+               inOrder.verify(console).printLine("10/04/2014 | 500,00 | 1400,00");
+               inOrder.verify(console).printLine("02/04/2014 | -100,00 | 900,00");
+               inOrder.verify(console).printLine("01/04/2014 | 1000,00 | 1000,00");
        }
 
        private List<Transaction> transactionsContaining(Transaction...transactions) {