File tree Expand file tree Collapse file tree 5 files changed +51
-5
lines changed
main/java/guru/springframework
test/java/guru/springframework Expand file tree Collapse file tree 5 files changed +51
-5
lines changed Original file line number Diff line number Diff line change 22
33public class Bank {
44 public Money reduce (Expression source , String toCurrency ) {
5- return Money . dollar ( 10 );
5+ return source . reduce ( toCurrency );
66 }
77}
Original file line number Diff line number Diff line change 11package guru .springframework ;
22
33public interface Expression {
4+ Money reduce (String to );
45}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments