From 895a1c0b62e8c1779d079e17b0f4529dfbf319cc Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 4 Dec 2016 21:09:46 +0100 Subject: [PATCH] StatementPrinter, test for print method green --- .../org/craftedsw/feature/StatementPrinter.java | 22 +++++++++++++++++++++- .../java/org/craftedsw/feature/Transaction.java | 8 ++++++++ .../craftedsw/feature/StatementPrinterShould.java | 6 +++--- 3 files changed, 32 insertions(+), 4 deletions(-) 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) { -- 2.1.4