Permalink
Browse files

Inner loop - Blue: Introduce Withdrawal and Deposit abstractions

  • Loading branch information...
CarlosMChica committed May 2, 2016
1 parent 83a326c commit 27da82654dea535d61284cc0b0f44e3f3e7781f1
@@ -2,8 +2,9 @@
import me.panavtec.androidbankkata.account.statement.StatementFormatter;
import me.panavtec.androidbankkata.account.statement.StatementView;
import me.panavtec.androidbankkata.account.transaction.Transaction;
import me.panavtec.androidbankkata.account.transaction.Deposit;
import me.panavtec.androidbankkata.account.transaction.TransactionRepository;
import me.panavtec.androidbankkata.account.transaction.Withdrawal;
public class BankAccount {
@@ -19,11 +20,11 @@ public void attach(StatementView view) {
}
public void deposit(int amount) {
repository.store(new Transaction(amount, clock.today()));
repository.store(new Deposit(amount, clock.today()));
}
public void withdraw(int amount) {
repository.store(new Transaction(-amount, clock.today()));
repository.store(new Withdrawal(amount, clock.today()));
}
public void showStatement() {
@@ -0,0 +1,9 @@
package me.panavtec.androidbankkata.account.transaction;
import java.util.Date;
public class Deposit extends Transaction {
public Deposit(int amount, Date date) {
super(amount, date);
}
}
@@ -2,7 +2,8 @@
import java.util.Date;
public class Transaction {
public abstract class Transaction {
private final int amount;
private final Date date;
@@ -11,11 +12,12 @@ public Transaction(int amount, Date date) {
this.date = date;
}
@Override public String toString() {
return "Transaction{" +
"amount=" + amount +
", date=" + date +
'}';
public int getAmount() {
return amount;
}
public Date getDate() {
return date;
}
@Override public boolean equals(Object o) {
@@ -28,15 +30,19 @@ public Transaction(int amount, Date date) {
Transaction that = (Transaction) o;
if (amount != that.amount) {
return false;
}
return date != null ? date.equals(that.date) : that.date == null;
return amount == that.amount && date != null ? date.equals(that.date) : that.date == null;
}
@Override public int hashCode() {
int result = amount;
result = 31 * result + (date != null ? date.hashCode() : 0);
return result;
}
@Override public String toString() {
return "Transaction{" +
"amount=" + amount +
", date=" + date +
'}';
}
}
@@ -0,0 +1,9 @@
package me.panavtec.androidbankkata.account.transaction;
import java.util.Date;
public class Withdrawal extends Transaction {
public Withdrawal(int amount, Date date) {
super(-amount, date);
}
}
@@ -3,8 +3,10 @@
import java.util.Date;
import me.panavtec.androidbankkata.account.statement.StatementFormatter;
import me.panavtec.androidbankkata.account.statement.StatementView;
import me.panavtec.androidbankkata.account.transaction.Deposit;
import me.panavtec.androidbankkata.account.transaction.Transaction;
import me.panavtec.androidbankkata.account.transaction.TransactionRepository;
import me.panavtec.androidbankkata.account.transaction.Withdrawal;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -26,15 +28,19 @@
account.deposit(anAmount());
verifyStore(aTransactionWith(anAmount()));
verifyStore(aDepositWith(anAmount()));
}
@Test public void store_a_withdraw_transaction() {
BankAccount account = givenAccountWithMockedClock();
account.withdraw(anAmount());
verifyStore(aTransactionWith(-anAmount()));
verifyStore(aWithdrawalWith(anAmount()));
}
private Deposit aDepositWith(int amount) {
return new Deposit(amount, date());
}
private BankAccount givenAccountWithMockedClock() {
@@ -56,8 +62,8 @@ private int anAmount() {
return 100;
}
private Transaction aTransactionWith(int anyAmount) {
return new Transaction(anyAmount, date());
private Withdrawal aWithdrawalWith(int amount) {
return new Withdrawal(amount, date());
}
private void verifyStore(Transaction transaction) {
@@ -0,0 +1,29 @@
package me.panavtec.androidbankkata.account.transaction;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class WithdrawalShould {
@Test public void have_a_negative_amount() {
Withdrawal withdrawal = givenWithdrawalWith(anAmount());
int amount = withdrawal.getAmount();
assertThat(amount, is(aNegativeAmount()));
}
private Withdrawal givenWithdrawalWith(int amount) {
return new Withdrawal(amount, null);
}
private int aNegativeAmount() {
return -anAmount();
}
private int anAmount() {
return 1000;
}
}

0 comments on commit 27da826

Please sign in to comment.