From: Gustavo Martin Morcuende Date: Sun, 4 Dec 2016 20:09:46 +0000 (+0100) Subject: StatementPrinter, test for print method green X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=895a1c0b62e8c1779d079e17b0f4529dfbf319cc;p=JavaForFun StatementPrinter, test for print method green --- diff --git a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/StatementPrinter.java b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/StatementPrinter.java index e55d7a7..7304ad9 100644 --- a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/StatementPrinter.java +++ b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/StatementPrinter.java @@ -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 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())); } } diff --git a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Transaction.java b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Transaction.java index 8ae726c..f5f09da 100644 --- a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Transaction.java +++ b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Transaction.java @@ -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; diff --git a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/StatementPrinterShould.java b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/StatementPrinterShould.java index 84eea6a..de914f6 100644 --- a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/StatementPrinterShould.java +++ b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/StatementPrinterShould.java @@ -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 transactionsContaining(Transaction...transactions) {