Skip to content

Commit 37424db

Browse files
springframeworkguru#29 - TDD Chapter 13 - Make It - Implement Bank.reduce method - Strange code!
1 parent 2264819 commit 37424db

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

src/main/java/guru/springframework/Bank.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public class Bank {
44
public Money reduce(Expression source, String toCurrency) {
5-
return Money.dollar(10);
5+
return source.reduce(toCurrency);
66
}
77
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package guru.springframework;
22

33
public interface Expression {
4+
Money reduce(String to);
45
}

src/main/java/guru/springframework/Money.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public boolean equals(Object object) {
3636
return amount == money.amount && this.currency.equals(money.currency);
3737
}
3838

39+
@Override
40+
public Money reduce(String to){
41+
return this;
42+
}
43+
3944
@Override
4045
public String toString() {
4146
return "Money{" +
@@ -54,7 +59,7 @@ public Money times(int multiplier) {
5459
return new Money(amount * multiplier, this.currency);
5560
}
5661

57-
public Expression plus(int amount) {
58-
return new Money(this.amount + amount, this.currency);
62+
public Expression plus(Money addend) {
63+
return new Sum(this, addend);
5964
}
6065
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package guru.springframework;
2+
3+
public class Sum implements Expression {
4+
Money augmend;
5+
Money addmend;
6+
7+
public Sum(Money augmend, Money addmend) {
8+
this.augmend = augmend;
9+
this.addmend = addmend;
10+
}
11+
12+
public Money reduce(String toCurrency){
13+
int amount = augmend.amount + addmend.amount;
14+
return new Money(amount, toCurrency);
15+
}
16+
}

src/test/java/guru/springframework/MoneyTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,35 @@ void testCurrency() {
3535
}
3636

3737
@Test
38-
void testAddition() {
38+
void testSimpleAddition() {
3939
Money five = Money.dollar(5);
40-
Expression sum = five.plus(5);
40+
Expression sum = five.plus(five);
4141
Bank bank = new Bank();
4242
Money reduced = bank.reduce(sum, "USD");
4343
assertEquals(Money.dollar(10), reduced);
4444
}
45+
46+
@Test
47+
void testPlusReturnsSum() {
48+
Money five = Money.dollar(5);
49+
Expression result = five.plus(five);
50+
Sum sum = (Sum) result;
51+
assertEquals(five, sum.augmend);
52+
assertEquals(five, sum.addmend);
53+
}
54+
55+
@Test
56+
void testReduceSum() {
57+
Expression sum = new Sum(Money.dollar(3), Money.dollar(4));
58+
Bank bank = new Bank();
59+
Money result = bank.reduce(sum, "USD");
60+
assertEquals(Money.dollar(7), result);
61+
}
62+
63+
@Test
64+
void testReduceMoney() {
65+
Bank bank = new Bank();
66+
Money result = bank.reduce(Money.dollar(1), "USD");
67+
assertEquals(Money.dollar(1), result);
68+
}
4569
}

0 commit comments

Comments
 (0)