-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 92a8469
Showing
19 changed files
with
898 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/target | ||
/.settings | ||
/.idea/ | ||
.project | ||
.classpath | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
Bank account kata | ||
================= | ||
|
||
Think of your personal bank account experience | ||
When in doubt, go for the simplest solution | ||
|
||
Requirements | ||
------------ | ||
|
||
* Deposit and Withdrawal | ||
* Transfer | ||
* Account statement (date, amount, balance) | ||
* Statement printing | ||
* Statement filters (just deposits, withdrawal, date) | ||
|
||
Statement should have transactions in the following format: | ||
|
||
``` | ||
DATE | AMOUNT | BALANCE | ||
10/04/2014 | 500.00 | 1400.00 | ||
02/04/2014 | -100.00 | 900.00 | ||
01/04/2014 | 1000.00 | 1000.00 | ||
``` | ||
|
||
|
||
The Rules | ||
--------- | ||
|
||
1. One level of indentation per method | ||
2. Don’t use the 'else' keyword | ||
3. Wrap all primitives and Strings | ||
4. First class collections | ||
5. One dot per line | ||
6. Don’t abbreviate | ||
7. Keep all entities small (50 lines) | ||
8. No classes with more than two instance variables | ||
9. No getters/setters/properties | ||
|
||
|
||
Implementation Guidelines | ||
========================= | ||
|
||
**Note:** Start with an acceptance test through the Account class: | ||
|
||
public class Account { | ||
|
||
public void deposit(int amount); | ||
|
||
public void withdraw(int amount); | ||
|
||
public void printStatement(); | ||
} | ||
|
||
You are not allowed to add any other methods to that class | ||
|
||
#### For more information: | ||
|
||
- [Object Calisthenics pdf](http://www.cs.helsinki.fi/u/luontola/tdd-2009/ext/ObjectCalisthenics.pdf) | ||
- Object Calisthenics (full book), Jeff Bay in: The ThoughtWorks Anthology. | ||
Pragmatic Bookshelf 2008 | ||
- Original idea for the kata: [How Object-Oriented Are You Feeling Today?](https://www.slideshare.net/KrzysztofJelski/how-object-oriented-are-you-feeling-today) - Krzysztof Jelski (Session on the Software Craftsmanship UK 2011 conference) | ||
- sample solution: [https://github.com/sandromancuso/Bank-kata.git](https://github.com/sandromancuso/Bank-kata.git) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.pragmatists</groupId> | ||
<artifactId>bank-kata</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>bank-kata</name> | ||
|
||
<properties> | ||
<java.version>1.9</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>5.6.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.17.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jbehave</groupId> | ||
<artifactId>jbehave-core</artifactId> | ||
<version>4.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-junit-jupiter</artifactId> | ||
<version>3.4.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
</configuration> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.pragmatists.bank; | ||
|
||
public class Printable { | ||
|
||
private String value = ""; | ||
|
||
public void println(String line){ | ||
value += line + '\n'; | ||
} | ||
|
||
public void println(StringBuilder builder) { | ||
println(builder.toString()); | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void printWith(java.io.PrintStream out) { | ||
out.print(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.pragmatists.bank; | ||
|
||
import com.pragmatists.bank.domain.Account; | ||
import com.pragmatists.bank.domain.Amount; | ||
import com.pragmatists.bank.domain.Statement; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static com.pragmatists.bank.domain.Amount.amountOf; | ||
|
||
public class StartApp { | ||
|
||
public static void main(String[] args) { | ||
Account account = new Account(new Statement()); | ||
Printable printer = new Printable(); | ||
|
||
account.deposit(amountOf(1000), LocalDate.of(2012, 1, 10)); | ||
account.deposit(amountOf(2000), LocalDate.of(2012, 1, 12)); | ||
account.withdrawal(Amount.amountOf(500), LocalDate.of(2012, 1, 14)); | ||
|
||
account.printStatement(printer); | ||
|
||
printer.printWith(System.out); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.pragmatists.bank.domain; | ||
|
||
import com.pragmatists.bank.Printable; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static com.pragmatists.bank.domain.Amount.amountOf; | ||
|
||
|
||
public class Account { | ||
|
||
private Statement statement; | ||
|
||
private Amount balance = amountOf(0); | ||
|
||
public Account(Statement statement) { | ||
this.statement = statement; | ||
} | ||
|
||
public void deposit(Amount value, LocalDate date) { | ||
recordTransaction(value, date); | ||
} | ||
|
||
public void withdrawal(Amount value, LocalDate date) { | ||
recordTransaction(value.negative(), date); | ||
} | ||
|
||
public void printStatement(Printable printer) { | ||
statement.printTo(printer); | ||
} | ||
|
||
private void recordTransaction(Amount value, LocalDate date) { | ||
Transaction transaction = new Transaction(value, date); | ||
Amount balanceAfterTransaction = transaction.balanceAfterTransaction(balance); | ||
balance = balanceAfterTransaction; | ||
statement.addLineContaining(transaction, balanceAfterTransaction); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.pragmatists.bank.domain; | ||
|
||
import java.text.DecimalFormat; | ||
|
||
public class Amount { | ||
|
||
private DecimalFormat decimalFormat = new DecimalFormat("#.00"); | ||
|
||
private int value; | ||
|
||
public Amount(int value) { | ||
this.value = value; | ||
} | ||
|
||
public static Amount amountOf(int value) { | ||
return new Amount(value); | ||
} | ||
|
||
public Amount plus(Amount otherAmount) { | ||
return amountOf(this.value + otherAmount.value); | ||
} | ||
|
||
public boolean isGreaterThan(Amount otherAmount) { | ||
return this.value > otherAmount.value; | ||
} | ||
|
||
public Amount absoluteValue() { | ||
return amountOf(Math.abs(value)); | ||
} | ||
|
||
public String moneyRepresentation() { | ||
return decimalFormat.format(value); | ||
} | ||
|
||
public Amount negative() { | ||
return amountOf(-value); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
Amount other = (Amount) obj; | ||
if (value != other.value) | ||
return false; | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.pragmatists.bank.domain; | ||
|
||
import com.pragmatists.bank.Printable; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class Statement { | ||
|
||
private static final int TOP_OF_THE_LIST = 0; | ||
|
||
public static final String STATEMENT_HEADER = "date | credit | debit | balance"; | ||
|
||
private final List<StatementLine> statementLines = new LinkedList<StatementLine>(); | ||
|
||
public void addLineContaining(Transaction transaction, Amount currentBalance) { | ||
statementLines.add(TOP_OF_THE_LIST, new StatementLine(transaction, currentBalance)); | ||
} | ||
|
||
public void printTo(Printable printer) { | ||
printer.println(STATEMENT_HEADER); | ||
printStatementLines(printer); | ||
} | ||
|
||
private void printStatementLines(Printable printer) { | ||
for (StatementLine statementLine : statementLines) { | ||
statementLine.printTo(printer); | ||
} | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/pragmatists/bank/domain/StatementLine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.pragmatists.bank.domain; | ||
|
||
import com.pragmatists.bank.Printable; | ||
|
||
public class StatementLine { | ||
|
||
private Transaction transaction; | ||
private Amount currentBalance; | ||
|
||
public StatementLine(Transaction transaction, Amount currentBalance) { | ||
this.transaction = transaction; | ||
this.currentBalance = currentBalance; | ||
} | ||
|
||
public void printTo(Printable printer) { | ||
this.transaction.printTo(printer, currentBalance); | ||
} | ||
|
||
} |
Oops, something went wrong.