Skip to content

Commit

Permalink
[#3] Use test to express the design, as Kent Beck suggested in his book
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanLin-TWer committed Jan 14, 2017
1 parent 1f4f73e commit 61fb649
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.thoughtworks.linesh.multicurrencymoney;

public class Bank {
public Money reduced(Expression money, String currency) {
return Money.dollar(10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.thoughtworks.linesh.multicurrencymoney;

public interface Expression {
Expression plus(Money money);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.thoughtworks.linesh.multicurrencymoney;

public class Money {
public class Money implements Expression {
protected int amount;
public Money(int amount, String currency) {
this.amount = amount;
Expand Down Expand Up @@ -40,7 +40,9 @@ public Money times(int multiplier) {
return new Money(this.amount * multiplier, currency);
}

public Money plus(Money addend) {
return Money.dollar(this.amount + addend.getAmount());

@Override
public Expression plus(Money money) {
return Money.dollar(this.amount + money.getAmount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ void should_test_different_class_equality() {
class should_test_simple_addition {
@Test
void should_get_10_dollars_when_adding_5_dollars_with_5_dollars() {
Money sum = Money.dollar(5).plus(Money.dollar(5));
assertEquals(Money.dollar(10), sum);
Money fiveDollars = Money.dollar(5);
Expression sum = fiveDollars.plus(fiveDollars);
Bank bank = new Bank();
Money reduced = bank.reduced(sum, "USD");
assertEquals(Money.dollar(10), reduced);
}
}
}

0 comments on commit 61fb649

Please sign in to comment.